网络数据包处理Scapy
Whisper Lv4

安装

1
pip install scapy

Windows使用

直接输入scapy(没有安装额外的matplotlib,PyX忽略即可)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
C:\Users\luna>scapy
INFO: Can't import matplotlib. Won't be able to plot.
INFO: Can't import PyX. Won't be able to use psdump() or pdfdump().
WARNING: No route found for IPv6 destination :: (no default route?)
WARNING: IPython not available. Using standard Python shell instead.
AutoCompletion, History are disabled.
WARNING: On Windows, colors are also disabled

aSPY//YASa
apyyyyCY//////////YCa |
sY//////YSpcs scpCY//Pp | Welcome to Scapy
ayp ayyyyyyySCP//Pp syY//C | Version 2.4.3
AYAsAYYYYYYYY///Ps cY//S |
pCCCCY//p cSSps y//Y | https://github.com/secdev/scapy
SPPPP///a pP///AC//Y |
A//A cyP////C | Have fun!
p///Ac sC///a |
P////YCpc A//A | Craft packets like I craft my beer.
scccccp///pSP///p p//Y | -- Jean De Clerck
sY/////////y caa S//P |
cayCyayP//Ya pY/Ya
sY/PsY////YCc aC//Yp
sc sccaCY//PCypaapyCP//YSs
spCPY//////YPSps
ccaacs

定制终端

设置颜色

1
conf.color_theme = BrightTheme()

可选的color_theme: DefaultTheme, BrightTheme, RastaTheme, ColorOnBlackTheme, BlackAndWhite, HTMLTheme, LatexTheme,设置完后立即生效。

各颜色演示:

其他定制见conf.prompt

基础使用

构造数据包

使用IP()创建一个默认的数据包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
>>> a = IP()  //创建一个IP对象
>>> a
<IP |>
>>> a.dst = "192.168.1.110" //设置该对象的目标地址为192.168.1.110
>>> a
<IP dst=192.168.1.110 |>
>>> a.summary() //查看概要信息
'192.168.127.161 > 192.168.1.110 ip'
>>> ls(a) //查看该对象的参数
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 (3 bits) = <Flag 0 ()> (<Flag 0 ()>)
frag : BitField (13 bits) = 0 (0)
ttl : ByteField = 64 (64)
proto : ByteEnumField = 0 (0)
chksum : XShortField = None (None)
src : SourceIPField = '192.168.127.161' (None)
dst : DestIPField = '192.168.1.110' (None)
options : PacketListField = [] ([])
>>> a.show() //查看数据包信息
###[ IP ]###
version = 4
ihl = None
tos = 0x0
len = None
id = 1
flags =
frag = 0
ttl = 64
proto = ip
chksum = None
src = 192.168.127.161
dst = 192.168.1.110
\options \
>>> hexdump(a) //查看数据包的字节信息
0000 45 00 00 14 00 01 00 00 40 00 78 89 C0 A8 7F A1 E.......@.x.....
0010 C0 A8 01 6E ...n

查看IP数据包可以有哪些参数

  • ls(IP())
  • ls(TCP())

TCP/IP 数据传输流程

客户端在应用层发出一个 HTTP 请求->
为了方便传输,在传输层 (TCP 协议) 把从应用层处收到的数据 (HTTP 请求报文) 进行分割,并在各个报文上打上标记序号及端口号后转发给网络层。
在网络层 (IP 协议),增加作为通信目的地的 MAC 地址后转发给数据链路层。这样,发送给服务端的请求就准备齐全了。
当服务端在链路层接收到数据时,按序往上层发送,一直到应用层。当传输到应用层时,才算真正的接收到由客户端发送过来的请求。

参考

官网
文档