如何解决使用 YARA python 扫描目录
被这个问题困扰了一段时间。
我正在使用我自己的 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/)
}
我的代码是否正确? 有人可以帮我吗?
解决方法
代码没有问题。 出于某种原因,yara-python 在 Windows 上无法正常运行。 在 Linux 上试过这段代码,效果很好。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。