[Python] python实现文件快照加密保护 →→→→→进入此内容的聊天室

来自 , 2019-10-08, 写在 Python, 查看 108 次.
URL http://www.code666.cn/view/c0cccc24
  1. # Hello, this is a script written in Python. See http://www.pyhon.org
  2. #
  3. # Snapper 1.2p
  4. #
  5. # This script will walk a directory (and its subdirectories) and compute
  6. # SHA (Secure Hash Algorithm) for specific files (according to their
  7. # extensions) and ouput a CSV file (suited for loading into a spreadsheet
  8. # editor,a database or simply comparing with diff or ExamDiff.).
  9. #
  10. # You can redirect the output of this script to a file.
  11. # eg.  python snapper.py > todayCheck.csv
  12. #
  13. # This script can be usefull to check system files tampering.
  14. #
  15. # This script is public domain. Feel free to reuse it.
  16. # The author is:
  17. #       Sebastien SAUVAGE
  18. #       <sebsauvage at sebsauvage dot net>
  19. #       http://sebsauvage.net
  20. #
  21. # More quick & dirty scripts are available at http://sebsauvage.net/python/
  22. #
  23. # Directory to scan and extensions are hardcoded below:
  24. directoryStart = r'c:\windows'
  25. extensionList=['.exe','.dll','.ini','.ocx','.cpl','.vxd','.drv','.vbx','.com','.bat','.src',
  26.                '.sys','.386','.acm','.ax', '.bpl','.bin','.cab','.olb','.mpd','.pdr','.jar']
  27.  
  28. import os,string,sha,stat,sys
  29.  
  30. def snapper ( directoryStart , extensionList ) :
  31.     os.path.walk( directoryStart, snapper_callback, extensionList )
  32.  
  33. def snapper_callback ( extensionList , directory, files ) :
  34.     sys.stderr.write('Scanning '+directory+'\n')
  35.     for fileName in files:
  36.         if os.path.isfile( os.path.join(directory,fileName) ) :
  37.             if string.lower(os.path.splitext(fileName)[1]) in extensionList :
  38.                 filelist.append(fileSHA ( os.path.join(directory,fileName) ))
  39.  
  40. def fileSHA ( filepath ) :
  41.     sys.stderr.write('  Reading '+os.path.split(filepath)[1]+'\n')
  42.     file = open(filepath,'rb')
  43.     digest = sha.new()
  44.     data = file.read(65536)
  45.     while len(data) != 0:
  46.         digest.update(data)
  47.         data = file.read(65536)
  48.     file.close()
  49.     return '"'+filepath+'",'+str(os.stat(filepath)[6])+',"'+digest.hexdigest()+'"'
  50.  
  51. sys.stderr.write('Snapper 1.1p - http://sebsauvage.net/python/\n')
  52. filelist = []
  53. snapper( directoryStart , extensionList )
  54. sys.stderr.write('Sorting...\n')
  55. filelist.sort()
  56. filelist.insert(0, '"File path","File size","SHA"' )
  57. sys.stderr.write('Printing...\n')
  58. for line in filelist:
  59.         print line
  60. sys.stderr.write('All done.\n')
  61. #//python/5172

回复 "python实现文件快照加密保护"

这儿你可以回复上面这条便签

captcha