3

我是 pyshark 的新手。我正在尝试为自定义 UDP 数据包编写解析器。我正在使用该FileCapture对象从文件中读取数据包。

>>> cap = pyshark.FileCapture('sample.pcap')
>>> pkt = cap.next()
>>> pkt
<UDP/DATA Packet>
>>> pkt.data.data
'01ca00040500a4700500a22a5af20f830000b3aa000110da5af20f7c000bde1a000006390000666e000067f900000ba7000026ce000001d00000000100001726000100000000000000000000000017260500a4700500a22a608600250500a8c10500a22a608601310500a8c10500a22b608601200500a8cc0500a22a6086000c'
>>> dir(pkt.udp)
['DATA_LAYER', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__format__', '__getattr__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_all_fields', '_field_prefix', '_get_all_field_lines', _get_all_fields_with_alternates', '_get_field_or_layer_repr', '_get_field_repr', '_layer_name', '_sanitize_field_name', 'checksum', 'checksum_status', 'dstport', 'field_names', 'get', 'get_field', 'get_field_by_showname', get_field_value', 'layer_name', 'length', 'port', 'pretty_print', raw_mode', 'srcport', 'stream']

我需要一种方法来简单地访问数据包的 UDP 有效负载。我发现访问原始数据包数据的唯一方法是 pkt.data.data,但这会返回数据包的全部内容,而我只对 UDP 部分感兴趣。类似的东西pkt.udp.data。有没有办法简单地做到这一点,或者我需要使用pkt.data.data和计算我的数据放置的偏移量?

4

2 回答 2

3

我发现访问原始数据包数据的唯一方法是 pkt.data.data,

正确的。

但这会返回数据包的全部内容,而我只对 UDP 部分感兴趣。

不正确:该.data.data属性是仅 UDP 有效负载本身的十六进制字符串表示。

例如,如果您的 UDP 有效负载是 ASCII 字符串“hello”,您可以这样检索它:bytearray.fromhex(pkt.data.data).decode()

echo -n hello >/dev/udp/localhost/12345这是在 lo:12345 上执行 pyshark 捕获时在 linux 控制台上验证这一点的快速方法。)

于 2019-01-11T03:43:02.877 回答
0

pyshark_parser 可能会帮助你: https ://github.com/jlents/pyshark_parser/blob/master/pyshark_parser/

我正在查看他们的代码以及您可能在这里寻找的内容: https ://github.com/jlents/pyshark_parser/blob/master/pyshark_parser/packet_util.py

def get_all_field_names(packet, layer=None):
'''
    Builds a unique list of field names, that exist in the packet,
    for the specified layer.
    If no layer is provided, all layers are considered.
    Args:
        packet: the pyshark packet object the fields will be gathered from
        layer: the string name of the layer that will be targeted
    Returns:
        a set containing all unique field names
        or None, if packet is None
'''

if not packet:
    return None

field_names = set()
for current_layer in packet.layers:
    if not layer or layer == current_layer.__dict__['_layer_name']:
        for field in current_layer.__dict__['_all_fields']:
            field_names.add(field)
return field_names

def get_value_from_packet_for_layer_field(packet, layer, field):
'''
    Gets the value from the packet for the specified 'layer' and 'field'
    Args:
        packet: The packet where you'll be retrieving the value from
        layer: The layer that contains the field
        field: The field that contains the value
    Returns:
        the value at packet[layer][key] or None
        or None, if any of the arguments are None
'''
if not packet or not layer or not field:
    return None
for current_layer in packet.layers:
    if layer == current_layer.__dict__['_layer_name'] and \
       current_layer.__dict__['_all_fields']:
        return current_layer.__dict__['_all_fields'][field]
return None
于 2018-06-18T15:07:40.080 回答