コンテンツにスキップ

カテゴリ目次へ戻る / コマンド全体目次へ戻る

RemCom のサービスバイナリを対象へ配置・起動し、名前付きパイプを通じてコマンドや対話シェルを実行します。

項目内容
実装examples/psexec.py
カテゴリリモート実行 / WMI / サービス
規模677 行
配布setup.py の scripts=examples/*.py によりスクリプトとしてインストール

オプションの最新の定義は python examples/psexec.py -h で確認できます。

引数説明
target[[domain/]username[:password]@]
commandcommand (or arguments if -c is used) to execute at the target (w/o path) - (default:cmd.exe)
-ccopy the filename for later execution, arguments are passed in the command option
-pathpath of the command to execute
-filealternative RemCom binary (be sure it doesn’t require CRT)
-tsadds timestamp to every logging output
-debugTurn DEBUG output ON
-codec実行時の値を使って動的に生成される help
-hashesNTLM hashes, format is LMHASH:NTHASH
-no-passdon’t ask for password (useful for -k)
-kUse 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
-aesKeyAES key to use for Kerberos Authentication (128 or 256 bits)
-keytabRead keys for SPN from keytab file
-dc-ipIP Address of the domain controller. If omitted it will use the domain part (FQDN) specified in the target parameter
-target-ipIP 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
-portDestination port to connect to SMB Server(選択肢: 139, 445、既定値: 445)
-service-nameThe name of the service used to trigger the payload
-remote-binary-nameThis will be the name of the executable uploaded on the target

関連技術: SMB の named pipe、DCE/RPC の SCMR。

SMB session を確立し、ServiceInstall が service binary を share へ配置して SCMR service を作成・開始します。main pipe で実行要求を送り、stdin/stdout/stderr の三本の pipe thread が I/O を中継します。終了時に service と配置 binary を削除します。-c は追加 executable の転送経路です。

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 から取得します。

シンボル位置責務を追う入口
RemComMessage45構造体/定数定義
RemComResponse55構造体/定数定義
PSEXEC67__init__()、run()、openPipe()、doStuff()
Pipes226__init__()、connectPipe()
RemoteStdOutPipe265__init__()、run()
RemoteStdErrPipe383__init__()、run()
RemoteShell471__init__()、connect_transferClient()、do_help()、do_shell()、do_lget()、do_lput()、do_lcd()、emptyline()、default()、send_data()
RemoteStdInPipe580__init__()、run()
モジュール使用する主な名前
impacket.exampleslogger, remcomsvc, serviceinstall
impacketsmb, version
impacket.smbconnectionSMBConnection
impacket.dcerpc.v5transport
impacket.structureStructure
impacket.examples.utilsparse_target
impacket.krb5.keytabKeytab
import 名配布パッケージ経路・宣言状態
sixsix直接 import、宣言済み
  • 対象の状態を変更する操作があります。変更前に対象名や SID、パスを確認してください。
  • remote service、task、file、share、WMI/RPC resource などの一時資源を利用する経路があります。異常終了時の回収も動作契約です。
  • 失敗時は、名前解決、接続、認証、サービスの状態、権限のどこで止まったかを、プロトコルのステータスから切り分けます。
  • 明示的な cleanup 名がなくても finally 相当の経路と protocol handle の close/delete を確認します。

SMBConnection、dcerpc.v5.transport、examples.remcomsvc / serviceinstall、threading に依存します。PSEXEC、Pipes、各 stream class の接続順序と cleanup が重要です。