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 local network, and the other to the network device. It looks something like this:

enter image description here

I sniff the packages on both interfaces and then send them to others using sendp(x,iface='eth0') for the package that I sniffed on eth1 and vice versa.

I checked the packets on both interfaces and found them correct, but for some reason I can not get the IP address for the device. Below is a snippet 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 someone help me with a problem or suggest me an alternative solution for this implementation?

SOLUTION: Combining Python + IPTABLES and using TRIGGER principles solves this problem.

+3
python network-programming scapy
source share
1 answer

Posting a Bridge Class Fragment

 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() 
0
source share

All Articles