Impacket smbexec.py
SCMR で一時サービスを作り、コマンドの実行結果を SMB 共有経由で取得します。対象へ実行用バイナリを常設しません。
| 項目 | 内容 |
|---|---|
| 実装 | examples/smbexec.py |
| カテゴリ | リモート実行 / WMI / サービス |
| 規模 | 404 行 |
| 配布 | setup.py の scripts=examples/*.py によりスクリプトとしてインストール |
CLI と入力
Section titled “CLI と入力”オプションの最新の定義は python examples/smbexec.py -h で確認できます。
| 引数 | 説明 |
|---|---|
target | [[domain/]username[:password]@] |
-share | share where the output will be grabbed from (default C$) |
-mode | mode to use (default SHARE, SERVER needs root!)(選択肢: SERVER, SHARE) |
-ts | adds timestamp to every logging output |
-debug | Turn DEBUG output ON |
-codec | 実行時の値を使って動的に生成される help |
-shell-type | choose a command processor for the semi-interactive shell(選択肢: cmd, powershell、既定値: cmd) |
-dc-ip | IP Address of the domain controller. If omitted it will use the domain part (FQDN) specified in the target parameter |
-target-ip | IP Address of the target machine. If ommited it will use whatever was specified as target. This is useful when target is the NetBIOS name and you cannot resolve it |
-port | Destination port to connect to SMB Server(選択肢: 139, 445、既定値: 445) |
-service-name | The name of theservice used to trigger the payload |
-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) |
-keytab | Read keys for SPN from keytab file |
詳細な処理フロー
Section titled “詳細な処理フロー”SMB transport で Service Control Manager へ接続し、command を service の binPath として登録・開始します。出力は target から指定 share へ書かせ、client 側または local SMB server で回収します。SERVER/SHARE mode により出力経路が変わり、終了時に service と一時出力を削除します。
仕組みの詳細
Section titled “仕組みの詳細”SMBEXEC は executable service binary を配置せず、SCMR service の binPath
自体に shell command を設定して起動します。service process として長時間動く
ことを期待せず、command interpreter に処理させた後で一時 service entry を
削除します。PSEXEC との大きな違いは専用 named-pipe agent を使わない点です。
stdout/stderr は service から直接 RPC へ戻せないため、command line で file または UNC path へ redirect します。SHARE mode は対象上の share から回収し、 SERVER mode は client 側で一時 SMB server を立てて対象から書き込ませます。 この選択は inbound connection の要否、firewall、credential の露出範囲を変えます。
対話 shell は入力ごとに新しい service command を作り、出力 file の生成を polling して表示します。service start が timeout/error を返しても command が 実行済みの場合があるため、SCMR status と出力 file の存在を別に評価します。 一時 service、local server thread、出力 file の cleanup を確実に行います。
主要な実装要素
Section titled “主要な実装要素”| シンボル | 位置 | 責務を追う入口 |
|---|---|---|
SMBServer | 62 | __init__()、cleanup_server()、run()、stop() |
CMDEXEC | 116 | __init__()、run() |
RemoteShell | 173 | __init__()、finish()、do_shell()、do_exit()、do_EOF()、emptyline()、do_cd()、do_CD()、default()、get_output()、execute_remote()、send_data() |
Impacket 内部
Section titled “Impacket 内部”| モジュール | 使用する主な名前 |
|---|---|
impacket.examples | logger |
impacket.examples.utils | parse_target |
impacket | smbserver, version |
impacket.dcerpc.v5 | scmr, transport |
impacket.krb5.keytab | Keytab |
出力・副作用・必要権限
Section titled “出力・副作用・必要権限”- 対象の状態を変更する操作があります。変更前に対象名や SID、パスを確認してください。
- remote service、task、file、share、WMI/RPC resource などの一時資源を利用する経路があります。異常終了時の回収も動作契約です。
失敗経路と後始末
Section titled “失敗経路と後始末”- 失敗時は、名前解決、接続、認証、サービスの状態、権限のどこで止まったかを、プロトコルのステータスから切り分けます。
- 後始末に関係する実装として
cleanup_server(),stop(),finish()があります。新しい早期 return や例外経路でも到達するか確認します。
実装を変更するとき
Section titled “実装を変更するとき”dcerpc.v5.scmr / transport、impacket.smbserver、keytab に依存します。CMDEXEC が接続、RemoteShell が command lifecycle、SMBServer が callback share を担当します。
- 構文:
python -m py_compile examples/smbexec.py - CLI:
python examples/smbexec.py -h - 関連テスト:
tests/misc/test_dcerpc_v5_ndr.py

