我有一个使用TCP与服务器通信的应用程序,我正在尝试模拟该服务器并与我的应用程序通信。
我还做了mitm,将应用程序流量重定向到我的计算机。问题是我不能在python中使用套接字,因为IP是我的IP,而不是服务器的IP,所以我试着在替身中这样做,以欺骗源IP,但我没有套接字,很容易使用API。
在scapy模块中有没有写成的套接字模块或者内置的函数/类,允许你使用类似套接字的API来处理TCP通信?或者,使用python中的socket模块并假冒源IP。
发布于 2019-05-18 09:23:10
嗯,我不得不自己实现它,我开始用python编写socket类,它可以工作,但它并不完美。
from scapy.all import *
class socket(object):
def __init__(self, src_ip, dst_ip, port):
self.src_ip = src_ip
self.dst_ip = dst_ip
self.port = port
self._seq = 10000
self._ack = 0
self.dport = None
def handshake(self):
#print self.src_ip, self.dst_ip
syn = sniff(count=1, lfilter = lambda x: x.haslayer(IP) and x.haslayer(TCP) and x[IP].dst == self.src_ip and x[IP].src == self.dst_ip and x[TCP].dport == self.port)[0]
self.dport = syn[TCP].sport
syn_ack = IP(src = self.src_ip, dst = self.dst_ip) / TCP(flags = 'SA', seq = 20000, ack = syn[TCP].seq + 1, sport = self.port, dport = syn[TCP].sport)
send(syn_ack)
self._ack = syn_ack[TCP].ack
def recv(self):
push = sniff(count=1, lfilter = lambda x: x.haslayer(IP) and x.haslayer(TCP) and x[IP].dst == self.src_ip and x[IP].src == self.dst_ip and x[TCP].dport == self.port and
x[TCP].flags == 24)[0]
#self._ack += len(push[TCP].load)
#pack = IP(src = self.src_ip, dst = self.dst_ip) / TCP(flags = 'A', seq = self._seq, ack = self._ack, sport = 8070, dport = push[TCP].sport)
#send(pack)
# saves last packet's seq, ack and length of data
a, b, c = push[TCP].seq, push[TCP].ack, len(push[Raw].load)
self._seq = b
self._ack = a + c
pack = IP(src = self.src_ip, dst = self.dst_ip) / TCP(flags = 'A', seq = self._seq, ack = self._ack, sport = self.port, dport = push[TCP].sport)
send(pack)
return push[Raw].load
def send(self, data):
pack = IP(src = self.src_ip, dst = self.dst_ip) / TCP(flags = 24, seq = self._seq, ack = self._ack, sport = self.port, dport = self.dport) / data
send(pack)
def fin(self):
fin_ack = sniff(count=1, lfilter = lambda x: x.haslayer(IP) and x.haslayer(TCP) and x[IP].dst == self.src_ip and x[IP].src == self.dst_ip and x[TCP].dport == self.port and
x[TCP].flags == 17)[0]
a, b = fin_ack[TCP].seq, fin_ack[TCP].ack
pack = IP(src = self.src_ip, dst = self.dst_ip) / TCP(flags = 17, seq = b, ack = a + 1, sport = self.port, dport = self.dport)
send(pack)您可以在my github中遵循此模块的未来版本:https://github.com/dindibo/Scapy-socket
https://stackoverflow.com/questions/56152992
复制相似问题