C # self injector into Non managed process

Source: Internet
Author: User
Tags visual studio 2010

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.

Code:
string targetProcess = "BlackOps";

Now we are going to get the process by name. Which will be right under it.

Code:
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.

Code:
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 shoshould 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

Code:
public class Main : VInjDn.IInjectable{    public int OnCommand(VInjDn.LiquidCommand command)    {        return 1;    }    public int OnLoad()    {        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 onload 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 onload, 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.