mirror of
https://github.com/carlospolop/hacktricks
synced 2024-11-22 04:33:28 +00:00
5.3 KiB
5.3 KiB
Uninitialized Variables
Learn AWS hacking from zero to hero with htARTE (HackTricks AWS Red Team Expert)!
Other ways to support HackTricks:
- If you want to see your company advertised in HackTricks or download HackTricks in PDF Check the SUBSCRIPTION PLANS!
- Get the official PEASS & HackTricks swag
- Discover The PEASS Family, our collection of exclusive NFTs
- Join the 💬 Discord group or the telegram group or follow us on Twitter 🐦 @hacktricks_live.
- Share your hacking tricks by submitting PRs to the HackTricks and HackTricks Cloud github repos.
Basic Information
The core idea here is to understand what happens with uninitialized variables as they will have the value that was already in the assigned memory to them. Example:
- Function 1:
initializeVariable
: We declare a variablex
and assign it a value, let's say0x1234
. This action is akin to reserving a spot in memory and putting a specific value in it. - Function 2:
useUninitializedVariable
: Here, we declare another variabley
but do not assign any value to it. In C, uninitialized variables don't automatically get set to zero. Instead, they retain whatever value was last stored at their memory location.
When we run these two functions sequentially:
- In
initializeVariable
,x
is assigned a value (0x1234
), which occupies a specific memory address. - In
useUninitializedVariable
,y
is declared but not assigned a value, so it takes the memory spot right afterx
. Due to not initializingy
, it ends up "inheriting" the value from the same memory location used byx
, because that's the last value that was there.
This behavior illustrates a key concept in low-level programming: Memory management is crucial, and uninitialized variables can lead to unpredictable behavior or security vulnerabilities, as they may unintentionally hold sensitive data left in memory.
Example
#include <stdio.h>
// Function to initialize and print a variable
void initializeAndPrint() {
int initializedVar = 100; // Initialize the variable
printf("Initialized Variable:\n");
printf("Address: %p, Value: %d\n\n", (void*)&initializedVar, initializedVar);
}
// Function to demonstrate the behavior of an uninitialized variable
void demonstrateUninitializedVar() {
int uninitializedVar; // Declare but do not initialize
printf("Uninitialized Variable:\n");
printf("Address: %p, Value: %d\n\n", (void*)&uninitializedVar, uninitializedVar);
}
int main() {
printf("Demonstrating Initialized vs. Uninitialized Variables in C\n\n");
// First, call the function that initializes its variable
initializeAndPrint();
// Then, call the function that has an uninitialized variable
demonstrateUninitializedVar();
return 0;
}
How This Works:
initializeAndPrint
Function: This function declares an integer variableinitializedVar
, assigns it the value100
, and then prints both the memory address and the value of the variable. This step is straightforward and shows how an initialized variable behaves.demonstrateUninitializedVar
Function: In this function, we declare an integer variableuninitializedVar
without initializing it. When we attempt to print its value, the output might show a random number. This number represents whatever data was previously at that memory location. Depending on the environment and compiler, the actual output can vary, and sometimes, for safety, some compilers might automatically initialize variables to zero, though this should not be relied upon.main
Function: Themain
function calls both of the above functions in sequence, demonstrating the contrast between an initialized variable and an uninitialized one.
Learn AWS hacking from zero to hero with htARTE (HackTricks AWS Red Team Expert)!
Other ways to support HackTricks:
- If you want to see your company advertised in HackTricks or download HackTricks in PDF Check the SUBSCRIPTION PLANS!
- Get the official PEASS & HackTricks swag
- Discover The PEASS Family, our collection of exclusive NFTs
- Join the 💬 Discord group or the telegram group or follow us on Twitter 🐦 @hacktricks_live.
- Share your hacking tricks by submitting PRs to the HackTricks and HackTricks Cloud github repos.