I needed a script that would monitor network traffic and capture and process only DHCP traffic. It turned out I couldn't find such script so I decided to write one (more about that script in another post). For a language I decided to use Python. That was the easy part. Now, I had to decide which libraries I will use that will allow me to capture network traffic, decode DHCP request and responses, and manipulate IP addresses on interfaces.
I started with the network traffic capturing. pcap library is the library for network capture, so it was natural for me to search for a Python interface to this library. I found several such interfaces, i.e. pcap, pylibpcap, pypcap, and pcapy. There is also library interface specifically for Python 3, i.e. py3kcap. While searching for pcap interface, three other Python libraries poped out: libdnet (here is the old project page), dpkt and scapy.
But, not all libraries are equal, nor they serve the same purpose. libdnet allows sending packets, manipulation with kernel's routing tables, firewall and arp cache. So, besides Ethernet and IP, it doesn't offer much more in term of supported protocols. dpkt, on the other hand, is made just for this purpose! It supports easy creation and parsing of different TCP/IP protocols. Finally, Scapy is a swiss army knife of network manipulation. It offers shell in which one can manipulate packets, but also can be used within other scripts. Unfortunately, while browsing the source of Scapy I realized that it uses os.popen interface and calls external programs. So, this actually was enough for me to eliminate scapy from further consideration.
The next elimination criteria is availability of the packages within CentOS and Fedora. I try to hold on prepackaged software as much as possible, so quick search (yum search) showed that on both, CentOS 6 and Fedora 17, there are packages for pcapy and dpkt (named python-dpkt). For some reason, there is dnet, but python interface isn't packaged. I found this bugzilla entry, but without any answer!
So, I settled on pcapy and dpkt. The only piece of puzzle that was missing now is how to manipulate interface addresses. I stumbled on netifaces, which allows me to obtain information about interfaces and also on this post
for Windows. But all the results I got were on how to obtain IP
address. In the end, I gave up and decided that I'll try to use libdnet even
though I'll have to compile it from the source. Either that, or I'll use raw sockets and ioctls which are accessible from Python using standard libraries.
And for the end, as a curiosity, I'll mention that there is Python interface to IPTables, python-iptables, which is also packaged for Fedora.
Random notes of what's on my mind. Additional materials you'll find on my homepage.
Subscribe to:
Post Comments (Atom)
About Me
- Stjepan Groš (sgros)
- scientist, consultant, security specialist, networking guy, system administrator, philosopher ;)
Blog Archive
-
▼
2012
(124)
-
▼
July
(17)
- Searching for packet catpuring and interface manip...
- ntop 5 on CentOS 6...
- A case against wizards...
- Integrating FreeIPA and Alfresco...
- Biseri naših neukih novinara 5...
- Querying SNORT SQL database
- Temeljne dozvole na Linux/Unix operacijskom sustav...
- Korisnici i grupe u Linux operacijskom sustavu...
- Research paper: "Lessons from the PSTN for Dependa...
- ASLR to extreme
- Biseri naših neukih novinara 4...
- Zimbra log cluttering...
- Plaća i sindikati...
- VMWare Workstation DNS server...
- Colors in terminal...
- Sigurnost Hrvatskih Web stranica...
- Linux and Canon ImageRunner 2520
-
▼
July
(17)
2 comments:
Sounds similar to something I am keen to put together. I would like to monitor the DHCP traffic (wifi) and enumerate each time a device is issued an IP.
In this way a program can be run on the network and record when a new device is connected.
Even perhaps at a more advanced level initiating an nmap scan to further recon the device.
I started to write some kind of a DHCP monitor, and I also plan to integrate ARP monitor in it (also, partially written). It is written in Python (for a speed of writing, not executing :)) and for the time being, the code just listens on the network and displays received packets.
As soon as that becomes a little bit more useful, I'll put it on GitHub.
Post a Comment