简体   繁体   中英

C# code to inject 64bit dll to process

I have a code to inject 32bit library(C++) to foreign 32bit process:

[DllImport("kernel32")]
        public static extern IntPtr CreateRemoteThread(
          IntPtr hProcess,
          IntPtr lpThreadAttributes,
          uint dwStackSize,
          UIntPtr lpStartAddress, // raw Pointer into remote process  
          IntPtr lpParameter,
          uint dwCreationFlags,
          out IntPtr lpThreadId
        );

        ...

        public static bool InjectDLL(Process p, string dll)
        {
            IntPtr bytesout;
            Int32 LenWrite = dll.Length + 1;
            IntPtr AllocMem = (IntPtr)VirtualAllocEx(p.Handle, (IntPtr)null, (uint)LenWrite, 0x1000, 0x40);
            WriteProcessMemory(p.Handle, AllocMem, dll, (UIntPtr)LenWrite, out bytesout);
            UIntPtr Injector = (UIntPtr)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
            IntPtr hThread = (IntPtr)CreateRemoteThread(p.Handle, (IntPtr)null, 0, Injector, AllocMem, 0, out bytesout);
            return true;
        }

But how to fix that code to inject 64 bit libraries to 64bit processes? Code above doesn't work to 64bit processes and dlls.

Thanks!

Your injector , your target process and the DLL must all be x64.

The reason is because of this line:

GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");

That will return the address of the x86 LoadLibrary() not the x64 address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM