How to send source ethernet frame in python

I need to make a project in a few days, its basic client and server interface. The trick is that it should be all raw sockets. I have no problem creating this, I am just stuck when sending packets.

At first I tried to bind it to the 'en1' interface, but it continues to give me a nodename not known error. When I bind it to the local IP address, it works fine. Upon completion of this, I created a class of raw packages, all in hexadecimal. Then I sent the package to be sent by wire.

The problem is that when capturing a packet using wirehark, it appears as the payload of the ipv4 packet. I don't want it to automatically create headers, so my package class was there anyway. Do you know how I can take out these headers?

Here is my code - just a raw function:

 def raw(): HOST = gethostbyname('192.168.1.10') s = socket(AF_INET, SOCK_RAW, IPPROTO_IP) s.bind((HOST, 0)) s.setsockopt(IPPROTO_IP, IP_HDRINCL, 0) #no headers - it wont work!! pckt = packet("\x68\x65\x6c\x6c\x6f") netpacket = pckt.getpacket() print "Sending.. " print "" s.sendto(netpacket, ('192.168.1.1', 80)) data = s.recv(4096) print data 

o and here is the captured packet with a greeting at the end:

 007f 2809 6da2 28cf daee 2156 0800 4500 004d 1bfc 0000 4000 db59 c0a8 010a c0a8 0101* 007f 2809 6da2 28cf daee 2156 0800 4500 0036 2352 4000 4006 0000 c0a8 010a c0a8 0101 15c0 0050 0000 0000 0000 0000 8010 813b 0000 68656c6c6f -hello 

* this is the beginning of the payload, although it is assumed that this will be the beginning of the package also I will not use any other modules that I should use the socket.

+4
source share
2 answers

thanks to the comments on this, I was able to get a connection to the server. all that is needed is to change the address family to af_packet in linux. then I tied it to my nic and sent it. it worked. Thanks for helping people! here is a sample code:

 s = socket(AF_PACKET, SOCK_RAW) s.bind(("en1", 0)) pckt = packet() #my class that initializes the raw hex data = pckt.getpacket() s.send(data) message = s.recv(4096) print s print s.decode('hex') 

It must be on linux or debian. to my knowldege it does not work on mac osx. idk about windows. if you have mac pycap or scapy, they work fine.

+5
source

In answer to your question from Andrew, here is an example:

This answer helped me get WoL to work. In this case, the data:

 preamble = bytearray((0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)) MAC = bytearray((0x00, 0x14, 0x85, 0xa4, 0x73, 0xce)) data = PREAMBLE + 16*MAC 
0
source

All Articles