Skip to content

Instantly share code, notes, and snippets.

@9b
Created March 22, 2011 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 9b/881790 to your computer and use it in GitHub Desktop.
Save 9b/881790 to your computer and use it in GitHub Desktop.
Rename a directory of malicious PDFs with the hash.pdf.vir format
import hashlib
import optparse
import os
def get_hash_data(file, type):
if type == "md5":
output = hashlib.md5()
elif type == "sha1":
output = hashlib.sha1()
elif type == "sha256":
output = hashlib.sha256()
else:
output = "Error"
with open(file,'rb') as f:
for chunk in iter(lambda: f.read(8192), ''):
output.update(chunk)
return output.hexdigest()
def main():
oParser = optparse.OptionParser(usage='usage: %prog [options]\n' + __description__, version='%prog ' + __version__)
oParser.add_option('-d', '--dir', default='', type='string', help='dir to build an object from')
(options, args) = oParser.parse_args()
if options.dir:
files = []
dirlist = os.listdir(options.dir)
for fname in dirlist:
files.append(fname)
files.sort()
for file in files:
if file != "renamer.py":
hash = get_hash_data(options.dir + file, "md5")
filename = hash + ".pdf.vir"
os.rename(file, filename)
else:
oParser.print_help()
return
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment