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 ??

1
  • 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 or handle multiple arguments. And then loop over the different files obviously. Commented Jun 6, 2014 at 5:40

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.