python两个基本的库管理工具_python scapy 网络包管理工具--基础篇

本文介绍了Python的Scapy库,用于网络协议构造和请求。讲解了Scapy的安装、交互模式、数据包构建,包括IP、TCP等层的信息添加,并展示了如何发送网络包。此外,还提到了相关警告信息的处理和不同层次的数据包发送方法。
摘要由CSDN通过智能技术生成

写在前面

scapy 是python的一个库,提供网络协议的构造,请求等

scrapy 是python的爬虫框架。

这两个差一个字母,大家还是要分清楚的。

因为涉及到发包等系统层面的操作,所以请保证具备root权限,这一点贯穿全文。

安装

博主使用的环境有

centos6、centos7、windows10

直接使用pip install scapy就可以安装

运行模式

你可以直接在命令行敲 scapy 进入交互模式

可以采用在py文件中运行。

交互模式

以linux下为例子,直接输入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().

INFO: No IPv6 support in kernel

WARNING: No route found for IPv6 destination :: (no default route?)

INFO: Can't import python Crypto lib. Won't be able to decrypt WEP.

INFO: Can't import python Crypto lib. Disabled certificate manipulation tools

INFO: Can't import python ecdsa lib. Disabled certificate manipulation tools

提示缺少库文件,以及IPv6的支持性问题。

这里基本功能用不到这些库,可以选择安装也可以暂时不用安装。

pip install matplotlib提示已经安装

pip install matplotlib --upgrade更新下,(更新的包比较多约30M,不先更新的可以跳过)

(别更新了,更新完还是提示不能使用plot,以及后面几个,即便安装了那个库还是提示不可用啦,巴拉巴拉。但是还好大部分是info,个别是warning.)

开始

>>>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

构建多层数据包

如何加入TCP层信息,或者以太网信息呢?

>>> IP() >>> IP()/TCP() > >>> Ether()/IP()/TCP() >> >>> IP()/TCP()/"GET / HTTP/1.0\r\n\r\n" >> >>> Ether()/IP()/IP()/UDP() >>> >>> IP(proto=55)/TCP() >

不用觉得晕,这里只是说明,不同网络层级之间的连接用 “/”

这里我还试了下TCP()/IP() 。显然这是一个错误的层次,但是依然可以返回结果。说明,在构造包的时候你需要自己注意网络层次的分布。

>>> str(IP())

'E\x00\x00\x14\x00\x01\x00\x00@\x00|\xe7\x7f\x00\x00\x01\x7f\x00\x00\x01'

>>> 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

\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

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='' |

ack=0L dataofs=5L reserved=0L flags=S window=8192 chksum=0xbb39 urgptr=0

options=[] |>>>

不要被以上一长串的返回值所吓到。划重点一下

1.str(IP()), IP(_) 可以查看IP协议的首部信息

2.hexdump() 方法可以16进制显示数据报,类似wireshark中一样

构造多个数据包

>>> a=IP(dst="www.slashdot.org/30") >>> a >>> [p for p in a] [, , , ] >>> b=IP(ttl=[1,2,(5,9)]) >>> b >>> [p for p in b] [, , , , , , ] >>> c=TCP(dport=[80,443]) >>> [p for p in a/c] [>, >, >, >, >, >, >, >]

发送包

讲完包的构造,我们接下来说说如何发送这些报文。

>>>send(IP(dst="1.2.3.4")/ICMP())

.

Sent 1 packets.

>>>sendp(Ether()/IP(dst="1.2.3.4",ttl=(1,4)), iface="eth1")

....

Sent 4 packets.

>>>sendp("I'm travelling on Ethernet", iface="eth1", loop=1, inter=0.2)

................^C

Sent 16 packets.

>>>sendp(rdpcap("/tmp/pcapfile")) # tcpreplay

...........

Sent 11 packets.

Returns packets sent by send()

>>>send(IP(dst='127.0.0.1'), return_packets=True)

.

Sent 1 packets.

send()在第三层发包。

sendp() 在第二层发包。

这里很多教程都有提及,说实话,这个我的理解也不是很深入。

你只要知道第一层是链路层。sendp()更靠近底层。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值