Impacket mssqlclient.py
SQL Server に TCP または SMB 名前付きパイプで接続し、対話シェルや指定した SQL クエリを実行します。SQL 認証と Windows/Kerberos 認証に対応します。
| 項目 | 内容 |
|---|---|
| 実装 | examples/mssqlclient.py |
| カテゴリ | MSSQL / Exchange / MQTT |
| 規模 | 167 行 |
| 配布 | setup.py の scripts=examples/*.py によりスクリプトとしてインストール |
CLI と入力
Section titled “CLI と入力”オプションの最新の定義は python examples/mssqlclient.py -h で確認できます。
| 引数 | 説明 |
|---|---|
target | [[domain/]username[:password]@] |
-db | MSSQL database instance (default None) |
-windows-auth | whether or not to use Windows Authentication (default False) |
-named-pipe | 指定した SMB 名前付きパイプを使い、TCP の代わりに接続します。 |
-debug | Turn DEBUG output ON |
-ts | Adds timestamp to every logging output |
-show | show the queries |
-command | Commands to execute in the SQL shell. Multiple commands can be passed. |
-file | input file with commands to execute in the SQL shell |
--host-name | HostName property to use when connecting to the MSSQLServer |
--app-name | AppName property to use when connecting to the MSSQLServer |
--client-interface-name | CltIntName property to use when connecting to the MSSQLServer |
-hashes | NTLM hashes, format is LMHASH:NTHASH |
-auth-smb | 名前付きパイプへの SMB 接続に使う資格情報。SQL 接続用とは別に [domain/]username[:password] 形式で指定できます。 |
-hashes-smb | 名前付きパイプへの SMB 接続に使う NTLM ハッシュ。形式は 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 omitted it will use whatever was specified as target. This is useful when target is the NetBIOS name and you cannot resolve it |
-port | target MSSQL port (default 1433) |
関連技術: TDS / MQTT / Exchange の PRELOGIN、LOGIN7、token stream。
詳細な処理フロー
Section titled “詳細な処理フロー”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 から表示されます。
仕組みの詳細
Section titled “仕組みの詳細”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 を正しく表示します。
主要な実装要素
Section titled “主要な実装要素”| シンボル | 位置 | 責務を追う入口 |
|---|---|---|
| module-level entry | 1 | トップレベルで引数解析と処理を実行 |
Impacket 内部
Section titled “Impacket 内部”| モジュール | 使用する主な名前 |
|---|---|
impacket.examples | logger |
impacket.examples.mssqlshell | SQLSHELL |
impacket.examples.utils | parse_target |
impacket | tds, version |
出力・副作用・必要権限
Section titled “出力・副作用・必要権限”- 対象の状態を変更する操作があります。変更前に対象名や SID、パスを確認してください。
失敗経路と後始末
Section titled “失敗経路と後始末”- 入力ファイルがない場合、壊れている場合、読み取りが途中で終わる場合、未対応の形式だった場合を区別します。
実装を変更するとき
Section titled “実装を変更するとき”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 - このコマンド専用のテストは見つかりません。共有モジュールのテストと、必要に応じて隔離環境での動作確認を選びます。

