1.pip安装scapy库
2.相关函数-Ether()链路层、IP()网络层、TCP()传输层
使用ls()函数查看对应三个函数的参数
代码:from scapy.layers.inet import *
from scapy.all import *
pkE=Ether()
pkI=IP()
pkT=TCP()
print("----------------------")
ls(pkE)
print("----------------------")
ls(pkI)
print("----------------------")
ls(pkT)
print("----------------------")
执行结果:
----------------------
dst : DestMACField = 'ff:ff:ff:ff:ff:ff' ('None')
src : SourceMACField = '00:00:00:00:00:00' ('None')
type : XShortEnumField = 36864 ('36864')
----------------------
version : BitField (4 bits) = 4 ('4')
ihl : BitField (4 bits) = None ('None')
tos : XByteField = 0 ('0')
len : ShortField = None ('None')
id : ShortField = 1 ('1')
flags : FlagsField =
frag : BitField (13 bits) = 0 ('0')
ttl : ByteField = 64 ('64')
proto : ByteEnumField = 0 ('0')
chksum : XShortField = None ('None')
src : SourceIPField = '0.0.0.0' ('None')
dst : DestIPField = '127.0.0.1' ('None')
options : PacketListField = [] ('[]')
----------------------
sport : ShortEnumField = 20 ('20')
dport : ShortEnumField = 80 ('80')
seq : IntField = 0 ('0')
ack : IntField = 0 ('0')
dataofs : BitField (4 bits) = None ('None')
reserved : BitField (3 bits) = 0 ('0')
flags : FlagsField =
window : ShortField = 8192 ('8192')
chksum : XShortField = None ('None')
urgptr : ShortField = 0 ('0')
options : TCPOptionsField = [] ("b''")
3.其他相关函数
from scapy.layers.inet import *
from scapy.all import *
pkI=IP(src="10.191.96.28",dst="10.191.96.27",ttl=32)#构造数据包
print(raw(pkI))#字节形式
print(hexdump(pkI))#十六进制形式
pkS=pkI.show()
print(pkS)#详细查看数据包每个属性
wrpcap("temp.cap",pkI)#保存信息到temp.cap中
pkI=rdpcap("temp.cap")#读取保存的temp.cap
print(pkI)
S_res=IP(dst="39.156.66.10")/ICMP()#1.构造数据包
#s=send(S_res)#2.调用send函数进行发送如果是要发送链路层数据包使用sendp()方法,【注意】:调用此命令需要管理员权限或者root身份运行
ans,uans=sr(S_res)#使用sr函数进行数据包发送,返回两个列表
ans.summary()#使用summary方法进行数据包简述
sniff(filter="icmp and host 192.168.1.1",ifaces="any",prn=lambda x:x.summary(),count=3)#抓包器1.过滤内容2.指定的网卡3.捕获数据包处理函数4.希望捕获数据包数量