Scapy学习笔记.0×02-创造数据包

ziDANe 撰写  

之前展示了scapy的安装启动等,并学习了ls命令,并创造了一个IP包,通过ls查看了创建的这个IP包的内容。现在看看创建、修改包的更多方法。

构造包


>>> a=IP(ttl=10)
>>> a
< IP ttl=10 |>
>>> a.src
’127.0.0.1’
>>> a.dst="192.168.1.1"
>>> a
< IP ttl=10 dst=192.168.1.1 |>
>>> a.src
’192.168.8.14’
>>> del(a.ttl)
>>> a
< IP dst=192.168.1.1 |>
>>> a.ttl
64

代码很清晰的介绍了,创建一个包的时候添加属性,查看包的某个内容。以及删除包中的内容,删除后恢复为默认值。

构造更复杂的包(包含多个层的包)

/ 操作符用来链接两不同层的包,其实就是把两个包的封装起来,比如一个IP层的包,当作包头加上一个TCP层的包。使用方法如下


>>> IP()
<IP |>
>>> IP()/TCP()
<IP frag=0 proto=TCP |<TCP |>>
>>> Ether()/IP()/TCP()
<Ether type=0x800 |<IP frag=0 proto=TCP |<TCP |>>>
>>> IP()/TCP()/"GET / HTTP/1.0\r\n\r\n"
<IP frag=0 proto=TCP |<TCP |<Raw load='GET / HTTP/1.0\r\n\r\n' |>>>
>>> Ether()/IP()/IP()/UDP()
<Ether type=0x800 |<IP frag=0 proto=IP |<IP frag=0 proto=UDP |<UDP |>>>>
>>> IP(proto=55)/TCP()
<IP frag=0 proto=55 |<TCP |>>

分析包

scapy提供了多种查看包结构并分析包的方法。支持抓包工具里常见的几种形式。看下面的代码:

>>> str(IP())
'E\x00\x00\x14\x00\x01\x00\x00@\x00|\xe7\x7f\x00\x00\x01\x7f\x00\x00\x01'
>>> IP(_)
<IP version=4L ihl=5L tos=0x0 len=20 id=1 flags= frag=0L ttl=64 proto=IP
 chksum=0x7ce7 src=127.0.0.1 dst=127.0.0.1 |>
>>>  a=Ether()/IP(dst="www.slashdot.org")/TCP()/"GET /index.html HTTP/1.0 \n\n"
>>>  hexdump(a)
00 02 15 37 A2 44 00 AE F3 52 AA D1 08 00 45 00  ...7.D...R....E.
00 43 00 01 00 00 40 06 78 3C C0 A8 05 15 42 23  .C....@.x<....B#
FA 97 00 14 00 50 00 00 00 00 00 00 00 00 50 02  .....P........P.
20 00 BB 39 00 00 47 45 54 20 2F 69 6E 64 65 78   ..9..GET /index
2E 68 74 6D 6C 20 48 54 54 50 2F 31 2E 30 20 0A  .html HTTP/1.0 .
0A                                               .
>>> b=str(a)
>>> b
'\x00\x02\x157\xa2D\x00\xae\xf3R\xaa\xd1\x08\x00E\x00\x00C\x00\x01\x00\x00@\x06x<\xc0
 \xa8\x05\x15B#\xfa\x97\x00\x14\x00P\x00\x00\x00\x00\x00\x00\x00\x00P\x02 \x00
 \xbb9\x00\x00GET /index.html HTTP/1.0 \n\n'
>>> c=Ether(b)
>>> c
<Ether dst=00:02:15:37:a2:44 src=00:ae:f3:52:aa:d1 type=0x800 |<IP version=4L
 ihl=5L tos=0x0 len=67 id=1 flags= frag=0L ttl=64 proto=TCP chksum=0x783c
 src=192.168.5.21 dst=66.35.250.151 options='' |<TCP sport=20 dport=80 seq=0L
 ack=0L dataofs=5L reserved=0L flags=S window=8192 chksum=0xbb39 urgptr=0
 options=[] |<Raw load='GET /index.html HTTP/1.0 \n\n' |>>>>

