コンテンツにスキップ

Impacket mssqlclient.py

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

SQL Server に TCP または SMB 名前付きパイプで接続し、対話シェルや指定した SQL クエリを実行します。SQL 認証と Windows/Kerberos 認証に対応します。

項目内容
実装examples/mssqlclient.py
カテゴリMSSQL / Exchange / MQTT
規模167 行
配布setup.py の scripts=examples/*.py によりスクリプトとしてインストール

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

引数説明
target[[domain/]username[:password]@]
-dbMSSQL database instance (default None)
-windows-authwhether or not to use Windows Authentication (default False)
-named-pipe指定した SMB 名前付きパイプを使い、TCP の代わりに接続します。
-debugTurn DEBUG output ON
-tsAdds timestamp to every logging output
-showshow the queries
-commandCommands to execute in the SQL shell. Multiple commands can be passed.
-fileinput file with commands to execute in the SQL shell
--host-nameHostName property to use when connecting to the MSSQLServer
--app-nameAppName property to use when connecting to the MSSQLServer
--client-interface-nameCltIntName property to use when connecting to the MSSQLServer
-hashesNTLM hashes, format is LMHASH:NTHASH
-auth-smb名前付きパイプへの SMB 接続に使う資格情報。SQL 接続用とは別に [domain/]username[:password] 形式で指定できます。
-hashes-smb名前付きパイプへの SMB 接続に使う NTLM ハッシュ。形式は 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)
-dc-ipIP Address of the domain controller. If ommited it 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
-porttarget MSSQL port (default 1433)

関連技術: TDS / MQTT / Exchange の PRELOGIN、LOGIN7、token stream。

target を解析し、TCP または -named-pipe で指定した SMB 名前付きパイプ上で MSSQL.connect()、pre-login、login の順に session を確立します。 -command / -file は batch query として実行し、それ以外は MSSQLShell が SQL と xp_cmdshell 等の補助 command を dispatch します。結果 row と server message は TDS token parser から表示されます。

TDS session は既定では TCP 接続、PRELOGIN、必要なら TLS、LOGIN7、認証 token exchange の順に成立します。-named-pipe では SMB 接続上のパイプを搬送路に使います。 SQL authentication では LOGIN7 に user/password を TDS 形式で格納し、Windows authentication では SSPI field で NTLM/Kerberos token を交換します。-windows-auth は単なる username 表記ではなく、この login mechanism を切り替えます。名前付きパイプで -windows-auth または -k を使う場合、SQL Server が見る Windows identity は SMB 接続に使った 資格情報です。-auth-smb を指定した場合は target 引数の資格情報とは異なり得ます。

login 後、query は SQL Batch packet として送られ、server は COLMETADATA、 ROW、DONE、ERROR、INFO 等の token stream を返します。MSSQL は token を 順に parse して column schema と row value を対応付けます。-command と -file は batch 実行、interactive mode は MSSQLShell が local helper と SQL text を振り分けます。enum_tables helper は現在の database の sys.tables を照会し、schema、テーブル名、種類を表示します。

database/context 変更や transaction state は同じ session に残ります。 interactive helper が実行する server-side procedure も通常 query と同様に 対象状態を変え得ます。ERROR token があっても後続 DONE token が来る場合があり、 packet boundary と token boundary を混同せず、複数 result set と NULL/type conversion を正しく表示します。

シンボル位置責務を追う入口
module-level entry1トップレベルで引数解析と処理を実行
モジュール使用する主な名前
impacket.exampleslogger
impacket.examples.mssqlshellSQLSHELL
impacket.examples.utilsparse_target
impackettds, version
  • 対象の状態を変更する操作があります。変更前に対象名や SID、パスを確認してください。
  • 入力ファイルがない場合、壊れている場合、読み取りが途中で終わる場合、未対応の形式だった場合を区別します。

impacket.tds.MSSQL、impacket.examples.mssqlshell、Kerberos/NTLM helper に依存します。CLI は接続 bootstrap で、TDS wire 変更は impacket/tds.py、shell command 変更は共有 shell 側が責務です。

  • 構文: python -m py_compile examples/mssqlclient.py
  • CLI: python examples/mssqlclient.py -h
  • このコマンド専用のテストは見つかりません。共有モジュールのテストと、必要に応じて隔離環境での動作確認を選びます。