Python Raw Socket to Ethernet Interface (Windows)

后端 未结 5 697
滥情空心
滥情空心 2021-01-15 06:14

I\'m trying to create a DHCP Server and the first step is for me to send packets through my ethernet port. I\'m trying to send packets to my Ethernet interface and having an

5条回答
  •  臣服心动
    2021-01-15 06:24

    As said multiple times already, ETH_P_ALL is not implemented on Windows, due to Win32 limitations. The alternative is called Winpcap (more recently Npcap), which sets up Windows to access such low-level things (it adds an extra driver)

    What you can do then is to use a Winpcap/Npcap based library such as Scapy, to access Raw low-level sockets. This requires to install Npcap (or Winpcap) on the computer.

    Then you can either use the library as-is (it has lots of capabilities of handling packets), or if you want to have access to the raw data

    from scapy.all import *
    IFACES.show() # let’s see what interfaces are available. Windows only
    iface = <<"full iface name">> or <> or <>
    socket = conf.L2socket(iface=iface)
    # socket is now an Ethernet socket
    ### RECV
    packet_raw = socket.recv_raw()[0]  # Raw data
    packet_decoded = socket.recv() # Using the library (also contains things like sent time...)
    ### SEND
    socket.send(b"\x00......"). # send raw data
    socket.send(Ether()/IP(dst="www.google.com")/TCP()/Raw(load=b"data")) # use library
    

提交回复
热议问题