Python-Scapy发送IP地址变化的报文

scapy这个python包真的是网工的利器。最近遇到一个需求,要发IP地址递增的报文,有没有优雅一些的解决方法呢?

IP地址递增

  • 终端1
$ sudo 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: Can't import python-cryptography v1.7+. Disabled WEP decryption/encryption. (Dot11)
INFO: Can't import python-cryptography v1.7+. Disabled IPsec encryption/authentication.
WARNING: IPython not available. Using standard Python shell instead.
AutoCompletion, History are disabled.
                                      
                     aSPY//YASa       
             apyyyyCY//YCa       |
            sY//YSpcs  scpCY//Pp     | Welcome to Scapy
 ayp ayyyyyyySCP//Pp           syY//C    | Version 2.4.4
 AYAsAYYYYYYYY///Ps              cY//S   |
         pCCCCY//p          cSSps y//Y   | https://github.com/secdev/scapy
         SPPPP///a          pP///AC//Y   |
              A//A            cyPC   | Have fun!
              p///Ac            sC///a   |
              PYCpc           A//A   | Craft packets before they craft
       scccccp///pSP///p          p//Y   | you.
      sY/y  caa           S//P   |                      -- Socrate
       cayCyayP//Ya              pY/Ya   |
        sY/PsYYCc          aC//Yp 
         sc  sccaCY//PCypaapyCP//YSs  
                  spCPY//YPSps    
                       ccaacs         
                                      
>>>
>>> dst_ip_list=['192.168.1.'+str(x) for x in range(1,10)]
>>> dst_ip_list
['192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4', '192.168.1.5', '192.168.1.6', '192.168.1.7', '192.168.1.8', '192.168.1.9']
>>> pkt=Ether()/IP(src='192.168.2.254',dst=dst_ip_list)/('0'*80)
>>> 
>>> sendp(pkt,iface='ens3')
.........
Sent 9 packets.
>>> 
  • 终端2上tcpdump到了9个报文
10:02:03.662241 IP 192.168.2.254 > 192.168.1.1:  hopopt 80
10:02:03.662681 IP 192.168.2.254 > 192.168.1.2:  hopopt 80
10:02:03.663102 IP 192.168.2.254 > 192.168.1.3:  hopopt 80
10:02:03.663506 IP 192.168.2.254 > 192.168.1.4:  hopopt 80
10:02:03.663900 IP 192.168.2.254 > 192.168.1.5:  hopopt 80
10:02:03.664282 IP 192.168.2.254 > 192.168.1.6:  hopopt 80
10:02:03.664657 IP 192.168.2.254 > 192.168.1.7:  hopopt 80
10:02:03.665033 IP 192.168.2.254 > 192.168.1.8:  hopopt 80
10:02:03.665422 IP 192.168.2.254 > 192.168.1.9:  hopopt 80

src/dst IP地址都递增

  • 终端1
>>> ds_ip_list=['_ip_list=['192.168.1.'+str(x) for x in range(1,10)]
>>> src_ip_list=['172.16.1.'+str(x) for x in range(1,3)]
>>> sendp(pkt,iface='ens3')
..................
Sent 18 packets.
>>> 
  • 终端2
10:06:00.614891 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.1 > 192.168.1.1:  ip-proto-0 80
10:06:00.615758 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.2 > 192.168.1.1:  ip-proto-0 80
10:06:00.616440 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.1 > 192.168.1.2:  ip-proto-0 80
10:06:00.617066 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.2 > 192.168.1.2:  ip-proto-0 80
10:06:00.617735 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.1 > 192.168.1.3:  ip-proto-0 80
10:06:00.618390 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.2 > 192.168.1.3:  ip-proto-0 80
10:06:00.619039 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.1 > 192.168.1.4:  ip-proto-0 80
10:06:00.619680 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.2 > 192.168.1.4:  ip-proto-0 80
10:06:00.620331 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.1 > 192.168.1.5:  ip-proto-0 80
10:06:00.620951 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.2 > 192.168.1.5:  ip-proto-0 80
10:06:00.621634 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.1 > 192.168.1.6:  ip-proto-0 80
10:06:00.622259 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.2 > 192.168.1.6:  ip-proto-0 80
10:06:00.622898 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.1 > 192.168.1.7:  ip-proto-0 80
10:06:00.623542 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.2 > 192.168.1.7:  ip-proto-0 80
10:06:00.624215 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.1 > 192.168.1.8:  ip-proto-0 80
10:06:00.624861 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.2 > 192.168.1.8:  ip-proto-0 80
10:06:00.625537 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.1 > 192.168.1.9:  ip-proto-0 80
10:06:00.626160 52:54:00:2f:40:c6 > 52:54:00:0d:a2:33, ethertype IPv4 (0x0800), length 114: 172.16.1.2 > 192.168.1.9:  ip-proto-0 80
  • 可以看到发出的报文是src/dst两个ip list的两两组合,2*9共18种组合,所以是发出了18个报文

其它字段的变化也可以做类似处理,就是对应字段的赋值为一个列表即可,无需赘述。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值