python判断文件后缀_Python 判断文件后缀是否被篡改

python判断文件后缀_Python 判断文件后缀是否被篡改自己用Python写了个对文件后缀判断的脚本,目前支持的文件类型还不是很多,还有待完善。支持MicrosoftOffice(.pptx.docx.xlsx)Pyhton版本为3.6#!usr/bin/envpython#-*-coding:UTF-8-*-#@Time:2018/7/1015:16#@Author:gumguiimportstructimportos,sy…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

自己用Python写了个对文件后缀判断的脚本,

目前支持的文件类型还不是很多,还有待完善。

支持Microsoft Office (.pptx .docx .xlsx)

Pyhton版本为3.6

#!usr/bin/env python

# -*-coding:UTF-8 -*-

# @Time: 2018/7/10 15:16

# @Author:gumgui

import struct

import os,sys,time

from zipfile import ZipFile

filelist = []

disk_list = []

def typeList():

# 支持文件类型

# 用16进制字符串的目的是可以知道文件头是多少字节

# 各种文件头的长度不一样,少半2字符,长则8字符

return {

#办公类型文件

“255044462D312E”: [“Adobe Acrobat”, [“.pdf”]],

“D0CF11E0”: [“Microsoft Office Word/Excel”, [“.xls”,”.doc”]],

“0902060000001000B9045C00”: [“Microsoft Office Excel V2”, [“.xls”]],

“0904060000001000F6055C00”: [“Microsoft Office Excel V4”, [“.xls”]],

“7FFE340A”: [“Microsoft Office Word”, [“.doc”]],

“1234567890FF”: [“Microsoft Office Word V6.0”, [“.doc”]],

“31BE000000AB0000”: [“Microsoft Office Word For DOS 6.0”, [“.doc”]],

“5374616E64617264204A”: [“Microsoft Access”, [“.mdb”]],

#压缩格式文件

“52617221”: [“RAR”, [“.rar”]],

“504B0304”: [“ZIP”, [“.zip”]],

“504B3030504B0304”: [“ZIP”, [“.zip”]],

#图片格式文件

“7E424B00”: [“PaintShop Pro Image File”, [“.psp”]],

“41433130”: [“CAD”, [“.dwg”]],

“FFD8FF”: [“JPEG”, [“.jpg”]],

“89504E47”: [“PNG”, [“.png”]],

“47494638”: [“GIF”, [“.gif”]],

“49492A00”: [“TIFF”, [“.tif”]],

#网页格式文件

“3C3F786D6C”: [“XML”, [“.xml”]],

“3C21454E54495459”: [“XML DTD”, [“.dtd”]],

“68746D6C3E”: [“HTML”, [“.html”]],

#视频格式文档

“57415645”: [“Wave”, [“.wav”]],

“41564920”: [“AVI”, [“.avi”]],

“2E7261FD”: [“Real Audio”, [“.ram” ,”.ra”]],

“2E524D46”: [“Real Media”, [“.rm”]],

“000001BA”: [“MPEG”, [“.mpg”]],

“000001B3”: [“MPEG”, [“.mpg”]],

“6D6F6F76”: [“Quicktime”, [“.mov”]],

“3026B2758E66CF11”: [“Windows Media”, [“.asf”]],

“4D546864”: [“MIDI”, [“.mid”]],

#邮件格式文件

“44656C69766572792D646174653A”: [“Email [thorough only]”, [“.eml”]],

“CFAD12FEC5FD746F”: [“Outlook Express”,[“.dbx”]],

“2142444E”: [“Outlook”, [“.pst”]],

#文本文档

“7B5C727466”: [“Rich Text Format”, [“.rtf”]],

#可执行文件

“vbxMZ”: [“Win Executable”, [“.exe”, “.dll”, “.drv”, “.vxd”, “.sys”, “.ocx”]],

}

def bytes2hex(bytes):

# 字节码转16进制字符串

num = len(bytes)

hexstr = u””

for i in range(num):

t = u”%x” % bytes[i]

if len(t) % 2:

hexstr += u”0″

hexstr += t

return hexstr.upper()

def discern_zip_file(sfile,cfile,ex,filesize):

# 细分ZIP类型的文件

with ZipFile(cfile, “r”) as zfile:

dir = zfile.namelist()

# print(dir)

if “[Content_Types].xml” in dir:

for file in dir:

if file == “word/document.xml”:

if ex == “.docx”:

print(“[*]文件类型为Microsoft Office Word”)

else:

print(“[!]%s,文件大小%.3f KB” % (sfile, filesize))

print(“[!]文件后缀被篡改,文件类型为Microsoft Office Word”)

elif file == “ppt/styles.xml”:

if ex == “.pptx”:

print(“[*]文件类型为Microsoft Office PowerPoint”)

else:

print(“[!]%s,文件大小%.3f KB” % (sfile, filesize))

print(“[!]文件后缀被篡改,文件类型为Microsoft Office PowerPoint”)

elif file == “xl/styles.xml”:

if ex == “xlsx”:

print(“[*]文件类型为Microsoft Office Excel”)

else:

print(“[!]%s,文件大小%.3f KB” % (sfile, filesize))

print(“[!]文件后缀被篡改,文件类型为Microsoft Office Excel”)

else:

print(“[*]%s,文件大小%.3f KB” % (sfile, filesize))

print(“[*]文件类型为ZIP”)

def filetype(filepath,filesize):

global f_hcode

# 获取文件类型

with open(filepath, ‘rb’) as binfile:

tl = typeList()

ftype = ‘未知!’

try:

for hcode in tl.keys():

numOfBytes = int(len(hcode) / 2) # 需要读多少字节

binfile.seek(0) # 每次读取都要回到文件头,不然会一直往后读取

hbytes = struct.unpack_from(“B” * numOfBytes, binfile.read(numOfBytes)) # 一个 “B”表示一个字节

f_hcode = bytes2hex(hbytes)

if f_hcode == hcode:

ftype = tl[hcode][0]

break

except struct.error:

print(“[!]%s,文件大小%.3f KB” % (filepath, filesize))

print(“[!]文件头部缺失”)

pass

except KeyError:

print(“[!]%s,文件大小%.3f KB” % (filepath, filesize))

print(“[!]文件类型未知”)

pass

# 判断zip类型文件做进一步细分

sfile = filepath

(filepath, tempfilename) = os.path.split(filepath)

(filename, extension) = os.path.splitext(tempfilename)

cfile = filepath + “\\” + filename + “.zip”

ex = extension

if ftype == “ZIP”:

os.rename(sfile, cfile)

discern_zip_file(sfile, cfile, ex, filesize)

os.rename(cfile, sfile)

else:

if ex in tl[f_hcode][1]:

print(“[*]%s,文件大小%.3f KB” % (sfile, filesize))

print(“[*]文件类型为%s” % ftype)

pass

else:

filelist.append(filepath)

print(“[!]%s,文件大小%.3f KB” % (sfile, filesize))

print(“[!]文件后缀被篡改,文件类型为%s” % ftype)

def bianli(rootDir):

#遍历目录

for root,dirs,files in os.walk(rootDir):

for file in files:

filesize = os.path.getsize(os.path.join(root,file))/1024

path = os.path.join(root, file)

filetype(path,filesize)

def count(rootDir):

#统计文件数量

count1 = 0 # 计数大文件夹下共有多少个小文件夹

for root,dirs,fs in os.walk(rootDir):

for f in fs:

path = os.path.join(root, f)

count1 += 1

print(“[*]目录中文件数量为%s” %count1)

if __name__ == ‘__main__’:

rootdir = input(“[+]请输入目录:”)

count = count(rootdir)

bianli(rootdir)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/193575.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Microsoft Platform SDK Febrary 2003下载(更新VC6的SDK)

    Microsoft Platform SDK Febrary 2003下载(更新VC6的SDK)http://www.x86pro.com/article/sdk-update-for-vc6VC6自带的SDK实在太旧了, 因此很多人抱怨,有很多网上下载的代码在VC6中无法编译. 所以我们需要更新一下SDK,但是不能太新,因为太新可能不支持VC6. 支持VC++6.0的SDK,就只有2003年2月的那版了. 更新SDK后,你的VC6会重新焕发生机. 另外,如果再安装个VisualAs

    2022年5月4日
    56
  • db2有没有rownum_row_number() over order by

    db2有没有rownum_row_number() over order byrank和rownumber都是自动生成序号,后面都可以跟partitionby分组和orderby排序。不同之处在于,rownumber在orderby后面的字段,排序字段数值相等时,rownumber字段依次递增。   rank在orderby后面的字段,排序字段数值相等时,rownumber都相同,直接跳到下一个不同的序号。selectrank

    2022年5月3日
    83
  • 西班牙语dele等级_西班牙语DELE考试分几个等级?难度如何 ?

    西班牙语dele等级_西班牙语DELE考试分几个等级?难度如何 ?原标题:西班牙语DELE考试分几个等级?难度如何?什么是西班牙语DELE考试?DELE考试有什么用?分多少个等级?难度呢?DELE基础常识介绍:据介绍,西班牙语DELE考试西班牙语的全称DiplomasdeEspa?olComoLenguaExtranjera,分别取了4个单词的首字母,所以是DELE,中文官方翻译是:作为一门外语的西班牙语水平考试,你也可以简单理解成一个国际承认的西班…

    2022年5月29日
    36
  • linux卸载socat,socat在Linux下的使用「建议收藏」

    目录0x01socat介绍0x02socat进行文件传输0x03socat正向端口转发0x04socat反向端口转发注:边界机器Ubuntu192.168.222.177内网机器win7192.168.222.1370x01socat介绍socat我们在前面也已经介绍过了,之前说的是Windows下的利用,如果没有看到的朋友请移步【socat在Windows下的使用】,socat…

    2022年4月10日
    161
  • 数据库选型之内存数据库eXtremeDB

    数据库选型之内存数据库eXtremeDB鉴于内存数据库访问速率快的特点,本文分别从单线程、多线程(并发访问)和多线程读/写混合访问角度对eXtremeDB数据库读写速率展开测试。需要指出的是,本文读取操作包含将数据读取后,并在控制台显示出来

    2022年7月2日
    44
  • nfs之端口设置

    nfs之端口设置1,修改/etc/sysconfig/nfs文件  MOUNTD_PORT="4002"STATD_PORT="4003"LOCKD_TCPPORT="4004"LOCKD_UDPPORT="4004"查看rpcinfo-p能看到mouted_port=4002,其余三项未显示2,tcp 1112049端口udp111 4046端口 把…

    2022年6月27日
    32

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号