0

First of all I'm using Windows 7, (I've read that this might be causing my problems), I'm trying to send a RAW IP packet with a customized IP header using python, but when I'm trying to catch it with wireshark, I manage to catch a packet, but the packet looks like this, my windows kernel created an auto IP-header and added the IP-header I created as the IP payload, now what I'm trying to understand is what's wrong with my code? And how can I fix it so I'll be able send a raw IP packet with a customized IP header? Here's my code:

import socket
import struct

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)



ipheader = ''

byte1 = int(bin(4)[2:].zfill(4)+bin(5)[2:].zfill(4),2)

ipheader = ipheader + struct.pack('!B',byte1)

byte2 = 0

ipheader = ipheader + struct.pack('!B',byte2)

byte34 = 0

ipheader = ipheader + struct.pack('!H',byte34)

byte56 = 16

ipheader = ipheader + struct.pack('!H',byte56)

byte78 = 0

ipheader = ipheader + struct.pack('!H',byte78)

byte9 = 50 

ipheader = ipheader + struct.pack('!B',byte9)

byte10 = 6

ipheader = ipheader + struct.pack('!B',byte10)

byte1112 = 0 

ipheader = ipheader + struct.pack('!H',byte1112)

byte13 = 10
byte14 = 0
byte15 = 0
byte16 = 1

ipheader = ipheader + struct.pack('!4B',byte13,byte14,byte15,byte16)

byte17 = 8
byte18 = 8
byte19 = 8
byte20 = 8

ipheader = ipheader + struct.pack('!4B',byte17,byte18,byte19,byte20)

s.sendto(ipheader,("8.8.8.8",0))

print len(ipheader)

Thanks in advance.

user3687265
  • 103
  • 3
  • 12
  • Try calling `s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)` before you send the data. This tells the socket that the header is included in the data and therefore no default header should be created. – a_guest Sep 17 '14 at 17:17
  • Tried it already, doesn't help at all – user3687265 Sep 17 '14 at 17:26
  • And what if you use `socket.IPPROTO_RAW` instead of `socket.IPPROTO_IP`? So you'd create it like `socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)`. – a_guest Sep 17 '14 at 17:50
  • Already tried that as well, doesn't seem to work, tried almost every combination of this – user3687265 Sep 17 '14 at 18:18
  • Ok and from what do you see that it doesn't work? I just tried it using Ubuntu and I got with wireshark `length=34`, `info=[Malformed Packet]`. Only the checksum (which you set to 0) changed in my header. – a_guest Sep 17 '14 at 21:22
  • Yeah I've suspected this is a windows problem, If you try setting the TTL to I dont know 12, would it show in wireshark that the TTL is 12? ( That's the best way to see if it works) – user3687265 Sep 18 '14 at 05:13
  • Yep, just tried it with changing TTL and the chosen value appears in Wireshark in the header. So seems to work here. – a_guest Sep 19 '14 at 17:26
  • Alright, so it means it's an OS system, that's great thanks! – user3687265 Sep 19 '14 at 20:30
  • Hello, I am having the same issue under windows, using raw sockets I can not seem to send them, they do not appear on wireshar. I have used: s = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_IP) and s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, True) However no luck, packets are not sent. They are sent if I remove the last line. How did you fix this? – Cristiano Coelho Dec 28 '14 at 08:16
  • Wow this was a while ago, Install a linux based OS, you really can't program with raw sockets in python unless you use Linux, trust me – user3687265 Jan 09 '15 at 15:03

0 Answers0