支持的命令如下:
Command            Effect
summary()            displays a list of summaries of each packet
nsummary()         same as previous, with the packet number
conversations()   displays a graph of conversations
show()                   displays the prefered representation (usually nsummary())
filter()                    returns a packet list filtered with a lambda function
hexdump()            returns a hexdump of all packets
hexraw()                returns a hexdump of the Raw layer of all packets
padding()              returns a hexdump of packets with padding
nzpadding()          returns a hexdump of packets with non-zero padding
plot()                      plots a lambda function applied to the packet list
make table()          displays a table according to a lambda function
可以看到,支持的显示形式非常丰富,我们几乎可以修改任何一个字段的内容。
如果包的内容很长,而一些内容我们没有修改过,我们只想查看我们修改过的内容,那么可以使用 hide_defaults() 这个函数来查看,它将不显示所以字段为默认值的字段。

>>> c.hide_defaults()
>>> c
<Ether dst=00:0f:66:56:fa:d2 src=00:ae:f3:52:aa:d1 type=0x800 |<IP ihl=5L len=67
 frag=0 proto=TCP chksum=0x783c src=192.168.5.21 dst=66.35.250.151 |<TCP dataofs=5L
 chksum=0xbb39 options=[] |<Raw load='GET /index.html HTTP/1.0 \n\n' |>>>>

导入PACP文件

可以使用Scapy查看抓包工具抓到的PACP文件,这个功能很方便。

>>> a=rdpcap("/spare/captures/isakmp.cap")
>>> a
<isakmp.cap: UDP:721 TCP:0 ICMP:0 Other:0>

生成图像

如果安装了Pyx库,则可以将包的结构导出为PDF形式的图像。

>>> a[423].pdfdump(layer_shift=1)
>>> a[423].psdump("/tmp/isakmp_pkt.eps",layer_shift=1)
效果很炫

生成一组包

前面都是手动生成了一个包,现在我们一次性多造点包。

>>> a=IP(dst="www.d-up.org/30")
>>> a
<IP  dst=Net('www.d-up.org/30') |>
>>> [p for p in a]
[<IP dst=66.35.250.148 |>, <IP dst=66.35.250.149 |>,
 <IP dst=66.35.250.150 |>, <IP dst=66.35.250.151 |>]
>>> b=IP(ttl=[1,2,(5,9)])
>>> b
<IP ttl=[1, 2, (5, 9)] |>
>>> [p for p in b]
[<IP ttl=1 |>, <IP ttl=2 |>, <IP ttl=5 |>, <IP ttl=6 |>,
 <IP ttl=7 |>, <IP ttl=8 |>, <IP ttl=9 |>]
>>> c=TCP(dport=[80,443])
>>> [p for p in a/c]
[<IP frag=0 proto=TCP dst=66.35.250.148 |<TCP dport=80 |>>,
 <IP frag=0 proto=TCP dst=66.35.250.148 |<TCP dport=443 |>>,
 <IP frag=0 proto=TCP dst=66.35.250.149 |<TCP dport=80 |>>,
 <IP frag=0 proto=TCP dst=66.35.250.149 |<TCP dport=443 |>>,
 <IP frag=0 proto=TCP dst=66.35.250.150 |<TCP dport=80 |>>,
 <IP frag=0 proto=TCP dst=66.35.250.150 |<TCP dport=443 |>>,
 <IP frag=0 proto=TCP dst=66.35.250.151 |<TCP dport=80 |>>,
 <IP frag=0 proto=TCP dst=66.35.250.151 |<TCP dport=443 |>>]

通过上面方法,可以很轻松的造出很多包。也可以很方便的将很多层的包封装起来。

包的构造基本学完了,下面该学习如何发送和接收包了。

相关文章列表:

第一篇 认识Scapy   http://d-up.org/man/2010/01/scapy0×1/


3 条评论

  1. 发表了 2010年03月5日 在 10:43 上午 | 永久链接

    我看晕了,怎么没人和我一起看啊?

  2. nagaregawa
    发表了 2010年06月1日 在 10:08 上午 | 永久链接

    PyX 库是什么,我的 RHEL5 上总提示没有安装它,不能使用 pdfdump ,请问如何解决,谢谢~

  3. 发表了 2010年06月5日 在 8:34 下午 | 永久链接

    请参见:http://pyx.sourceforge.net/

发表评论

Your email is never shared.