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实现抓取百度搜索结果页的网站标题信息
Jan 22 Python
Python与shell的3种交互方式介绍
Apr 11 Python
Python错误: SyntaxError: Non-ASCII character解决办法
Jun 08 Python
python实现二叉树的遍历
Dec 11 Python
python机器学习实战之最近邻kNN分类器
Dec 20 Python
python的Tqdm模块的使用
Jan 10 Python
Python绘制堆叠柱状图的实例
Jul 09 Python
python SVD压缩图像的实现代码
Nov 05 Python
Python中的全局变量如何理解
Jun 04 Python
使用python脚本自动生成K8S-YAML的方法示例
Jul 12 Python
Python数据模型与Python对象模型的相关总结
Jan 26 Python
Python之qq自动发消息的示例代码
Feb 18 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/12/14 PHP
一道求$b相对于$a的相对路径的php代码
2010/08/08 PHP
几款免费开源的不用数据库的php的cms
2010/12/19 PHP
nginx+php-fpm配置文件的组织结构介绍
2012/11/07 PHP
php mail to 配置详解
2014/01/16 PHP
php中strtotime函数性能分析
2016/11/20 PHP
PHP学习记录之数组函数
2018/06/01 PHP
js用Date对象的setDate()函数对日期进行加减操作
2014/09/18 Javascript
Javascript动态创建div的方法
2015/02/09 Javascript
javascript Array 数组常用方法
2015/04/05 Javascript
js console.log打印对像与数组用法详解
2016/01/21 Javascript
Vue.js快速入门教程
2016/09/07 Javascript
jQuery+ajax读取并解析XML文件的方法
2016/09/09 Javascript
JS动态给对象添加属性和值的实现方法
2016/10/21 Javascript
JS常见DOM节点操作示例【创建 ,插入,删除,复制,查找】
2018/05/14 Javascript
vue实现添加与删除图书功能
2018/10/07 Javascript
浅谈JS和jQuery的区别
2019/03/27 jQuery
这15个Vue指令,让你的项目开发爽到爆
2019/10/11 Javascript
vue npm install 安装某个指定的版本操作
2020/08/11 Javascript
python执行等待程序直到第二天零点的方法
2015/04/23 Python
Python第三方库的安装方法总结
2016/06/06 Python
python增加矩阵维度的实例讲解
2018/04/04 Python
win7 x64系统中安装Scrapy的方法
2018/11/18 Python
浅谈python requests 的put, post 请求参数的问题
2019/01/02 Python
python实现代码统计器
2019/09/19 Python
python读取raw binary图片并提取统计信息的实例
2020/01/09 Python
pyinstaller 3.6版本通过pip安装失败的解决办法(推荐)
2020/01/18 Python
Python Web项目Cherrypy使用方法镜像
2020/11/05 Python
python中delattr删除对象方法的代码分析
2020/12/15 Python
5 个强大的HTML5 API 函数推荐
2014/11/19 HTML / CSS
白色公司:The White Company
2017/10/11 全球购物
碧欧泉Biotherm加拿大官方网站:法国高端护肤品牌
2019/10/18 全球购物
如何将字串String转换成整数int
2015/02/21 面试题
2014年小学元旦活动方案
2014/02/12 职场文书
2014年体育教学工作总结
2014/12/09 职场文书
分位数回归模型quantile regeression应用详解及示例教程
2021/11/02 Python