启动
直接执行scapy启动交互式CLI界面。
在脚本中:from scapy.all import *
常用命令
- ls()
常用的有ARP、Ether、ICMP、IP、UDP、TCP,也支持SNMP、DHCP、STP等。 - ls(IP)
查看IP包结构 - lsc()
查看支持的命令
构造报文
//类型+字段,缺省的不用设置
a=IP(dst="172.16.1.40")
查看报文内容
// 查看特定字段
>>> a.dst
'172.16.1.40'
//查看IP全部字段
a.show()
###[ IP ]###
version=4
ihl= None
tos=0x0
len= None
id=1
flags=
frag=0
ttl=64
proto= ip
chksum= None
src=10.1.159.51
dst=172.16.1.40
\options\
// 多层报文
a= Ether(dst=‘001122334455’)/IP(dst=‘2.2.2.2’)/TCP(dport=23)
a.show()
###[ Ethernet ]###
dst=001122334455
src=00:1c:c4:aa:c9:36
type=0x800
###[ IP ]###
version=4
ihl= None
tos=0x0
len= None
id=1
flags=
frag=0
ttl=64
proto= tcp
chksum= None
src=10.1.159.51
dst=2.2.2.2
\options
###[ TCP ]###
sport= ftp_data
dport= telnet
seq=0
ack=0
dataofs= None
reserved=0
flags= S
window=8192
chksum= None
urgptr=0
options= {}
发送报文
>>> send(a,iface="eth1")
.
Sent 1 packets.
接收报文
>>> a=sniff(filter="icmp and host 10.1.159.71", count=2)
>>> a.show()
0000 Ether / IP / ICMP 10.1.159.71 > 10.1.159.51 echo-request 0 / Raw
0001 Ether / IP / ICMP 10.1.159.51 > 10.1.159.71 echo-reply 0 / Raw
>>> a[1]
<Ether dst=00:1f:29:63:41:62 src=00:1c:c4:aa:c9:36 type=0x800 |<IP version=4L ihl=5L tos=0x0 len=84 id=523 flags= frag=0L ttl=64 proto=icmp chksum=0x2622 src=10.1.159.51 dst=10.1.159.71 options=[] |<ICMP type=echo-reply code=0 chksum=0xf549 id=0x597b seq=0x1 |<Raw load='\xdc\x1b\x82Q\x00\x00\x00\x00\x86\xf9\r\x00\x00\x00\x00\x00\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./01234567' |>>>>
发送和接收报文
// 发送ICMP请求,接收应答
>>> a = sr(IP(dst="10.1.159.71")/ICMP())
Begin emission:
....Finished to send 1 packets.
*
Received 5 packets, got 1 answers, remaining 0 packets
>>> a[0]
<Results: TCP:0 UDP:0 ICMP:1 Other:0>
>>> a[1]
<Unanswered: TCP:0 UDP:0 ICMP:0 Other:0>
>>> a[0].show()
0000 IP / ICMP 10.1.159.51 > 10.1.159.71 echo-request 0 ==> IP / ICMP 10.1.159.71 > 10.1.159.51 echo-reply 0 / Padding
读取cap内容
>>> d = rdpcap("./test.cap")
>>> d
<test.cap: TCP:2 UDP:0 ICMP:0 Other:1>
>>> d.show()
0000 Ether / IP / TCP 10.1.159.51:ssh > 10.7.244.6:65464 PA / Raw
0001 Ether / IP / TCP 10.7.244.6:65464 > 10.1.159.51:ssh A / Padding
0002 802.3 00:25:83:b8:8f:83 > 01:00:0c:cc:cc:cd / LLC / SNAP / STP / Raw