Code To Create IPv4 packet header in Python

Namaste Hackers,



Today, In this tutorial I am going to show you how you can easily create a raw IP header packet using python socket module and struct module.


Friend, If You Are New on our Blog, Then Don't Forget To Follow Us On Social Media Because We Create These Types of Tutorials in Every Small Gap of the week and of course, with social media, you can easily get updates of this blog or you can also directly connect with me on facebook.

In today's module basically, we going to play with socket modules. so, To Make you skill more familiar with python and socket module. You Can Also Read Our Other Networking Posts Also.




Or You Can Also Check Our Complete Project List Here


Introduction


Many times for large networking project we have to write codes for creating raw networking packets without using any external libraries or modules. And at that time, to accomplish our work we have to play with binary level data. So, Basically, In Today's tutorial, I am going to show you how you can easily create IPv4 header packets with socket and struct module. This tutorial will give you important knowledge of python struct module methods. How we can use python for networking packets analyzing, creating, manipulation etc.



Requirements


1. Python 2.x Or Python 3.x
2. Linux
3. Python Socket and Struct
4. Wireshark (For Verifying Create IPv4 Packet)


How it's going to work?


As we know, python is not that much compatible with binary number operations if compared to C language but yes, there are some modules that provide us the facilities to play with binary level concepts. well, here struct module is our life-saving module which provides us the facility to play with the binary network packet. I am assuming that you all are already aware of python struct module and python socket module.


Example Codes To Understand.



Before Starting Our Example Codes, Take a look at this diagram.

This diagram is a representation of IPv4 Header Packet Structure. As you can see in this diagram there are various types of field, flags are available in IPv4 header packet. so, we going to load all these fields into our IPv4 packet to make it real. Now, let's move to practical codes that will provide you more understanding of today's concept

Step 1

Let's try to understand with a simple example. Basically Here In preview codes, I created a simple starting structure for python class object. In this initialize function, we will take two basic inputs from the user, destination IP address and source IP address.

class IPPacket:
 def __init__(self, dst='127.0.0.1', src='192.168.1.101'):
  self.dst = dst
  self.src = src
  self.raw = None
  self.create_ipv4_feilds_list()




Step 2

As I already mention IPv4 Diagram above, here in this function we are going to create all required fields with possible values.

For Example, In Internet Protocol Version Section i used 4 bit for ip_ver and another 4 bit for ip_vhl, IPv4 packet structure only provide 8 bits to pack Internet Protocol Version Feild. 

I hope now you all got the basic idea.

 def create_ipv4_feilds_list(self):

  # ---- [Internet Protocol Version] ----
  ip_ver = 4
  ip_vhl = 5

  self.ip_ver = (ip_ver << 4 ) + ip_vhl

  # ---- [ Differentiate Servic Field ]
  ip_dsc = 0
  ip_ecn = 0

  self.ip_dfc = (ip_dsc << 2 ) + ip_ecn

  # ---- [ Total Length]
  self.ip_tol = 0

  # ---- [ Identification ]
  self.ip_idf = 54321

  # ---- [ Flags ]
  ip_rsv = 0
  ip_dtf = 0
  ip_mrf = 0
  ip_frag_offset = 0

  self.ip_flg = (ip_rsv << 7) + (ip_dtf << 6) + (ip_mrf << 5) + (ip_frag_offset)

  # ---- [ Total Length ]
  self.ip_ttl = 255

  # ---- [ Protocol ]
  self.ip_proto = socket.IPPROTO_TCP

  # ---- [ Check Sum ]
  self.ip_chk = 0

  # ---- [ Source Address ]
  self.ip_saddr = socket.inet_aton(self.src)

  # ---- [ Destination Address ]
  self.ip_daddr = socket.inet_aton(self.dst)

  return


Step 3

Assemble All Feilds with the help of struct module and create an IPv4 Binary Structure. I hope my comments are providing you hints to understand the codes. (if struct module looks hard to you, comment below I will make a tutorial on it.)

 def assemble_ipv4_feilds(self):
  self.raw = struct.pack('!BBHHHBBH4s4s' , 
   self.ip_ver,   # IP Version 
   self.ip_dfc,   # Differentiate Service Feild
   self.ip_tol,   # Total Length
   self.ip_idf,   # Identification
   self.ip_flg,   # Flags
   self.ip_ttl,   # Time to leave
   self.ip_proto, # protocol
   self.ip_chk,   # Checksum
   self.ip_saddr, # Source IP 
   self.ip_daddr  # Destination IP
   )
  return self.raw





Finally

Final Preview To Understand Actually How Our Class Object Code looks like


class IPPacket:
 def __init__(self, dst='127.0.0.1', src='192.168.1.101'):
  self.dst = dst
  self.src = src
  self.raw = None
  self.create_ipv4_feilds_list()

 def assemble_ipv4_feilds(self):
  self.raw = struct.pack('!BBHHHBBH4s4s' , 
   self.ip_ver,   # IP Version 
   self.ip_dfc,   # Differentiate Service Feild
   self.ip_tol,   # Total Length
   self.ip_idf,   # Identification
   self.ip_flg,   # Flags
   self.ip_ttl,   # Time to leave
   self.ip_proto, # protocol
   self.ip_chk,   # Checksum
   self.ip_saddr, # Source IP 
   self.ip_daddr  # Destination IP
   )
  return self.raw


 def create_ipv4_feilds_list(self):

  # ---- [Internet Protocol Version] ----
  ip_ver = 4
  ip_vhl = 5

  self.ip_ver = (ip_ver << 4 ) + ip_vhl

  # ---- [ Differentiate Servic Field ]
  ip_dsc = 0
  ip_ecn = 0

  self.ip_dfc = (ip_dsc << 2 ) + ip_ecn

  # ---- [ Total Length]
  self.ip_tol = 0

  # ---- [ Identification ]
  self.ip_idf = 54321

  # ---- [ Flags ]
  ip_rsv = 0
  ip_dtf = 0
  ip_mrf = 0
  ip_frag_offset = 0

  self.ip_flg = (ip_rsv << 7) + (ip_dtf << 6) + (ip_mrf << 5) + (ip_frag_offset)

  # ---- [ Total Length ]
  self.ip_ttl = 255

  # ---- [ Protocol ]
  self.ip_proto = socket.IPPROTO_TCP

  # ---- [ Check Sum ]
  self.ip_chk = 0

  # ---- [ Source Address ]
  self.ip_saddr = socket.inet_aton(self.src)

  # ---- [ Destination Address ]
  self.ip_daddr = socket.inet_aton(self.dst)

  return





Run

To Run Above, Just Copy These Code In The End Of script.


if __name__=='__main__':
        # Create Raw Socket
 s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)

 ip = IPPacket()
 ip.assemble_ipv4_feilds()

 s.sendto(ip.raw, ('127.0.0.1' , 0 ))
 

Run it With Sudo permission.

In Linux:

:~# sudo script/name/path.py


I hope you enjoyed this tutorial.
for any suggestion or query
comment below.


have a nice day.

Share this

Related Posts

Previous
Next Post »