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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java图书馆新地址_推荐20个5月最热门的Java开源项目

    java图书馆新地址_推荐20个5月最热门的Java开源项目以下涉及到的数据统计与2019年6月1日18点,数据来源:https://github.com/trending/java?since=monthly[1]。下面推荐的内容从Java学习文档到最热门的框架再到热门的工具应有尽有,建议收藏+在看!1.LeetCodeAnimation•Github地址:https://github.com/MisterBooo/L…

    2022年7月7日
    22
  • GIT、GITLAB、GITHUB、GITLIB[通俗易懂]

    GIT、GITLAB、GITHUB、GITLIB[通俗易懂]Git是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。Git是一个开源的分布式版本控制系统,可以有效、高速的处理从很小到非常大的项目版本管理。Github-一个网站,提供给用户空间创建git仓储,保存用户的一些数据文档或者代码等作为开源代码库以及版本控制系统,Github目前拥有140多万开发者用户。随着越来越多的应用程序转移到了云上

    2025年5月31日
    0
  • RelativeLayout.LayoutParams

    RelativeLayout.LayoutParams通过id设置相对兄弟元素对齐。<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android&qu

    2022年7月1日
    22
  • 请描述django模板中标签的作用?_html object标签

    请描述django模板中标签的作用?_html object标签常用的模板标签if标签if标签相当于Python中的if语句,有elif和else相对应,但是所有的标签都需要用标签符号({%%})进行包裹。if标签中可以使用==、!=、<、<=、&

    2022年7月30日
    3
  • 编程实现strstr函数「建议收藏」

    编程实现strstr函数「建议收藏」函数接口为:char*mystrstr(char*str1,char*str2)要求:在字符串str1中查找第一次出现字符串str2的位置,如果找到匹配的字符串,返回第一次匹配的指针,否则返回NULL。#include&lt;iostream&gt;usingnamespacestd;char*mystrstr(char*str1,char*str2){ char*p,*q;…

    2022年6月25日
    26
  • 二进制/十六进制转浮点数的编程(互转类似)

    转换的程序:应用:原理就是复制内存数据再以不同的数据类型来解释。推荐:http://www.cnblogs.com/roucheng/p/cpp11.html

    2021年12月24日
    54

发表回复

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

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