Year of Python (YOP) – Week Six


Hello Reader!

This week, I wanted to get back to my code from Week Two, where I created a program to download the md5 hash files from the VXShare website.  What I wanted to do was create a configuration file to track the last file that I downloaded, as well as create a new file if it was the first time I was running the program.

So I decided to use the ConfigObj python module for the configuration piece.  No real reason to be honest, except it was the first one on the ConfigParserShootout page of the Python Wiki, and it did what I needed it to do.  Which was basically to act like an INI file.

The code looks like this:

if os.path.exists(config_file):
    print "File exists"
    config = ConfigObj(config_file)
    vxshare_no = int(config['last_vxshare_num'])
    print "File doesn't exist"
    config = ConfigObj()
    config.filename = config_file
    config['last_vxshare_num'] = 0
    vxshare_no = int(config['last_vxshare_num'])

For the first part of the code, I’m checking to see if the configuration file already exists.  If it does, I use that as my configuration file, and I pull out the “last_vxshare_num” value, which represents the next file to check the site for.  You have to pass it into the “vxshare_no” variable as an integer, because we’re going to increment this number (if there is an updated file), and by default the configuration value is a string.

If the configuration file doesn’t exist, we’re going to make the assumption that it’s the first time we’re running this program, and we need to create the configuration file.  The ConfigObj module makes it really easy to do that, by just opening up an empty ConfigObj class (I believe it’s a class, I’m still getting all the Python terminology down), and then you create the file with the filename method.  Since it’s a new file we initialize the “last_vxshare_num” variable to zero, and then pass it to the “vxshare_no” variable.  Again it needs to be passed as a string.  In both instances I added a print statement just as a check if the code sees the file or not.  In the overall code I’ve removed that.

My next step with this, is to configure the hash files after they are downloaded for import into X-Ways Forensic.  What needs to be done is remove the first six lines of the file, and then add “MD5” as the first line.  That’s what I’m looking to do next week…

Until then!


No Responses Yet to “Year of Python (YOP) – Week Six”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: