tags:

views:

465

answers:

1

Hello I'm in trouble injecting a process with C# code. I'm using CreateRemoteThread method by Mike Stall. However I get Last Win32 Error 1008 when trying to inject process.

(btw. I tried injecting DLL (which runs my code) but I didn't succeed. I would post links to codes, but I can't)

+2  A: 

This is by no means a simple task, and involves more than a bit of Win32 API hackery.

I did in fact attempt exactly this some time ago and it took a lot of messing around with API calls/argument marshalling to get it working - though I eventually made it quite stable. You can find the code I wrote in the repository for my Windows SSH Server project (specifically the ConsoleDotNet Visual Studio project). The code for the native C++ DLL should also be included in full.

I believe you should be able to reuse much of that code verbatim (or with only small modifications). It should also be reasonably well commented, if I remember rightly. Still, let me know if you have any trouble understanding it. Good luck!

Noldorin
Thanks, but I'm unable to find it.
mnn
Find what? Just download the code and open the Visual Studio solution.
Noldorin
https://code.launchpad.net/~noldorin/windows-ssh-server/devel
Noldorin
Well, thanks but that's not what I was looking for.
mnn
What are you looking for, then? My code does exactly what you've specified in the question (although it may not be in the form you want as the end result).
Noldorin
So it's not what I'm looking for. I want to load/inject assembly in native process and run some method (even static if it has to be).
mnn
@mnn: That's exactly my code does (and a lot more - you can ignure the shared-memory stuff). Are you having trouble understanding a certain part? I recommend you just browse through the code a bit and find out what part is appropiate for you (the InjectDll method is a good place to start).
Noldorin
Well I've found it. But I need to inject .NET assembly, or code. Your method is similar to Mike Stall's solution, but it doesn't work even for C DLL
mnn
@mnn: It should work fine for a C DLL, provided you specify the "main" function correctly, I believe. It is simply *not possible* to inject a .NET assembly into a running process, for various reasons (basically, managed and native code don't mix well!). Sorry if I misunderstood your original question...
Noldorin