###[ Padding ]###load= ' 00 00 00 00 00 00 00 00 00 00 00 00 00 00'
1.数据包sniff
a=sniff(count=1,filter=\"tcp and host 192.168.1.1 and port 80\")
使⽤sniff主要是⽤于数据包的接收,根据filter设定的条件,将符合条件的数据包接收回来。3. 场景构造
scapy的缺点是,他只负责构造包,是单向的。不像packetdrill这么完美,packetdrill 不但可以构造包,还能实现系统调⽤构造不同的场景,还能帮你检查协议栈发出的数据包是否符合预期。撩协tcp协议栈的过程不外乎两端,⼀端使⽤系统调⽤模拟协议栈⾏为,另外⼀端则是我们构造的包。常见场景主要是:服务器场景、客户端场景。
服务器场景:
服务器场景使⽤系统调⽤(即⽤户态程序),⽽客户端则是scapy构造的包。
在这⾥构造⼀个简单的三次握⼿后向服务器端发送数据。为了防⽌Linux客户端rst。
iptables -A OUTPUT -p tcp --tcp-flags RST RST -s 192.168.56.1 -j DROP
#!/usr/local/bin/pythonfrom scapy.all import *# VARIABLESsrc = sys.argv[1]dst = sys.argv[2]
sport = random.randint(1024,65535)dport = int(sys.argv[3])# SYN
ip=IP(src=src,dst=dst)
SYN=TCP(sport=sport,dport=dport,flags='S',seq=1000)SYNACK=sr1(ip/SYN)# ACK
ACK=TCP(sport=sport, dport=dport, flags='A', seq=SYNACK.ack, ack=SYNACK.seq + 1)send(ip/ACK)
在这⾥可以安装⼀个nginx来验证。
客户端场景:
客户端场景使⽤系统调⽤(即⽤户态程序),⽽服务器端则是scapy构造包。
在这⾥使⽤scapy构造⼀个简单的http服务器。为了防⽌协议栈发送RST,需要对iptables进⾏设置。
iptables -A OUTPUT -p tcp --tcp-flags RST RST --sport 80 -j DROP
#!/usr/bin/python
from scapy.all import *
# Interacts with a client by going through the three-way handshake.
# Shuts down the connection immediately after the connection has been established.# Akaljed Dec 2010, http://www.akaljed.wordpress.com# Wait for client to connect.
a=sniff(count=1,filter=\"tcp and host 192.168.1.1 and port 80\")# some variables for later use.ValueOfPort=a[0].sportSeqNr=a[0].seqAckNr=a[0].seq+1
# Generating the IP layer:
ip=IP(src=\"192.168.1.1\# Generating TCP layer:
TCP_SYNACK=TCP(sport=80, dport=ValueOfPort, flags=\"SA\#send SYNACK to remote host AND receive ACK.ANSWER=sr1(ip/TCP_SYNACK)
# Capture next TCP packets with dport 80. (contains http GET request)
GEThttp = sniff(filter=\"tcp and port 80\AckNr=AckNr+len(GEThttp[0].load)SeqNr=a[0].seq+1
# Print the GET request
# (Sanity check: size of data should be greater than 1.)if len(GEThttp[0].load)>1: print GEThttp[0].load# Generate custom http file content.
html1=\"HTTP/1.1 200 OK 0d 0aDate: Wed, 29 Sep 2010 20:19:05 GMT 0d 0aServer: Testserver 0d 0aConnection: Keep-Alive 0d 0aContent-Type: text/html; charset=UTF-8 0d 0aContent-Length: 291 0d 0a 0d 0adata1=TCP(sport=80, dport=ValueOfPort, flags=\"PA\# Construct whole network packet, send it and fetch the returning ack.ackdata1=sr1(ip/data1/html1)# Store new sequence number.SeqNr=ackdata1.ack
# Generate RST-ACK packet
Bye=TCP(sport=80, dport=ValueOfPort, flags=\"FA\send(ip/Bye)# The End
这个服务器只需要使⽤wget或者curl就可以实现验证了。4. 参考资料总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作具有⼀定的参考学习价值,谢谢⼤家对的⽀持。如果你想了解更多相关内容请查看下⾯相关链接