python实现文件快照加密保护的方法


Posted in Python onJune 30, 2015

本文实例讲述了python实现文件快照加密保护的方法。分享给大家供大家参考。具体如下:

这段代码可以对指定的目录进行扫描,包含子目录,对指定扩展名的文件进行SHA-1加密后存储在cvs文件,以防止文件被篡改

调用方法:python snapper.py > todayCheck.csv

# Hello, this is a script written in Python. See http://www.pyhon.org
#
# Snapper 1.2p
#
# This script will walk a directory (and its subdirectories) and compute
# SHA (Secure Hash Algorithm) for specific files (according to their
# extensions) and ouput a CSV file (suited for loading into a spreadsheet
# editor,a database or simply comparing with diff or ExamDiff.).
#
# You can redirect the output of this script to a file.
# eg. python snapper.py > todayCheck.csv
#
# This script can be usefull to check system files tampering.
#
# This script is public domain. Feel free to reuse it.
# The author is:
#    Sebastien SAUVAGE
#    <sebsauvage at sebsauvage dot net>
#    http://sebsauvage.net
#
# More quick & dirty scripts are available at http://sebsauvage.net/python/
#
# Directory to scan and extensions are hardcoded below:
directoryStart = r'c:\windows'
extensionList=['.exe','.dll','.ini','.ocx','.cpl','.vxd','.drv','.vbx','.com','.bat','.src',
        '.sys','.386','.acm','.ax', '.bpl','.bin','.cab','.olb','.mpd','.pdr','.jar']
import os,string,sha,stat,sys
def snapper ( directoryStart , extensionList ) :
  os.path.walk( directoryStart, snapper_callback, extensionList )
def snapper_callback ( extensionList , directory, files ) :
  sys.stderr.write('Scanning '+directory+'\n')
  for fileName in files:
    if os.path.isfile( os.path.join(directory,fileName) ) :
      if string.lower(os.path.splitext(fileName)[1]) in extensionList :
        filelist.append(fileSHA ( os.path.join(directory,fileName) ))
def fileSHA ( filepath ) :
  sys.stderr.write(' Reading '+os.path.split(filepath)[1]+'\n')
  file = open(filepath,'rb')
  digest = sha.new()
  data = file.read(65536)
  while len(data) != 0:
    digest.update(data)
    data = file.read(65536)
  file.close()
  return '"'+filepath+'",'+str(os.stat(filepath)[6])+',"'+digest.hexdigest()+'"'
sys.stderr.write('Snapper 1.1p - http://sebsauvage.net/python/\n')
filelist = []
snapper( directoryStart , extensionList )
sys.stderr.write('Sorting...\n')
filelist.sort()
filelist.insert(0, '"File path","File size","SHA"' )
sys.stderr.write('Printing...\n')
for line in filelist:
 print line
sys.stderr.write('All done.\n')

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
python 判断一个进程是否存在
Apr 09 Python
python2.7的编码问题与解决方法
Oct 04 Python
Python3中的列表,元组,字典,字符串相关知识小结
Nov 10 Python
Python基础语言学习笔记总结(精华)
Nov 14 Python
ubuntu16.04制作vim和python3的开发环境
Sep 23 Python
使用python将请求的requests headers参数格式化方法
Jan 02 Python
Python基础教程之异常详解
Jan 10 Python
Python实现滑动平均(Moving Average)的例子
Aug 24 Python
Python之Django自动实现html代码(下拉框,数据选择)
Mar 13 Python
Anaconda和ipython环境适配的实现
Apr 22 Python
解决IDEA 的 plugins 搜不到任何的插件问题
May 04 Python
通过代码实例了解Python3编程技巧
Oct 13 Python
Python实现高效求解素数代码实例
Jun 30 #Python
python实现DES加密解密方法实例详解
Jun 30 #Python
python实现的系统实用log类实例
Jun 30 #Python
python实现在windows服务中新建进程的方法
Jun 30 #Python
python实现线程池的方法
Jun 30 #Python
python实现的简单FTP上传下载文件实例
Jun 30 #Python
编写Python CGI脚本的教程
Jun 29 #Python
You might like
require(),include(),require_once()和include_once()的异同
2007/01/02 PHP
深入分析PHP引用(&amp;)
2014/09/04 PHP
PHP获取youku视频真实flv文件地址的方法
2014/12/23 PHP
laravel 5 实现模板主题功能(续)
2015/03/02 PHP
thinkphp中多表查询中防止数据重复的sql语句(必看)
2016/09/22 PHP
cnblogs TagCloud基于jquery的实现代码
2010/06/11 Javascript
JavaScript Date对象 日期获取函数
2010/12/19 Javascript
DWZ刷新dialog解决方法
2013/03/03 Javascript
JS在textarea光标处插入文本的小例子
2013/03/22 Javascript
jCallout 轻松实现气泡提示功能
2013/09/22 Javascript
Javascript遍历table中的元素示例代码
2014/07/08 Javascript
node.js中的fs.chmodSync方法使用说明
2014/12/18 Javascript
Javascript常用小技巧汇总
2015/06/24 Javascript
JS判断页面是否出现滚动条的方法
2015/07/17 Javascript
基于JS实现数字+字母+中文的混合排序方法
2016/06/06 Javascript
JS判断输入字符串长度实例代码(汉字算两个字符,字母数字算一个)
2016/08/02 Javascript
javascript十六进制数字和ASCII字符之间的转换方法
2016/12/27 Javascript
JS中Safari浏览器中的Date
2017/07/17 Javascript
es7学习教程之fetch解决异步嵌套问题的方法示例
2017/07/21 Javascript
在Python中使用dict和set方法的教程
2015/04/27 Python
python进程管理工具supervisor的安装与使用教程
2017/09/05 Python
Python使用struct处理二进制的实例详解
2017/09/11 Python
python函数调用,循环,列表复制实例
2020/05/03 Python
python实现文件分片上传的接口自动化
2020/11/19 Python
龟牌英国商店:Turtle Wax Brand Store UK
2019/07/02 全球购物
一些关于MySql加速和优化的面试题
2014/01/30 面试题
军校本科大学生自我评价
2014/01/14 职场文书
老师对学生的评语
2014/04/18 职场文书
机械机修工岗位职责
2014/08/03 职场文书
颂军魂爱军营演讲稿
2014/09/13 职场文书
党员个人批评与自我批评
2014/10/14 职场文书
大学生国家助学金感谢信
2015/01/23 职场文书
兵马俑导游词
2015/02/02 职场文书
2015年财务人员工作总结
2015/04/10 职场文书
班主任开场白
2015/06/01 职场文书
中秋节作文(五年级)之关于月亮
2019/09/11 职场文书