The general trick has to do with how the program's code and variables are layed out in memory. For example, when a function is called the program (code inserted by the compiler) must store the address of the instruction to return to. So if this is the 32 bit word just before the beginning of the stack, one could do:

 void foo()
 {
    int array[5];
    int var = 0;
    int var2 = 0;

    // read in user input
    printf("Enter index and value to write:");
    scanf("%i", var);
    scanf("%i", var2);

    // malicious user might set var to -1 and var2 to an address to execute
    // if say the 32-bit value before the stack variables is the instruction to
    // return to
    array[var] = var2

    // return now goes to malicious code
 }

(So your job is to construct code so that such a thing is not possible. :) )

The rules for how a function call is implemented, stack variables allocated, values passed, and return values returned back is called the calling convention. I reccomend reading the attached article for a good indepth coverage of C calling conventionts.

Answer from Doug T. on Stack Overflow
🌐
GitHub
github.com › topics › code-injection
code-injection · GitHub Topics · GitHub
Advanced Game Hacking Library for C, Modern C++, Rust and Python (Windows/Linux/FreeBSD) (Process/Memory Hacking) (Hooking/Detouring) (Cross Platform) (x86/x64) (DLL/SO Injection) (Internal/External) (Assembler/Disassembler) python c rust hook c-plus-plus memory assembler disassembler process game-hacking code-injection syscall function-call detour-hook library-injection
🌐
GitHub
cocomelonc.github.io › tutorial › 2021 › 09 › 18 › malware-injection-1.html
Classic code injection into the process. Simple C++ malware. - cocomelonc
September 18, 2021 - In this post we will discuss about a classic technique which are payload injection using debugging API. Firstly, let’s go to prepare our payload. For simplicity, we use msfvenom reverse shell payload from Kali linux. ... where 10.9.1.6 is our attacker’s machine IP address, and 4444 is port which we run listener later. Let’s start with simple C++ code of our malware, which is used by me in AV evasion part 1 post: /* cpp implementation malware example with msfvenom payload */ #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> // our payload: reverse shell (msfv
🌐
GitHub
cocomelonc.github.io › tutorial › 2021 › 12 › 13 › malware-injection-12.html
Code injection via memory sections. Simple C++ example. - cocomelonc
December 13, 2021 - Changes to the local view of the section will also cause remote views to be modified as well, thus bypassing the need for APIs such as KERNEL32.DLL!WriteProcessMemory to write malicious code into remote process address space.
🌐
GitHub
github.com › topics › process-injection
process-injection · GitHub Topics · GitHub
This novel way of using NtQueueApcThreadEx by abusing the ApcRoutine and SystemArgument[0-3] parameters by passing a random pop r32; ret gadget can be used for stealthy code injection.
🌐
GitHub
github.com › Linthora › dynamic_code_optimisation_injection
GitHub - Linthora/dynamic_code_optimisation_injection: This project allows you to dynamically optimize/inject new code into a running program in C. · GitHub
This project allows you to dynamically optimize/inject new code into a running program in C. - Linthora/dynamic_code_optimisation_injection
Author   Linthora
Top answer
1 of 3
11

The general trick has to do with how the program's code and variables are layed out in memory. For example, when a function is called the program (code inserted by the compiler) must store the address of the instruction to return to. So if this is the 32 bit word just before the beginning of the stack, one could do:

 void foo()
 {
    int array[5];
    int var = 0;
    int var2 = 0;

    // read in user input
    printf("Enter index and value to write:");
    scanf("%i", var);
    scanf("%i", var2);

    // malicious user might set var to -1 and var2 to an address to execute
    // if say the 32-bit value before the stack variables is the instruction to
    // return to
    array[var] = var2

    // return now goes to malicious code
 }

(So your job is to construct code so that such a thing is not possible. :) )

The rules for how a function call is implemented, stack variables allocated, values passed, and return values returned back is called the calling convention. I reccomend reading the attached article for a good indepth coverage of C calling conventionts.

