Ethical Hacking

Learn to find vulnerabilities before the bad guys do! Gain real world hands on hacking experience in our state of the art hacking lab. Course designed and taught by expert instructors with years of penetration testing experience. 12 student maximum in every class. Certification attempt included in every package.
Computer Forensics Training at InfoSec Institute

Gain the in-demand skills of a certified computer examiner, learn to recover trace data left behind by fraud, theft, and cybercrime perpetrators. Discover the source of computer crime and abuse at your organization so that it never happens again. All of our class sizes are guaranteed to be 12 students or less to facilitate one-on-one interaction with one of our expert instructors.




Network Security Focus-Microsoft
[Top] [All Lists]

Python Hash File Builder/Checker

Subject: Python Hash File Builder/Checker
Date: Fri, 20 Aug 2004 09:52:21 -0400
A few folks asked about this so inlined below is a short and simple python script that hashes files on a system and stick them in a simple text file. Works on Linux and Windows. (I love interpreted languages.)

It currently uses SHA1 though it'll do MD5 with just a few tweaks.

I do apologize because it isn't terribly well commented. All the configuration is in the form of global variables found at the top of the file.

Hope folks find this useful. USE AT YOUR OWN RISK.

Sam

#!/usr/bin/python

import sys # for argv
import os  # for files
import sha # for hashes

import stat
from stat import *

# Case sensitive list of name and paths that will not be hashed by hashTree.
exceptions = [ "/dev", "/proc", "/sys", "lost+found",
"/etc/apache/lib/apache2/lib/apache2"
"/etc/apache2/lib/apache2/lib/apache2"
]


# case insensitive list of names and paths that wil not be hashed by hashTree.
# All entries should be lowercase.
CIexceptions = [ "tmp", "temp" ]


# Directories that we will try to hash.
# Note that there are both Windows and Unix directories here.
defaultDirectories = [ "C:\\WINDOWS",
                      "C:\\WINNT",
                      "C:\\Program Files",
                      "C:\\Documents and Settings" ]

defaultLog = "hashes.log"
defaultDB  = "hashes.db"

# Report when a file hash matches.
# Often we want explicit confirmation but other times we just want problems.
reportOK = False
verbose = True



# This is how we hash a file! :D def hashFile(name): try: hash = sha.new () f = file(name, "r")

       d = f.read(4096000)

       while(len(d) > 0):
           hash.update(d);
           d = f.read(4096000)

       f.close()

result = hash.hexdigest()
except(IOError):
result = "IOError"


   return result

def hashTree(root, output):
try: # If the path is not in the exceptions lists.
if root.lower() not in CIexceptions and root not in exceptions:


           # We use lstat so that we don't follow links to directories.
           if stat.S_ISDIR(os.lstat(root)[ST_MODE]) :
               for d in os.listdir(root):

# Don't hash exceptions if the name matches.
if d.lower() not in CIexceptions and d not in exceptions:
hashTree(os.path.join(root, d), output)


           # We only has reagular files.
           elif stat.S_ISREG(os.lstat(root)[ST_MODE]):
               if verbose:
                   sys.stderr.write("ADDING: "+root+"\n")

h = hashFile(root)
output.write(h+" "+root+"\n")
elif verbose:
sys.stderr.write("SKIPPED: "+root+"\n")
except(IOError):
output.write("TreeError "+root+"\n")
if(verbose):
sys.stderr.write("ADDED: "+h+" "+root+"\n")
except(OSError):
None


def verifyTree(input, output):
   line = input.readline()

   while(len(line) > 0):
       [origHash, fileName] = line.split(' ',1)

       fileName = fileName[0:-1]

       if(not os.path.isfile(fileName)):
           output.write("MISSING: "+origHash+" "+fileName+"\n")

       else:
           newHash = hashFile(fileName)

if(origHash != newHash):
output.write("MISMATCH: "+ origHash + " " + newHash + " "+fileName+"\n")
elif reportOK:
output.write("OK: "+origHash+" "+fileName+"\n")


       line = input.readline()


def buildDB(dirs = defaultDirectories, dbfile=defaultDB):

   if(dbfile is "-"):
       f = sys.stdout
   else:
       f = open(dbfile, "w+")

   for t in dirs:
       hashTree(t, f);

def checkDB(dbfile = defaultDB, logfile = defaultLog):
   if(dbfile is "-"):
       db = sys.stdin
   else:
       db = file(dbfile, "r")

   if(logfile is "-"):
       log = sys.stdout
   else:
       log = file(logfile, "w+")

   verifyTree(db, log)

def ussage():
print("Ussage: "+sys.argv[0]+
" [ mk [db [directories...]] | ch [log [db]] ]")
if(len(sys.argv) >= 2):
if(sys.argv[1] == "mk"):
if(len(sys.argv) == 2):
buildDB()
if(len(sys.argv) == 3):
buildDB(dbfile = sys.argv[2])
elif(len(sys.argv) > 3):
buildDB(dbfile = sys.argv[2], dirs=sys.argv[3:])
elif(sys.argv[1] == 'ch'):
if(len(sys.argv) == 2):
checkDB()
elif(len(sys.argv) == 3):
checkDB(logfile=sys.argv[2])
elif(len(sys.argv) == 4):
checkDB(logfile=sys.argv[2], dbfile=sys.argv[3])
else:
ussage()
else:
ussage()
else:
ussage()



--------------------------------------------------------------------------- ---------------------------------------------------------------------------

<Prev in Thread] Current Thread [Next in Thread>
  • Python Hash File Builder/Checker, Sam Baskinger <=