0x01 功能
刚刚写了一个小工具:用于批量获取文件的摘要:md5、sha1、sha256
具体用法:
>>>> python3 getFileFeatures.py [参数:当前目录下的文件夹或者文件]
批量计算,只需要把所有文件放入当前目录下的文件夹中,参数为文件夹名
计算后的结果:在当前目录下生成“filefeatures.txt”文件,存在该文件中
0x02源码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/12/25/025 22:53
# @Author : H
# @File : getFileFeatures.py
"""批量获取文件夹中各个文件的MD5、SHA1、SHA256"""
import hashlib
import os
import sys
def getFileFeatures(dirpath):
path = os.getcwd() + "\\" + dirpath
subpaths = []
datas = {} # 结果存为json,以便后需
if os.path.exists(path):
# 路径存在
if os.path.isdir(path):
# 获得的路径为文件夹
for root, dirs, files in os.walk(path):
for file in files:
subpaths.append([file, os.path.join(root, file)])
else:
# 获得的路径为文件
file = path.split("\\")[-1]
subpaths.append([file, path])
for i in subpaths:
f = open(i[1], "rb")
rb = f.read()
data = {'MD5': hashlib.md5(rb).hexdigest(),
'SHA1': hashlib.sha1(rb).hexdigest(),
'SHA256': hashlib.sha256(rb).hexdigest()}
datas[i[0]] = data
f.close()
with open("filefeatures.txt", "a")as f:
for k, v in datas.items():
f.writelines("filename:\t" + k + "\n")
print("[+]succeed get the teatures of " + k)
for key, value in v.items():
f.writelines(key + ":\t" + value + "\n")
f.writelines("\n")
else:
print("Folder does not exist")
if sys.argv:
print("""
_ _ _ _ _ _ _ _ _ _ _ _
(c).-.(c) (c).-.(c) (c).-.(c) (c).-.(c) (c).-.(c) (c).-.(c)
/ ._. \ / ._. \ / ._. \ / ._. \ / ._. \ / ._. \
__\( Y )/__ __\( Y )/__ __\( Y )/__ __\( Y )/__ __\( Y )/__ __\( Y )/__
(_.-/'-'\-._)(_.-/'-'\-._)(_.-/'-'\-._)(_.-/'-'\-._)(_.-/'-'\-._)(_.-/'-'\-._)
|| K || || I || || L || || L || || E || || R ||
_.' `-' '._ _.' `-' '._ _.' `-' '._ _.' `-' '._ _.' `-' '._ _.' `-' '._
(.-./`-'\.-.)(.-./`-'\.-.)(.-./`-'\.-.)(.-./`-'\.-.)(.-./`-'\.-.)(.-./`-`\.-.)
`-' `-' `-' `-' `-' `-' `-' `-' `-' `-' `-' `-'
====== function: Get the characteristics of file or files in folder ========
=>=>=>=>=>usage : python getFileFeatures.py file/dir <=<=<=<=<=<=<=<=<=<=
""")
if len(sys.argv) == 2:
getFileFeatures(sys.argv[1])
else:
print("[-]Usage errors")
0x03运行
1、使用
2、结果
!!!成长的道路没有一帆风顺!!!