假设我设法处于客户端和服务器之间的通信中间(假设我打开一个热点并导致客户端仅通过我的机器连接到服务器)。
如何更改客户端发送和接收的数据包而不中断我自己与其他服务的通信?必须有一种方法可以通过我的脚本路由客户端发送和即将接收(在将它们转发给他之前)的所有数据包。
我认为实现这一目标的正确方向是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 http://www.netfilter.org/projects/libnetfilter_queue/doxygen/其中有python 绑定 https://pypi.python.org/pypi/NetfilterQueue.
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 http://www.netfilter.org/projects/libnetfilter_queue/doxygen/group__Queue.html#gae36aee5b74d0c88d2f8530e356f68b79到 nfqueue,告诉它应该接受数据包 - 即允许它继续沿着内核中的正常路线前进。要修改数据包,而不是accept
ing它,你需要复制它,返回一个drop
判决,最后重新发送包含修改的内容。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)