Skip to content

Instantly share code, notes, and snippets.

@vivami
Last active April 18, 2019 04:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vivami/54bca3ce83ef14d9bdae549691e24e91 to your computer and use it in GitHub Desktop.
Save vivami/54bca3ce83ef14d9bdae549691e24e91 to your computer and use it in GitHub Desktop.
Load Assembly and dynamically create an instance and invoke Main method
public static string srcTemplate = @"using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
namespace Loader {
public static class Loader {
private static readonly byte[] SALT = new byte[] { 0xba, 0xdc, 0x0f, 0xfe, 0xeb, 0xad, 0xbe, 0xfd, 0xea, 0xdb, 0xab, 0xef, 0xac, 0xe8, 0xac, 0xdc };
public static void Main(string[] args) {
byte[] bytes = decrypt(Convert.FromBase64String(Package.dotnetfile), Package.key);
Assembly a = Assembly.Load(bytes);
foreach (Type type in a.GetTypes()) {
try {
object instance = Activator.CreateInstance(type);
object[] procargs = new object[] { args };
var methodInfo = type.GetMethod(""Main"", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
var result = methodInfo.Invoke(instance, procargs);
}
catch (Exception e) { }
}
}
public static byte[] decrypt(byte[] cipher, string key) { // Left out }
public class Package {
public static string dotnetfile = @""INSERTHERE"";
public static string key = @""KEY"";
}
}";
String obfuscatedBin = obfuscateBinary(path);
String tmpStr = srcTemplate.Replace("INSERTHERE", obfuscatedBin);
String srcFinal = tmpStr.Replace("KEY", key);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment