Skip to content

Instantly share code, notes, and snippets.

@K0G0
Last active November 26, 2015 01:40
Show Gist options
  • Select an option

  • Save K0G0/32553b470ccb89b29cde to your computer and use it in GitHub Desktop.

Select an option

Save K0G0/32553b470ccb89b29cde to your computer and use it in GitHub Desktop.
AdvancedFIM_CalculateHash
import os,hashlib,time,base64
monitor=[
{'path':'E:\Dropbox\SVL\Projects\AdvancedFIM_GatherFiles','recursive':True},
{'path':'E:\Dropbox\SVL\Projects','recursive':False},
{'path':'E:\Dropbox\SVL\Projects\BasicFIM\BasicFIM.py','recursive':False}
]
files={}
def getFiles():
filesList=[]
for x in monitor:
if os.path.isdir(x['path']):
if x['recursive']:
filesList.extend([os.path.join(root, f) for (root, dirs, files) in os.walk(x['path']) for f in files])
else:
filesList.extend([item for item in os.listdir(x['path']) if os.path.isfile(item)])
elif os.path.isfile(x['path']):
filesList.append(x['path'])
return filesList
def getBytes(file):
return base64.b64encode(open(file, "rb").read())
while True:
for file in getFiles():
hash = hashlib.sha256()
with open(file) as f:
for chunk in iter(lambda: f.read(2048), ""):
hash.update(chunk)
sha256 = hash.hexdigest()
if file in files and sha256 <> files[file]['sha256']:
print '%s\t%s has been changed!'%(time.strftime("%Y-%m-%d %H:%M:%S") , file)
files[file]={'sha256':sha256,'bytes':getBytes(file)}
time.sleep(1)
import os,hashlib,base64
file='E:\Dropbox\SVL\Projects\BasicFIM\BasicFIM.py'
def getBytes(file):
return base64.b64encode(open(file, "rb").read())
print getBytes(file)
import os,hashlib,time
monitor=[
{'path':'E:\Dropbox\SVL\Projects\AdvancedFIM_GatherFiles','recursive':True},
{'path':'E:\Dropbox\SVL\Projects','recursive':False},
{'path':'E:\Dropbox\SVL\Projects\BasicFIM\BasicFIM.py','recursive':False}
]
files={}
def getFiles():
filesList=[]
for x in monitor:
if os.path.isdir(x['path']):
if x['recursive']:
filesList.extend([os.path.join(root, f) for (root, dirs, files) in os.walk(x['path']) for f in files])
else:
filesList.extend([item for item in os.listdir(x['path']) if os.path.isfile(item)])
elif os.path.isfile(x['path']):
filesList.append(x['path'])
return filesList
while True:
for file in getFiles():
hash = hashlib.sha256()
with open(file) as f:
for chunk in iter(lambda: f.read(2048), ""):
hash.update(chunk)
sha256 = hash.hexdigest()
if file in files and sha256 <> files[file]:
print '%s\t%s has been changed!'%(time.strftime("%Y-%m-%d %H:%M:%S") , file)
files[file]=sha256
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment