using System; using System.Diagnostics; using System.IO; using System.Runtime.Serialization; using System.Web; using System.Web.UI; namespace LabAntiCsrf { /* * INTENTIONALLY VULNERABLE TRAINING CODE — CVE-2026-131914 pattern. * * This module models the unsafe Idunno.AntiCsrf request path for an * isolated lab. Do not copy it into an application or publish this image. */ public sealed class LabAntiCsrfModule : IHttpModule { internal const string CookieName = "__CSRFCOOKIE"; internal const string FormFieldName = "__CSRFTOKEN"; public void Init(HttpApplication application) { application.PreRequestHandlerExecute += OnPreRequestHandlerExecute; application.PreRequestHandlerExecute += AttachPageToken; } private static void OnPreRequestHandlerExecute(object sender, EventArgs args) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; Page page = context.CurrentHandler as Page; if (page == null || !String.Equals(context.Request.HttpMethod, "POST", StringComparison.OrdinalIgnoreCase)) { return; } string cookieValue = ReadCookie(context); string serializedToken = context.Request.Form[FormFieldName]; if (String.IsNullOrEmpty(cookieValue) || String.IsNullOrEmpty(serializedToken)) { RejectRequest(); return; } // Deliberately mirrors the vulnerable implementation: an unkeyed, // unsigned ObjectStateFormatter deserializes attacker-controlled input. object tokenObject = new ObjectStateFormatter().Deserialize(serializedToken); string tokenValue = tokenObject as string; if (!String.Equals(cookieValue, tokenValue, StringComparison.Ordinal)) { RejectRequest(); } } private static void AttachPageToken(object sender, EventArgs args) { HttpApplication application = (HttpApplication)sender; Page page = application.Context.CurrentHandler as Page; if (page != null) { page.PreRender += OnPagePreRender; } } private static void OnPagePreRender(object sender, EventArgs args) { Page page = (Page)sender; if (page.Form == null) { return; } HttpContext context = HttpContext.Current; string token = ReadCookie(context); if (String.IsNullOrEmpty(token)) { token = Guid.NewGuid().ToString("D"); context.Response.Cookies.Add(new HttpCookie(CookieName, token) { HttpOnly = true }); } // This output is compatible with the vulnerable POST path above. string serializedToken = new ObjectStateFormatter().Serialize(token); page.ClientScript.RegisterHiddenField(FormFieldName, serializedToken); } private static string ReadCookie(HttpContext context) { HttpCookie cookie = context.Request.Cookies[CookieName]; return cookie == null ? null : cookie.Value; } private static void RejectRequest() { throw new HttpException(400, "LabAntiCsrf rejected the request."); } public void Dispose() { } } [Serializable] public sealed class DeserializationProof : ISerializable { /* /tmp under Mono/XSP, %TEMP% under IIS: no file surface in the site. */ public static readonly string MarkerPath = Path.Combine(Path.GetTempPath(), "anticsrf-rce-proof.txt"); public DeserializationProof(string proofId) { if (!Guid.TryParse(proofId, out _)) { throw new ArgumentException("Invalid proof ID.", "proofId"); } ProofId = proofId; } private DeserializationProof(SerializationInfo info, StreamingContext context) { string proofId = info.GetString("proofId"); if (!Guid.TryParse(proofId, out _)) { throw new SerializationException("Invalid proof ID."); } ProofId = proofId; File.WriteAllText(MarkerPath, proofId + "\n"); } public string ProofId { get; private set; } public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("proofId", ProofId); } } /* * Command-capable proof type for the lab. * * Mono's BinaryFormatter binds delegates through the public, validating * Delegate.CreateDelegate, so the classic signature-confusion gadget * (Comparison -> Process.Start) cannot bind on this runtime. * This type gives the same pre-auth deserialization path a direct process * sink, so a payload proves real command execution rather than object * creation alone. It is reachable only through the vulnerable module. */ [Serializable] public sealed class CommandProof : ISerializable { private CommandProof(SerializationInfo info, StreamingContext context) { bool windows = Path.DirectorySeparatorChar == '\\'; string command = info.GetString("cmd"); if (String.IsNullOrEmpty(command)) { command = windows ? "whoami" : "id"; } /* * Windows passes the argument tail through to cmd.exe intact, but * Mono splits ProcessStartInfo.Arguments on whitespace, which would * truncate "/bin/sh -c " at the first space. Hand the * command to the child through the environment instead and let the * shell expand a single variable. */ ProcessStartInfo startInfo; string scriptPath = null; if (windows) { startInfo = new ProcessStartInfo("cmd.exe", "/c " + command); } else { /* Mono splits Arguments on whitespace, so "-c " would be truncated at the first space. Run the command from a script file: one argument, full shell syntax preserved. */ scriptPath = Path.Combine( Path.GetTempPath(), "cs-cmd-" + Guid.NewGuid().ToString("N") + ".sh"); File.WriteAllText(scriptPath, command); startInfo = new ProcessStartInfo("/bin/sh", scriptPath); } startInfo.UseShellExecute = false; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; string output; using (Process process = Process.Start(startInfo)) { output = process.StandardOutput.ReadToEnd(); output += process.StandardError.ReadToEnd(); process.WaitForExit(15000); } if (scriptPath != null) { try { File.Delete(scriptPath); } catch (Exception) { } } File.WriteAllText(DeserializationProof.MarkerPath, output); } public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("cmd", String.Empty); } } }