Skip to content

Instantly share code, notes, and snippets.

@secdev02
Created December 17, 2025 18:35
Show Gist options
  • Select an option

  • Save secdev02/0c4de0312791b4921095aa0885ef1164 to your computer and use it in GitHub Desktop.

Select an option

Save secdev02/0c4de0312791b4921095aa0885ef1164 to your computer and use it in GitHub Desktop.
Capability Diffusion - Part Two

A single file that has 2 different ways of behaving

IN this case we simply load and compile 2 difference C# calls.

Use your imagination.

image
using System;
using System.CodeDom.Compiler;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.CSharp;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("HelloWorld Dynamic Compiler");
Console.WriteLine("===========================");
// Check for --Hyde parameter
bool hydeMode = args.Any(a => a.Equals("--Hyde", StringComparison.OrdinalIgnoreCase));
if (hydeMode)
{
Console.WriteLine("Hyde mode activated - Loading MessageBox code...");
string base64Code = ConfigurationManager.AppSettings["HydeCode"];
if (string.IsNullOrEmpty(base64Code))
{
Console.WriteLine("Error: HydeCode not found in app.config");
return;
}
// Decode base64
byte[] data = Convert.FromBase64String(base64Code);
string sourceCode = Encoding.UTF8.GetString(data);
Console.WriteLine("Compiling and executing code...");
CompileAndExecute(sourceCode, true);
}
else
{
Console.WriteLine("Default mode - Starting cmd process...");
string processCode = ConfigurationManager.AppSettings["DefaultCode"];
if (string.IsNullOrEmpty(processCode))
{
Console.WriteLine("Error: DefaultCode not found in app.config");
return;
}
// Decode base64
byte[] data = Convert.FromBase64String(processCode);
string sourceCode = Encoding.UTF8.GetString(data);
Console.WriteLine("Compiling and executing code...");
CompileAndExecute(sourceCode, false);
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
static void CompileAndExecute(string sourceCode, bool needsWinForms)
{
// Setup the C# compiler
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
// Add necessary references
parameters.ReferencedAssemblies.Add("System.dll");
if (needsWinForms)
{
parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
}
parameters.GenerateInMemory = true;
parameters.GenerateExecutable = false;
// Wrap the code in a class and method
string fullCode = string.Format(@"
using System;
using System.Diagnostics;
{0}
namespace DynamicCode
{{
public class Runner
{{
public static void Execute()
{{
{1}
}}
}}
}}", needsWinForms ? "using System.Windows.Forms;" : "", sourceCode);
Console.WriteLine(string.Concat("Source code: ", Environment.NewLine, sourceCode));
// Compile the code
CompilerResults results = provider.CompileAssemblyFromSource(parameters, fullCode);
if (results.Errors.HasErrors)
{
Console.WriteLine("Compilation errors:");
foreach (CompilerError error in results.Errors)
{
Console.WriteLine(string.Concat(" ", error.ErrorText));
}
return;
}
Console.WriteLine("Compilation successful!");
// Execute the compiled code
try
{
Assembly assembly = results.CompiledAssembly;
Type type = assembly.GetType("DynamicCode.Runner");
MethodInfo method = type.GetMethod("Execute");
method.Invoke(null, null);
Console.WriteLine("Execution completed!");
}
catch (Exception ex)
{
Console.WriteLine(string.Concat("Execution error: ", ex.Message));
if (ex.InnerException != null)
{
Console.WriteLine(string.Concat("Inner exception: ", ex.InnerException.Message));
}
}
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- Base64 encoded: MessageBox.Show("KaBoom Beacon Launched!"); -->
<add key="HydeCode" value="TWVzc2FnZUJveC5TaG93KCJLYUJvb20gQmVhY29uIExhdW5jaGVkISIpOw==" />
<!-- Base64 encoded: Process.Start("cmd.exe"); -->
<add key="DefaultCode" value="UHJvY2Vzcy5TdGFydCgiY21kLmV4ZSIpOw==" />
</appSettings>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment