yara规则初识

一、YARA介绍

YARA 是一个免费开源工具,旨在帮助安全人员检测和分类恶意软件,但它不应仅限于这一种用途。YARA规则还可以帮助检测特定文件或您可能想要检测的任何内容。目前使用YARA 的知名软件有赛门铁克、火眼、卡巴斯基、VirusTotal、安天等。
YARA的每一条描述、规则都由一系列字符串和一个布尔型表达式构成,并阐述其逻辑。YARA规则可以与文件或在运行的进程,以帮助研究人员识别其是否属于某个已进行规则描述的恶意软件等。
rule silent_banker : banker
{
    meta:
        description = "This is just an example"
        threat_level = 3
        in_the_wild = true
        author="zq" 
        version="0.1" 
        date="2022/08/12"
    strings:
        $a = {6A 40 68 00 30 00 00 6A 14 8D 91}  # 文本转16进制:16进制转换,16进制转换文本字符串,在线16进制转换 | 在线工具
        $b = {8D 4D B0 2B C1 83 C0 27 99 6A 4E 59 F7 F9}
        $c = "UVODFRYSIHLNWPEJXQZAKCBGMT"
    condition:
        $a or $b or $c
}
上述例子中,任意一个YARA文件,只要包含了三个字符串如a、b、c之一的文件都必须报告为silent_banker。
其中:
  • rule silent_banker : banker 是声明当前规则名称,这个名称可以解释为检测banker类型的样本;
  • meta 后面的是一些描述信息,比如可以有规则说明、作者信息、威胁等级、在野情况、文件MD5、来源等内容;
  • strings后面是检测规则,有字符串、字节序列等内容;
  • condition为判断条件。在本例检测规则处定义的字节序列和字符串,判断条件是or,所以只要匹配到$a $b $c三个字符串或字节序列中的任意一个,那么样本就会被识别成silent_banker 。
YARA可以跨平台在Windows,Linux和Mac OS X上运行,也可通过其命令行界面使用,或从带有yara-python扩展名的Python脚本中使用。

二、安装

sudo apt install libmagic-dev libssl-dev
sudo apt install yara
sudo pip3 install --global-option="build" --global-option="--enable-magic" yara-python==4.2.3 -i Https://pypi.tuna.tsinghua.edu.cn/simple/

三、yara使用参数

yara --help 可查看yara的使用参数:所以预编译可以节省时间。
root@node1:/home/zqtest# yara --help
YARA 3.7.1, the pattern matching swiss army knife.
Usage: yara [OPTION]... [NAMESPACE:]RULES_FILE... FILE | DIR | PID
Mandatory arguments to long options are mandatory for short options too.

  -t,  --tag=TAG                       打印标记为TAG的规则,并忽略其他规则
  -i,  --identifier=IDENTIFIER          print only rules named IDENTIFIER
  -c,  --count                          print only number of matches
  -n,  --negate                         打印不满足的规则
  -D,  --print-module-data              打印模块数据
  -g,  --print-tags                     打印标签
  -m,  --print-meta                     打印元数据
  -s,  --print-strings                  print matching strings
  -L,  --print-string-length            打印匹配字符串的长度
  -e,  --print-namespace                打印规则的名称空间
  -p,  --threads=NUMBER                 使用指定的线程数number扫描目录
  -l,  --max-rules=NUMBER               匹配多个规则后终止扫描
  -d VAR=VALUE                          define external variable
  -x MODULE=FILE                        将文件内容作为额外数据传递给模块
  -a,  --timeout=SECONDS                扫描x秒后终止
  -k,  --stack-size=SLOTS               set maximum stack size (default=16384)
       --max-strings-per-rule=NUMBER    设置每个规则的最大字符串数 (default=10000)
  -r,  --recursive                      递归搜索目录
  -f,  --fast-scan                      fast matching mode
  -w,  --no-warnings                    禁用警告
       --fail-on-warnings               fail on warnings
  -v,  --version                        显示版本信息
  -h,  --help                           show this help and exit

Send bug reports and suggestions to: vmalvarez@virustotal.com.

使用举例:

四、提取编译后的yara规则

yarac 为编译yara规则工具,使用编译后的yara规则可以加快速度,正常使用yara会先编译规则之后再加载,所以预编译可以节省时间。
yarac.exe作用就是将多个yara源码文件编译成一个yara文件。这样不仅方便存储,同时方便加解密。
yarac.exe要比python编译好的yara文件体积小很多。
用官方的yarac 编译yara规则, yarac 的使用很简单,直接yarac --help 可看到:
root@node1:/home/zqtest# yarac --help
Usage: yarac [OPTION]... [NAMESPACE:]SOURCE_FILE... OUTPUT_FILE
  -d VAR=VALUE                     define external variable
  -w,  --no-warnings               disable warnings
       --fail-on-warnings          fail on warnings
       --max-strings-per-rule=NUMBERset maximum number of strings per rule (default=10000)
  -v,  --version                   show version information
  -h,  --help                      show this help and exit
Send bug reports and suggestions to: vmalvarez@virustotal.com

# 预编译yara规则命令
cat ./rules/software_components/*.yar > ./rules/software_components.yar
yarac ./rules/software_components.yar ./compile_rule/software_components.yc

# 注意
1 yara-python和yara编译出来的规则不通用
2 yarac.exe要比python编译好的yara文件体积小很多。

五、在python中调用

import yara
1、编译YARA规则
# default, compiled from a file path
rules = yara.compile(filepath='/foo/bar/myrules')

# compile your rules from a file object
fh = open('/foo/bar/myrules')
rules = yara.compile(file=fh)
fh.close()

# compile them directly from a Python string
rules = yara.compile(source='rule dummy { condition: true }')

# compile a group of files or strings, you can do it by using the filepaths or sources named arguments
rules = yara.compile(filepaths={
    'namespace1': '/my/path/rules1',
    'namespace2': '/my/path/rules2'
})

rules = yara.compile(sources={
    'namespace1': 'rule dummy { condition: true }',
    'namespace2': 'rule dummy { condition: false }'
})
2、在所有情况下,compile 都会返回类 yara.Rules 规则的实例。 该类有一个 save 方法,可用于将编译后的规则保存到文件中
rules.save('/foo/bar/my_compiled_rules')
3、编译后的规则可以稍后使用 load 方法加载,load 的结果也是 yara.Rules 类的一个实例
rules = yara.load('/foo/bar/my_compiled_rules')
 4、规则实例也有一个 match 方法,它允许您将规则应用于文件
matches = rules.match('/foo/bar/test_file')

# 当然规则也可用于Python string
with open('/foo/bar/my_file', 'rb') as f:
    matches = rules.match(data=f.read())
   
# 或者到一个正在运行的进程
matches = rules.match(pid=1234) # match 方法返回 yara.Match 类的实例列表

print(matches)
print(matches[0])
print(matches[0].rule)
print(matches[0].tag)
print(matches[0].string)
'''结果类似:
{
    'tags': ['foo', 'bar'], # 包含与匹配规则关联的标记的字符串数组
    'matches': True,
    'namespace': 'default', # 与匹配规则关联的命名空间
    'rule': 'my_rule', # 匹配规则的名称
    'meta': {}, # 包含与匹配规则关联的元数据的字典
    'strings': [(81L, '$a', 'abc'), (141L, '$b', 'def')] # 包含有关匹配字符串的信息的元组列表,每个元组有以下形式:(偏移,字符串标识符,字符串数据)
}'''
您还可以在调用 match 方法时指定模块回调函数。 将为扫描文件的每个导入模块调用提供的函数。 您的回调函数应该期望一个字典类型的参数,并且应该返回 CALLBACK_CONTINUE 以继续执行下一个规则或 CALLBACK_ABORT 以停止将规则应用于您的数据。
比如:
import yara
def modules_callback(data):
    print(data)
    return yara.CALLBACK_CONTINUE
matches = rules.match('/foo/bar/my_file', modules_callback=modules_callback)
您还可以在调用 match 方法时指定警告回调函数。 将为每个运行时警告调用提供的函数。 您的回调函数应该有两个参数。 第一个是包含警告类型的整数,第二个是带有警告消息的字符串。 您的回调应返回 CALLBACK_CONTINUE 以继续扫描或 CALLBACK_ABORT 以停止。
比如:
import yara
def warnings_callback(warning_type, message):
    if warning_type == yara.CALLBACK_TOO_MANY_MATCHES:
        print(f"namespace:'{message.namespace}' rule:'{message.rule}' string:'{message.string}'")
    return yara.CALLBACK_CONTINUE
matches = rules.match('/foo/bar/my_file', warnings_callback=warnings_callback)

六、参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值