picoCTF 2025 YaraRules0x100 writeup

YaraRules0x100

Description

Dear Threat Intelligence Analyst,Quick heads up – we stumbled upon a shady executable file on one of our employee’s Windows PCs. Good news: the employee didn’t take the bait and flagged it to our InfoSec crew.Seems like this file sneaked past our Intrusion Detection Systems, indicating a fresh threat with no matching signatures in our database.Can you dive into this file and whip up some YARA rules? We need to make sure we catch this thing if it pops up again.Thanks a bunch!

YARAルールとは

自分で文字列やバイナリパターンを定義してマルウェアや不正なファイルを検出するパターンマッチングツールみたいです。
書き方は次のサイトを参考:YARA によるマルウェア検出入門

実践

ヒントをもとにいろいろやってみます。

1.Your rule should also not generate any false positives (or false negatives). Refine your rule to perfection! One YARA rule file can have multiple rules! Maybe define one rule for Packed binary and another rule for Unpacked binary in the same rule file?

2.Since this is a Windows executable file, some strings within this binary can be “wide” strings. Try declaring your string variables something like $str = "Some Text" wide ascii wherever necessary.

3.Your rule should also not generate any false positives (or false negatives). Refine your rule to perfection! One YARA rule file can have multiple rules! Maybe define one rule for Packed binary and another rule for Unpacked binary in the same rule file?

どうやらバイナリをパックされているみたいで、stringsコマンドでファイルを見てみると、UPXを使って圧縮されていることがわかります。

方針としてはstringsコマンドを使って悪意のある文字列を圧縮前と圧縮後のファイルから探していく感じになります。

まずは圧縮後のファイルを見ていきます。

!This program cannot be run in DOS mode.
xRich
UPX0
UPX1
.rsrc
4.21
UPX!

とりあえず文字列をUPX0に設定して、テストに出してみます。
YARAルールはこんな感じ。

rule test
{
    strings:
        $str1 = "UPX0" wide ascii

    condition:
        $str1
}

結果

:::::

Status: Failed
False Negatives Check: Testcase failed. Your rule generated a false negative.
False Positives Check: Testcases passed!
Stats: 62 testcase(s) passed. 1 failed. 1 testcase(s) unchecked. 64 total testcases.
Pass all the testcases to get the flag.

:::::

偽陽性はなく偽陰性が残っているので、次は圧縮前のファイルから文字列を設定していきます。

stringsコマンドで見ていくと

GDI32.dll
OpenProcessToken
AdjustTokenPrivileges
LookupPrivilegeValueW
ADVAPI32.dll
CommandLineToArgvW
SHELL32.dll

windows APIはあまり詳しくはないのですが、AdjustTokenPrivilegesとLookupPrivilegeValueWはセキュリティに関連しそうなので、とりあえずこれらを文字列に指定してやってみます。

最初は片方だけでYARAルールはこんな感じ。

rule test
{
    strings:
        $str1 = "UPX0" wide ascii
        $str2 = "AdjustTokenPrivileges" wide ascii

    condition:
        $str1 or $str2
}

結果

:::::

Status: Failed
False Negatives Check: Testcases passed!
False Positives Check: Testcase failed. Your rule generated a false positive.
Stats: 25 testcase(s) passed. 1 failed. 38 testcase(s) unchecked. 64 total testcases.
Pass all the testcases to get the flag.

:::::

偽陰性はなくなりましたが、偽陽性が出てきましたね。偽陽性をなくすためにもう1つの文字列も設定します。

YARAルールはこんな感じ。

rule test
{
    strings:
        $str1 = "UPX0" wide ascii
        $str2 = "AdjustTokenPrivileges" wide ascii
        $str3 = "LookupPrivilegeValueW" wide ascii

    condition:
        $str1 or ($str2 and $str3)
}

結果

:::::

Status: Passed
Congrats! Here is your flag: picoCTF{yara_rul35_r0ckzzz_b2b2ee8c}

:::::

感想

この問題を解き始めたとき「YARAルール、何それ?」って感じでしたが、それっぽい文字列を指定してたら解けてしまいました。多分、本格的な問題が来たらこんな感じでは解けない気がする。まあ、YARAルールとは何かなんとなく理解できたので、良い学びになったと思います。

タイトルとURLをコピーしました