【发布时间】:2021-03-10 04:19:25
【问题描述】:
这个问题已经困扰了一段时间了。
我正在使用自己的 yara 规则扫描目录,当我尝试使用我的代码处理单个文件时它可以工作,但是当我在 for loop 上使用相同的代码时,它不匹配任何内容。
我尝试搜索我的问题,但它总是向我显示 yara 基础知识的文档。
def scan_test(): // works
file_source = 'index.php'
match_list = []
externals = {'filename': file_source}
rules = yara.compile('rules.yar', externals=externals)
with open('filepath') as f:
matches = rules.match(data=f.read(), externals=externals)
if len(matches) > 0:
match_list.append(matches)
return match_list
def scan_test3(dir_source): // not working
match_list = []
for folder,subfolders, files in os.walk(dir_source):
for file in files:
path = os.path.join(folder, file)
try:
file_name, file_extension = os.path.splitext(file)
if (file_extension == '.txt' or file_extension == '.php'):
rules = yara.compile('rules.yar', externals={'filename': file})
with open(path) as f:
matches = rules.match(data=f.read(), externals={'filename': file})
if len(matches) > 0:
match_list.append(matches)
except Exception as e:
print(e)
pass
return match_list
测试 yara 规则:
rule test_rule
{
meta:
description = "This is a test rule"
strings:
$a = "<?php"
condition:
$a and (filename matches /index\.php/ or filename matches /login\.php/)
}
我的代码是否正确? 谁能帮我解决这个问题?
【问题讨论】:
-
我在this colab 中尝试了您的代码,但没有发现任何问题。有匹配扫描单个文件和目录。请更具体地说明您遇到的问题。
-
您的代码也适用于我,(只是 cmets 需要是 # 而不是 // 并且
filepath需要是 file_source -
您使用的是什么操作系统/平台?