Network bridge using Scapy and Python

女生的网名这么多〃 提交于 2019-12-28 18:13:18

问题


I am creating a network bridge that connects two ethernet cards on the same machine. One of the cards is connected to the LAN and the other is connected to a network device. It looks something like this,

I am sniffing packets on both the interfaces and then sending them to the other using sendp(x,iface='eth0') for a packet that I sniffed on eth1 and vice versa.

I verified the packets at both the interfaces and found them to be correct, but somehow I am unable to get an IP for the device. Below is a piece of my code, I create two threads, one for each interface:

from scapy.all import*

**THREAD1:**
pkt=sniff(iface="eth0",store=1,count=1)
outbuff=[]
outbuff+=pkt[:]
for src in outbuff[:]
srcmac=src.sprintf(r"%Ether.src%")
if srcmac==deviceMAC:
    pass
else:
    sendp(self.outbuff[:],iface="eth1",verbose=0)

**THREAD2:**
pkt=sniff(iface="eth1",store=1,count=1)
outbuff=[]
outbuff+=pkt[:]
for src in outbuff[:]
srcmac=src.sprintf(r"%Ether.src%")
if srcmac==deviceMAC:
    sendp(self.outbuff[:],iface="eth1",verbose=0)
else:
    pass

Can some one help me with the problem or suggest me an alternative solution for this implementation?

SOLVED: Combining Python+IPTABLES and using the principles of TRIGGER solves this problem.


回答1:


Posting a snippet of the bridging class

from threading import Thread
import threading
import socket
import thread   


class iface0(threading.Thread):
    def __init__(self, MAC):
        Thread.__init__(self)
        pass

    def run(self):
        self.a = socket.gethostbyname_ex(socket.gethostname())[2]

        while 1:
            self.sSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
            self.sSock.bind((self.a[1],23432))
            self.iface0_sniff()
            self.sSock.close()


    def iface0_sniff(self):        
        self.sSock.sendto("THISISATESTWORLD",(self.a[1],78456))
        data = ''             


class iface1(threading.Thread):
    def __init__(self,MAC):
        Thread.__init__(self)
        pass

    def run(self):
        self.a=socket.gethostbyname_ex(socket.gethostname())[2]

        while 1:
            self.sSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
            self.iface1_sniff()
            self.sSock.close()            

    def iface1_sniff(self):

        self.sSock.sendto("THISISATESTWORLD",(self.a[1],98658))
        data = ''


if __name__ == '__main__':
    MAC = ['XX:XX:XX:XX:XX:XX']

    iface0 = iface0(MAC)       
    iface1 = iface1(MAC)

    iface1.start()
    iface0.start()


来源:https://stackoverflow.com/questions/12619068/network-bridge-using-scapy-and-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!