自己用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)

python判断文件后缀_Python 判断文件后缀是否被篡改相关推荐

  1. python判断文件后缀_Python 判断文件后缀

    原博文 2019-04-24 12:43 − # 方法1, str的endswith方法: ```python ims_path='data/market1501/Market-1501-v15.09 ...

  2. python 怎么判断是文件还是文件夹_python判断是文件夹还是文件的方法

    python判断是文件夹还是文件的方法 发布时间:2020-07-18 11:16:34 来源:亿速云 阅读:152 作者:清晨 小编给大家分享一下python判断是文件夹还是文件的方法,相信大部分人 ...

  3. python文件函数_Python之文件操作及常用函数

    常见全局函数: 1.dir(nameStr): 常见系统变量: 1.sys.path:一个包含Python自动搜索文件的路径或目录的列表.可使用列表的操作方法修改.增加默认搜索路径. 一.模块 1.创 ...

  4. python新建文件夹和文件夹_Python创建文件夹与文件的快捷方法

    前言 Python快捷创建文件夹和文件详解 自己做文件时发现  简单的反复操作十分浪费时间,于是想到了 使用Python,这个分享给大家,快捷高效办公. 项目目录 file11 创建文件夹 file2 ...

  5. python os模块创建文件夹_Python创建文件夹与文件的快捷方法

    前言 Python快捷创建文件夹和文件详解 自己做文件时发现  简单的反复操作十分浪费时间,于是想到了 使用Python,这个分享给大家,快捷高效办公. 项目目录 file11 创建文件夹 file2 ...

  6. python如何进入文件夹_python之文件的读写和文件目录以及文件夹的操作实现代码...

    这篇文章主要介绍了python之文件的读写和文件目录以及文件夹的操作实现代码,需要的朋友可以参考下 为了安全起见,最好还是给打开的文件对象指定一个名字,这样在完成操作之后可以迅速关闭文件,防止一些无用 ...

  7. python写sql语句_Python操作文件模拟SQL语句功能

    一.需求 当然此表你在文件存储时可以这样表示 1,Alex Li,22,13651054608,IT,2013-04-01 现需要对这个员工信息文件,实现增删改查操作 1. 可进行模糊查询,语法至少支 ...

  8. python读取坐标文本文件_Python 实现文件读写、坐标寻址、查找替换功能

    读文件 打开文件(文件需要存在) #打开文件 f = open("data.txt","r") #设置文件对象 print(f)#文件句柄 f.close() ...

  9. python回到首行_python读取文件首行和最后一行

    python读取文件最后一行两种方式 1)常规方法:从前往后依次读取 步骤:open打开文件. 读取文件,把文件所有行读入内存. 遍历所有行,提取指定行的数据. 优点:简单,方便 缺点:当文件大了以后 ...

  10. python遍历读取文件夹下所有文件内容_python遍历文件夹下所有文件

    python遍历文件夹下所有文件的方法:首先打开相应的代码文件:然后通过"for f in files:print(os.path.join(root, f))"方式遍历所有的文件 ...

最新文章

  1. 高并发系统搭建:web负载均衡
  2. Angular2入门教程-1
  3. 提示计算机未安装flash,大师应对安装了flash,但浏览器提示没安装,如何解决...
  4. ITK:创建前向差异内核
  5. 深入浅出学Hive:Hive QL
  6. SAP Fiori Elements 应用里和 Fiori 3 相关的外观设置
  7. Netbeans、Eclipse中查看JDK源码
  8. 职工考勤管理信息系统数据库课设_职工考勤管理信息系统数据库课程设计
  9. SQLAlchemy 嵌套事务的解决方案
  10. linux缺页异常处理--用户空间
  11. 【windows】修复win7便签
  12. matlab 回调函数,在Matlab中将实际值传递给回调函数
  13. 软件测试电商web项目如何描述,测试web项目实战
  14. python循环5次_Python入门五:循环
  15. Facebook 如何识别出性工作者?
  16. 台湾批准联电7.1亿美元投资联芯科技
  17. svm matlab 图像分割,勇哥的视觉实验:SVM分类器(二) 支持向量机的应用例子,图片分割...
  18. 栈和队列的区别,栈和堆得区别
  19. 个人博客制作——其他页面
  20. EXCEL利用正则匹配去替换内容

热门文章

  1. 软件生命周期模型及其选择
  2. 苦于记不住八大排序?现成的模板它不香?
  3. 口袋对决怎么在电脑上玩 口袋对决安卓模拟器教程
  4. 绘画教程:鼻子怎么画?人物的五官怎么画?
  5. DellXPS-L502x OsxMountainLion驱动
  6. 计算机毕业设计—火锅店在线点餐小程序
  7. win10wifi间歇性断网重启后恢复_解决win10无线无故断网,重启才恢复正常的情况【原】...
  8. 四平青年背景音乐及经典台词选
  9. 王者荣耀官网不为人知的秘密
  10. 互联网黑话,我忍你很久了!