我必须从PCAP文件中获取所有不同协议的名称。基本上,我必须解析它。我研究了一下,并得知dpkt在这方面非常有效。我正在用python编写脚本,下面是代码-
def inet_to_str(inet):
# First try ipv4 and then ipv6
try:
return socket.inet_ntop(socket.AF_INET, inet)
except ValueError:
return socket.inet_ntop(socket.AF_INET6, inet)
def read_packet(pcap):
with open('/XYZ/XYZ/XYZ/XYZ/XYZ/' + str(pcap),
"rb") as f:
pcap = dpkt.pcap.Reader(f)
for timestamp, buf in pcap:
#Not printing out the timestamp for now
#print('Timestamp: ', str(datetime.datetime.utcfromtimestamp(timestamp)))
#Unpacking the ethernet frame
eth = dpkt.ethernet.Ethernet(buf)
#Not printing the ethernet frame
#print('Ethernet Frame: ', mac_addr(eth.src), mac_addr(eth.dst), eth.type)
#Making sure the ethernet packet contains an IP packet
if not isinstance(eth.data, dpkt.ip.IP):
print('Non IP Packet type not supported %s\n' % eth.data.__class__.__name__)
continue
#Now unpack the data within the Ethernet frame (the IP packet)
#Pulling out src, dst, length, fragment info, TTL, and Protocol
ip = eth.data
#dp = ip.data
#proto = type(udp.data)
#print(proto)
time.sleep(3)
# Pull out fragment information (flags and offset all packed into off field, so use bitmasks)
do_not_fragment = bool(ip.off & dpkt.ip.IP_DF)
more_fragments = bool(ip.off & dpkt.ip.IP_MF)
fragment_offset = ip.off & dpkt.ip.IP_OFFMASK
# Print out the info
print('IP: %s -> %s (len=%d ttl=%d DF=%d MF=%d offset=%d) Protocol=%s\n' % \
(inet_to_str(ip.src), inet_to_str(ip.dst), ip.len, ip.ttl, do_not_fragment, more_fragments, fragment_offset, ip.get_proto(ip.p).__name__))
time.sleep(5)问题是代码给我的是传输层协议(TCP/UDP),而不是应用层协议(SSH、DHCP、DNS等)。我阅读了这些文档,发现有一些模块可以分析不同类型的数据包(如果您知道的话),但是我想自动完成它,因为我有数百万个pcap。我想自动识别应用层协议,然后调用适当的函数来分析它。至少我能知道协议的名字吗?
发布于 2022-10-21 06:21:16
您可以查看传输层数据包的端口。通过这种方式,您可以确定正在使用的是哪个应用层协议。下面是一个指向端口号和使用它的应用层协议的链接:https://en.wikibooks.org/wiki/A-level_Computing/AQA/Paper_2/Fundamentals_of_communication_and_networking/Standard_application_layer_protocols
使用dpkt,可以将以下内容添加到代码中:
ip = eth.data
tcp = ip.data如果您正在寻找一个特定的协议,您可以设置如下的If语句:
if tcp.dport == 80:
print("Application Layer protocol used: HTTP")https://stackoverflow.com/questions/74091369
复制相似问题