Scapy样例三则

1. 演示ls()/lsc()用法: 

##Exec1.py

from scapy.all import *

## 列出scapy支持的命令
def ListScapyCmd():
    lsc()

## 列出指定协议的各个字段, 用于构成packet
def ListProtocolField(protoclName):
    ls(protoclName)

if __name__ == "__main__":
    print("\nexample of lsc()\n")
    ListScapyCmd()
    print("\nexample of ls()\n")
    ListProtocolField(TCP)

输出:

 

2.Scapy "/" 符号生成数据包, sr/send发送3层包. srp/sendp发送2层包.

## Exec2.py

from scapy.all import *

ifaceName = 'VMware Network Adapter VMnet8'
dstIP = '192.168.70.134'
dstMac = '00:0C:29:FB:48:0A'
srcIP = '192.168.70.1'
srcMac = '00:50:56:C0:00:08'

def ARPPacket():
    ## 构造以太网层
    etherLayer = Ether(dst=dstMac)
    
    ## 构造ARP-echo包
    arpLayer = ARP(hwtype=1,
                   ptype=0x800,
                   hwsrc=srcMac,
                   psrc=srcIP,
                   hwdst=dstMac,
                   pdst=dstIP)
    
    arpRequest = etherLayer/arpLayer
    ## use sendp to send level 2 packet
    ## 二层包需要用sendp发送
    sendp(arpRequest, iface=ifaceName, loop=200)
    
def ICMPPacket():
    ipLayer = IP(dst=dstIP)
    ## 模仿nmap -PP command, 构造ICMP包
    icmpTimestampRequest = ICMP(type=13,code=0) ## ICMP, timestamp request
    
    ## 模仿nmap -PM command
    icmpMacRequest = ICMP(type=17,code=0) ## ICMP, Mac address request

    ## 模仿nmap -PE command
    icmpEchoRequest = ICMP(type=8,code=0) ## ICMP, echo request

    for i in range(500):    
        pack = ipLayer/icmpTimestampRequest
        send(pack,iface=ifaceName)
        pack = ipLayer/icmpMacRequest
        send(pack,iface=ifaceName)
        pack = ipLayer/icmpEchoRequest
        ##  use sendp to send level 3 packet
        send(pack,iface=ifaceName)

def TCPPacket():
    ipLayer = IP(dst=dstIP, src=srcIP)
    tcpLayer = TCP(dport=[22,23,80,443,8080])
    pack = ipLayer/tcpLayer
    sr1(pack,iface=ifaceName,timeout=3)
    
def TCPPacketFlags():
    ## 构造IP层
    ipLayer = IP(dst=dstIP, src=srcIP)

    ## 构造TCP层, 向192.168.70.134:22,23,80,443,8080 5个端口发送TCP reset包(flags=RST)
    tcpLayer = TCP(dport=[22,23,80,443,8080],flags="R")

    ## 构造包
    pack = ipLayer/tcpLayer
    sr1(pack,iface=ifaceName,timeout=3)

if __name__ == "__main__":
    TCPPacket()
    TCPPacketFlags()
    ICMPPacket()
    ARPPacket()

Wireshark输出:

3.Scapy+PyShark实时抓包/TCPReplay. Scapy.sniff函数无法用display filter, 只能用PyShark代替. Scapy读取/重放 PyShark生成的pcap文件

## Exec3.py

from scapy.all import *
from pyshark import *

## live capture and file capture

ifaceName = 'VMware Network Adapter VMnet8'
path2tshark = 'C:\\Program Files\\Wireshark\\tshark.exe'
path2pCapFile = 'C:\\Users\\Eugene\\Desktop\\studio\\scapyMod\\1.pcap'

## scapy.sniff只能应用wireshark capture-filter,不能应用wireshark display-filter, 抓特定类型的packet需要通过pyshark中转.
## pyshark.LiveCapture一定要指定tshark_path(ex:C:\Program Files\Wireshark\tshark.exe)
## pyshark.LiveCapture.output_file指定pcap保存路径, 供scapy模块rdpcap/wrpcap使用
def PysharkLiveCapture():
    capObj = LiveCapture(interface=ifaceName,
                display_filter = "",
                bpf_filter = "",
                tshark_path = path2tshark,
                output_file = path2pCapFile)
    
    capObj.sniff(timeout=120)

def HandleLiveCapture():
    capturedPacks = rdpcap(path2pCapFile)
    for pack in capturedPacks:
        try:
            ## 用haslayer判断是否为IP包
            if pack.haslayer(IP) == True:
                print("pack.SrcIP: "+pack[IP].src+"\tpack.DstIp: "+pack[IP].dst)

                ## 用haslayer判断是否为ICMP包
                if pack.haslayer(ICMP) == True:
                    
                    ## 解析ICMP包中的各个字段
                    print("pack[ICMP].type:"+str(pack[ICMP].type)+" pack[ICMP].code:"+str(pack[ICMP].code))
        except:
            print("exception")

if __name__ == "__main__":
    ## PysharkLiveCapture()
    HandleLiveCapture()
    

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值