Impacket tstool.py
Terminal Services の RPC を使い、セッションやプロセスの列挙、切断、ログオフ、終了などを行います。
| 項目 | 内容 |
|---|---|
| 実装 | examples/tstool.py |
| カテゴリ | リモート実行 / WMI / サービス |
| 規模 | 746 行 |
| 配布 | setup.py の scripts=examples/*.py によりスクリプトとしてインストール |
CLI と入力
Section titled “CLI と入力”オプションの最新の定義は python examples/tstool.py -h で確認できます。
subcommand: qwinsta、tasklist、taskkill、tscon、tsdiscon、logoff、shutdown、msg、shadow
| 引数 | 説明 |
|---|---|
target | [[domain/]username[:password]@] |
-debug | Turn DEBUG output ON |
-ts | Adds timestamp to every logging output |
-v | Turn VERBOSE output ON |
-pid | Specifies process id (PID) |
-name | Specifies process name (ImageName). Internally it willexecute tasklist to retrieve PID by ImageName. |
-source | Source SessionId(必須) |
-dest | Destination SessionId(必須) |
-password | Destination Session’s password |
-session | SessionId to disconnect(必須) |
-logoff | Forces sessions to logoff. |
-shutdown | Shuts down the system. |
-reboot | Reboots after shutdown. |
-poweroff | Powers off after shutdown. |
-title | Title of the MessageBox [Optional] |
-message | Contents of the MessageBox(必須) |
-control | Request control of the session (default is view only) |
-prompt | Request user permission (default is silent) |
-file | Save invitation to file(既定値: invite.msrcIncident) |
-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 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) |
詳細な処理フロー
Section titled “詳細な処理フロー”SMB session と Terminal Services endpoint へ接続し、qwinsta、tasklist、taskkill、tscon、tsdiscon、logoff、shutdown、msg、shadow subcommand を TSHandler.run() が dispatch します。必要に応じて LSA で SID を user 名へ解決します。
仕組みの詳細
Section titled “仕組みの詳細”Terminal Services は session と process を独自の RPC interface で公開します。
このコマンドは SMB/RPC transport を確立し、subcommand に応じて session 列挙、
process 列挙、session control、system action を異なる TSTS request へ dispatch
します。TSHandler.run() は CLI action と protocol helper の対応表です。
qwinsta は session ID、state、station/user 情報を集め、tasklist は process
record の SID を LSA lookup で名前へ補完します。taskkill、tscon、
tsdiscon、logoff は既存 ID/name を解決して状態を変えます。msg と
shadow は可変長 text、permission/control flag、応答 code を追加で扱います。
列挙結果は session が同時に変化するため、取得後の ID が操作時には消えている ことがあります。RPC transport error、対象 session 不在、access denied、 利用者拒否などを区別します。shutdown/poweroff は全 session に影響するため、 引数 validation と確認可能なログは protocol call より前に置きます。
主要な実装要素
Section titled “主要な実装要素”| シンボル | 位置 | 責務を追う入口 |
|---|---|---|
TSHandler | 59 | __init__()、connect()、run()、get_session_list()、enumerate_sessions_config()、enumerate_sessions_info()、do_qwinsta()、lookupSids()、sidToUser()、do_tasklist()、do_taskkill()、do_tscon() ほか |
Impacket 内部
Section titled “Impacket 内部”| モジュール | 使用する主な名前 |
|---|---|
impacket | LOG, version |
impacket.examples | logger |
impacket.examples.utils | parse_target |
impacket.smbconnection | SMBConnection |
impacket.dcerpc.v5 | lsad, lsat, transport, tsts |
impacket.dcerpc.v5.rpcrt | DCERPCException, RPC_C_AUTHN_GSS_NEGOTIATE, RPC_C_AUTHN_LEVEL_PKT_PRIVACY |
impacket.dcerpc.v5.dtypes | MAXIMUM_ALLOWED |
impacket.dcerpc.v5.tsts | SHADOW_CONTROL_REQUEST, SHADOW_PERMISSION_REQUEST, SHADOW_REQUEST_RESPONSE |
出力・副作用・必要権限
Section titled “出力・副作用・必要権限”- 対象の状態を変更する操作があります。変更前に対象名や SID、パスを確認してください。
失敗経路と後始末
Section titled “失敗経路と後始末”- 失敗時は、名前解決、接続、認証、サービスの状態、権限のどこで止まったかを、プロトコルのステータスから切り分けます。
実装を変更するとき
Section titled “実装を変更するとき”dcerpc.v5.tsts、transport、lsad / lsat、SMBConnection に依存します。subcommand 固有引数の検証は RPC 呼び出し前に行い、session handle を action 後に閉じてください。
- 構文:
python -m py_compile examples/tstool.py - CLI:
python examples/tstool.py -h - 関連テスト:
tests/SMB_RPC/test_rpcrt.py - 関連テスト:
tests/misc/test_dcerpc_v5_ndr.py - 関連テスト:
tests/misc/test_tsts.py

