我写了一个客户端,它将数据包发送到服务器,现在我需要创建一个攻击程序(在本地主机上)侦听客户端和服务器之间的连接,并打印数据包内容,该攻击程序不是该连接程序的一部分。我遇到一些问题,我知道我需要使用原始套接字,但是我不知道为什么我不能这样做。
服务器:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('localhost', 12321)
print >> sys.stderr, 'starting up on localhost port 12321'
sock.bind(server_address)
while True:
data, address = sock.recvfrom(100)
if data:
sent = sock.sendto(data, address)
print >> sys.stderr, 'sent %s bytes back to %s' % (sent, address)
客户端:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('localhost', 12321)
i = 0
while True:
f = open("poem.txt", "r")
for line in f:
time.sleep(3)
i += 1
sent = sock.sendto(line, server_address)
data, server = sock.recvfrom(100)
f.close()
print >>sys.stderr, 'closing socket'
sock.close()
对手:
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)
s.bind(("localhost", 1))
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
while True:
print s.recvfrom(12321)
在对手中,我收到各种消息,但没有收到客户端发送的消息(客户端发送了歌曲)。请帮助...
答案 0 :(得分:1)
问题在于您的绑定, socket.bind()接受一个地址元组(IP,PORT)
您的客户端绑定到端口12321,但您的对手设置为端口1
s.bind(("localhost", 1)) #change 1 to 12321
此外,socket.recvfrom()会将缓冲区大小作为参数而不是端口。
print s.recvfrom(12321) #change to buffer size
查看套接字的文档: https://docs.python.org/2/library/socket.html
另外,我是否建议使用Scapy工具, 在Windows和Linux上都易于使用
只需在您的cmd中键入pip install scapy
,然后
在Windows上,请确保您在scapy中安装了npcap
https://nmap.org/npcap/windows-10.html
你准备好了
在安装scapy之后,您只需要这样一行:
sniff(filter="udp and host 127.0.0.1 and dst port 12321", prn=lambda x:x.sprintf("{IP:%IP.src% -> %IP.dst%\n}{Raw:%Raw.load%\n}"))