#!/usr/bin/env python
import sys
import struct
import os
from scapy.all import sniff, sendp, hexdump, get_if_list, get_if_hwaddr
from scapy.all import Packet, IPOption
from scapy.fields import ShortField, IntField, LongField, BitField, FieldListField, FieldLenField, SourceIPField, Emph, ShortEnumField, ByteEnumField, ByteField
from scapy.all import IP, TCP, UDP, Raw
from scapy.layers.inet import _IPOption_HDR, DestIPField
from scapy.data import IP_PROTOS, TCP_SERVICES
def get_if():
ifs=get_if_list()#获取网卡接口
iface=None
for i in get_if_list():
if "eth0" in i:
iface=i
break;
if not iface:
print "Cannot find eth0 interface"
exit(1)
return iface
class IPOption_TELEMETRY(IPOption):
name = "TELEMETRY"
option = 31
fields_desc = [ _IPOption_HDR,
ByteField("length", 2),
Emph(SourceIPField("src", "dst")),
Emph(DestIPField("dst", "127.0.0.1")),
ShortEnumField("sport", 20, TCP_SERVICES),
ShortEnumField("dport", 80, TCP_SERVICES),
ByteEnumField("proto", 0, IP_PROTOS),
BitField("ingress_timestamp", 0, 48),
BitField("egress_timestamp", 0, 48),
BitField("enqQdepth", 0, 19),
BitField("deqQdepth", 0, 19),
BitField("padding", 0, 2) ]
def handle_pkt(pkt):
if TCP in pkt and pkt[TCP].dport == 1234:
print "got a packet"
#显示详细的包头
pkt.show2()
# hexdump(pkt)
sys.stdout.flush()
def main():
#os.listdir('/sys/class/net/'):从该路径获取文件夹中的所有文件名并生成列表,从而得到该路径下的所有网卡,
#Python内置的filter()函数能够从可迭代对象(如字典、列表)中筛选某些元素,并生成一个新的迭代器。
ifaces = filter(lambda i: 'eth' in i, os.listdir('/sys/class/net/'))
iface = ifaces[0]
print "sniffing on %s" % iface
sys.stdout.flush()#显示地让缓冲区的内容输出
#1.iface用来指定要在哪个网络接口上进行抓包【即网卡的名称】(通常不指定即所有网络接口);例如:dpkg = sniff(filter="host 192.168.191.5 and tcp",iface="Microsoft Wi-Fi Direct Virtual Adapter #2",count=2)
#2.prn指定回调函数,每当一个符合filter的报文被探测到时,就会执行回调函数,通常使用lambda表达式来写回调函数
sniff(iface = iface,
prn = lambda x: handle_pkt(x))
if __name__ == '__main__':
main()
Burstradar_receive.py
最新推荐文章于 2024-08-16 18:41:12 发布