首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用YARA python扫描目录

用YARA python扫描目录
EN

Stack Overflow用户
提问于 2021-03-10 04:19:25
回答 1查看 1K关注 0票数 2

现在被这个问题困扰了一段时间。我正在用自己的yara规则扫描一个目录,当我尝试为单个文件编写代码时,它可以工作,但是当我在for loop上使用相同的代码时,它不匹配任何东西。

我尝试过搜索我的问题,但是它总是向我展示关于yara基础知识的文档。

代码语言:javascript
复制
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规则:

代码语言:javascript
复制
    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/)
    }

我的代码对吗?有人能帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-18 07:37:52

密码没什么问题。由于某些原因,yara-python没有在Windows上正常运行。在Linux上尝试了这段代码,它运行得非常好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66558321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档