2016-09-29 108 views
1

假设我设法处于客户端和服务器之间的通信过程中(假设我打开一个热点并导致客户端仅通过连接到服务器我的机器)。用scapy改变数据包作为MITM

如何在不中断自己与其他服务的通信的情况下更改我的客户端发送和接收的数据包?必须有一种方法可以将客户端发送并即将接收的所有数据包(在将其转发给他之前)通过我的脚本进行路由。

我认为完成此操作的正确方向是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 

如何实现重定向的每个数据包的客户端发送和IS-约到接收?

回答

1

您可以使用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,告诉它它应该接受数据包 - 即允许它在内核中沿着它的正常路线继续。要修改数据包而不是accept,您需要复制数据包,返回drop判定,最后重新发送包含的修改。

+0

似乎工作。但是,如何识别哪些数据包来自设备,哪些来自我的机器? –

+0

你可以使用iptables或你的应用程序。 1.使用IP表可以根据数据源对数据包进行过滤,并只将相关的数据发送到nfqueue中(或根据源发送到两个单独的NFQUEUES)。 2.由于您的应用程序从NFQUEUE收到整个数据包,因此您可以简单地检查您收到的每个数据包的IP地址。 – Malt

+0

但不是在后期确定?我的意思是来自某些本地地址的数据包,我认为这些数据可以很容易地确定,但数据包**指向该设备的IP地址是什么?他们到我的外部知识产权,这样的壮举会起作用吗? –