简体   繁体   English

在python中生成一个欺骗性的UDP数据包

[英]generating a spoofed UDP packet in python

how can i create a spoofed UDP packet using python sockets,without using scapy library.如何在不使用 scapy 库的情况下使用 python 套接字创建欺骗性 UDP 数据包。 i have created the socket like this我已经创建了这样的套接字

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.sendto(bytes('', "utf-8"), ('192.168.1.9', 7043))# 192.168.1.9dest 7043 dest port

I think you mean changing the source and destination addresses from the IP layer (on which the UDP layer is based).我认为您的意思是从 IP 层(UDP 层所基于的)更改源地址和目标地址。

To do so, you will need to use raw sockets.为此,您需要使用原始套接字。 (SOCK_RAW), meaning that you have to build everything starting from the Ethernet layer to the UDP layer. (SOCK_RAW),这意味着您必须构建从以太网层到 UDP 层的所有内容。

Honestly, without scapy, that's a lot of hard work.老实说,没有 scapy,这是一项艰巨的工作。 If you wanted to use scapy, it would take 2 lines:如果你想使用 scapy,它将需要 2 行:

pkt = Ether()/IP(src=“...”, dst=“...”)/UDP()/...
sendp(pkt)

I really advice you to use scapy.我真的建议你使用 scapy。 The code itself is quite small so I don't see a reason not to use it.代码本身非常小,所以我没有看到不使用它的理由。 It's defiantly the easiest in python这无疑是 Python 中最简单的

This is one of the first results for google searches like "spoofing udp packet python" so I am going to expand @Cukic0d's answer using scapy.这是诸如“欺骗 udp 数据包 python”之类的 google 搜索的第一个结果之一,因此我将使用 scapy 扩展 @Cukic0d 的答案。

Using the scapy CLI tool (some Linux distributions package it separately to the scapy Python library ):使用 scapy CLI 工具(一些 Linux 发行版将其单独打包到 scapy Python 库):

pkt = IP(dst="1.1.1.1")/UDP(sport=13338, dport=13337)/"fm12abcd"
send(pkt)

This sends a UDP Packet to the IP 1.1.1.1 with the source port 13338 , destination port 13337 and the content fm12abcd .这将发送一个UDP数据包的IP 1.1.1.1与源端口13338 ,目标端口13337和内容fm12abcd

If you need to a certain interface for some reason (like sending over a VPN that isn't your default route) you can use send(pkt, iface='tun0') to specify it.如果您出于某种原因需要某个接口(例如通过不是默认路由的 VPN 发送),您可以使用send(pkt, iface='tun0')来指定它。

One difference to @Cukic0d's answer is that this solution is more flexible by sending a layer 3 packet with send instead of a layer 2 packet with sendp .与@Cukic0d 的答案的一个不同之处在于,通过使用send发送第 3 层数据包而不是使用sendp send第 2 层数据包,该解决方案更加灵活。 So it isn't necessary to prepend the correct Ethernet header with Ether() which can cause issues in some scenarios, eg:因此,没有必要在Ether()前面添加正确的以太网标头,这可能会在某些情况下导致问题,例如:

WARNING: Could not get the source MAC: Unsupported address family (-2) for interface [tun0]
WARNING: Mac address to reach destination not found. Using broadcast.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM