With increased MITM (Man In The Middle) attacks it is essential that you check the authenticity of files you download from the Internet. One of the ways of doing so is checking the MD5 sum of the file and comparing it with the checksum given on the download providers website.
Here's how to perform this task in Python:
#! /usr/bin/env python
# ChecksumChecker.py
# Author: Abdul Fatir
# E-Mail: abdulfatirs@gmail.com
from threading import Thread
import hashlib
import optparse
def getChecksum(file_path,check_hash):
file_handle = open(file_path,"rb")
_md5 = hashlib.md5()
while True:
_buffer = file_handle.read()
if not _buffer:
break
_md5.update(_buffer)
digest = _md5.hexdigest()
print "[ ] File's MD5 checksum is: " digest
if (check_hash != None):
if(check_hash.lower() == digest):
print "[ ] Hash matched: The file is authentic."
else:
print "[-] Hash mis-match: The file is not authentic."
def Main():
parser = optparse.OptionParser('usage: %prog -f <filename> [-m <md5 hash>]')
parser.add_option('-f', dest='file_path', type='string', help='Please specify a file')
parser.add_option('-m', dest='check_hash', type='string')
(options,arg) = parser.parse_args()
if (options.file_path == None):
print parser.usage
exit(0)
else:
file_path = options.file_path
check_hash = options.check_hash
print "[*] Hashing file '" file_path "'...."
hash_thread = Thread(target=getChecksum,args=(file_path,check_hash))
hash_thread.start()
hash_thread.join()
if __name__ == '__main__':
Main()
Please don't copy paste this code, download it from pastebin.
Usage examples:
Suppose you downloaded a file named hello.exe
which has an MD5 hash 5D41402ABC4B2A76B9719D911017C592
given on the Internet then to check if you downloaded the correct file use ChecksumChecker.py as follows:
$ python ChecksumChecker.py -f <path to hello.exe> -m 5D41402ABC4B2A76B9719D911017C592
To simply get the MD5 sum of a file, do:
$ python ChecksumChecker.py -f <path to hello.exe>
Note:
- This is a prototype. You can added more algorithms like SHA1 and SHA256 as per your requirements.
- This code executes in Python 2.7.x