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.

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 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

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!

时间: 2024-12-08 17:25:59

C# Self Injector into non managed process的相关文章

Node.js进程管理器PM2浅析

作者:zhanhailiang 日期:2014-11-02 PM2是Node.js应用程序的进程管理管理,目前已在生产环境被普遍使用,提供以下特性: Transitional state of apps Process listing Automatic restart process based on memory Monitoring CPU/Memory usage Logs management Clustering Watch & Restart Reloading without d

WCF学习系列一【WCF Interview Questions-Part 1 翻译系列】

http://www.topwcftutorials.net/2012/08/wcf-faqs-part1.html WCF Interview Questions – Part 1 This WCF Tutorial is a collection of most frequently asked interview questions about Windows Communication Foundation (WCF) covering the beginner to professio

Advanced .NET Debugging: Managed Heap and Garbage Collection(转载,托管堆查内存碎片问题解决思路)

原文地址:http://www.informit.com/articles/article.aspx?p=1409801&seqNum=4 Debugging Managed Heap Fragmentation Earlier in the chapter, we described a phenomenon known as heap fragmentation, in which free and busy blocks are arranged and interleaved on th

Windows Dll Injection、Process Injection、API Hook

catalogue 1. 引言 2. 使用注册表注入DLL 3. 使用Windows挂钩来注入DLL 4. 使用远程线程来注入DLL 5. 使用木马DLL来注入DLL 6. 把DLL作为调试器来注入 7. 使用createprocess来注入代码 8. api拦截 9. Detours - Inline Hook 1.  引言 应用程序需要跨越进程边界来访问另一个进程的地址空间的情况如下 1. 我们想要从另一个进程创建的窗口派生子类窗口 2. 我们需要一些手段来辅助调试,例如我们需要确定另一个进

怎么监视跟踪一个进程(Process)中的dll的详细性能(performance)【asp.net C#】

This tutorial will show how to instrument a unit test DLL for performance profiling. Visual Studio will allow you to do performance profiling on individual tests (i.e. functions) in a test suite. This can be done in the user interface (i.e. UI or IDE

How to do Physical inventory process in Warehouse SAP仓库盘点流程

Please go with following transaction MI01>Create physical inventory document>Enter Document Date>Planned count date>Plant Code>Storage Location>Click on Enter button>you will get the separate screen>Enter material code>and click

Information centric network (icn) node based on switch and network process using the node

The present invention relates to an apparatus for supporting information centric networking. An information centric network (ICN) node based on a switch according to the present invention includes an ICN process configured to request information for

Optimize Managed Code For Multi-Core Machines

Parallel Performance Optimize Managed Code For Multi-Core Machines Daan Leijen and Judd Hall This article discusses: Task Parallel Library Parallel.For versus ThreadPool Static Work Distribution Futures This article uses the following technologies: P

Project Management Process

Project Management ProcessDescription .......................................................................................................................................................................................1STAGE/STEP/TASK SUMMARY LIST