Code Ethernet II raw packet in python

Namaste Hackers,



Today, In this tutorial I am going to show you how you can easily create a raw Ethernet II 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 your 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

As we know, 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.
Now, let me share with you some important information about Ethernet frame.

Q 1. What is Ethernet II?
Ans. Well, According To Wikipedia  "A data packet on an Ethernet link is called an Ethernet packet, which transports an Ethernet frame as its payload.

An Ethernet frame is preceded by a preamble and start frame delimiter (SFD), which are both parts of the Ethernet packet at the physical layer. Each Ethernet frame starts with an Ethernet header, which contains destination and source MAC addresses as its first two fields. The middle section of the frame is payload data including any headers for other protocols (for example, Internet Protocol) carried in the frame. The frame ends with a frame check sequence (FCS), which is a 32-bit cyclic redundancy check used to detect any in-transit corruption of data." :- Wikipedia(Ethernet II)



Q 2. Ethernet Structure Format?
Ans.


As you can see In Above Image,  Today We Just going to play header Section of Ethernet II (Dix) Frame.


  • DST MAC Stands For Destination MAC Address
  • SRC MAC Stands For Source MAC Address
  • TYPE Stands For Protocol Type


Requirements


1. Python 2.x Or Python 3.x
2. Linux
3. Python Socket and Struct
4. Wireshark (For Verifying Create Ethernet 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. Basically, we just going to create an Ethernet frame using struct module and then send it using python socket module. Capture it With Wireshark And Then Verify it.



Example Codes



#import modules
import struct            # struct module
import binascii          # binary ASCII module
from IP import IPPacket  # Class To Create IPv4 Packet. (Check Code_IPv4_packet_using_socket Link)  





# Ethernet II (DIX) Protocol Types

ETH_P_IP       = 0x0800    # ternet Protocol packet 


# Simple Ethernet Frame Class
class EtherPacket:
 def __init__(self, dst='ff:ff:ff:ff:ff:ff', src='ff:ff:ff:ff:ff:ff', protocol=ETH_P_IP):
  self.dst = dst                # Destination MAC
  self.src = src                # Source MAC
  self.protocol = protocol      # Protocol Types
  self.raw = None               # Raw Data
  self.assemble_eth_feilds()

 def assemble_eth_feilds(self):
  # Assemble All Feilds Of Ether Packet

  self.raw = struct.pack(\
      "!6s6sH",\
      binascii.unhexlify(self.dst.replace(":","")),\ 
      binascii.unhexlify(self.src.replace(":","")),\
      self.protocol)
  return self.raw



Final Codes With Protocol Types



#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#
# --------------------------------------------------------------------------
#                         Don't Remove Authors Info                        |
# --------------------------------------------------------------------------


__author__         = 'Suraj Singh Bisht                  ' #  Name Of Author
__credit__         = '[]                                 ' #  Contributers Name
__contact__        = 'surajsinghbisht054@gmail.com       ' #  Email
__copyright__      = 'Copyright 2018 Suraj Singh Bisht   ' #  Copyright
__license__        = 'Apache 2.0                         ' #  LICENSE
__Update__         = '2018-01-11 12:00:29.991758         ' #  Last Update 
__version__        = '0.1                                ' #  Version
__maintainer__     = 'Suraj Singh Bisht                  ' #  Project Current Maintainer
__status__         = 'Production                         ' #  Project Status

#import modules
import struct            # struct module
import binascii          # binary ASCII module
from IP import IPPacket  # Class To Create IPv4 Packet. (Check Code_IPv4_packet_using_socket Link)  





# Ethernet II (DIX) Protocol Types

ETH_P_LOOP     = 0x0060    # hernet Loopback packet
ETH_P_PUP      = 0x0200    # rox PUP packet  
ETH_P_PUPAT    = 0x0201    # rox PUP Addr Trans packet 
ETH_P_IP       = 0x0800    # ternet Protocol packet 
ETH_P_X25      = 0x0805    # ITT X.25   
ETH_P_ARP      = 0x0806    # dress Resolution packet 
ETH_P_IEEEPUP  = 0x0a00    # rox IEEE802.3 PUP packet 
ETH_P_IEEEPUPAT= 0x0a01    # rox IEEE802.3 PUP Addr Trans packet 
ETH_P_DEC      = 0x6000    # C Assigned proto           
ETH_P_DNA_DL   = 0x6001    # C DNA Dump/Load            
ETH_P_DNA_RC   = 0x6002    # C DNA Remote Console       
ETH_P_DNA_RT   = 0x6003    # C DNA Routing              
ETH_P_LAT      = 0x6004    # C LAT                      
ETH_P_DIAG     = 0x6005    # C Diagnostics              
ETH_P_CUST     = 0x6006    # C Customer use             
ETH_P_SCA      = 0x6007    # C Systems Comms Arch       
ETH_P_TEB      = 0x6558    # ans Ether Bridging  
ETH_P_RARP     = 0x8035    # verse Addr Res packet 
ETH_P_ATALK    = 0x809B    # pletalk DDP  
ETH_P_AARP     = 0x80F3    # pletalk AARP  
ETH_P_8021Q    = 0x8100    # 2.1Q VLAN Extended Header  
ETH_P_IPX      = 0x8137    # X over DIX   
ETH_P_IPV6     = 0x86DD    # v6 over bluebook  
ETH_P_PAUSE    = 0x8808    # EE Pause frames. See 802.3 31B 
ETH_P_SLOW     = 0x8809    # ow Protocol. See 802.3ad 43B 
ETH_P_WCCP     = 0x883E    # b-cache coordination protocol



# Simple Ethernet Frame Class
class EtherPacket:
 def __init__(self, dst='ff:ff:ff:ff:ff:ff', src='ff:ff:ff:ff:ff:ff', protocol=ETH_P_IP):
  self.dst = dst                # Destination MAC
  self.src = src                # Source MAC
  self.protocol = protocol      # Protocol Types
  self.raw = None               # Raw Data
  self.assemble_eth_feilds()

 def assemble_eth_feilds(self):
  # Assemble All Feilds Of Ether Packet

  self.raw = struct.pack(\
      "!6s6sH",\
      binascii.unhexlify(self.dst.replace(":","")),\ 
      binascii.unhexlify(self.src.replace(":","")),\
      self.protocol)
  return self.raw



def main():
 pkt = EtherPacket()
 pkt1 = IPPacket()
 try:
  from samples.wsk import ShowPacket
  ShowPacket([pkt.raw+pkt1.raw], link_type=1)
 except:
  print "[+] Unable To Find pye.samples.wsk script."
 return


if __name__=='__main__':
 main()



Run


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



if __name__=='__main__':
 # import module
 import socket

 # Create Raw Socket
 s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)

 pkt = EtherPacket()
 pkt1 = IPPacket()
 
  s.sendto(pkt.raw+pkt1.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 »