信息安全 | YARA特征规则介绍与编写

YRAR规则

通过编写YARA规则,我们可以基于文本或二进制模式对已掌握的恶意样本特征进行检测匹配,从而帮助我们更好的识别和分类恶意样本。

支持平台

Windows,Linux和Mac OS

官方文档

yara.readthedocs.io/en/v3.7.0/writingrules.html

规则组成

  1. 规则名

常用规则名的例子:恶意样本类型_家族

  1. 描述 meta

该区域主要是对规则进行描述,详细的规则描述能够大大的降低后续维护规则的成本。

  1. 文本字符串区域

支持类型有三种:

  • 十六进制字符串
//通配符:可以代替某些未知字节,与任何内容匹配
rule WildcardExample
{
    strings:
       //使用‘?’作为通配符
       $hex_string = { 00 11 ?? 33 4? 55 }
 
    condition:
       $hex_string
}
 
//跳转:可以匹配长度可变的字符串
rule JumpExample
{
        strings:
           //使用‘[]’作为跳转,与任何长度为0-2字节的内容匹配
           $hex_string1 = { 00 11 [2] 44 55 }
           $hex_string2 = { 00 11 [0-2] 44 55 }
           //该写法与string1作用完全相同
           $hex_string3 = { 00 11 ?? ?? 44 55 }
 
        condition:
           $hex_string1 and $hex_string2
}
 
//也可以使用类似于正则表达式的语法
rule AlternativesExample1
{
    strings:
       $hex_string = { 00 11 ( 22 | 33 44 ) 55 }
       /*
        可以匹配以下内容:
        00 11 22 55
        00 11 33 44 55
       */
 
    condition:
       $hex_string
}
 
//还可以将上面介绍的方法整合在一起用
rule AlternativesExample2
{
    strings:
       $hex_string = { 00 11 ( 33 44 | 55 | 66 ?? 88 ) 99 }
 
    condition:
       $hex_string
}
  • 文本字符串
//转义符:
    \"        双引号
    \\        反斜杠
    \t        制表符
    \n        换行符
    \xdd      十六进制的任何字节

//修饰符:
    nocase:  不区分大小写
    wide:    匹配2字节的宽字符
    ascii:   匹配1字节的ascii字符
    xor:     匹配异或后的字符串
    fullword:匹配完整单词
    private: 定义私有字符串

rule CaseInsensitiveTextExample
{
    strings:
        //不区分大小写
        $text_string = "foobar" nocase
        //匹配宽字符串
        $wide_string = "Borland" wide
        //同时匹配2种类型的字符串
        $wide_and_ascii_string = "Borland" wide ascii
        //匹配所有可能的异或后字符串
        $xor_string = "This program cannot" xor
        //匹配所有可能的异或后wide ascii字符串
        $xor_string = "This program cannot" xor wide ascii
        //限定异或范围
        $xor_string = "This program cannot" xor(0x01-0xff)
        /*
        全词匹配
        匹配:www.domain.com  
        匹配:www.my-domain.com  
        不匹配:www.mydomain.com
        */
        $wide_string = "domain" fullword
        //私有字符串可以正常匹配规则,但是永远不会在输出中显示
        $text_string = "foobar" private
    condition:
        $text_string
}
  • 正则表达式
  1. 条件区域

常见的匹配条件:

  • all of them // 匹配规则中的所有字符串
  • any of them // 匹配规则中的任意字符串
  • all of ($a*) // 匹配标识符以$a开头的所有字符串
  • any of ($a,$b,$c) // 匹配a, b,c中的任意一个字符串
  • 1 of ($*) // 匹配规则中的任意一个字符串

其他匹配条件:

  • 根据匹配字符串在文件或内存中出现的次数
rule CountExample
{
    strings:
        $a = "dummy1"
        $b = "dummy2"

    condition:
        //a字符串出现6次,b字符串大于10次
        #a == 6 and #b > 10
}
  • 根据匹配字符串在文件或内存中的偏移
rule AtExample
{
    strings:
        $a = "dummy1"
        $b = "dummy2"

    condition:
        //a和b字符串出现在文件或内存的100和200偏移处
        $a at 100 and $b at 200
}
  • 根据文件大小匹配
rule FileSizeExample
{
    condition:
       //filesize只在文件时才有用,对进程无效
       //KB MB后缀只能与十进制大小一起使用
       filesize > 200KB
}

规则例子

rule Rule_Name
{
    meta:
        description = "描述"
    strings:
        $str1 = "xxx" // 字符串区域
    condition:
        $str1   // 条件区域
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值