Writes raw IP data to interface (linux)
I have a file that contains raw IP packets in binary form. The data in the file contains the full IP header, TCP\UDP header, and data. I want to use any language (preferably python) to read this file and dump the data onto a line.
In Linux, I know you can write directly to certain devices (echo “DATA”>/dev/device_handle). Would opening on /dev/eth1 using python achieve the same effect (i.e. I can echo “DATA”>/dev/eth1).
#!/usr/bin/env python import socket s = socket.socket(socket.AF_PACKET, socket. SOCK_RAW) s.bind(("ethX", 0)) blocksize = 100; with open('filename.txt') as fh: while True: block = fh.read(blocksize) if block == "": break #EOF s.send(block)
It should work, but it hasn’t been tested yet.
ethXneeds to be changed to your interface (e.g.
- You might want to try
blocksize. 100 bytes at a time should be fine, you can consider increasing, but I would stay below the 1500-byte Ethernet PDU.
- You may need root/sudoer privileges to do this. I used to need them when reading data from raw sockets, but never tried simply writing to a socket.
- Provided that you do dump the packet (and only the packet) to a file. Nor is it any type of encoding (e.g. hexadecimal). If a byte is 0x30, it should be “0” in your text file, not “0x30”, “30” or something similar. If that’s not the case, you’ll need to replace the
whileloop with some processing, but
sendis still the same.
- Since I just learned that you are trying to send an IP packet – in this case, you may also need to build the entire packet at once and then push it to the socket. A simple
whileloop is not enough.