網站首頁 編程語言 正文
對比兩個文件相似度,python中可通過difflib.SequenceMatcher/ssdeep/python_mmdt/tlsh實現,在大量需要對比,且文件較大時,需要更高的效率,可以考慮模糊哈希(fuzzy hash),如ssdeep/python_mmdt
測試過程發現:
- difflib方法,讀取文件后,可以實現匹配度輸出
- ssdeep/mmdt/tlsh方法可以實現,實現提前模糊哈希值,驗證時,只讀取一次,完成對比,從而優化對比時間,及內存/cpu消耗
- tlsh測試時,值越小,相似度越高,在對比小文件時,很不理想
- 在對比小文件時,三種方法相差不大,在對比大文件(案例中81MB),difflib方法慢的難以接受
- 在實際環境中,建議使用mmdt方法,因為ssdeep在二進制對比中差別較大,失去參考價值,具體還有哪些文件類型存在此問題有待考量,
測試環境:
OS:ubuntu20.04
python:3.8.10
py-tlsh==4.7.2
python-mmdt==0.3.1
ssdeep==3.4
# -*- coding: utf-8 -*-
import ssdeep
import time
from python_mmdt.mmdt.mmdt import MMDT
from difflib import SequenceMatcher
def difflib_test(file1,file2):
start_time = time.time()
with open(file1,'rb') as f:
s1 = f.read()
with open(file2,'rb') as f:
s2 = f.read()
match_obj = SequenceMatcher(None,s1,s2)
print("difflib match:",match_obj.ratio())
end_time = time.time()
print('difflib_test cost :',end_time-start_time)
def mmdt_test(file1,file2):
start_time = time.time()
mmdt=MMDT()
r1 = mmdt.mmdt_hash(file1)
print(r1)
r2 = mmdt.mmdt_hash_streaming(file2)
print(r2)
# sim1 = mmdt.mmdt_compare(file1, file2)
# print("mmdt match:",sim1)
sim2 = mmdt.mmdt_compare_hash(r1, r2)
print("mmdt match:",sim2)
end_time = time.time()
print('mmdt_test cost :',end_time-start_time)
def ssdeep_test(file1,file2):
start_time = time.time()
sig1=ssdeep.hash_from_file(file1)
sig2=ssdeep.hash_from_file(file2)
print(sig1)
print(sig2)
print("ssdeep match:",ssdeep.compare(sig1,sig2))
end_time = time.time()
print('ssdeep_test cost :',end_time-start_time)
if __name__ == '__main__':
start_time = time.time()
file1='/root/test/fstab'
file2='/root/test/fstab2'
# file1 = '/root/test/initrd.img-5.4.0-125-generic'
# file2 = '/root/test/initrd.img-5.4.0-135-generic'
mmdt_test(file1,file2)
ssdeep_test(file1,file2)
difflib_test(file1,file2)
end_time = time.time()
print('總執行時間:',end_time-start_time)
下面給出對比小文件/大文件效果:
測試tlsh
import tlsh
import time
def tlsh_test(file1,file2):
start_time = time.time()
with open(file1,'rb') as f:
s1 = tlsh.hash(f.read())
with open(file2,'rb') as f:
s2 = tlsh.hash(f.read())
match_obj = tlsh.diff(s1,s2)
print("tlsh match:",match_obj)
end_time = time.time()
print('difflib_test cost :',end_time-start_time)
if __name__ == '__main__':
start_time = time.time()
# file1='/root/test/fstab'
# file2='/root/test/fstab2'
file1 = '/root/test/initrd.img-5.4.0-125-generic'
file2 = '/root/test/initrd.img-5.4.0-135-generic'
tlsh_test(file1,file2)
end_time = time.time()
print('總執行時間:',end_time-start_time)
對比小文件/大文件
原文鏈接:https://segmentfault.com/a/1190000043337815
相關推薦
- 2022-11-22 Python網絡請求模塊urllib與requests使用介紹_python
- 2022-08-01 React中的Hooks進階理解教程_React
- 2022-07-02 Python列表1~n輸出步長為3的分組實例_python
- 2023-01-09 使用C#?11的靜態接口方法改進?面向約定?的設計方法_C#教程
- 2022-04-10 解析React?中的Virtual?DOM_MsSql
- 2023-05-07 Go項目配置管理神器之viper的介紹與使用詳解_Golang
- 2023-04-19 SQLSERVER?的?truncate?和?delete?區別解析_MsSql
- 2022-10-18 C++數據結構之二叉搜索樹的實現詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支