winapi - C# Call win api in external process -


i'm trying call win api function in external process.

basic rundown:

  • get base address of user32.dll putty.exe (or 32 bit proc)
  • get base address of messageboxa user32.dll
  • populate messageboxa struct data, , allocate data locally.
  • allocate struct in putty
  • write struct putty.
  • createremotethread execute messageboxa.

all winapi calls succeed, , no error thrown, nor putty process crash. not show messagebox.

any appreciated.
thanks!

using system; using system.collections.generic; using system.diagnostics; using system.linq; using system.runtime.interopservices; using system.text;  namespace remote_api_call {     class program     {         #region api         [dllimport("kernel32.dll", charset = charset.auto)]         public static extern intptr getmodulehandle(string lpmodulename);          [dllimport("kernel32", charset = charset.ansi, exactspelling = true, setlasterror = true)]         static extern intptr getprocaddress(intptr hmodule, string procname);          [dllimport("kernel32.dll", setlasterror = true, exactspelling = true)]         static extern intptr virtualallocex(intptr hprocess, intptr lpaddress,             uint dwsize, uint flallocationtype, uint flprotect);          [dllimport("kernel32.dll", setlasterror = true)]         static extern bool writeprocessmemory(intptr hprocess, intptr lpbaseaddress, byte[] lpbuffer, uint nsize, out uintptr lpnumberofbyteswritten);          [dllimport("kernel32.dll")]         static extern intptr createremotethread(intptr hprocess,             intptr lpthreadattributes, uint dwstacksize, intptr lpstartaddress, intptr lpparameter, uint dwcreationflags, intptr lpthreadid);         #endregion         [dllimport("kernel32.dll", setlasterror = true)]         static extern bool writeprocessmemory(intptr hprocess, intptr lpbaseaddress, intptr lpbuffer, uint nsize, out uintptr lpnumberofbyteswritten);         // privileges         const int process_create_thread = 0x0002;         const int process_query_information = 0x0400;         const int process_vm_operation = 0x0008;         const int process_vm_write = 0x0020;         const int process_vm_read = 0x0010;          // used memory allocation         const uint mem_commit = 0x00001000;         const uint mem_reserve = 0x00002000;         const uint page_readwrite = 4;           [structlayout(layoutkind.sequential)]         struct messageboxa         {             public intptr hwnd;             [marshalas(unmanagedtype.lpstr)]             public string lptext;             [marshalas(unmanagedtype.lpstr)]             public string lpcaption;             [marshalas(unmanagedtype.u4)]             public uint type;         }          [dllimport("kernel32.dll")]         public static extern intptr openprocess(int dwdesiredaccess, bool binherithandle, int dwprocessid);         static void main(string[] args)         {             process putty = process.getprocessesbyname("putty")[0];              intptr user32calc = moduleaddr(calc, "user32.dll");             console.writeline("user32.dll addr putty.exe: " + user32calc.toint32());              intptr messageboxa = getprocaddress(user32calc, "messageboxa");             console.writeline("messageboxa addr putty.exe: " + messageboxa.toint32());              intptr prochandle = openprocess(process_create_thread | process_query_information | process_vm_operation | process_vm_write | process_vm_read, false, putty.id);               messageboxa rtp = new messageboxa();             rtp.hwnd = intptr.zero;             rtp.lptext = "hey mate!";             rtp.lpcaption = "caption";             rtp.type = 16;             uintptr byteswritten;              // allocate mem locally             intptr iptrtoparams = marshal.allochglobal(marshal.sizeof(rtp));              // copy data structure             marshal.structuretoptr(rtp, iptrtoparams, false);              // allocate mem in other process params             intptr allocmemaddress = virtualallocex(prochandle, intptr.zero, (uint)marshal.sizeof(rtp), mem_commit | mem_reserve, page_readwrite);             if (allocmemaddress != intptr.zero)                 console.writeline("allocmem success");             if (writeprocessmemory(prochandle, allocmemaddress, iptrtoparams, (uint)marshal.sizeof(rtp), out byteswritten))                 console.writeline("wpm success");             //   marshal.freehglobal(iptrtoparams);             if (createremotethread(prochandle, intptr.zero, 0, messageboxa, allocmemaddress, 0, intptr.zero) != intptr.zero)                 console.writeline("createremotethread success");               console.read();         }          private static intptr moduleaddr(process p, string modulename)         {             foreach (processmodule pmod in p.modules)             {                 if (pmod.modulename.tolower() == modulename.tolower())                     return pmod.baseaddress;             }             return intptr.zero;         }      } } 


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -