Inject C++ in compiled program

Hello,

I'd like to inject my C++ code into an already existing and compiled and running (dynamic edit) program via .dll or .so (currently working on .dll).

I know it is required to do some ASM, which is the reason why I've studied it a bit and worked on base operators.

I've been looking for the internet for things such as code caving, code injection, but couldn't find anything that would work on a simple example.

Here is my base program code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>

int sum(int a, int b)
{
	// [CODE INJECT]: Edit values of a & b for constants
	return a+b;
}

int main()
{
	// [CODE INJECT]: [BEGIN REMOVE]
	std::ofstream ofs("file.txt");
	ofs << "The SUM of 3 & 3 is equal to: " << sum(3,3) << std::endl;
	ofs.close();
	// [CODE INJECT]: [END REMOVE]
	/* [CODE INJECT]:
	std::cout << sum(1,1) << std::endl;
	*/
	
	return 0;
}


How would I be able to proceed to the actions I've putted in the comments ?
Would you be able to guide me doing so for this example ?

Thanks for your help !
Last edited on
In Windows you can use OpenProcess / ReadProcessMemory functions to find place you want to intercept. After it you have to use VirtualAllocEx / WriteProcessMemory to write your code into other process memory and rewrite binary existing instructions into jump to your code.
Topic archived. No new replies allowed.