2 of 3
2

If you allocate a buffer on the stack, and it overflows, it writes onto the stack. The stack contains the return pointer for the function that allocated the buffer. So, if you overflow a buffer on the stack, you can set the return pointer to something arbitrary; thereby giving you control of the thread of execution.

As to actually injecting the code, that depends. The stack - or rather, the page containing it - is often set not to allow code execution; but historically it would have been possible to store small malicious programs in the buffer itself on the stack. Return oriented programming is a fairly new variant of the return-to-libc attack, both of which work around NX bits.

🌐
GitHub
github.com › oblique › code-injector › blob › master › injector.c
code-injector/injector.c at master · oblique/code-injector
// in this case we can inject the ... && arch == a_x86_32) { #endif · unsigned char buf[4], tmp, dbgtrap = 0xCC; void *ret_addr = NULL; int index; ·...
Author   oblique
🌐
GitHub
github.com › theevilbit › injection
GitHub - theevilbit/injection
This is a POC for the DLL injection ... option 3 - RtlCreateUserThread · This injection uses QueueUserAPC API to start a thread in the remote process after writing a shell code to its memory....
Starred by 825 users
Forked by 207 users
Languages   C++ 92.4% | C 7.6% | C++ 92.4% | C 7.6%
Find elsewhere
🌐
GitHub
github.com › peperunas › injectopi
GitHub - peperunas/injectopi: A set of tutorials about code injection for Windows.
Just open Injectopi.sln with Visual Studio! Make sure to have the Desktop C++ x86 and x64 components enabled with your Visual Studio installation! You should be ready to go! I have commented what the shellcode does in the file SHELLCODE.md. Here below you can find a list of interesting articles / websites / papers which I found useful while writing down Injectopi!
Starred by 312 users
Forked by 72 users
Languages   C 65.6% | C++ 34.4% | C 65.6% | C++ 34.4%
🌐
GitHub
github.com › topics › injection-attacks
injection-attacks · GitHub Topics · GitHub
windows dll cpp injection dll-injection injection-attacks ... Code Injection, Inject malicious payload via pagetables pml4.
🌐
GitHub
github.com › secrary › InjectProc
GitHub - secrary/InjectProc: InjectProc - Process Injection Techniques [This project is not maintained anymore] · GitHub
Execute the remote code. Create target process and suspend it. Unmap from memory. Allocate space. Write headers and sections into the remote process. Resume remote thread. Find/Create process. ... InjectProc uses SetWindowsHookEx function, you can try different ways to installing hooks, for example...
Starred by 997 users
Forked by 211 users
Languages   C++
🌐
GitHub
github.com › MahmoudZohdy › Process-Injection-Techniques
GitHub - MahmoudZohdy/Process-Injection-Techniques: Various Process Injection Techniques
This is the C implementation of Diffrent Process Injection Technique.
Starred by 161 users
Forked by 24 users
Languages   C++ 89.5% | C 10.5% | C++ 89.5% | C 10.5%
🌐
GitHub
github.com › revsic › CodeInjection
GitHub - revsic/CodeInjection: Code Injection technique written in cpp language · GitHub
std::vector<LPVOID> list; BYTE pattern[] = { 0x48, 0x63, 0x4D, 0xC8, 0x89, 0x08, 0x49, 0x63, 0x47, 0x50 }; //target opcode ScanMemory(hProcess, pattern, sizeof(pattern), list); BYTE code[] = { 0xC7, 0x00, 0x04, 0x00, 0x00, 0x00 }; // patch opcode WriteProcessMemory(hProcess, list.back(), code, sizeof(code), NULL); QueueUserAPC adds user-mode Asynchronous Procedure Call (APC). Many anti-debugging agents watch CreateRemoteThread. In order to bypass this scenario, we can use APC to inject dll.
Starred by 34 users
Forked by 17 users
Languages   C++
🌐
GitHub
github.com › marcosd4h › memhunter
GitHub - marcosd4h/memhunter: Live hunting of code injection techniques
Live hunting of code injection techniques. Contribute to marcosd4h/memhunter development by creating an account on GitHub.
Starred by 383 users
Forked by 90 users
Languages   C++ 93.4% | C 6.6% | CMake 0.0% | HTML 0.0% | M4 0.0% | Perl 0.0% | C++ 93.4% | C 6.6% | CMake 0.0% | HTML 0.0% | M4 0.0% | Perl 0.0%
🌐
GitHub
github.com › punitganshani › CodeInject
GitHub - punitganshani/CodeInject: CodeInject - Code Inject and Runtime Intelligence · GitHub
It eases the inevitable task of ... intercept code for almost any purpose. When using CInject, you do not require any knowledge of the target application. You can create your own injectors very easily and inject them in any target assembly/executable. An example is provided ...
Starred by 146 users
Forked by 39 users
Languages   C#
🌐
Wikipedia
en.wikipedia.org › wiki › Code_injection
Code injection - Wikipedia
February 14, 2026 - Code injection could, for example: Introduce a useful new column that did not appear in the original design of a search results page. Offer a new way to filter, order, or group data by using a field not exposed in the default functions of the original design.
🌐
GitHub
github.com › 3xpl01tc0d3r › ProcessInjection
GitHub - 3xpl01tc0d3r/ProcessInjection: This program is designed to demonstrate various process injection techniques · GitHub
1 = Vanilla Process Injection 2 = DLL Injection 3 = Process Hollowing 4 = APC Queue Injection 5 = KernelCallbackTable Injection /m Specify the method to be used p = P/Invoke (Default) d = D/Invoke ds = Direct Syscalls ids = Indirect Syscalls ...
Starred by 1.2K users
Forked by 192 users
Languages   C#
🌐
GitHub
cocomelonc.github.io › tutorial › 2021 › 09 › 20 › malware-injection-2.html
Classic DLL injection into the process. Simple C++ malware. - cocomelonc
September 20, 2021 - Now we only need a code which will inject this library into the process of our choosing. In our case we are going talk about classic DLL injection. We allocate an empty buffer of a size at least the length of the path of our DLL from disk. And then we copy the path to this buffer. /* * evil_inj.cpp * classic DLL injection example * author: @cocomelonc * https://cocomelonc.github.io/tutorial/2021/09/20/malware-injection-2.html */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #include <tlhelp32.h> char evilDLL[] = "C:\\evil.dll"; unsigned int evilLen = sizeof(ev
🌐
ired.team
ired.team › offensive-security › code-injection-process-injection
Code & Process Injection | Red Team Notes
October 26, 2018 - CreateRemoteThread Shellcode InjectionDLL InjectionReflective DLL InjectionShellcode Reflective DLL InjectionProcess DoppelgangingLoading and Executing Shellcode From PE ResourcesProcess Hollowing and Portable Executable RelocationsAPC Queue Code InjectionEarly Bird APC Queue Code InjectionShellcode Execution in a Local Process with QueueUserAPC and NtTestAlertShellcode Execution through FibersShellcode Execution via CreateThreadpoolWaitLocal Shellcode Execution without Windows APIsInjecting to Remote Process via Thread HijackingSetWindowHookEx Code InjectionFinding Kernel32 Base and Function
🌐
GitHub
github.com › elegantchaos › ECInjection
GitHub - elegantchaos/ECInjection: Example of code injection using mach_star on Mac OS X
The injected code just adds a menu to the application's menubar called "Injector", with a single item in it that just logs stuff to the console. Once again I should point out that this example pretty much ignores security when it comes to the inter-process communication between the host application, the injection helper, and the injected code.
Starred by 9 users
Forked by 2 users
Languages   Objective-C 97.2% | Shell 2.8% | Objective-C 97.2% | Shell 2.8%