Impacket services.py
Service Control Manager の RPC を使い、サービスの一覧表示、状態確認、作成、設定変更、起動、停止、削除を行います。
| 項目 | 内容 |
|---|---|
| 実装 | examples/services.py |
| カテゴリ | リモート実行 / WMI / サービス |
| 規模 | 353 行 |
| 配布 | setup.py の scripts=examples/*.py によりスクリプトとしてインストール |
CLI と入力
Section titled “CLI と入力”オプションの最新の定義は python examples/services.py -h で確認できます。
subcommand: start、stop、delete、status、config、list、create、change
| 引数 | 説明 |
|---|---|
target | [[domain/]username[:password]@] |
-debug | Turn DEBUG output ON |
-ts | Adds timestamp to every logging output |
-name | service name(必須) |
-display | display name(必須) |
-path | binary path(必須) |
-service_type | service type |
-start_type | service start type |
-start_name | string that specifies the name of the account under which the service should run |
-password | string that contains the password of the account whose name was specified by the start_name parameter |
-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 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) |
詳細な処理フロー
Section titled “詳細な処理フロー”SMB/RPC transport で SCMR へ bind し、manager handle を開きます。subcommand に応じて service handle の取得、状態照会、制御 code、作成または構成変更を行い、結果を表示して各 handle を閉じます。
仕組みの詳細
Section titled “仕組みの詳細”Windows service は Service Control Manager の database entry と、現在の実行 state を持ちます。このコマンドは SCMR へ bind し、まず manager handle、 必要な action では service handle を開きます。start/stop は state transition、 create/delete/change は database 変更、status/config/list は読み取りです。
start は引数配列を service start request へ渡し、stop は control code を
送ります。要求が受理されたことと最終 state に到達したことは同じではないため、
status field の pending/running/stopped を解釈する必要があります。create と
change は service type、start type、account、binary path を RPC の enum と
Unicode field へ変換します。
service account password は RPC session key を使う protocol 固有形式へ暗号化 して送る必要があり、平文を structure に直接置くものではありません。handle access mask は action ごとに最小化でき、list の paging、既に存在する/削除済み service、dependent service による stop failure をそれぞれ扱います。
主要な実装要素
Section titled “主要な実装要素”| シンボル | 位置 | 責務を追う入口 |
|---|---|---|
SVCCTL | 38 | __init__()、run()、doStuff() |
Impacket 内部
Section titled “Impacket 内部”| モジュール | 使用する主な名前 |
|---|---|
impacket.examples | logger |
impacket.examples.utils | parse_target |
impacket | version |
impacket.dcerpc.v5 | scmr, transport |
impacket.dcerpc.v5.ndr | NULL |
impacket.crypto | encryptSecret |
出力・副作用・必要権限
Section titled “出力・副作用・必要権限”- 対象の状態を変更する操作があります。変更前に対象名や SID、パスを確認してください。
失敗経路と後始末
Section titled “失敗経路と後始末”- 失敗時は、名前解決、接続、認証、サービスの状態、権限のどこで止まったかを、プロトコルのステータスから切り分けます。
実装を変更するとき
Section titled “実装を変更するとき”dcerpc.v5.scmr / transport、NDR NULL、service password の encryptSecret に依存します。SVCCTL.run() / doStuff() が dispatch と protocol 操作を担当します。service type と start type の CLI 値を RPC 定数へ変換する箇所が主な拡張点です。
- 構文:
python -m py_compile examples/services.py - CLI:
python examples/services.py -h - 関連テスト:
tests/SMB_RPC/test_ndr.py - 関連テスト:
tests/misc/test_crypto.py - 関連テスト:
tests/misc/test_dcerpc_v5_ndr.py - 関連テスト:
tests/misc/test_krb5_crypto.py

