首页 > 代码库 > C# Self Injector into non managed process
C# Self Injector into non managed process
Hey all,
I‘m gonna explain you how make a self injecting program in C#.
I hope you guys thinks its usefull and have a nice reading
Requirements:
Visual Studio 20xx (I use Visual Studio 2010)
VInj (A nice library to inject managed dll‘s, its can be downloaded at the bottom.)
Simple Knowlege of C#
Getting Started
First we have to create a simple C# console project, in my case i call it SelfInjector.
Make sure the project framework is set to .NET Framework 2.0, else you‘ll get an error.
Then we have to copy the 2 DLL files from the VInj.rar into the solution and set the "Copy to Ouput" to Copy if Newer or Always.
Now that we have done this we add the VInjDn.dll as a reference to our project.
If everything goes as planned you‘ll get a project like this
Setting up the injector
Now were gonna start programming the injector.
What were gonna do is getting the process by name and then Inject our program into the target process with VInj.
First we define a string for the name of the target process. i use BlackOps as a example.
This part will be in the Main method.
string targetProcess = "BlackOps";
now we are going to get the process by name. which will be right under it.
Process remote_process = Process.GetProcessesByName(targetProcess)[0];
Ok, now we are getting to the part where we are going to inject our program into the process
this is done using the InjectableProcess from the VInj library.
The inject method returns a result which we can use if our program has been successfully injected.
InjectableProcess ip = InjectableProcess.Create(remote_process.Handle);int result = ip.Inject(Application.ExecutablePath, Path.GetFileNameWithoutExtension(Application.ExecutablePath) + ".Main");if (result == 0){ Console.WriteLine("Failed to inject."); Console.ReadKey(); return;}
We return directly after the failure message because it doesnt make any sense to go on :P
And this is it for the injector, after the IF you can also make something so you know if it is successfully injected or something.
Here a screenshot of how your code should look like.
Making the EntryPoint for the injection.
Now were gonna make the entrypoint, from here you can do whatever you like to do.
First we make a new class file and name it Main (including the Capital)
Now we remove the constructor because we dont need any, the entrypoint is called as an other method.
Ok i just fast forward this part because its small and i will just post a bare bone template for the Main.cs
public class Main : VInjDn.IInjectable{ public int OnCommand(VInjDn.LiquidCommand command) { return 1; } public int onl oad() { Thread t = new Thread(EntryThread); t.Start(); return 1; } public int OnUnload() { return 1; } private void EntryThread() { MessageBox.Show("Injected!"); }}
As you can see there are 4 methods in the Main.cs
The OnCommand can be used with the IPC of vinj to receive command given by the Program.cs through VInj.
The onl oad is where the real entrypoint is, here we create a new thread so the game wont freeze when we inject our program.
The OnUnload, well do i really have to explain this?
The EntryThread is the method thats called my the thread thats created in the onl oad, here you can do all your work while the game is running
I just show a messagebox so you can see that the program is injected.
Well thats all for now!
The full project can be downloaded, link is at the bottom of this post, also some credits to the guys who made VInj, i dont really know who made it but those persons will know :P
I hope you enjoyed this tutorial and maybe more are coming!
Tutorial 2: Changing values without Read/WriteMemory
Tutorial 3: Hooking functions with EasyHook
Tutorial 4: Direct3D9 Hook with EasyHook and SlimDX!
C# Self Injector into non managed process