您现在的位置是:热点 >>正文
数据包处理利器――Scapy基础知识
热点76496699人已围观
简介...
什么 是scapy
Scapy是功能强大的交互式数据包处理程序。它能够伪造或解码各种协议的数据包,在线发送,捕获,匹配请求和响应等。它可以轻松处理大多数经典任务,例如扫描,跟踪路由,探测,单元测试,攻击或网络发现,它可以代替hping,arpspoof,arp-sk,arping,p0f甚至Nmap,tcpdump和tshark的某些部分。。它在其他工具无法处理的许多其他特定任务上也表现出色,例如发送无效帧,组合技术(VLAN跳变+ ARP缓存中毒,WEP加密通道上的VOIP解码等等)
安装scapy
直接pip安装即可,我使用的是python3
pip3 install scapy scapy基本使用
输入scapy回车进入scapy的shell 可以使用ls()来查看scapy支持的协议
使用lsc()查看scapy支持的函数
还可以使用ls()获取协议包含的参数
发送和接收数据包
send
- 在第3层发送数据包(Scapy创建第2层标头),不接收任何数据包。
loop 参数默认为0,如果它的值不是0,那么数据包将一直循环发送,直到按CTRL-C为止。 count 可用于设置要发送的数据包的确切数量。 inter 可用于设置每个数据包之间的秒数。 >>> send(IP(dst='8.8.8.8')/TCP(dport=53, flags='S')) . Sent 1 packets. >>> >>> send(IP(dst='8.8.8.8')/TCP(dport=53, flags='S'), count=10) .......... Sent 10 packets. >>> >>> send(IP(dst='8.8.8.8')/TCP(dport=53, flags='S'), loop=1) ......................... [... snipped ...] Sent 1503 packets.
sendp
与send()相同,但在第2层发送数据包(必须提供第2层标头),不接收任何数据包。 使用iface到设置界面上发送数据包。(如果未设置,将使用conf.iface数据的值) >>> sendp(Ether()/IP(dst="1.2.3.4",ttl=(1,4)), iface="eth0") .... Sent 4 packets. >>> sendp("I’m travelling on Ethernet", iface="eth0", loop=1, inter=0.2) >>> sendp(rdpcap("/tmp/pcapfile")) # tcpreplay ........... Sent 11 packets.
sr
发送数据包并接收响应。 sr()返回两个列表,第一个列表包含响应的,第二个列表包含未响应的。 >>> sr(IP(dst="60.205.177.168")/TCP(dport=[21,22,23])) Begin emission: Finished sending 3 packets. ...**...............................^C Received 36 packets, got 2 answers, remaining 1 packets (, ) >>> ans,unans=_ >>> unans.summary() IP / TCP 172.17.51.80:ftp_data > 60.205.177.168:telnet S >>> ans[0] (>, >) >>> ans[0][0] >
sr1
发送所有数据包并仅记录第一个响应。 >>> p=sr1(IP(dst="www.baidu.com")/ICMP()/"asdqwe")包处 Begin emission: Finished sending 1 packets. .* Received 2 packets, got 1 answers, remaining 0 packets
srloop
循环发送,接收响应并显示响应。 该函数返回几个数据包和响应,以及未响应的。 >>> packet = IP(dst='60.205.177.168')/ICMP() >>> srloop(packet) RECV 1: IP / ICMP 60.205.177.168 > 172.17.51.80 echo-reply 0 RECV 1: IP / ICMP 60.205.177.168 > 172.17.51.80 echo-reply 0 RECV 1: IP / ICMP 60.205.177.168 > 172.17.51.80 echo-reply 0 RECV 1: IP / ICMP 60.205.177.168 > 172.17.51.80 echo-reply 0 ^C Sent 4 packets, received 4 packets. 100.0% hits. (, ) 使用Scapy创建数据包 Scapy数据包的创建与网络中的分层方法一致。 数据包的基本构建块是一层,而整个数据包则是通过将各个层堆叠在一起而构建的。 scapy通过在TCP / IP的不同层上为每个协议定义数据包头,然后按顺序堆叠这些层,来构造数据包。
在一行中创建数据包
>>理利> packet = Ether()/IP(dst='8.8.8.8')/TCP(dport=53,flags='S')
分别创建每个图层并使用'/'运算符将它们堆叠
>>> l2 = Ether() >>> l3 = IP(dst='8.8.8.8/30') >>> l4 = TCP(dport=53, flags = 'S') >>> packet = l2/l3/l4
Scapy IP表示法
Scapy接受普通的IP表示法,CIDR表示法,主机名。
>>> packet = IP(dst = '8.8.8.8') >>> packet = IP(dst = 'scanme.nmap.org') >>> packet = IP(dst = '8.8.8.8/30') >>> [a for a in packet] [, , , ] >>> packet = IP(dst = 'egadz.metasploit.com/30')
创建一组数据包
我们可以使用Scapy创建一组数据包
>>> pkts = IP(ttl=[1,3,5,(7,10)])/TCP() >>> [pkt for pkt in pkts] [>, >, >, >, >, >, >] >>> packet=IP(dst="1器―92.168.*.1-10")/TCP(dport=(0,100)) >>> [a for a in packet] [>, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, >, ...
检查数据包
获取数据包的详细说明以及数据类型
>>> packet = IP()/TCP() >>> ls(packet) version : BitField = 4 (4) ihl : BitField ―S = None (None) tos : XByteField = 0 (0) len : ShortField = None (None) id : ShortField = 1 (1) flags : FlagsField = 0 (0) frag &canbsp; : BitField = 0 (0) ttl : ByteField = 64 (64) proto : ByteEnumField = 6 (0) chksum : XShortField = None (None) src : Emph = '127.0.0.1' (None) dspyt : Emph = '127.0.0.1' ('127.0.0.1') options : PacketListField = [] ([]) [-- snipped --]
show
显示详细的包头
>>> packet.show() ###[ IP ]### version= 4 ihl= None tos= 0x0 len= None id= 1 flags= frag= 0 ttl= 64 proto= tcp chksum= None src= 127.0.0.1 dst= 127.0.0.1 options ###[ TCP ]### sport= ftp_data dport= http 基础 seq= 0 ack= 0 dataofs= None reserved= 0 flags= S window= 8192 chksum= None urgptr= 0 options= []
show2
与show()类似,但可以组装数据包并计算校验和和IHL(报头长度,最小值是5)。
>>> packet.show2() ###[ IP ]### version= 4 ihl= 5 tos= 0x0 len= 40 id= 1 flags= frag= 0 ttl= 64 proto= tcp chksum= 0x7ccd src= 127.0.0.1 dst= 127.0.0.1 options ###[ TCP ]### sport= ftp_data dport=&nb知识sp;http seq= 0 ack= 0 dataofs= 5 reserved= 0 flags= S window= 8192 chksum= 0x917c urgptr= 0 options= []
summary
显示数据包的简短的摘要
>>> packet.summary() 'IP / TCP 127.0.0.1:ftp_data > 127.0.0.1:http S'
与数据包内部的字段进行交互
>>> Ether(dst="d8:55:a3:fe:80:78")/IP(dst="8.8.8.8") > >>> packet=_ >>> packet.dst 'd8:55:a3:fe:80:78' >>> packet[IP].dst '8.8.8.8'
检查数据包中是否存在层
haslayer方法
>>> if packet.haslayer(IP): ...: print (packet[IP].dst) ...: 8.8.8.8
使用in构造
>>> pkt = IP()/TCP()/DNS() >>>&nbs数据p;DNS in pkt True Scapy的sprintf sprintf()方法是Scapy的强大功能之一,在编写自定义工具时非常方便。 sprintf 用数据包中的值填充格式字符串,就像C语言库中的sprintf一样,不同的是这里用数据包中的字段值填充格式字符串。 >>> packet.sprintf("Ethernet source is %Ether.src% and IP proto is %IP.proto%") 'Ethernet source is 00:16:3e:0c:d1:ad and IP proto is tcp' >>> a.sprintf("%dst% %IP.dst% vlan=%Dot1Q.vlan%") '00:00:d4:ae:3f:71 192.168.0.1 vlan=42' >>> >>>a.sprintf(" %TCP.flags% | %5s,TCP.flags% | %#05xr,TCP.flags%") ' RA | RA | 0x014'
数据包处理程序
我们可以使用lambda函数编写处理TCP数据包的数据包处理程序,但该功能仅适用于TCP数据包。
>>> f=lambda x:x.sprintf("%IP.dst%:%TCP.dport%") >>> f(IP(dst="8.8.8.8")/TCP()) '8.8.8.8:http' >>> f(IP(dst="8.8.8.8")/UDP()) '8.8.8.8:??'
还可以使用sprintf()中的条件子字符串来实现处理其它层的目的。条件子字符串仅在数据包中存在某个层时才触发,否则将被忽略。还可以!用于检查是否缺少图层。条件子字符串格式: {[!]层:子字符串}
>包处>> f=lambda x: x.sprintf("=> {IP:ip=%IP.dst% {UDP:dport=%UDP.dport%} ...: ... {TCP:%TCP.dport%/%TCP.flags%}{ICMP:type=%r,ICMP.type%}} ...: ... {!IP:not an IP packet}") >>> f(IP()/TCP()) '=> ip=127.0.0.1 http/S' >>> f(IP()/UDP()) '=> ip=127.0.0.1 dport=domain' >>> f(IP()/ICMP()) '=> ip=127.0.0.1 type=8' >>> f(Ether()/ARP()) '=> not an IP packet' 导入与导出数据
PCAP格式
从PCAP文件导入数据包。
pkts = rdpcap("temp.cap") pkts = sniff(offline="temp.cap")
将数据包导出到pcap文件。
wrpcap("temp.cap",pkts)
十六进制转储格式
Scapy允许以各种十六进制格式导出数据包。 使用hexdump()函数使用hexdump格式显示一个或多个数据包: >>> hexdump(s) 0000 D8 55 A3 FE 80 78 00 16 3E 0C D1 AD 08 00 45 00 .U...x..>.....E. 0010 00 28 00 01 00&nbs理利p;00 40 06 8B 5E AC 11 33 50 08 08 .(....@..^..3P.. 0020 08 08 00 14 00 50 00 00 00 00 00 00 00 00 50 02 .....P........P. 0030 20 00 A0 0D 00 00
十六进制字符串
还可以使用str()函数将整个数据包转换为十六进制字符串
>>> s >> >>> str(s) WARNING: Calling str(pkt) on Python 3 makes no sense! "b'\xd8U\xa3\xfe\x80x\x00\x16>\x0c\xd1\xad\x08\x00E\x00\x00(\x00\x01\x00\x00@\x06\x8b^\xac\x113P\x08\x08\x08\x08\x00\x14 \x00P\x00\x00\x00\x00\x00\x00\x00\x00P\x02 \x00\xa0\r\x00\x00'"
Base64
Scapy可以使用export_object()函数导出数据包的base64编码数据。 >>> export_object(s) b'eNprYEouTk4sqNTLSaxMLSrWyzHici3JSC3iKmTQDCpk1EiOT85PSU0u5krNAzG4Cpki7BkYGA7PCD20+PC+Qw0VDGJ2PIcnHlrLweDKwKDBwMjA4MB2qDvu0BpB4wAOIGAQYQhggIIAJgWGQwt4GRgKmSPYgPycxJLMPMNClrZC1qBCNnfHGxoeDcsdkv2AoKSQPUkPALURLMU器―=' >>> new_pkt = import_object 嗅探
Sniff()
sniff()函数可帮助我们捕获所有流量: 包括count,filter,iface,lfilter,prn,timeout选项。 >>> sniff(count=4, iface='eth0')
可以添加过滤以捕获需要的数据包,使用标准的tcpdump / libpcap语法:
>>> pkts = sniff(count=1,filter="tcp and host 60.205.177.168 and port 80") >>> pkts.summary() Ether / IP / TCP 172.17.51.80:54578 > 60.205.177.168:http S 可以做类似tcpdump的简单流量分析器 >>> pkts = sniff(count=5,filter="host 60.205.177.168",prn=lambda x:x.summary()) Ether / IP / TCP 172.17.51.80:54624 > 60.205.177.168:http S Ether / IP / TCP 60.205.177.168:54624 > 172.17.51.80:http S Ether / IP / TCP 172.17.51.80:http > 60.205.177.168:54624 SA Ether / IP / TCP 60.205.177.168:http > 172.17.51.80:54624 SA Eth―Ser / IP / TCP 172.17.51.80:54624 > 60.205.177.168:http A 也可以从pcap文件中嗅探数据包。 pkts = sniff(offline='test.pcap') >>> pkts.nsummary() 0000 Ether / IP / TCP 172.16.16.128:1606 > 74.125.95.104:http S 0001 Ether / IP / TCP 74.125.95.104:http > 172.16.16.128:1606 SA 0002 Ether / IP / TCP 172.16.16.128:1606 > 74.125.95.104:http A 0003 Ether / IP / TCP 172.16.16.128:1606 > 74.125.95.104:http PA / Raw 0004 Ether / IP / TCP 74.125.95.104:http > 172.16.16.128:1606 A / Padding >>> sniff(offline='test.pcap', lfilter = lambda s: s[TCP].flags == 18, prn = lambda x: x[IP].dst) 192.168.1.1
Tags:
相关文章
标签: 传音控股 非洲手机之王 港股上市 AI技术
热点作者|张宇 编辑|杨博丞 图源 |IC photo 近日,“非洲手机之王”传音控股正式向港交所递交招股书,拟于港股主板挂牌上市,稳步推进“A+H”双资本平台战略布局。 传音控股主要从事以手机为核心的智...
【热点】
阅读更多标签: 西山居 金山软件 仙剑情缘网络版 剑侠情缘网络版叁
热点云梦级4月23日云梦级消息,4月23日,@西山居游戏 发布声明。 近日,互联网平台出现假冒媒体名义发布的恶意伪造截图,捏造不实信息,系网络谣言,已严重损害我司合法权益和违反相关法律法规。对此,我司郑重...
【热点】
阅读更多标签: 爱美客 嗨体 濡白天使 医美
热点云梦级8月24日云梦级消息,据北京商报报道,近日,爱美客发布的 2026 年半年报显示,公司上半年实现营收 12.16 亿元、归母净利润 5.93 亿元,同比分别下降 6.42% 和 24.84%,连...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.swecobelgium.be/?page=9&s=谷歌霸屏留痕【tg:seogoogle987】,UaL
- https://www.paxnet.co.kr/search/jongmok?kwd=谷歌霸屏留痕【tg:seogoogle987】vzF
- https://www.remit.co.jp/pt/?action=CHECK&s=谷歌霸屏留痕【tg:seogoogle987】,vaI
- https://www.hanbat.ac.kr/search/front/Search.jsp?qt=谷歌霸屏留痕【tg:seogoogle987】Ja5
- https://wydden.com/?ID=r8w9WMSKWkOcmiaFWaUv0dzMdqZWfKtF&s=谷歌霸屏留痕【tg:seogoogle987】,RI3
- https://www.realsporting.com/buscador?buscar=谷歌霸屏留痕【tg:seogoogle987】,c5u
- https://www.openstreetmap.org/search?query=谷歌霸屏留痕【tg:seogoogle987】,BOa
- https://www.swanagerailway.co.uk/search?q=谷歌霸屏留痕【tg:seogoogle987】,5pi
- https://www.varian.com/en-au/search?cat=113&search=谷歌霸屏留痕【tg:seogoogle987】,OOi
- https://www.temu.com/search_result.html?search_key=谷歌霸屏留痕【tg:seogoogle987】KZr
- https://xehay.vn/tim-kiem/=谷歌霸屏留痕【tg:seogoogle987】,fop
- https://www.whatsonstage.com/search?cat=64&term=谷歌霸屏留痕【tg:seogoogle987】,Zqw
- https://www.kelsa.co.uk/catalogsearch/result/index/?T=1686132119291&q=谷歌霸屏留痕【tg:seogoogle987】,3Tl
- https://www.seagrant.wisc.edu/?page=63&s=谷歌霸屏留痕【tg:seogoogle987】,os6
- https://www.trockenmittel.ch/catalogsearch/result/?iC=ZM3&q=谷歌霸屏留痕【tg:seogoogle987】,VTy
- https://www.roberthalf.com/us/en/jobs/all/谷歌霸屏留痕【tg:seogoogle987】6Zg
- https://www.rotherham.gov.uk/site-search/results/?q=谷歌霸屏留痕【tg:seogoogle987】,5FR
- https://www.abbreviations.com/谷歌霸屏留痕【tg:seogoogle987】u5y
- https://www.mdbg.net/chinese/dictionary?page=worddict&wdrst=0&wdqb=谷歌霸屏留痕【tg:seogoogle987】,tmb
- https://www.paspaley.com/catalogsearch/result/?action=QUERY&q=谷歌霸屏留痕【tg:seogoogle987】,A9f
- https://www.remit.co.jp/vi/?s=谷歌霸屏留痕【tg:seogoogle987】6eD
- https://www.temu.com/search_result.html?search_key=谷歌霸屏留痕【tg:seogoogle987】XQl
- https://www.paulaschoice.hk/zh_hant/catalogsearch/result/?q=谷歌霸屏留痕【tg:seogoogle987】,AsD
- https://www.teamlab.art/fr/?submit=谷歌霸屏留痕【tg:seogoogle987】,z1O
- https://www.writingforward.com/?action=SEARCH&s=谷歌霸屏留痕【tg:seogoogle987】,QPg
- https://www.lindseymedicalsupply.com/search/谷歌霸屏留痕【tg:seogoogle987】,PrD
- https://www.rockngem.com/?action=CHECK&s=谷歌霸屏留痕【tg:seogoogle987】,90B
- https://www.sdcard.org/ja/?s=谷歌霸屏留痕【tg:seogoogle987】,7Vx
- https://www.monroecounty.gov/search/?q=谷歌霸屏留痕【tg:seogoogle987】,zFs
- https://www.teamlab.art/zh-hans/?submit=谷歌霸屏留痕【tg:seogoogle987】,VXg
- https://jihoon471.cafe24.com/product/search.html?banner_action=&keyword=谷歌霸屏留痕【tg:seogoogle987】qxz
- https://www.teamlab.art/?submit=谷歌霸屏留痕【tg:seogoogle987】,ulR
- https://www.jusoen.com/addreng.asp?p1=谷歌霸屏留痕【tg:seogoogle987】thh
- https://www.teamlab.art/ko/?submit=谷歌霸屏留痕【tg:seogoogle987】,svE
- https://www.nbm.org/?action=QUERY&s=谷歌霸屏留痕【tg:seogoogle987】,5Rp
- https://www.lifesciencesweden.se/search?q=谷歌霸屏留痕【tg:seogoogle987】,oJx
- https://www.tumblr.com/tagged/谷歌霸屏留痕【tg:seogoogle987】,6Xn
- https://www.tombow.com/search/谷歌霸屏留痕【tg:seogoogle987】,01q
- https://www.vfw.org/search-results?q=谷歌霸屏留痕【tg:seogoogle987】,mp7
- https://www.selfsufficientish.com/main/?s=谷歌霸屏留痕【tg:seogoogle987】,ElL
- https://www.nextavenue.org/?action=QUERY&s=谷歌霸屏留痕【tg:seogoogle987】,K4l
- https://www.sebson.de/en/search?query=谷歌霸屏留痕【tg:seogoogle987】,1qy
- https://www.youm7.com/Home/Search?allwords=谷歌霸屏留痕【tg:seogoogle987】,hYE
- https://www.kaiyueleather.com/search/谷歌霸屏留痕【tg:seogoogle987】,ASL
- https://www.wilsonart.com/catalogsearch/result/?action=CHECK&q=谷歌霸屏留痕【tg:seogoogle987】,gv6
- https://www.symatoys.com/search.html?ID=UyMD6aO58bPrJvQAB1I0N69KzJBZeNPC&keyword=谷歌霸屏留痕【tg:seogoogle987】,BqF
- https://www.sonepar.ch/fr/search?q=谷歌霸屏留痕【tg:seogoogle987】,mHj
- https://5gaa.org/?token=6e2l7hjt2i26gzgctgwdrv7v0j6nxorq&s=谷歌霸屏留痕【tg:seogoogle987】H7a
- https://www.prattlibrary.org/search?q=谷歌霸屏留痕【tg:seogoogle987】,QW2
- https://hsmoa.com/search?query=谷歌霸屏留痕【tg:seogoogle987】X3l