0

I'm having a problem for computing all checksums of all the files under the /bin/* directory. I'm implementing a HIDS in Python, so i need to compute the checksums of each file and save it, say, in a list .... so my code here only returns the first checksum of the /bin/* directory.

import sys
import haslib
path = sys.argv[1] #PATH OF THE FILES, ex: /etc/shadow, /bin/*, etc.

with open(path,'rb') as fh:
   md5 = hashlib.md5()
   while True:
      data = fh.read(8192)
      if not data:
         break
      md5.update(data)
print md5.hexdigest()

Any suggestions ??

Emiliano
  • 357
  • 2
  • 8
  • 21
  • If you pass `/bin/*` as the argument to your script, *your shell* does the globbing (the expansion of the wildcard), and your script will actually be passed `/bin/a bin/b ..` as multiple arguments. So you either need to do the [globbing in Python](https://docs.python.org/2/library/glob.html) or handle multiple arguments. And then *loop* over the different files obviously. – Lukas Graf Jun 06 '14 at 05:40

1 Answers1

1
import sys
from os import listdir
from os.path import isfile, join
import hashlib
path = sys.argv[1] #PATH OF THE FILES, ex: /etc/shadow, /bin/*, etc.
files = [ f for f in listdir(path) if isfile(join(path,f)) ]
my_files = {}
for fil in files:
    with open(fil,'rb') as fh:
       md5 = hashlib.md5()
       while True:
          data = fh.read(8192)
          if not data:
             break
          md5.update(data)
    my_files[fil] = md5.hexdigest()
for k,v in my_files.iteritems():
    print 'file_name is {} | hash is {}'.format(k,v)
salparadise
  • 5,699
  • 1
  • 26
  • 32