1 回答
TA贡献1806条经验 获得超8个赞
假设我们正在嗅探带有scapy的数据包,并希望查看其中的值。其中大部分是使用 scapy 文档来查找每个层具有的属性的问题。您也可以在python / scapy解释器中执行此操作,以查看它具有哪些属性和方法。例如:dir(packet)
>>> dir(packet)
...
'show',
'show2',
'show_indent',
'show_summary',
'sniffed_on',
'sprintf',
'src',
...
要从数据包中动态获取源属性,我们需要使用 getattr 函数,该函数可以从对象获取方法和属性。
# Required if you are not using the scapy interpreter
from scapy.all import sniff, IP
layer = "IP"
field = "src"
# Sniff 4 packets, filtering for packets with an IP layer
packet_list = sniff(filter="ip", count=4)
# Choose first packet arbitrarily
packet0 = packet_list[0]
# We can get the attribute reflexively because python allows it
field_value = getattr(packet0[layer], field)
# Print this information
print("Layer: ", layer, " Field: ", field, " Value: ", field_value)
---
> Layer: IP Field: src Value: 192.168.1.246
添加回答
举报