0

I am trying to implement DHCP client in python.I used a raw socket.

s=socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
def ip_packet(source_ip,dest_ip): 
     ip_ihl = 5
     ip_ver = 4
     ip_tos = 0
     ip_tot_len = 0 
     ip_id = 54321   #Id of this packet
     ip_frag_off =0
     ip_ttl = 255
     ip_proto = socket.IPPROTO_UDP
     ip_check = 0    
     ip_saddr = socket.inet_aton ( source_ip )  
     print ip_saddr.encode('hex')
     ip_daddr = socket.inet_aton ( dest_ip )

     ip_ihl_ver = (ip_ver << 4) + ip_ihl


     ip_header = struct.pack('!BBHHHBBH4s4s' , ip_ihl_ver, ip_tos, ip_tot_len, ip_id, ip_frag_off, ip_ttl, ip_proto, ip_check, ip_saddr, ip_daddr)
     return  ip_header

s.sendto(packet,('255.255.255.255',67)) 

I sat the source ip address to 0.0.0.0 ,but when I sniffed the generated packet in wireshark ,I was surprised to see that the source ip address is sat to my real ip address.

But when I used Scapy everything worked as expacted

sendp(Ether(dst="ff:ff:ff:ff:ff:ff")/IP(src="0.0.0.0",dst="255.255.255.255")/UDP(sport=68,dport=67)/BOOTP(chaddr=hw)/DHCP(options=[("message-type","request")]),count=3)

So, how dose Scapy manage to set the source ip address to 0.0.0.0?? Is it possible to do that using raw socket ??

  • 2
    you haven't provided testable code. – Karoly Horvath Mar 22 '14 at 11:51
  • 1
    You know 0.0.0.0 is a not a [real address](http://en.wikipedia.org/wiki/0.0.0.0) right? – Burhan Khalid Mar 22 '14 at 12:27
  • so how dhcp clients send packets where ip source and destination are set to 0.0.0.0? – user3379214 Mar 22 '14 at 19:04
  • Firstly to clarify the IP address of 0.0.0.0, as @user3379214 stated, the source IP address of DHCP discover packets must be 0.0.0.0. This is correct. As for the actual question itself, I think [this question and answer](http://stackoverflow.com/questions/12229155/how-do-i-send-an-raw-ethernet-frame-in-python) may help you. – wookie919 Aug 04 '14 at 21:42

0 Answers0