简体   繁体   中英

Network bridge using Scapy and Python

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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