• Scapy样例三则


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

    1. ##Exec1.py
    2. from scapy.all import *
    3. ## 列出scapy支持的命令
    4. def ListScapyCmd():
    5. lsc()
    6. ## 列出指定协议的各个字段, 用于构成packet
    7. def ListProtocolField(protoclName):
    8. ls(protoclName)
    9. if __name__ == "__main__":
    10. print("\nexample of lsc()\n")
    11. ListScapyCmd()
    12. print("\nexample of ls()\n")
    13. ListProtocolField(TCP)

    输出:

     

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

    1. ## Exec2.py
    2. from scapy.all import *
    3. ifaceName = 'VMware Network Adapter VMnet8'
    4. dstIP = '192.168.70.134'
    5. dstMac = '00:0C:29:FB:48:0A'
    6. srcIP = '192.168.70.1'
    7. srcMac = '00:50:56:C0:00:08'
    8. def ARPPacket():
    9. ## 构造以太网层
    10. etherLayer = Ether(dst=dstMac)
    11. ## 构造ARP-echo包
    12. arpLayer = ARP(hwtype=1,
    13. ptype=0x800,
    14. hwsrc=srcMac,
    15. psrc=srcIP,
    16. hwdst=dstMac,
    17. pdst=dstIP)
    18. arpRequest = etherLayer/arpLayer
    19. ## use sendp to send level 2 packet
    20. ## 二层包需要用sendp发送
    21. sendp(arpRequest, iface=ifaceName, loop=200)
    22. def ICMPPacket():
    23. ipLayer = IP(dst=dstIP)
    24. ## 模仿nmap -PP command, 构造ICMP包
    25. icmpTimestampRequest = ICMP(type=13,code=0) ## ICMP, timestamp request
    26. ## 模仿nmap -PM command
    27. icmpMacRequest = ICMP(type=17,code=0) ## ICMP, Mac address request
    28. ## 模仿nmap -PE command
    29. icmpEchoRequest = ICMP(type=8,code=0) ## ICMP, echo request
    30. for i in range(500):
    31. pack = ipLayer/icmpTimestampRequest
    32. send(pack,iface=ifaceName)
    33. pack = ipLayer/icmpMacRequest
    34. send(pack,iface=ifaceName)
    35. pack = ipLayer/icmpEchoRequest
    36. ## use sendp to send level 3 packet
    37. send(pack,iface=ifaceName)
    38. def TCPPacket():
    39. ipLayer = IP(dst=dstIP, src=srcIP)
    40. tcpLayer = TCP(dport=[22,23,80,443,8080])
    41. pack = ipLayer/tcpLayer
    42. sr1(pack,iface=ifaceName,timeout=3)
    43. def TCPPacketFlags():
    44. ## 构造IP层
    45. ipLayer = IP(dst=dstIP, src=srcIP)
    46. ## 构造TCP层, 向192.168.70.134:22,23,80,443,8080 5个端口发送TCP reset包(flags=RST)
    47. tcpLayer = TCP(dport=[22,23,80,443,8080],flags="R")
    48. ## 构造包
    49. pack = ipLayer/tcpLayer
    50. sr1(pack,iface=ifaceName,timeout=3)
    51. if __name__ == "__main__":
    52. TCPPacket()
    53. TCPPacketFlags()
    54. ICMPPacket()
    55. ARPPacket()

    Wireshark输出:

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

    1. ## Exec3.py
    2. from scapy.all import *
    3. from pyshark import *
    4. ## live capture and file capture
    5. ifaceName = 'VMware Network Adapter VMnet8'
    6. path2tshark = 'C:\\Program Files\\Wireshark\\tshark.exe'
    7. path2pCapFile = 'C:\\Users\\Eugene\\Desktop\\studio\\scapyMod\\1.pcap'
    8. ## scapy.sniff只能应用wireshark capture-filter,不能应用wireshark display-filter, 抓特定类型的packet需要通过pyshark中转.
    9. ## pyshark.LiveCapture一定要指定tshark_path(ex:C:\Program Files\Wireshark\tshark.exe)
    10. ## pyshark.LiveCapture.output_file指定pcap保存路径, 供scapy模块rdpcap/wrpcap使用
    11. def PysharkLiveCapture():
    12. capObj = LiveCapture(interface=ifaceName,
    13. display_filter = "",
    14. bpf_filter = "",
    15. tshark_path = path2tshark,
    16. output_file = path2pCapFile)
    17. capObj.sniff(timeout=120)
    18. def HandleLiveCapture():
    19. capturedPacks = rdpcap(path2pCapFile)
    20. for pack in capturedPacks:
    21. try:
    22. ## 用haslayer判断是否为IP包
    23. if pack.haslayer(IP) == True:
    24. print("pack.SrcIP: "+pack[IP].src+"\tpack.DstIp: "+pack[IP].dst)
    25. ## 用haslayer判断是否为ICMP包
    26. if pack.haslayer(ICMP) == True:
    27. ## 解析ICMP包中的各个字段
    28. print("pack[ICMP].type:"+str(pack[ICMP].type)+" pack[ICMP].code:"+str(pack[ICMP].code))
    29. except:
    30. print("exception")
    31. if __name__ == "__main__":
    32. ## PysharkLiveCapture()
    33. HandleLiveCapture()

  • 相关阅读:
    PY32F002A系列单片机:高性价比、低功耗,满足多样化应用需求
    java计算机毕业设计ssm框架的天气预报系统
    Briefings in bioinformatics2021 | QSAR模型中,传统表征要优于molecular embedding?
    django configparser.NoSectionError: No section: ‘Samples
    VIM 编辑文件命令
    第二届全国高校计算机技能竞赛——C++赛道 题解
    AVFrame相关api内存管理
    【BurpSuite】插件学习之Log4shell
    【无标题】
    通义千问-7B-Chat-8-9 离线整合包 使用 4bit 运行的代码
  • 原文地址:https://blog.csdn.net/lixiangminghate/article/details/133553324