Invoke/Call Functions Externally
So it seems like text tutorials are all the rage as of lately so I decided to whip one up myself.I hope to explain, as thoroughly as I possibly can, all the techniques that I know of to invoke a function in a remote process. It’s really just two; Use hooking techniques to route execution to some sort of injected code, and then a bunch of different ways of using CreateRemoteThread. Nothing groundbreaking but it’d be nice to have the explanations and several different code examples all in one place along with the benefits of being able to change it and add to it much easier.
Disclaimer: This should end up being a rather long writeup, but I’ll try to separate things as much as possible so you can jump around and go directly to what you need.
Tools & Prerequisites
I’ll be using C++ and Visual Studio to write and compile the code, so you should probably have Visual Studio installed and have some sort of familiarity with C++. With that said, the code provided will be C++, but most of the techniques demonstrated make use of the Windows API so this stuff can be implemented in many other languages. We’ll make a little bit of use of a debugger, I use x64dbg.So the official tool list:
- Visual Studio
- Debugger, i.e.: x64dbg
So make sure you’re ALWAYS compiling in release mode!
Prerequisites for this tut:
- Familiarity with programming, preferably C++.
- Familiarity with different calling conventions.
-
To view link: Login
To view link: Login
Tutorial - Calling Conventions, and why you need to know them!
-
- Understand the concept of hooking and code injection.
Set up
We’re going to be coding both programs, a target, and an invoker. Hopefully, through the naming we all understand what each program will be doing but for those who don’t… The target will have different functions in it, we’re going to invoke these different functions from another program, the invoker. *mindblown*So go ahead, crank that badboy up and start two (2) instances of Visual Studio. One project we’ll name Target, the other… You guessed it! Invoker.
Next, go to Target’s project settings. Again, make sure you’re in release mode and that you’re editing the project settings for the correct configuration and platform. You’ll notice I have it set to all configurations just to be safe. Now disable optimizations, remember to hit Apply. Do the same for the Invoker.
![](https://i.imgur.com/oyekaye.png)
Now that we have all that out of the way, we’ll write a few functions with different signatures. Nothing crazy.
So I broke the all the code up into two files. In Functions.h there are several functions that we will be invoking, they all have different signatures to cover different situations. In Target.cpp you’ll see a simple class with a couple of functions, I’ll also demonstrate calling member functions externally for the sake of being thorough.
Just check out the git for the target code or download it to your computer and familiarize yourself with it really quickly.
To view link:
Login
To view link:
Login
I believe if you clone the projects from github then the project settings should still be intact.
Not sure, but that's why I also included an image of the project settings.
Alright, now that all that is out of the way, lets get started 😉
CreateRemoteThread
I think the CreateRemoteThread (from now on CRT) method is a bit more straightforward and easier to grasp so I’ll go ahead and start with this and the 1000 ways to skin a cat.
From MSDN:
“Creates a thread that runs in the virtual address space of another process.”
To view link:
Login
There are really only 3 parameters that we’ll be interested, or make use of, in for this tutorial. The process handle (hProcess), the LPTHREAD_START_ROUTINE (lpStartAddress) which is essentially a pointer to the code we want to start a thread at, and lpParameter which can be either a valid pointer to the arguments, or if the function expects a simple value, you can just shove that value right in here and it’ll work. This will be made clearer later on in the code example.
Let’s get started…
Simple Invoke
To view link:
Login
in the target process. It’s a simple function that doesn’t take any arguments and doesn’t return anything. Calling a function like this is dead easy, you simply need to start a thread at the address of the function.
To view link:
Login
It’s really that easy.
Invoke With Argument
Next, we’ll call
To view link:
Login
, very similar to Function1 but this time it expects an integer as an argument. For a function like this we just have to change a parameter to the value we want to pass. The argument lpParameter expects a pointer, the function expects an integer though, so how are we gonna fix this? We’re not, we’ll tell CRT it’s a pointer and just let it pass the value to the function.
To view link:
Login
And we’re done.
To be continued... Post is already too long, we're nowhere near finished lol.
- Game Downloading
- Download
Last edited: