May
12

查询某关键词在MSSQL数据库位置的ASP脚本

晚上皇子丢给我一段SQL语句,功能是:以一个关键字为索引,搜索整个数据库,然后返回那个关键字所在的表名和列名。(很赞...特别是入侵的时候找不到用户名与密码所在的表的时候,如果能直接通过输入admin这个关键词找出字段...省得一个表一个表的看了。)于是根据那段语句,写了个asp的脚本,方便大家以后搜寻数据库。

整体而言,这段SQL只适宜用在小型数据库上,ASP中的循环也没效率,大库还是别用了,小库可以考虑。如果是大库的话,请联系皇子要另外一份牛人写的SQL查询语句。

代码如下:

<%

'Confirm a keyword's position of a database(which table & which column)
'By oldjun(http://www.oldjun.com)
'Based on huangzi(http://www.2chuizi.com)'s sql

Server.ScriptTimeout=999999999
Response.Buffer =true
On Error Resume Next
keyword=request("keyword")
if keyword="" then
response.write "Need keyword!"
response.End
End if
dim conn
Set conn = Server.CreateObject("ADODB.Connection")
Dim ConnStr
'ConnectionString,Pls change!
ConnStr="Driver={SQL SERVER};Server=localhost;UID=sa;PWD=sa;Database=master"
Conn.open ConnStr
conn.execute("CREATE TABLE huangzi_table(id int identity(1,1),biaoid int,biaoname nvarchar(1000))")
conn.execute("insert huangzi_table select [id],[name] from sysobjects where xtype='U'")

set rs =conn.execute("select count(id) as tnum from huangzi_table")
tnum=rs("tnum")
rs.close
set rs=nothing
for i=1 to tnum

set rsbiao =conn.execute("select biaoid from huangzi_table where id="&i&"")
biaoid=rsbiao("biaoid")
set rst =conn.execute("select [biaoname] from huangzi_table where biaoid="&biaoid&"")
tname=rst("biaoname")

set rsl=conn.execute("select count([name]) as lnum from syscolumns where id="&biaoid&"")
lnum=rsl("lnum")

for j=1 to lnum

topnum=j-1
set rslie=conn.execute("select top 1 [name] from syscolumns where id="&biaoid&" and [name] not in 
(select top "&topnum&" [name] from syscolumns where id="&biaoid&")")
liename=rslie("name")
set rsresult=conn.execute("select top 1 ["&liename&"] from ["&tname&"] where CAST(["&liename&"] AS NVARCHAR(1000))='"&keyword&"'")

if rsresult.bof or rsresult.eof then
'response.write "Nothing-"&tname&":"&liename
'response.write "<br>"
else
result=rsresult(liename)
response.write result&"("&tname&":"&liename&")"
response.write "<br>"
End if

rslie.close
set rslie=nothing
rsresult.close
set rsresult=nothing
next

rsbiao.close
set rsbiao=nothing
rst.close
set rst=nothing
rsl.close
set rsl=nothing
next

conn.execute("DROP TABLE huangzi_table")
%>

注:效率很差,使用时可能出现假死, 请耐心等待,大库还是别用了;代码简单,实现的简单功能,没技术含量,留着以后备用;换连接语句的时候有个缓存问题,建议重启下浏览器!

共有19条评论

  1. rolin: May,12th,2009

    看不懂
    别再求我做你徒弟了
    o(>﹏

  2. 生存之民工: May,12th,2009

    又来膜拜大牛了 今天碰到一问题 来请教下 sql server 2000数据库
    db_owner权限,无法列目录(应该是数据库和网站分离),管理账号密码搞到了,后台貌似是个假的什么都不能干所以后台就不用想了。至于上传我用工具扫了下没扫到什么漏洞,服务器在内网,网站是asp加aspx的语言写的。。大致就这些
    还有其他解决方法吗?谢谢

  3. oldjun: May,12th,2009

    什么叫无法列目录,试过手工没?无法列目录怎么得出数据库与网站分离?后台为什么是假的?asp或aspx除了备份,上传,写配置文件,貌似没别的好办法了,一般拿shell都是围绕这几点着手的。另外可以继续搜寻有用的路径与页面!

  4. 生存之民工: May,13th,2009

    用啊D列目录列不出来,手工没试。后台随便填写一个用户名密码就进去了。只能发布公告,编辑文章页面点开是错误的页面。所以怀疑后台是假的。还有一点我一直认为无法列目录就是因为数据库和后台分离了,难道不是这个原因,貌似在非安全上有提过,忘记了。。。

  5. 生存之民工: May,13th,2009

    忘记说了,注入点是支持多行执行的

  6. 雨中风铃: May,13th,2009

    只能查每个表的第一行记录

  7. oldjun: May,13th,2009

    to 生存之民工:能不能列目录与web库是否分离没直接联系,一般而言DB权限下基本是可以列目录的,哪怕是分离的,也能列出数据库服务器的目录;为什么无法列目录就说分离?另外怎么能这么依靠工具,即使权限被限制了列不出来,也要看看哪一步过不去;

    to 雨中风铃:寒,看清楚介绍,此语句是查询某关键词存在于此数据库的哪个表哪个列,所以用top 1 ,意思是只要存在就返回查询到了。

  8. 生存之民工: May,13th,2009

    谢谢。我手工试试!

  9. 生存之民工: May,13th,2009

    终于知道为什么不能列目录呢。工具误报,啊D显示是db_owner权限,我手工猜解竟然是public。。还是谢谢呢

  10. 雨中风铃: May,13th,2009

    不好意思,CAST(["&liename&"] AS NVARCHAR(1000))='"&keyword&"'")这句没仔细看,早上看成["&liename&"]='"&keyword&"'" 了

  11. bloodsword: May,18th,2009

    膜拜下大黑客,啊D只要不是sa他就报dbowner
    public下也有办法列目录的,而且成功率很高

  12. oldjun: May,21st,2009

    BS大牛就是厉害~~~赞~~~

  13. XSSER: May,22nd,2009

    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_NULLS ON
    GO

    ALTER PROCEDURE searchname @sname varchar(10) As

    BEGIN
    CREATE TABLE #TableList(
    tablename char(200),
    colname char(200),
    content char(200)
    )
    DECLARE @table varchar(200)
    DECLARE @col varchar(200)

    SET nocount on

    DECLARE curTab scroll cursor for SELECT name FROM sysobjects WHERE xtype='u'

    OPEN curTab

    FETCH NEXT FROM curTab INTO @table

    WHILE @@FETCH_STATUS=0
    BEGIN
    DECLARE curCol SCROLL CURSOR FOR SELECT name FROM syscolumns WHERE (xtype=175 or xtype=167 or xtype=231) and (id in (SELECT id FROM sysobjects WHERE name=@table))

    OPEN curCol

    FETCH NEXT FROM curCol INTO @col

    WHILE @@FETCH_STATUS=0
    BEGIN
    EXECUTE('insert into #TableList select '''+@table+''','''+@col+''','+@col+' from '+@table+' where '+@col+' LIKE '''+@sname+'''')
    FETCH NEXT FROM curCol INTO @col
    END

    CLOSE curCol

    DEALLOCATE curCol

    FETCH NEXT FROM curTab INTO @table

    END

    CLOSE curTab

    DEALLOCATE curTab

    SET NOCOUNT OFF

    SELECT DISTINCT tablename as 表名,COLNAME AS 字段名 ,content as 内容 from #TableList

    DROP TABLE #tablelist

    END

    GO
    SET QUOTED_IDENTIFIER OFF
    GO
    SET ANSI_NULLS ON
    GO

  14. 行情: May,23rd,2009

    人气好旺啊 我也来了

  15. oldjun: May,24th,2009

    to XSSER牛:当时皇子的SQL存储过程跟你这个思路差不多~~~

  16. VcOr: May,25th,2009

    这种只能对mssql有效是吧
    这个就是查看系统表的是否存在这个数据库。
    这个权限至少要是db_owner。
    不过感觉你这个思路不错。之前虽然知道,但是没想过要写出这种可以方便查询的。但是一直都是利用手动列

  17. tttest: Jul,15th,2009

    收藏个

  18. oer: Nov,4th,2009

    public 权限怎么列目录? 代码那么长怎么提交

  19. oldjun: Nov,5th,2009

    临时表...db怎么提交,就怎么提交!

NAME:

required

E-MAIL:

required, will not be published

HOMEPAGE:

CONTENT: