impacket-mssqlclient是Kali Linux中Impacket工具集的重要组成部分,专门用于与Microsoft SQL Server(MSSQL)数据库进行交互的客户端工具。它支持TDS(Tabular Data Stream)协议,能够建立与MSSQL服务器的连接,并执行各种SQL命令、管理数据库会话。
该工具在渗透测试和安全审计中应用广泛,主要用于验证MSSQL服务器的身份认证机制、执行SQL查询、测试数据库权限,以及利用数据库漏洞进行进一步攻击。其支持多种认证方式(包括Windows认证、Kerberos认证等)和加密连接,是针对MSSQL服务器进行安全测试的必备工具。
工具功能描述:TDS client implementation (SSL supported)(支持SSL的TDS客户端实现)
二、impacket-mssqlclient参数说明
1. 位置参数(positional arguments)
| 参数 | 英文说明 | 中文说明 |
|---|---|---|
| target | [[domain/]username[:password]@] | 目标服务器信息,格式为[[域名/]用户名[:密码]@]<目标名称或地址>,用于指定连接的目标MSSQL服务器及认证信息 |
2. 基本选项(options)
| 参数 | 英文说明 | 中文说明 |
|---|---|---|
| -h, --help | show this help message and exit | 显示帮助信息并退出 |
| -db DB | MSSQL database instance (default None) | 指定要连接的MSSQL数据库实例(默认无) |
| -windows-auth | whether or not to use Windows Authentication (default False) | 是否使用Windows认证(默认不使用) |
| -debug | Turn DEBUG output ON | 开启调试模式,输出详细的调试信息 |
| -show | show the queries | 显示执行的SQL查询语句 |
| -command [COMMAND ...] | Commands to execute in the SQL shell. Multiple commands can be passed. | 在SQL shell中执行的命令,可传递多个命令 |
| -file FILE | input file with commands to execute in the SQL shell | 包含要在SQL shell中执行的命令的输入文件 |
3. 认证选项(authentication)
| 参数 | 英文说明 | 中文说明 |
|---|---|---|
| -hashes LMHASH:NTHASH | NTLM hashes, format is LMHASH:NTHASH | NTLM哈希值,格式为LMHASH:NTHASH,用于基于哈希的认证 |
| -no-pass | don't ask for password (useful for -k) | 不提示输入密码(与-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 | 使用Kerberos认证。根据目标参数从ccache文件(KRB5CCNAME)获取凭据。如果找不到有效的凭据,将使用命令行中指定的凭据 |
| -aesKey hex key | AES key to use for Kerberos Authentication (128 or 256 bits) | 用于Kerberos认证的AES密钥(128位或256位) |
4. 连接选项(connection)
| 参数 | 英文说明 | 中文说明 |
|---|---|---|
| -dc-ip ip address | IP Address of the domain controller. If ommited it use the domain part (FQDN) specified in the target parameter | 域控制器的IP地址。如果省略,将使用目标参数中指定的域部分(FQDN) |
| -target-ip ip address | 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 | 目标机器的IP地址。如果省略,将使用指定的目标名称。当目标是NetBIOS名称且无法解析时,此参数很有用 |
| -port PORT | target MSSQL port (default 1433) | 目标MSSQL服务器的端口(默认1433) |
三、impacket-mssqlclient工具使用教程
1. 工具准备
在Kali Linux中,impacket-mssqlclient通常随Impacket工具集预装,可直接通过命令mssqlclient.py调用。若未安装,可通过以下命令安装:
sudo apt install impacket-scripts
2. 基本连接操作
场景1:使用SQL Server认证连接数据库
使用用户名和密码通过SQL Server认证连接到目标MSSQL服务器:
mssqlclient.py username:password@192.168.1.100
说明:连接到IP为192.168.1.100的MSSQL服务器,使用指定的用户名和密码进行认证。
场景2:指定端口连接非默认端口的MSSQL服务器
若MSSQL服务器运行在非默认端口(如1434),使用-port参数指定端口:
mssqlclient.py username:password@192.168.1.100 -port 1434
场景3:使用Windows认证连接
通过Windows认证(集成认证)连接到MSSQL服务器:
mssqlclient.py domain/username:password@192.168.1.100 -windows-auth
说明:domain为Windows域名,使用域内用户进行Windows认证。
3. 执行SQL命令
场景1:交互式执行SQL命令
连接成功后进入交互式shell,直接输入SQL命令执行:
mssqlclient.py username:password@192.168.1.100
SQL> SELECT name FROM sys.databases;
SQL> GO
说明:执行查询数据库名称的SQL命令,每条命令后需输入GO执行。
场景2:非交互式执行单条命令
使用-command参数在命令行直接指定要执行的SQL命令:
mssqlclient.py username:password@192.168.1.100 -command "SELECT @@VERSION" -show
说明:-show参数用于显示执行的SQL查询语句。
场景3:执行文件中的多条命令
将多条SQL命令写入文件,通过-file参数执行:
mssqlclient.py username:password@192.168.1.100 -file commands.sql
commands.sql文件内容示例:
SELECT name FROM sys.databases;
SELECT username FROM sys.sysusers;
GO
4. 特殊认证方式
场景1:使用NTLM哈希认证
通过NTLM哈希值进行认证,无需明文密码:
mssqlclient.py username@192.168.1.100 -hashes LMHASH:NTHASH
说明:LMHASH和NTHASH为获取到的用户NTLM哈希值,格式为LMHASH:NTHASH。
场景2:使用Kerberos认证
利用Kerberos协议进行认证,需提前获取有效的ccache凭证:
export KRB5CCNAME=/path/to/credentials.ccache
mssqlclient.py domain/username@192.168.1.100 -k -no-pass
说明:-k参数启用Kerberos认证,-no-pass参数表示不输入密码,从ccache文件获取凭据。
5. 调试与信息查看
场景1:开启调试模式
当连接出现问题时,使用-debug参数查看详细调试信息:
mssqlclient.py username:password@192.168.1.100 -debug
场景2:指定目标IP和域控制器IP
当目标名称无法解析或需要指定域控制器时,使用-target-ip和-dc-ip参数:
mssqlclient.py domain/username:password@targetname -target-ip 192.168.1.100 -dc-ip 192.168.1.10
说明:targetname为目标NetBIOS名称,-target-ip指定其IP地址,-dc-ip指定域控制器IP。
6. 渗透测试常用操作
场景1:启用xp_cmdshell执行系统命令
若数据库用户具有足够权限,可启用xp_cmdshell扩展存储过程执行系统命令:
mssqlclient.py sa:password@192.168.1.100
SQL> EXEC sp_configure 'show advanced options', 1;
SQL> RECONFIGURE;
SQL> EXEC sp_configure 'xp_cmdshell', 1;
SQL> RECONFIGURE;
SQL> xp_cmdshell 'whoami';
SQL> GO
场景2:获取数据库信息
查询数据库版本、用户、表等信息:
mssqlclient.py username:password@192.168.1.100 -command "SELECT @@VERSION; SELECT SYSTEM_USER; SELECT name FROM sys.tables" -show
四、注意事项
- 权限问题:不同用户权限不同,部分操作(如启用xp_cmdshell)需要sysadmin权限
- 防火墙:确保目标MSSQL端口(默认1433)未被防火墙阻止
- 认证方式:根据目标服务器配置选择合适的认证方式(SQL Server认证或Windows认证)
- 法律合规:使用该工具对目标系统进行测试时,需获得合法授权,避免违反法律法规
- 版本兼容性:不同版本的MSSQL服务器可能存在差异,部分命令可能需要调整