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中定义和使用抽象类的方法
Jun 30 Python
python实现应用程序在右键菜单中添加打开方式功能
Jan 09 Python
Python基于贪心算法解决背包问题示例
Nov 27 Python
PyCharm安装第三方库如Requests的图文教程
May 18 Python
python删除文本中行数标签的方法
May 31 Python
python-str,list,set间的转换实例
Jun 27 Python
python判断数字是否是超级素数幂
Sep 27 Python
关于python中密码加盐的学习体会小结
Jul 15 Python
解决python cv2.imread 读取中文路径的图片返回为None的问题
Jun 02 Python
在keras 中获取张量 tensor 的维度大小实例
Jun 10 Python
Python3 用什么IDE开发工具比较好
Nov 28 Python
Python爬取用户观影数据并分析用户与电影之间的隐藏信息!
Jun 29 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
提升PHP速度全攻略
2006/10/09 PHP
基于PHP 面向对象之成员方法详解
2013/05/04 PHP
解析php中session的实现原理以及大网站应用应注意的问题
2013/06/17 PHP
解析yahoo邮件用phpmailer发送的实例
2013/06/24 PHP
javascript处理table表格的代码
2010/12/06 Javascript
JS编程小常识很有用
2012/11/26 Javascript
js计算两个时间之间天数差的实例代码
2013/11/19 Javascript
jquery组件使用中遇到的问题整理及解决
2014/02/21 Javascript
JavaScript实现基于十进制的四舍五入实例
2015/07/17 Javascript
基于JavaScript实现全屏透明遮罩div层锁屏效果
2016/01/26 Javascript
jQuery ajax请求返回list数据动态生成input标签,并把list数据赋值到input标签
2016/03/29 Javascript
Bootstrap自定义文件上传下载样式
2016/05/26 Javascript
使用Ajax与服务器(JSON)通信实例
2016/11/04 Javascript
JS实现弹出下载对话框及常见文件类型的下载
2017/07/13 Javascript
React应用中使用Bootstrap的方法
2017/08/15 Javascript
Vue.js组件通信的几种姿势
2017/10/23 Javascript
在react-antd中弹出层form内容传递给父组件的操作
2020/10/24 Javascript
[44:40]2018DOTA2亚洲邀请赛3月30日 小组赛A组Liquid VS OG
2018/03/31 DOTA
Python实现的百度站长自动URL提交小工具
2014/06/27 Python
Python实现 多进程导入CSV数据到 MySQL
2017/02/26 Python
Django中利用filter与simple_tag为前端自定义函数的实现方法
2017/06/15 Python
Python SMTP发送邮件遇到的一些问题及解决办法
2018/10/24 Python
numpy给array增加维度np.newaxis的实例
2018/11/01 Python
Flask框架踩坑之ajax跨域请求实现
2019/02/22 Python
OpenCV图像颜色反转算法详解
2019/05/13 Python
Python学习笔记之变量、自定义函数用法示例
2019/05/28 Python
python自动循环定时开关机(非重启)测试
2019/08/26 Python
利用Python复制文件的9种方法总结
2019/09/02 Python
tensorflow中tf.slice和tf.gather切片函数的使用
2020/01/19 Python
python 实现超级玛丽游戏
2020/11/25 Python
使用CSS3制作倾斜导航条和毛玻璃效果
2017/09/12 HTML / CSS
模具毕业生推荐信
2014/02/15 职场文书
《鸿门宴》教学反思
2014/04/22 职场文书
民间个人借款协议书
2014/09/30 职场文书
加强作风建设演讲稿
2014/10/24 职场文书
生日宴会家属答谢词
2015/09/29 职场文书