pylibnet

Welcome

pylibnet is a python module for the libnet packet injection library that was originally developed by David Margrave at the time of libnet 0.9.8a. The project has since been revived and is now under active maintenance by Nadeem Douba. The project now features support for libnet 1.1.x and has all of the packet building functionality that is provided by libnet. You can download the latest from here.

Todo

Some of the packet chaining and context queuing functionality that is available in libnet is still not available in the current release of pylibnet. However, it will be coming very soon.

Download

You can download the latest from here

Sample script

Before we begin you can always see the documentation for this module by using pydoc or help(libnet) within the Python interpretter.

The script below shows how easy it is to make an TCP SYN request packet with pylibnet.

Code for TCP-SYN request:

#!/usr/bin/python

import sys
import libnet
from libnet.constants import * 

# injection types can be LINK, LINK_ADV, RAW4, RAW4_ADV, RAW6, or RAW6_ADV
#
# LINK* injection types will offer you the added control of injecting the packet into
# the desired interface. However, you will have to build the link layer everytime
#
# RAW* injection types do not guarantee the packet will exit the desired interface.
# Instead, the kernel will route your packet accordingly. The link layer is automatically
# built.
l = libnet.context(
  RAW4,                        # The injection type
  'eth0'                       # Device name
  )

# We just need to get our destination IP since the RAW4 context and autobuild/build functions 
# will take care of the rest
dst_ip = l.name2addr4(
  'www.google.com', # Let's get the network byte ordered representation of this IP
  RESOLVE
  )


# Build the packet from the highest layer and work your way to the lowest
# i.e tcp->ipv4->ethernet. Order matters!
# Here we're going to start with the TCP header. Notice how we no longer 
# have to fill in all the other fields if we don't want to. They will be
# filled in automagically!
tcp_tag = l.build_tcp(
   dp=80,                      # destination port
   control=TH_SYN,             # control flags
   ) 


print 'TCP header:'
print l.getpacket(tcp_tag)     # What does this header look like?


# Now onto the IPv4 header
ipv4_tag = l.autobuild_ipv4(
   len=IPV4_H + TCP_H,         # IPV4 packet length
   prot=IPPROTO_TCP,           # Protocol type
   dst=dst_ip
   )


print '\nIPV4 header:'
print l.getpacket(ipv4_tag)

# Now let's write the packet and check for an error
# tcp syn google.com

l.write()

# Let's look at our stats
print '\nPacket stats:'
print l.stats()
 

And you should hopefully get this output:

TCP header:
{'th_urp': 0, 'th_x2': 0, 'th_ece': 0, 'th_win': 56904, 'th_off': 5, 'th_syn': 1, 
'th_cwr': 0, 'th_flags': 0, 'th_rst': 0, 'th_seq': 2078520984, 'th_dport': 80, 
'th_fin': 0, 'th_sum': 0, 'th_sport': 25497, 'th_ack': 0, 'th_push': 0}

IPV4 header:
{'ip_sum': 0, 'ip_v': 4, 'ip_dst': 'iy-in-f99.google.com', 'ip_off': 0, 'ip_p': 6, 
'ip_ttl': 64, 'ip_id': 1, 'ip_tos': 0, 'ip_len': 40, 'ip_src': 'u15367794.onlinehome-server.com', 
'ip_hl': 5}

Packet stats:
{'bytes_written': 40, 'packets_sent': 1, 'packet_errors': 0}

Support This ProjectSourceForge Logo