Impacket wmiexec.py
DCOM/WMI の Win32_Process.Create でコマンドや半対話シェルを実行します。
| 項目 | 内容 |
|---|---|
| 実装 | examples/wmiexec.py |
| カテゴリ | リモート実行 / WMI / サービス |
| 規模 | 469 行 |
| 配布 | setup.py の scripts=examples/*.py によりスクリプトとしてインストール |
CLI と入力
Section titled “CLI と入力”オプションの最新の定義は python examples/wmiexec.py -h で確認できます。
| 引数 | 説明 |
|---|---|
target | [[domain/]username[:password]@] |
-share | share where the output will be grabbed from (default ADMIN$) |
-nooutput | whether or not to print the output (no SMB connection created) |
-ts | Adds timestamp to every logging output |
-silentcommand | does not execute cmd.exe to run given command (no output) |
-debug | Turn DEBUG output ON |
-codec | 実行時の値を使って動的に生成される help |
-shell-type | choose a command processor for the semi-interactive shell(選択肢: cmd, powershell、既定値: cmd) |
-com-version | DCOM version, format is MAJOR_VERSION:MINOR_VERSION e.g. 5.7 |
command | command to execute at the target. If empty it will launch a semi-interactive shell |
-hashes | NTLM hashes, format is LMHASH:NTHASH |
-no-pass | don’t ask for password (useful for -k) |
-k | Use Kerberos authentication. Grabs credentials from ccache file (KRB5CCNAME) based on target parameters. If valid credentials cannot be found, it will use the ones specified in the command line |
-aesKey | AES key to use for Kerberos Authentication (128 or 256 bits) |
-dc-ip | IP Address of the domain controller. If ommited it use the domain part (FQDN) specified in the target parameter |
-target-ip | IP Address of the target machine. If omitted it will use whatever was specified as target. This is useful when target is the NetBIOS name and you cannot resolve it |
-A | smbclient/mount.cifs-style authentication file. See smbclient man page’s -A option. |
-keytab | Read keys for SPN from keytab file |
関連技術: DCE/RPC の DCOM / WMI、SMB の出力読み戻し。
詳細な処理フロー
Section titled “詳細な処理フロー”DCOM へ接続し、WMI root\\cimv2 login、Win32_Process 取得、process create の順に進みます。出力ありでは command を share 上の一時ファイルへ redirect し SMB で読み戻します。-nooutput 相当の経路では SMB 接続を作らず fire-and-forget で実行できます。
仕組みの詳細
Section titled “仕組みの詳細”WMIEXEC は DCOM で WMI service へ接続し、root\cimv2 namespace の
Win32_Process class method Create を呼びます。SCMR service を作る方式と
異なり、Windows Management Instrumentation が process creation の制御面に
なります。DCOM authentication と WMI namespace login は別段階です。
WMI method の戻り値には process creation status と PID は含まれますが、
stdout は含まれません。そのため出力ありの command は shell redirection で
SMB share の一時 file へ書かせ、RemoteShell.get_output() が存在するまで
待って読み出します。-silentcommand/no-output 経路ではこの SMB dependency
を省けますが、終了 code や完了の観測能力も減ります。
interactive shell の cd は remote current directory を local state として
保持し、次の command に prefix します。WMI process 自体に永続 current directory
があるわけではありません。DCOM interface release、WMI login object、SMB
session、一時 file の lifecycle を別々に管理することが重要です。
主要な実装要素
Section titled “主要な実装要素”| シンボル | 位置 | 責務を追う入口 |
|---|---|---|
WMIEXEC | 49 | __init__()、run() |
RemoteShell | 121 | __init__()、do_shell()、do_help()、do_lcd()、do_lget()、do_lput()、do_exit()、do_EOF()、emptyline()、do_cd()、default()、get_output() ほか |
AuthFileSyntaxError | 305 | __init__() |
load_smbclient_auth_file() | 319 | モジュール関数 |
Impacket 内部
Section titled “Impacket 内部”| モジュール | 使用する主な名前 |
|---|---|
impacket.examples | logger |
impacket.examples.utils | parse_target |
impacket | version |
impacket.smbconnection | SMB2_DIALECT_002, SMB2_DIALECT_21, SMBConnection, SMB_DIALECT |
impacket.dcerpc.v5.dcomrt | COMVERSION, DCOMConnection |
impacket.dcerpc.v5.dcom | wmi |
impacket.dcerpc.v5.dtypes | NULL |
impacket.krb5.keytab | Keytab |
出力・副作用・必要権限
Section titled “出力・副作用・必要権限”- 対象の状態を変更する操作があります。変更前に対象名や SID、パスを確認してください。
- remote service、task、file、share、WMI/RPC resource などの一時資源を利用する経路があります。異常終了時の回収も動作契約です。
失敗経路と後始末
Section titled “失敗経路と後始末”- 失敗時は、名前解決、接続、認証、サービスの状態、権限のどこで止まったかを、プロトコルのステータスから切り分けます。
- 明示的な cleanup 名がなくても
finally相当の経路と protocol handle の close/delete を確認します。
実装を変更するとき
Section titled “実装を変更するとき”dcerpc.v5.dcomrt / dcom.wmi、SMBConnection、keytab に依存します。WMIEXEC が WMI session、RemoteShell が command と file transfer を所有します。COM release と一時ファイル削除を維持します。
- 構文:
python -m py_compile examples/wmiexec.py - CLI:
python examples/wmiexec.py -h - 関連テスト:
tests/dcerpc/test_dcomrt.py

