Skip to content

Impacket mssqlclient.py

Back to the Category Index / Back to the Command Index

Connects to SQL Server over TCP or an SMB named pipe and starts an interactive shell or runs specified SQL queries. It supports SQL and Windows/Kerberos authentication.

ItemDetails
Implementationexamples/mssqlclient.py
CategoryMSSQL / Exchange / MQTT
Size167 lines
DistributionInstalled as a script through scripts=examples/*.py in setup.py

Check python examples/mssqlclient.py -h for the current option definitions.

ArgumentDescription
target[[domain/]username[:password]@]<targetName or address>
-dbMSSQL database instance (default: None).
-windows-authUse Windows authentication (default: false).
-named-pipeConnect through the specified SMB named pipe instead of TCP.
-debugEnable debug logging.
-tsAdd timestamps to log output.
-showDisplay queries.
-commandCommands to run in the SQL shell; multiple commands are accepted.
-fileFile containing commands to run in the SQL shell.
--host-nameHostName property used when connecting to SQL Server.
--app-nameAppName property used when connecting to SQL Server.
--client-interface-nameCltIntName property used when connecting to SQL Server.
-hashesNTLM hashes in LMHASH:NTHASH format.
-auth-smbCredentials for the named pipe’s SMB connection, separate from SQL credentials, in [domain/]username[:password] format.
-hashes-smbNTLM hashes for the named pipe’s SMB connection in LMHASH:NTHASH format.
-no-passSkip the password prompt (useful with -k).
-kUse Kerberos authentication. Read credentials from the ccache (KRB5CCNAME) based on the target; fall back to command-line credentials if none are valid.
-aesKey128- or 256-bit AES key for Kerberos authentication.
-dc-ipDomain controller IP address. Defaults to the domain (FQDN) in target.
-target-ipTarget machine IP address. Defaults to target; useful when the target is a NetBIOS name that cannot be resolved.
-portTarget MSSQL port (default: 1433).

Related technology: PRELOGIN, LOGIN7, and token streams in TDS / MQTT / Exchange.

After parsing the target, the command connects through TCP or the SMB named pipe specified by -named-pipe, then calls MSSQL.connect(), performs pre-login, and logs in. -command / -file run batch queries; otherwise, MSSQLShell dispatches SQL and helper commands such as xp_cmdshell. Result rows and server messages are displayed from the TDS token parser.

A TDS session normally proceeds through TCP connection, PRELOGIN, TLS if needed, LOGIN7, and authentication token exchange. With -named-pipe, an SMB connection carries the traffic through a pipe. SQL authentication puts the username and password into LOGIN7 in TDS format; Windows authentication exchanges NTLM/Kerberos tokens through the SSPI field. -windows-auth switches the login mechanism, not just the username syntax. When -windows-auth or -k is used with a named pipe, SQL Server sees the Windows identity used for the SMB connection. With -auth-smb, these credentials may differ from those in the target argument.

After login, queries are sent as SQL Batch packets. The server returns a token stream containing COLMETADATA, ROW, DONE, ERROR, INFO, and others. MSSQL parses tokens in order and matches column schemas to row values. -command and -file run batches; in interactive mode, MSSQLShell routes local helpers and SQL text. The enum_tables helper queries sys.tables in the current database and displays schema, table name, and type.

Database and context changes, along with transaction state, remain in the same session. Server-side procedures invoked by interactive helpers can change the target’s state, just like ordinary queries. A DONE token may follow an ERROR token, so do not confuse packet and token boundaries; display multiple result sets and NULL/type conversions correctly.

SymbolLocationEntry Point for Tracing Behavior
module-level entry1Parses arguments and runs the operation at the top level
ModuleMain Names Used
impacket.exampleslogger
impacket.examples.mssqlshellSQLSHELL
impacket.examples.utilsparse_target
impackettds, version

Output, Side Effects, and Required Privileges

Section titled “Output, Side Effects, and Required Privileges”
  • Some operations change the target’s state. Confirm the target name, SID, and path before making changes.
  • Distinguish missing input files, damaged files, truncated reads, and unsupported formats.

This command depends on impacket.tds.MSSQL, impacket.examples.mssqlshell, and Kerberos/NTLM helpers. The CLI bootstraps connections; changes to the TDS wire format belong in impacket/tds.py, while shell command changes belong in the shared shell.

  • Syntax: python -m py_compile examples/mssqlclient.py
  • CLI: python examples/mssqlclient.py -h
  • No test specific to this command was found. Choose shared-module tests and, if needed, a run in an isolated environment.