Robert
Robert

Reputation: 527

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!

Upvotes: 1

Views: 2527

Answers (1)

GuidedHacking
GuidedHacking

Reputation: 3923

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.

Upvotes: 1

Related Questions