7

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,

enter image description here

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.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
Abhinav
  • 992
  • 2
  • 11
  • 26
  • 1
    [Setting up a bridge](http://wiki.debian.org/BridgeNetworkConnections#Setting_up_your_Bridge) – Jakob Bowyer Sep 27 '12 at 10:17
  • 1
    The development is that the ETH1 card is getting an IP but Device isn't. When I logged the packets and saw in Wireshark, I could see the request from Device being sent all the way, but when the server responded, the packet somehow disappeared somewhere after reaching ETH0, it never reaches ETH1. On the contrary, when ETH1 sends a request it gets the reply perfectly. What can be the problem? – Abhinav Sep 27 '12 at 13:38
  • The device, in the set-up sends a DHCP Discover on the network. This request is visible both at ETH1 and ETH0. The server then sends a DHCP Offer which I see at ETH1 and ETH0 as a broadcast frame from the server. The problem is that the device is not accepting the configuration sent by the server and continues to send the DHCP Discover message overlooking the offer. On the hind-side, if I connect the device directly to the LAN, the device accepts the same DHCP Offer configuration. Any kind of explanation for this kind of anomaly? – Abhinav Sep 28 '12 at 09:50
  • Why are you trying to do this in user space with a python program? The Linux kernel has a complete ethernet bridge implementation already in kernel space. – tMC Sep 29 '12 at 04:50
  • I am implementing this on Windows. – Abhinav Oct 01 '12 at 12:26
  • 1
    Windows can do it as well: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/hnw_understanding_bridge.mspx?mfr=true – tMC Oct 02 '12 at 01:15
  • I know about this utility but I want to implement the same using a Python Script. – Abhinav Oct 02 '12 at 09:46
  • The first immediate problem that I am facing is that the DHCP ACK doesnot cross the bridge and the device is constantly sending DHCP Request. I have implemented this using sockets but in vain. – Abhinav Oct 25 '12 at 08:51
  • 1
    Anything you do with scapy in user space is going to be pathetically slow compared to a kernel-level bridge in the OS, you are wasting your time. Scapy just drops packets without informing you when you try to capture at rates faster than it can handle... – Mike Pennington Oct 31 '12 at 07:46
  • @MikePennington Could you please elaborate on this problem and the probable solution. I have tried comparing it with a utility called NetDisturb which can be used to bridge two Ethernet cards which is exactly I am trying to do, there is one unique feature that I found i.e. NetDisturb implements its own TCP/IP stack and we have to disable the default Windows stack. But I cannot make anything productive out of this but this is an observation. – Abhinav Nov 05 '12 at 15:04
  • Can you simply [Use windows to bridge the connections](http://helpdeskgeek.com/windows-7/bridge-network-connections-in-windows-7/)? – Mike Pennington Nov 05 '12 at 17:29
  • 1
    You're encouraged to answer your own question (in the answers section) if you come up with one. That way, when someone else has a similar problem and gets brought here by a search engine, they find it. – nmichaels Jun 19 '14 at 19:12

1 Answers1

0

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()
Abhinav
  • 992
  • 2
  • 11
  • 26
  • Hi sHoM, I am also trying to get a scapy bridge to work without success. Your own answer however does not seem to be a fully working example, is it? Did you have any success in the end? – Philipp F Nov 18 '15 at 16:42
  • Back in the day it had worked for me, I was successfully able to send and receive packets. But the only problem for me was, I had to use py2.6 since scapy was not supported for py2.7. As I see it now on its website, it has been upgraded to support py2.7 as well. – Abhinav Nov 19 '15 at 09:56