假设我设法处于客户端和服务器之间的通信中间(假设我打开了一个热点并使客户端只能通过我的机器连接到服务器)。
如何在不中断我自己与其他服务的通信的情况下更改我的客户端发送和接收的数据包?必须有一种方法可以通过我的脚本路由客户端发送和将要接收(在将它们转发给他之前)的所有数据包。
我认为实现这一目标的正确方向是使用 iptables
,但不确定究竟哪些参数适合使这项工作成功。我已经有了以下简单的脚本:
hotspotd start #a script that runs dnsmasq as both a DNS and DHCP server, configures and starts a hotspot
iptables -P FORWARD ACCEPT
iptables --append FORWARD --in-interface wlan0 -j ACCEPT
iptables --table nat --append POSTROUTING --out-interface eth0 -j MASQUERADE
#wlan0 is the interface on which the hotspot is.
#eth0 is the interface that is connected to the internet
现在,这非常适用于被动 MITM - 我可以看到客户端发送和接收的所有内容。但现在我想加强它并重定向他通过我发送和接收的每条消息。
我的最终目的是达到可以执行以下脚本的水平:
from scapy.all import *
from scapy_http.http import *
def callback(pkt):
#Process the packet here, see source and destination addresses, ports, data
send(pkt)
sniff(filter='port 666', prn=callback) #Assuming all relevant packets are redirected to port 666
我如何完成重定向客户端发送和即将接收的每个数据包?
最佳答案
您可以使用 NFQUEUE其中有 python bindings .
NFQUEUE 是一个用户空间队列,它是一个有效的 iptables 目标。您可以将一些流量重定向到 NFQUQUE:
iptables -I INPUT -d 192.168.0.0/24 -j NFQUEUE --queue-num 1
然后从您的代码访问数据包:
from netfilterqueue import NetfilterQueue
def print_and_accept(pkt):
print(pkt)
pkt.accept()
nfqueue = NetfilterQueue()
nfqueue.bind(1, print_and_accept)
try:
nfqueue.run()
except KeyboardInterrupt:
print('')
nfqueue.unbind()
注意 pkt.accept()
调用。这将返回 verdict到 nfqueue,告诉它它应该接受数据包 - 即允许它继续沿着内核中的正常路径前进。要修改数据包,而不是接受
,您需要复制它,返回一个 drop
判断,最后重新发送包含修改的数据包。
关于python - 使用 scapy 作为 MITM 即时更改数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39765107/