|
scapy库一.概述Scapy是一个功能强大且灵活的Python库,用于操作和探索计算机网络中的数据包。它允许用户构建各种类型的网络数据包,并支持多种网络协议,如TCP、UDP、ICMP、ARP等。Scapy不仅可以发送和接收数据包,还可以捕获、修改和解析网络流量,使用户能够深入研究网络通信并进行网络安全测试。二.scapy的基本使用依赖npcap,最好是最新版本安装:pipinstallscapy有两种使用方式:1.交互式使用2.python中使用查看当前设备所有网卡信息>>>show_interfaces()SourceIndexNameMACIPv4IPv6libpcap1SoftwareLoopbackInterface100:00:00:00:00:00127.0.0.1::1libpcap10RealtekPCIeGbEFamilyController80:fa:5b:11:9b:9e169.254.136.18fe80::14b7:abf9:a634:1813libpcap14Intel(R)DualBandWireless-AC726560:57:18:26:1f:29192.168.2.8fe80::12a1:306f:c371:b4felibpcap17MicrosoftWi-FiDirectVirtualAdapter60:57:18:26:1f:2a169.254.106.74fe80::6b57:f4f4:f5cf:5cb2libpcap18WANMiniport(IP)libpcap21WANMiniport(IPv6)libpcap9WANMiniport(NetworkMonitor)123456789捕获数据包>>>pkg=sniff(count=4,iface='Intel(R)DualBandWireless-AC7265',filter='icmp')如果卡住,说明还未抓到包,当抓到4个包,就会恢复>>>状态12查看数据包>>>pkg>>>pkg.show()0000Ether/IP/ICMP192.168.2.8>192.168.2.1echo-request0/Raw0001Ether/IP/ICMP192.168.2.1>192.168.2.8echo-reply0/Raw0002Ether/IP/ICMP192.168.2.8>192.168.2.1echo-request0/Raw0003Ether/IP/ICMP192.168.2.1>192.168.2.8echo-reply0/Raw>>>pkg[0]>>>>>>pkg[0].show()###[Ethernet]###dst=80:05:88:e9:45:11src=60:57:18:26:1f:29type=IPv4###[IP]###version=4ihl=5tos=0x0len=31id=32438flags=frag=0ttl=128proto=icmpchksum=0x36cesrc=192.168.2.8dst=192.168.2.1\options\###[ICMP]###type=echo-requestcode=0chksum=0x5941id=0x64e1seq=0x78unused=''###[Raw]###load='\\xe1dX'>>>pkg[0][IP].ttl12812345678910111213141516171819202122232425262728293031323334353637383940414243保存离线文件wrpcap('H:\\demo.pcap',pkg)第一个参数是路径,第二个参数是包名12读离线文件>>>pkg1=rdpcap('H:\\demo.pcap')括号跟文件路径12编辑数据包pkg2=IP(src='192.168.2.8',dst='192.168.2.1')/ICMP(type=8,code=0)/'abcdefg'1发送数据包send(pkt):发送三层数据包,但不会受到返回的结果sr(pkt):发送三层数据包,返回两个结果,分别是接收到响应的数据包和未收到响应的数据包sr1(pkt):发送三层数据包,仅仅返回接收到响应的数据包sendp(pkt):发送二层数据包srp(pkt):发送二层数据包,并等待响应srp1(pkt):发送第二层数据包,并返回响应的数据包有数字1,就是发送1个数据包,有r就是有返回数据包,有p从二层协议发送数据包>>>pkg3=sr1(pkg2)Beginemission:Finishedsending1packets....*Received4packets,got1answers,remaining0packets发送完后返回的响应就存储在pkg3变量里123456789101112131415三.python使用scapy库#使用pycharm发送icmp报文fromscapy.allimport*fromscapy.layers.inetimport*#编写一个icmp包pkg=IP(src='192.168.2.8',dst='www.woniuxy.com')/ICMP()/'abcdefg'#发送数据包pkg1=sr1(pkg,verbose=0)print(pkg1[IP].ttl)123456789四.基于arp扫描arp作用:通过IP地址查找mac地址pkg=ARP(pdst='192.168.2.33',psrc='192.168.2.8',op=1)pkg2=sr1(pkg,verbose=0)print(pkg2[ARP].hwsrc)优化try:pkg=ARP(pdst='192.168.2.100',psrc='192.168.2.8',op=1)pkg2=sr1(pkg,timeout=1)print(pkg2[ARP].hwsrc)exceptException:print('该IP的主机不存在')优化:引入循环foriinrange(1,255):try:pkg=ARP(pdst=f'192.168.2.{i}',op=1)pkg2=sr1(pkg,verbose=0,timeout=1)print(f'192.168.2.{i}的MAC是:{pkg2[ARP].hwsrc}')exceptException:print(f'192.168.2.{i}的MAC不存在')优化:引入多线程(作业)1234567891011121314151617181920212223五.TCP三次握手F:FIN结束,结束会话S:SYN同步,表示开始会话请求R:RST复位,中断一个连接P:PUSH推送,数据包立即发送A:ACK应答U:URG紧急E:ECE显式拥塞提醒回应W:CWR拥塞窗口减少#第一次握手包pkg=IP(dst='192.172.0.100')/TCP(dport=3306,flags='S')pkg2=sr1(pkg,verbose=0,timeout=1)#第二次握手包(pkg2)#第三次握手包ack=pkg2[TCP].seq+1seq=pkg2[TCP].ackpkg3=IP(dst='192.172.0.100')/TCP(dport=3306,flags='A',seq=seq,ack=ack)#发送第三个握手包pkg4=sr1(pkg3,verbose=0,timeout=1)#返回了第四个包,整个三次握手就成功了12345678910111213141516171819202122六.基于TCP的端口扫描foriinrange(1,65536):random_port=random.randint(10000,20000)pkg=IP(dst='192.172.0.100')/TCP(dport=i,sport=random_port,flags='S')pkg2=sr1(pkg,verbose=0,timeout=1)ifpkg2[TCP].flags=='SA':print(f'端口:{i}已开放')elifpkg2[TCP].flags=='RA':#print(f'----端口:{i}未开放----')pass123456789七.基于scapy的三次握手四次挥手fromscapy.allimport*fromscapy.layers.inetimport*importrandomrandom_port=random.randint(10000,65535)pkg1=IP(src="192.168.7.25",dst="192.172.0.100")/TCP(sport=random_port,dport=8080,seq=3472901066,ack=0,flags="S")pkg2=sr1(pkg1,iface="Intel(R)DualBandWireless-AC7265")seq=pkg2[TCP].ackack=pkg2[TCP].seq+1pkg3=IP(src="192.168.7.25",dst="192.172.0.100")/TCP(sport=random_port,dport=8080,seq=seq,ack=ack,flags="A")send(pkg3,verbose=0,iface="Intel(R)DualBandWireless-AC7265")pkg4=IP(src="192.168.7.25",dst="192.172.0.100")/TCP(sport=random_port,dport=8080,seq=seq,ack=ack,flags="FA")pkg5=sr1(pkg4,iface="Intel(R)DualBandWireless-AC7265")pkg6=IP(src="192.168.7.25",dst="192.172.0.100")/TCP(sport=random_port,dport=8080,seq=seq+1,ack=ack+1,flags="A")send(pkg6,verbose=0,iface="Intel(R)DualBandWireless-AC7265")123456789101112131415161718
|
|