您的位置:首页 > 编程语言 > Python开发

Python:解压缩前检测一个zip文件否为加密,两种算法。

2012-05-04 19:37 1576 查看
前一篇文章介绍了如何用python自动解压缩加密的zip文件,

链接:/article/5787604.html

但是有一个问题,在服务器端脚本在解加密zip文件之前,需要检测其是否是加密文件,若是,则询问密码,结合密码解压缩;如果不是,则则直接解压缩。本文的灵感取自一篇文章《Linux zip 加密压缩》

链接:/article/4342679.html

所有加密的zip文件头8个字符好像都是这个样子的

0000000 4b50 0403 0014 0009
0000008

具体为什么会是这个格式,就不得而知了,需要以后做详细的研究。

还有另一个方法,就是通过python的 open() read()方法读取前十个字节,显示是十六进制数。经过测试,好像所有的zip文件前十个字节都是相同的,但是加密和未加密的文件则不同:

加密zip文件:'PK\x03\x04\x14\x00\t\x00\x08\x00'

未加密zip文件:'PK\x03\x04\x14\x00\x00\x00\x08\x00'

其中前四个字节‘PK\x03\x04' 是文件头标记。往后两个字节 ‘\x14\x00' 是解压文件所需的pkware的版本,再后来两个字节 '\x00\x00' 或者 '\t\x00' 是全局方式位标记,最后两个字节 '\x08\x00' 是压缩方式。

这里全局方式位置标记由于是否加密而有所不同,所以只需要比较这两个十六进制数字是否相等即可。

如果要查询zip文件格式在这里:http://www.utf.com.cn/article/s1045

第二种方法还稍微快一些。

顺便说一下,博客园的文件上传系统只能上传zip文件,但是检测模块只是对文件后缀做分析。

使用linux 下的hexdump十六进制查看器的代码如下:

#!/usr/bin/env python
#_*_coding=utf-8_*_

import subprocess
import getopt
import sys
import os

if __name__=='__main__':
try:
opts, args = getopt.getopt(sys.argv[1:], "")
except getopt.GetoptError, err:
print str(err)
sys.exit(2)
# check if the zipfile exists
for arg in args:
if not os.path.isfile(arg):
print "%s does not exists!" % arg
sys.exit(2)
# verify the zipfile
f = subprocess.Popen(['hexdump', '-n', '8', "%s" % arg], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].rstrip()
if f[23:27]=='0009':
print "%s 是加密文件" % arg
elif f[23:27]=='0000':
print "%s 不是加密文件" % arg
else:
print "error"


2. 使用python自带的方法判断的代码如下:

#!/usr/bin/env python
#_*_coding=utf-8_*_

import subprocess
import getopt
import sys
import os

if __name__=='__main__':
encryptedzip='\t\x00'
unencryptedzip='\x00\x00'
zipfile='PK\x03\x04'
try:
opts, args = getopt.getopt(sys.argv[1:], "")
except getopt.GetoptError, err:
print str(err)
sys.exit(2)
# check if the zipfile exists
for arg in args:
if not os.path.isfile(arg):
print "%s does not exists!" % arg
sys.exit(2)
# verify the zipfile
try:
f=open("%s" % arg, "rb")
top8hex=f.read(8)
if top8hex[6:] == encryptedzip:
print "%s 是加密文件" % arg
elif top8hex[6:] == unencryptedzip:
print "%s 不是加密文件" % arg
elif not top8hex[:4] == zipfile:
print "%s 不是zip文件!" % arg
except IOError, err:
print str(err)
finally:
f.close()


以上两种方法虽然是命令行程序,但是同样可以运用到cgi中。

如果有人能提供更到位、更精确的分析方法,请赐教:)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: