在那个只有零和一的世界里,对零的向往,终究是一的执着。

正在下载:yara.zip yara常用规则总结

基本介绍

项目地址:https://github.com/virustotal/yara

在线手册:http://yara.readthedocs.io/en/v3.4.0/

当前使用版本:3.4

工具组件及基本用法:

yara32.exe——扫描工具,输入规则文件和目标文件,输出匹配结果


    yara32.exe ruler.txt sample.exe
    yara32.exe ruler.ya sample.exe

yarac32.exe——规则编译工具,输入明文规则文件,输出编译版本规则文件


    yarac32.exe ruler.txt ruler.ya

规则文件


明文:原始规则定义


编译版本:事先编译好的二进制规则,直接载入内存速度更快;额外优点是保密性


规则内容示例


import "pe"
import "hash"
 
private rule IsPE
{
    condition:
        // MZ signature at offset 0 and ...
        uint16(0) == 0x5A4D and
        // ... PE signature at offset stored in MZ header at 0x3C
        uint32(uint32(0x3C)) == 0x00004550
}
 
rule test_spell_string
{
    meta:
        feature = "spell string"

    strings:
        $str1 = "winm" nocase wide ascii fullword
        $str2 = "gmts:\\\\.\\ro" nocase wide ascii fullword
        $str3 = "ot\\cim" nocase wide ascii fullword
        $str4 = "instances_" nocase wide ascii fullword
        $str5 = "ocess" nocase wide ascii fullword

    condition:
        IsPE and (all of ($str*)) 
}

场景介绍

提字符串:见示例,条件可以改成“any of”(命中任何一条字符串)

提16进制字符串:$loader1B = {6A 04 68 00 ?0 00 00 [0-32] 5? 5? FF}

提正则表达式:$strStringA = /cmp word ptr ds:\[eax\], 0x5A4D[^<]*jne 0x[0-9a-zA-Z]{8,}\r\n/ nocase

(支持的具体语法见在线手册,出于效率考虑不支持反向引用:(0x[0-9A-F]*)\tjmp[^\n]*Memory[\s\S]*call \1[^<]*5A4D[^<]*4550

提入口点代码:$hexcode at pe.entry_point

提文件版本信息:pe.version_info["LegalCopyright"] contains "\x66\x49\xdb\xb0\xd1\x80\x09\x50\x6c\xf8" //武汉xxx有限公司


Comments(注释) CompanyName(公司) FileDescription(文件描述) FileVersion(文件版本) InternalName(内部名称) LegalCopyright(版权)
LegalTrademarks(商标) OriginalFilename(源文件名) ProductName(产品) ProductVersion(产品版本)

提导出表:pe.exports("InitHelperDll")

提导入表:pe.imports("kernel32.dll", "WriteProcessMemory")

提导入表hash(获取样本导入表hash的python工具见附件):pe.imphash() == "eefaecbb5ffef097573972267249076f"

提图标hash(获取样本图标hash的python工具见附件):


condition:
    for any i in (0..pe.number_of_resources) : 
    ( 
        (pe.resources[i].type == pe.RESOURCE_TYPE_ICON ) 
        and
        (
            hash.md5(pe.resources[i].offset, pe.resources[i].length) == "2990a1182a0b0507dedef64ff540c468"
        )
    )
 

提节名:pe.number_of_sections == 5 and pe.sections[3].name == ".vmp0"

提签名信息(第一次测试失败,原因是使用改造版yara):pe.signatures[0].serial contains "‎0b:b0:68:dc:56:75:14:74:a3:d1:f8:02:7d:bb:c1:13"

其他条件:如文件大小(第一次测试失败,原因是使用改造版yara)——filesize == 5721840

特征点

代码:特殊函数(如加/解密算法、peloader函数等)

字符串:拼串字符串/十六进制代码、上线地址、pdb路径、特殊字符串(如互斥量名称、服务名、密钥、加密串等)

PE结构:文件版本信息、导入/导出表、图标hash、签名结构(颁发信息、指纹、签发算法等)、节特征(数量,节名)、入口点代码、PE头信息等

总结

yara是恶意软件特征匹配的利器,目前仍被世界各大安全厂商所采用。本文主要总结一些自己平时常用的一些提特征的技巧,限于时间成本就没有针对每种场景进行更详细的讲解了。除了以上场景,另外一点也值得一提,就是规则的复用,即提好的每条规则可以被当作其他规则的条件之一,进行规则嵌套,这点对于增加规则库的质量及对规则的管理来说应该是不错的特性。其他更多有用特性请详见官方手册。