Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
1,218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Security;
  4.  
  5. namespace ConsoleApp138
  6. {
  7.     class Program
  8.     {
  9.         [DllImport("kernel32.dll" )]
  10.         private static extern IntPtr LoadLibrary( IntPtr str);
  11.         [DllImport("kernel32.dll")]
  12.         private static extern IntPtr GetProcAddress(IntPtr lib, IntPtr str);
  13.  
  14.         /// <summary>
  15.         /// Declare our unknown function (in this case messagebox)
  16.         /// </summary>
  17.         /// <returns></returns>
  18.         delegate int UnknownFunction(int a, string x, string y, int z);
  19.  
  20.  
  21.         /// <summary>
  22.         /// Invented and created by gigajew @ www.hackforums.net
  23.         /// Afaik first of it's kind
  24.         /// </summary>
  25.         static void Main(string[] args)
  26.         {
  27.              // call user32!MessageBoxA
  28.             Example1();
  29.  
  30.             // call user32!MessageBoxA
  31.             Example2();
  32.         }
  33.  
  34.         static void Example2()
  35.         {
  36.             SecureString ker = new SecureString();
  37.             ker.AppendChar('U');
  38.             ker.AppendChar('s');
  39.             ker.AppendChar('e');
  40.             ker.AppendChar('r');
  41.             ker.AppendChar('3');
  42.             ker.AppendChar('2');
  43.             ker.AppendChar('.');
  44.             ker.AppendChar('d');
  45.             ker.AppendChar('l');
  46.             ker.AppendChar('l');
  47.             SecureString mes = new SecureString();
  48.             mes.AppendChar('M');
  49.             mes.AppendChar('e');
  50.             mes.AppendChar('s');
  51.             mes.AppendChar('s');
  52.             mes.AppendChar('a');
  53.             mes.AppendChar('g');
  54.             mes.AppendChar('e');
  55.             mes.AppendChar('B');
  56.             mes.AppendChar('o');
  57.             mes.AppendChar('x');
  58.             mes.AppendChar('A');
  59.             UnknownFunction unknownFunction1 = GetProcedureSecure<UnknownFunction>(ker, mes);
  60.             unknownFunction1(0, "Hello world 2 ", "Title 2", 2);
  61.  
  62.         }
  63.  
  64.         static void Example1()
  65.         {
  66.             SecureString ker = new SecureString();
  67.             ker.AppendChar('U');
  68.             ker.AppendChar('s');
  69.             ker.AppendChar('e');
  70.             ker.AppendChar('r');
  71.             ker.AppendChar('3');
  72.             ker.AppendChar('2');
  73.             ker.AppendChar('.');
  74.             ker.AppendChar('d');
  75.             ker.AppendChar('l');
  76.             ker.AppendChar('l');
  77.             IntPtr lib = LoadLibrarySecure(ker);
  78.  
  79.             SecureString mes = new SecureString();
  80.             mes.AppendChar('M');
  81.             mes.AppendChar('e');
  82.             mes.AppendChar('s');
  83.             mes.AppendChar('s');
  84.             mes.AppendChar('a');
  85.             mes.AppendChar('g');
  86.             mes.AppendChar('e');
  87.             mes.AppendChar('B');
  88.             mes.AppendChar('o');
  89.             mes.AppendChar('x');
  90.             mes.AppendChar('A');
  91.             IntPtr proc = GetProcAddressSecure(lib, mes);
  92.  
  93.             UnknownFunction unknownFunction1 = Marshal.GetDelegateForFunctionPointer(proc, typeof(UnknownFunction)) as UnknownFunction;
  94.             unknownFunction1(0, "Hello world", "Title", 0);
  95.         }
  96.  
  97.         static T GetProcedureSecure<T>(SecureString library, SecureString procedure) where T: class
  98.         {
  99.             IntPtr lib = LoadLibrarySecure(library);
  100.             IntPtr proc = GetProcAddressSecure(lib, procedure);
  101.             T del = Marshal.GetDelegateForFunctionPointer(proc, typeof(T)) as T;
  102.             return del;
  103.         }
  104.  
  105.         static IntPtr LoadLibrarySecure(SecureString secure )
  106.         {
  107.             IntPtr ptr = Marshal.SecureStringToGlobalAllocAnsi(secure);
  108.             IntPtr lib =  LoadLibrary(ptr);
  109.             if(Equals(lib, IntPtr.Zero) )
  110.             {
  111.                 throw new Exception("Couldn't load library");
  112.             }
  113.             Marshal.ZeroFreeGlobalAllocAnsi(ptr);
  114.             return lib;
  115.         }
  116.  
  117.         static IntPtr GetProcAddressSecure(IntPtr lib, SecureString secure )
  118.         {
  119.             IntPtr ptr = Marshal.SecureStringToGlobalAllocAnsi(secure);
  120.             IntPtr func = GetProcAddress(lib, ptr);
  121.             if(Equals(func, IntPtr.Zero))
  122.             {
  123.                 throw new Exception("Couldn't find procedure");
  124.             }
  125.             Marshal.ZeroFreeGlobalAllocAnsi(ptr);
  126.             return func;
  127.  
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement