So now that sniffMyPackets is plodding along nicely I decided to start on my next Maltego/Canari love child project. This one is called Watcher and is essentially wireless scanning (and some other stuff) live within Maltego. The finished project will be a cross between Kismet/Aircrack-ng & Snoopy (the one from the guys at Sensepost not the cartoon dog).
I have literally started this project within the last week but a bit further down (at the end of the post) you will find a preview video with some of the features that are in place. The end goal is to have a Maltego machine running that will refresh the Maltego graph every 60 seconds or so (still working on that though).
For the time being Watcher isn’t available to download not until I’ve got a 60% work solution with a good set of tested transforms. This time around though my code will be a mix of offensive, defensive and bit of OSint thrown in so should be a bit of something for everyone.
Now for those that might be thinking, why bother recreating (did I mention it uses Scapy) tools that already exist, well the answer is “because I want to”, if you rewind back to a year ago I couldn’t actually write python code so for me it’s all a learning experience. I’m trying not to “re-use” other peoples code and I have emailed Glenn @ Sensepost to say that I’m writing something that is similar (in some places) to Snoopy. My intention is not to copy or steal ideas but rather write something the way I want to use (and hopefully you will) and learn as I go, but anywhere I use people’s ideas/code etc. will of course be accredited to them in the source code.
So have a look at the video, tell me what you think and I will keep you updated on my progress.
Morning readers, I thought I would start Monday morning with another piece of Scapy/Python coding goodness. This time though for an added treat I’ve thrown in a bit of tshark not because Scapy isn’t awesome but for this piece of code tshark works much better.
The code today, takes a pcap file and extracts all the TCP and UDP streams into a folder of your choice. If you’ve ever used Wireshark then you know this is a useful feature and it’s something that I’ve put into sniffmypackets as it’s a good way to break down a pcap file for easier analysis.
The code isn’t perfect (when is it ever), at the moment you will get the standard “Running as user “root” and group “root”. This could be dangerous.” error message if like me you run this on Kali. If you are using “normal” Linux you should be fine. I am looking at how to suppress the messages but the Python subprocess module isn’t playing nice at the moment.
The code has 2 functions, one for handling TCP streams, the other for UDP streams. For TCP streams we use the tshark “Fields” option to run through the pcap file and list of the stream indexes, we save that to a list (if they don’t already exist) and then we re-run tshark through the pcap file pulling out each stream at a time using the index and then write it to another pcap file.
For the UDP streams it’s a bit more “special”, UDP streams don’t have an index the same as TCP streams do, so we have to be a bit more creative. For UDP streams we use Scapy to list all the conversations based on source ip, source port, destination ip, destination port we then add that to a list, we then create the reverse of that (I called it duplicate) and check the list, if the duplicate exists we delete it from the list.
Why do we do that?? Well because we can’t filter on stream, parsing the pcap file will list both sides of the UDP conversation which mean when we output it we end up with twice as many UDP streams as we should have. By creating the duplicate we can prevent that from happening (took me a while to figure that out originally).
Once we have created our list of UDP streams we then use tshark to re-run the pcap file but this time with a filter (the -R switch). The filter looks a bit like this:
tshark -r ' + pcap + ' -R "(ip.addr eq ' + s_ip + ' and ip.addr eq ' + d_ip + ') and (udp.port eq ' + str(s_port) + ' and udp.port eq ' + str(d_port) + ')" -w ' + dumpfile
The parts in bold are from the list we created when we pulled out all the UDP conversations. Each UDP stream is then saved to a separate file in the same directory as the TCP streams.
To run the code (which can be found HERE) you need to provide two command line variables. The first is the pcap file, the second is your folder to store the output. The code will create the folder if it doesn’t already exist.
./pcap-streams.py /tmp/test.pcap /tmp/output
When you run it, it will look something like this:
If you then browse to your output folder you should see something like this (depending on the number of streams etc.
So there you go, enjoy.
So the 3rd blog post of the day is a cool function in Scapy called conversations. Essentially this takes a pcap file and outputs an image of all the conversations between IP addresses. To run this in Scapy you would do something like this:
What you should get is an image pop up on your screen with all the IP conversations in it, now that’s some cool shit.
Now why would you write a script for something that simple?? Well if you want to output the file (i.e. save it) there seems to be a bug with Scapy that it errors (well does for me). If you try this..
You get this:
>>> Error: dot: can't open /tmp/1.jpg
I did some research and it seems that command dot which is used to create the image, when you output it to a file has a slightly different syntax in the version on Kali.
So rather than raising a bug issue with Scapy I ported the code into my own python script (I was in a rush to use it in sniffmypackets).
The pcap-convo.py file can be found in my GitHub repo HERE:
To use the script use the following syntax:
./pcap-convo.py pcapfile outputfile
In real life that would be something like this:
./pcap-convo /root/pcap/test.pcap /tmp/out.jpg
Once it’s run you should see something like this:
Check your output file and you should have something that looks like this:
So there you go, another cool Python/Scapy lovechild.
So the second piece of code in my series on the python & Scapy lovefest is another simple bit of code that looks through a pcap file and pulls out some DNS information. The initial thought behind this was making it easy to look for DNS domains that might be “dodgy”, i.e. has lots of DNS answer records and a low(ish) TTL.
Now the downside of using Scapy to look at DNS records is that it’s very very difficult to pull out all the DNS answer records (the IP’s) that might be contained in a DNS Response packet. They are essentially nested in the Scapy packet and I haven’t worked out a way to return them all (yet). However fear not my packet warriors there is a DNS ancount field which tells you have many answers exist in the DNS response, so I’ve used that as a measure of how “dodgy” the domain is.
Again the script can be found on my GitHub repo HERE:
To run the script you simply need to do this:
I’m not going to post the code on the post this time (formatting is a bitch) but you should get something like this back (I used a fastflux pcap file for the demo).
The red coloured lines are DNS responses that have over 4 IP addresses in the response, you can change this in the code if you want but it does give you a good idea of what is going on. I also throw back the TTL because if it is something like Fast Flux it would probably have a high number of ancount records and a low TTL.
So I’ve been a bit “relaxed” lately with blog posts, simply because I’ve not had anything to say or share. To be honest the last couple of months my training has gone a bit all over the place and I’ve not really focused on much other than some changes to sniffmypackets.
What I have been doing for the last few months though, as part of my sniffmypackets work is writing python code that then goes into a Maltego Transform. Most of these are Scapy based and have been sitting in a private GitHub repo. Rather than let them gather dust I thought I would share them with you here. That means for the next few weeks expect a lot more posts, all python & Scapy related.
Today I’m going to share with you my pcap-geoip python script. It’s nice and simple using the MaxMind’s geoip database it looks through a pcap file (of your choice) and returns you the Latitude, Longitude and Google Maps URL for all public IP addresses. If it spots a private IP address it gives you a warning and continues on it’s way.
First off you need the MaxMinds GeoIP database, you can download this manually if you want put I have a script that will do it for you.
NOTE: Both of these files are available in my GitHub repo HERE:
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz -O /tmp/geoipdb.dat.gz
gzip -d /tmp/geoipdb.dat.gz
mv /tmp/geoipdb.dat /opt/geoipdb/
Like I said nice and easy.. Feel free to change the download location as you wish but you will need to change the python script to accommodate the change.
Next up is the python script (again sorry if I offend the python kings and queens with my code):
The script only needs one user defined variable to run, which is the location of the pcap file. To run the script (once you’ve updated the geoip database location) just type:
Within the python file you need to change this to match your geoip database location:
# Change this to point to your geoip database file
gi = pygeoip.GeoIP('/opt/geoipdb/geoipdb.dat')
import pygeoip, sys
from scapy.all import *
# Add some colouring for printing packets later
YELLOW = '33[93m'
GREEN = '33[92m'
END = '33[0m'
RED = '33[91m'
# Change this to point to your geoip database file
gi = pygeoip.GeoIP('/opt/geoipdb/geoipdb.dat')
if len(sys.argv) != 2:
print 'Usage is ./pcap-geoip.py pcap-file'
print 'Example - ./pcap-geoip.py sample.pcap'
pkts = rdpcap(sys.argv)
ip_raw = 
# Exclude "local" IP address ranges
ip_exclusions = ['192.168.', '172.', '10.', '127.']
for x in pkts:
src = x.getlayer(IP).src
if src != '0.0.0.0':
if src not in ip_raw:
for s in ip_raw:
# Check to see if IP address is "local"
if ip_exclusions in s or ip_exclusions in s or ip_exclusions in s or ip_exclusions in s:
print YELLOW + 'Error local Address Found ' + str(s) + END
# Lookup the IP addresses and return some values
rec = gi.record_by_addr(s)
lng = rec['longitude']
lat = rec['latitude']
google_map_url = 'https://maps.google.co.uk/maps?z=20&q=%s,%s' %(lat, lng)
print GREEN + '[*] IP: ' + s + ', Latitude: ' +str(lat)+ ', Longtitude: ' +str(lng) + ', ' + google_map_url + END
And there you have it, enjoy.
So I’m a bit late with this blog post as I wrote the code a couple of weeks ago, but as they say “better late than never”.
A couple of weeks ago on Twitter @stevelord raised a question about the existence of some code that would allow you to take the files that evil hackers upload to a Kippo SSH Honeypot and run them through a Cuckoo Sandbox instance. The response was “No” such a thing didn’t exist but wouldn’t it be great if it did.
Well that’s more than enough reason for me to have a go at writing something, the code I put online (via my GitHub repo) is still quite raw, I’ve not had any feedback to say if it works properly or not but the limited testing I did seemed to indicate that it does. So this is how it works.
On your Kippo honeypot upload the python script and ensure that it has read access to your kippo directories. There are a number of variables in the script that might need changing these are:
# User defined settings - change these if they are different for your setup
sandbox_ip = '127.0.0.1' # Define the IP for the Cuckoo sandbox API
sandbox_port = '8090' # Here if you need to change the port that the Cuckoo API runs on
kip_dl = '/var/kippo/dl/' # Kippo dl file location - make sure to include trailing /
file_history = 'history.txt' # Keeps a history of files submitted to Cuckoo
log_file = 'output-log.txt' # Log file for storing a list of submitted files and the task ID from Cuckoo
Most of them should make sense to you, but you will need to change them to match your environment (give me a shout if you get stuck).
Once that’s all in place the script works like this.
1. Creates a new history.txt file if it doesn’t exist (in the same directory as the script is located).
2. Creates a catalogue of files in the download directory for kippo.
3. Checks to see if that file already exists in history.txt (so we don’t submit files more than once).
4. Submits the files one at a time to your Cuckoo sandbox using the Cuckoo api server (which needs to be running).
5. Updates the history.txt fiie with the new files.
6. Dumps out the task id and status code so you know if it’s worked.
And that’s it really, I still need to work on the logging side of things, the log_file variable doesn’t work (as in nothing calls it) so that’s next on my list to resolve. If you deleted the history.txt file it will case the script to submit everything again so be careful with it.
The script can be found HERE.
I hope it may be of use to you, and as always let me know if it doesn’t work (create an issue ticket on GitHub).
Hello reader(s), this is just a quick post to share some new code I wrote tonight, you might find it useful or you might not.
So I’ve been trying to think of some new transforms to write for sniffMyPackets and thought it would be cool to take a TCP stream and rewrite the source and destination IP addresses and then recalculate the IP/TCP checksums so you can “replay” the packets if you so desire.
The code is as always written in python and uses Scapy to work the magic, the link to the code is at the bottom of the page, and is stored in another one of my GitHub repo’s specially created to share all the “junk” code I write and want to share with you guys..
The usage is really simple, tell the script you source pcap file (the one that you want to rewrite), give it the new source and destination IP addresses and then finally the location you want to store the output pcap file. For example:
./pcap-rewrite.py input.pcap 192.168.1.1 192.168.1.254 /tmp/output.pcap
Now just to make clear this is to work on individual TCP streams (might even work on UDP thinking about it), so 1 source IP, 1 destination IP. NOT pcaps files with lots of different IP’s.
When you run the script with the variables defined above (did I just use variables in a sentence..too much coding). You should get something like this:
What you should get inside of your pcap file is something that looks like this (well not like this as it’s my pcap file):
Which use to look like this:
Notice all the green in the new rewritten pcap file (the first wireshark image for those not paying attention). That’s because both the IP and TCP checksum have been deleted and then Scapy recalculate it when the IP addresses are changed (thanks to Judy Novak for that bit of useful information).
So without making you read any more here’s the code: DOWNLOAD