Impacket psexec.py
RemCom のサービスバイナリを対象へ配置・起動し、名前付きパイプを通じてコマンドや対話シェルを実行します。
| 項目 | 内容 |
|---|---|
| 実装 | examples/psexec.py |
| カテゴリ | リモート実行 / WMI / サービス |
| 規模 | 677 行 |
| 配布 | setup.py の scripts=examples/*.py によりスクリプトとしてインストール |
CLI と入力
Section titled “CLI と入力”オプションの最新の定義は python examples/psexec.py -h で確認できます。
| 引数 | 説明 |
|---|---|
target | [[domain/]username[:password]@] |
command | command (or arguments if -c is used) to execute at the target (w/o path) - (default:cmd.exe) |
-c | copy the filename for later execution, arguments are passed in the command option |
-path | path of the command to execute |
-file | alternative RemCom binary (be sure it doesn’t require CRT) |
-ts | adds timestamp to every logging output |
-debug | Turn DEBUG output ON |
-codec | 実行時の値を使って動的に生成される help |
-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 |
-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 omitted 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 the service used to trigger the payload |
-remote-binary-name | This will be the name of the executable uploaded on the target |
関連技術: SMB の named pipe、DCE/RPC の SCMR。
詳細な処理フロー
Section titled “詳細な処理フロー”SMB session を確立し、ServiceInstall が service binary を share へ配置して SCMR service を作成・開始します。main pipe で実行要求を送り、stdin/stdout/stderr の三本の pipe thread が I/O を中継します。終了時に service と配置 binary を削除します。-c は追加 executable の転送経路です。
仕組みの詳細
Section titled “仕組みの詳細”PSEXEC 経路は二段階です。最初に通常の SMB session を使って writable share へ RemCom service binary を配置し、SCMR で一時 service を作成・開始します。 次に service が公開する named pipe 群へ接続し、実行要求と標準入出力を交換します。 RPC service control と SMB pipe transport を組み合わせる設計です。
main communication pipe には machine 名、process ID、command、working directory
等を持つ RemComMessage を送ります。stdin、stdout、stderr は互いに独立した
pipe で、RemoteStdInPipe と出力 thread が並行して処理します。対話 shell の
file upload/download は別 SMB connection を使い、command stream と大容量
file data を混ぜません。
service 起動後に pipe 接続が失敗する、出力 thread だけ切れる、追加 executable 転送後に command が失敗するなど、partial state が多い実装です。service stop/ delete、binary 削除、pipe/thread 終了を独立に行い、remote binary 名や service 名を再利用した際の衝突も扱います。終了 code は RemCom response から取得します。
主要な実装要素
Section titled “主要な実装要素”| シンボル | 位置 | 責務を追う入口 |
|---|---|---|
RemComMessage | 45 | 構造体/定数定義 |
RemComResponse | 55 | 構造体/定数定義 |
PSEXEC | 67 | __init__()、run()、openPipe()、doStuff() |
Pipes | 226 | __init__()、connectPipe() |
RemoteStdOutPipe | 265 | __init__()、run() |
RemoteStdErrPipe | 383 | __init__()、run() |
RemoteShell | 471 | __init__()、connect_transferClient()、do_help()、do_shell()、do_lget()、do_lput()、do_lcd()、emptyline()、default()、send_data() |
RemoteStdInPipe | 580 | __init__()、run() |
Impacket 内部
Section titled “Impacket 内部”| モジュール | 使用する主な名前 |
|---|---|
impacket.examples | logger, remcomsvc, serviceinstall |
impacket | smb, version |
impacket.smbconnection | SMBConnection |
impacket.dcerpc.v5 | transport |
impacket.structure | Structure |
impacket.examples.utils | parse_target |
impacket.krb5.keytab | Keytab |
外部パッケージ(直接使用)
Section titled “外部パッケージ(直接使用)”| import 名 | 配布パッケージ | 経路・宣言状態 |
|---|---|---|
six | six | 直接 import、宣言済み |
出力・副作用・必要権限
Section titled “出力・副作用・必要権限”- 対象の状態を変更する操作があります。変更前に対象名や SID、パスを確認してください。
- remote service、task、file、share、WMI/RPC resource などの一時資源を利用する経路があります。異常終了時の回収も動作契約です。
失敗経路と後始末
Section titled “失敗経路と後始末”- 失敗時は、名前解決、接続、認証、サービスの状態、権限のどこで止まったかを、プロトコルのステータスから切り分けます。
- 明示的な cleanup 名がなくても
finally相当の経路と protocol handle の close/delete を確認します。
実装を変更するとき
Section titled “実装を変更するとき”SMBConnection、dcerpc.v5.transport、examples.remcomsvc / serviceinstall、threading に依存します。PSEXEC、Pipes、各 stream class の接続順序と cleanup が重要です。
- 構文:
python -m py_compile examples/psexec.py - CLI:
python examples/psexec.py -h - 関連テスト:
tests/misc/test_dcerpc_v5_ndr.py - 関連テスト:
tests/misc/test_structure.py

