Video Tutorial C# DLL Injector Tutorial - How to Inject a DLL

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

Rake

Cesspool Admin
Administrator
Jan 21, 2014
11,529
78,998
2,309
Game Name
assault cube
Anticheat
None
How long you been coding/hacking?
5 Years
Coding Language
C#
What you need
Visual Studio Community 2019
C# DLL Injector Tutorial - How to Inject a DLL

In this How to Inject a DLL C# tutorial we will teach you how to convert our previous C# Trainer into a DLL injector. This is a beginner tutorial.

In the previous video we learned how to use platform invoke or pinvoke to expose native Windows API functions to our managed process, essentially we are using signatures to get the address of exported functions. We will continue that process with a couple new functions so we can make a simple CreateRemoteThread + LoadLibrary DLL injector.

I will show you different native and managed methods of doing a couple things, just to give you some more experience with C#. For example, OpenProcess() and CloseHandle() are native functions that are not necessary, these are taken care of you automatically with the managed methods.

There were 2 bugs in the video, so either watch until the end to see the fixes or just download the completed source code. You must run as administrator and you must compile for the same bitness as the target process, x86 vs x64.




You may also be interested in our C++ DLL Injector tutorial

Links from video

Want to learn more than the basics of C# DLL Injection?

Sample of code from C# DLL injector tutorial:
C#:
public static bool InjectDLL(string dllpath, string procname)
{
    Process[] procs = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(procname));

    if (procs.Length == 0)
    {
        return false;
    }

    Process proc = procs[0];

    //redundant native method example - GetProcessesByName will automatically open a handle
    int procid = GetProcId(procname);
    IntPtr hProc = OpenProcess(ProcessAccessFlags.All, false, proc.Id);
    //

    if (proc.Handle != IntPtr.Zero)
    {
        //proc.Handle = managed
        IntPtr loc = VirtualAllocEx(proc.Handle, IntPtr.Zero, MAX_PATH, AllocationType.Commit | AllocationType.Reserve,
            MemoryProtection.ReadWrite);

        if (loc.Equals(0))
        {
            return false;
        }

        IntPtr bytesRead = IntPtr.Zero;

        bool result = WriteProcessMemory(proc.Handle, loc, dllpath.ToCharArray(), dllpath.Length, out bytesRead);

        if (!result || bytesRead.Equals(0))
        {
            return false;
        }

        IntPtr loadlibAddy = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
        //redundant native method example - MUST BE CASE SENSITIVE CORRECT
        loadlibAddy = GetProcAddress(GetModuleBaseAddress(proc.Id, "KERNEL32.DLL"), "LoadLibraryA");

        IntPtr hThread = CreateRemoteThread(proc.Handle, IntPtr.Zero, 0, loadlibAddy, loc, 0, out _);

      
        if (!hThread.Equals(0))
            //native method example
            CloseHandle(hThread);
        else return false;
    }
    else return false;

    //this will CloseHandle automatically using the managed method
    proc.Dispose();
    return true;
}
 

Attachments

Last edited:
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods League of Legends Accounts