μ΄κΈ°νλμ§ μμ λ³μ
Tip
AWS ν΄νΉ λ°°μ°κΈ° λ° μ°μ΅νκΈ°:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ν΄νΉ λ°°μ°κΈ° λ° μ°μ΅νκΈ°:HackTricks Training GCP Red Team Expert (GRTE)
Azure ν΄νΉ λ°°μ°κΈ° λ° μ°μ΅νκΈ°:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks μ§μνκΈ°
- ꡬλ κ³ν νμΈνκΈ°!
- **π¬ λμ€μ½λ κ·Έλ£Ή λλ ν λ κ·Έλ¨ κ·Έλ£Ήμ μ°Έμ¬νκ±°λ νΈμν° π¦ @hacktricks_liveλ₯Ό νλ‘μ°νμΈμ.
- HackTricks λ° HackTricks Cloud κΉνλΈ λ¦¬ν¬μ§ν 리μ PRμ μ μΆνμ¬ ν΄νΉ νΈλ¦μ 곡μ νμΈμ.
κΈ°λ³Έ μ 보
ν΅μ¬ μμ΄λμ΄λ μ΄κΈ°νλμ§ μμ λ³μλ ν΄λΉ λ©λͺ¨λ¦¬ μ£Όμμ μ΄λ―Έ μλ κ°μ κ°κ² λλ€λ μ¬μ€μ μ΄ν΄νλ κ²μ΄λ€. μ:
- Function 1:
initializeVariable: λ³μxλ₯Ό μ μΈνκ³ κ°(μ:0x1234)μ ν λΉνλ€. μ΄ λμμ λ©λͺ¨λ¦¬μ ν μ리λ₯Ό μμ½νκ³ νΉμ κ°μ λ£λ κ²κ³Ό κ°λ€. - Function 2:
useUninitializedVariable: μ¬κΈ°μλ λ λ€λ₯Έ λ³μyλ₯Ό μ μΈνμ§λ§ κ°μ ν λΉνμ§ μλλ€. Cμμλ μ΄κΈ°νλμ§ μμ λ³μκ° μλμΌλ‘ 0μΌλ‘ μ€μ λμ§ μλλ€. λμ ν΄λΉ λ©λͺ¨λ¦¬ μμΉμ λ§μ§λ§μΌλ‘ μ μ₯λμ΄ μλ κ°μ κ·Έλλ‘ μ μ§νλ€.
μ΄ λ ν¨μλ₯Ό μμ°¨μ μΌλ‘ μ€ννλ©΄:
initializeVariableμμxμ κ°(0x1234)μ΄ ν λΉλλ©°, μ΄λ νΉμ λ©λͺ¨λ¦¬ μ£Όμλ₯Ό μ°¨μ§νλ€.useUninitializedVariableμμλyκ° μ μΈλμ§λ§ κ°μ΄ ν λΉλμ§ μμxλ°λ‘ λ€μ λ©λͺ¨λ¦¬ μμΉλ₯Ό μ°¨μ§νλ€.yλ₯Ό μ΄κΈ°ννμ§ μμκΈ° λλ¬Έμ κ·Έ λ©λͺ¨λ¦¬ μμΉμ λ§μ§λ§μΌλ‘ λ¨μ μλ κ°(μ¦xκ° μ¬μ©ν κ°)μ βμμβλ°κ² λλ€.
μ΄ λμμ μ μμ€ νλ‘κ·Έλλ°μ ν΅μ¬ κ°λ μ 보μ¬μ€λ€: λ©λͺ¨λ¦¬ κ΄λ¦¬κ° μ€μνλ€λ κ². μ΄κΈ°νλμ§ μμ λ³μλ λ©λͺ¨λ¦¬μ λ¨μ μλ λ―Όκ°ν λ°μ΄ν°λ₯Ό μλμΉ μκ² λ³΄μ ν μ μμ΄ μμΈ‘ λΆκ°λ₯ν λμμ΄λ 보μ μ·¨μ½μ μΌλ‘ μ΄μ΄μ§ μ μλ€.
μ΄κΈ°νλμ§ μμ μ€ν λ³μλ λ€μκ³Ό κ°μ μ¬λ¬ 보μ μνμ μ΄λν μ μλ€:
- Data Leakage: λΉλ°λ²νΈ, μνΈν ν€ λλ κ°μΈ μ 보 κ°μ λ―Όκ°ν μ λ³΄κ° μ΄κΈ°νλμ§ μμ λ³μμ λ¨μ μμΌλ©΄ λ ΈμΆλ μ μμΌλ©°, 곡격μκ° ν΄λΉ λ°μ΄ν°λ₯Ό μ½μ μ μλ€.
- Information Disclosure: μ΄κΈ°νλμ§ μμ λ³μμ λ΄μ©μ νλ‘κ·Έλ¨μ λ©λͺ¨λ¦¬ λ μ΄μμμ΄λ λ΄λΆ λμμ λν μ 보λ₯Ό λλ¬λ΄μ΄ 곡격μκ° νμ νλ μ΅μ€νλ‘μμ κ°λ°νλ λ° λμμ μ€ μ μλ€.
- Crashes and Instability: μ΄κΈ°νλμ§ μμ λ³μλ₯Ό μ¬μ©νλ μ°μ°μ μ μλμ§ μμ λμμ μΌμΌμΌ νλ‘κ·Έλ¨ μΆ©λμ΄λ μμΈ‘ λΆκ°λ₯ν κ²°κ³Όλ₯Ό μ΄λν μ μλ€.
- Arbitrary Code Execution: νΉμ μλ리μ€μμλ 곡격μκ° μ΄λ¬ν μ·¨μ½μ μ μ΄μ©ν΄ νλ‘κ·Έλ¨μ μ€ν νλ¦μ λ³κ²½νμ¬ μμμ μ½λλ₯Ό μ€νν μ μμΌλ©°, μ΄λ μ격 μ½λ μ€ν μνμ ν¬ν¨ν μ μλ€.
μμ
#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:
initializeAndPrintFunction: μ΄ ν¨μλ μ μ λ³μinitializedVarλ₯Ό μ μΈνκ³ κ°100μ λμ ν λ€, λ³μμ λ©λͺ¨λ¦¬ μ£Όμμ κ°μ μΆλ ₯ν©λλ€. μ΄ λ¨κ³λ λ¨μνλ©° μ΄κΈ°νλ λ³μκ° μ΄λ»κ² λμνλμ§ λ³΄μ¬μ€λλ€.demonstrateUninitializedVarFunction: μ΄ ν¨μμμλ μ μ λ³μuninitializedVarλ₯Ό μ΄κΈ°ννμ§ μκ³ μ μΈν©λλ€. κ°μ μΆλ ₯νλ € νλ©΄ μΆλ ₯μ΄ λ¬΄μμ μ«μλ₯Ό λ³΄μΌ μ μμ΅λλ€. μ΄ μ«μλ ν΄λΉ λ©λͺ¨λ¦¬ μμΉμ μ΄μ μ μλ λ°μ΄ν°λ₯Ό λνλ λλ€. νκ²½κ³Ό μ»΄νμΌλ¬μ λ°λΌ μ€μ μΆλ ₯μ λ¬λΌμ§ μ μκ³ , μΌλΆ μ»΄νμΌλ¬λ μμ μ μν΄ λ³μλ₯Ό μλμΌλ‘ 0μΌλ‘ μ΄κΈ°νν μλ μμ§λ§, μ΄λ₯Ό μ λ’°ν΄μλ μ λ©λλ€.mainFunction:mainν¨μλ μ λ ν¨μλ₯Ό μμλλ‘ νΈμΆνμ¬ μ΄κΈ°νλ λ³μμ μ΄κΈ°νλμ§ μμ λ³μμ μ°¨μ΄λ₯Ό 보μ¬μ€λλ€.
Practical exploitation patterns (2024β2025)
κ³ μ μ μΈ βread-before-writeβ λ²κ·Έλ μ¬μ ν μ ν¨ν©λλ€. νλμ μνμ± (ASLR, canaries)μ΄ μ’ μ’ λΉλ° μ μ§μ μμ‘΄νκΈ° λλ¬Έμ λλ€. μΌλ°μ μΈ κ³΅κ²© νλ©΄:
- Partially initialized structs copied to userland: 컀λμ΄λ λλΌμ΄λ²λ μ’
μ’
κΈΈμ΄ νλλ§
memsetνκ³copy_to_user(&u, &local_struct, sizeof(local_struct))λ₯Ό νΈμΆν©λλ€. ν¨λ©κ³Ό μ¬μ©λμ§ μμ νλλ stack canary halves, saved frame pointers λλ kernel pointersλ₯Ό leak ν©λλ€. ꡬ쑰체μ function pointerκ° ν¬ν¨λμ΄ μλ€λ©΄, μ΄λ₯Ό μ΄κΈ°ννμ§ μμΌλ©΄ λμ€μ μ¬μ¬μ©λ λ controlled overwriteλ₯Ό νμ©ν μλ μμ΅λλ€. - Uninitialized stack buffers reused as indexes/lengths: μ΄κΈ°νλμ§ μμ
size_t len;κ°read(fd, buf, len)μ κ²½κ³λ₯Ό μ νλ λ° μ¬μ©λλ©΄, ν΄λΉ μ€ν μ¬λ‘―μ΄ μ΄μ νΈμΆμ ν° κ°μ μ μ§νκ³ μμ λ 곡격μκ° λ²μλ₯Ό λ²μ΄λ μ½κΈ°/μ°κΈ°(out-of-bounds reads/writes)λ₯Ό νκ±°λ ν¬κΈ° κ²μ¬λ₯Ό μ°νν μ μμ΅λλ€. - Compiler-added padding: κ°λ³ λ©€λ²κ° μ΄κΈ°νλμ΄ μμ΄λ κ·Έ μ¬μ΄μ μ묡μ ν¨λ© λ°μ΄νΈλ μ΄κΈ°νλμ§ μμ΅λλ€. ꡬ쑰체 μ 체λ₯Ό userlandλ‘ λ³΅μ¬νλ©΄ μ΄μ μ€ν λ΄μ©(canaries, pointers)μ ν¬ν¨νλ ν¨λ©μ΄ leak λ©λλ€.
- ROP/Canary disclosure: ν¨μκ° λλ²κΉ μ μν΄ λ‘컬 ꡬ쑰체λ₯Ό stdoutμΌλ‘ 볡μ¬νλ©΄, μ΄κΈ°νλμ§ μμ ν¨λ©μ΄ stack canaryλ₯Ό λλ¬λ΄μ΄ μ΄νμ stack overflow 곡격μ brute-force μμ΄ κ°λ₯νκ² ν μ μμ΅λλ€.
Minimal PoC pattern to detect such issues during review:
struct msg {
char data[0x20];
uint32_t len;
};
ssize_t handler(int fd) {
struct msg m; // never fully initialized
m.len = read(fd, m.data, sizeof(m.data));
// later debug helper
write(1, &m, sizeof(m)); // leaks padding + stale stack
return m.len;
}
μνμ± λ° μ»΄νμΌλ¬ μ΅μ (μ°ν μ μΌλμ λ κ²)
- Clang/GCC auto-init: Recent toolchains expose
-ftrivial-auto-var-init=zeroor-ftrivial-auto-var-init=pattern, filling every automatic (stack) variable at function entry with zeros or a poison pattern (0xAA / 0xFE). μ΄λ ν¨μ μ§μ μ λͺ¨λ μλ(stack) λ³μλ₯Ό 0 λλ ν¨ν΄μΌλ‘ μ±μ λλΆλΆμ uninitialized-stack info leaksλ₯Ό μ°¨λ¨νκ³ , λΉλ°κ°μ μλ €μ§ κ°μΌλ‘ λ°κΏ exploitationμ λ μ΄λ ΅κ² λ§λλλ€. - Linux kernel hardening: Kernels built with
CONFIG_INIT_STACK_ALLor the newerCONFIG_INIT_STACK_ALL_PATTERNzero/pattern-initialize every stack slot at function entry, wiping canaries/pointers that would otherwise leak. μ΄λ¬ν μ΅μ μΌλ‘ λΉλλ(νΉν ClangμΌλ‘ λΉλλ) 컀λμ λ°°ν¬νλ λ°°ν¬νμ μ°Ύμ보μΈμ(6.8+ νλλ ꡬμ±μμ νν¨). - Opt-out attributes: Clang now allows
__attribute__((uninitialized))on specific locals/structs to keep performance-critical areas uninitialized even when global auto-init is enabled. μ΄λ¬ν μ΄λ Έν μ΄μ μ μ±λ₯ μ μ€μν μμμ μλμ μΌλ‘ μ΄κΈ°ννμ§ μκΈ° μν΄ μ¬μ©λλ―λ‘ μ£Όμ κΉκ² κ²ν νμΈμβμ’ μ’ side channelsμ λ Έλ¦° 곡격 νλ©΄μ νμν©λλ€.
곡격μ κ΄μ μμλ λ°μ΄λλ¦¬κ° μ΄λ¬ν νλκ·Έλ‘ λΉλλμλμ§ μ¬λΆκ° stack-leak primitivesκ° μ€μ©μ μΈμ§, μλλ©΄ heap/data-section disclosuresλ‘ μ νν΄μΌ νλμ§λ₯Ό κ²°μ ν©λλ€.
Finding uninitialized-stack bugs quickly
- μ»΄νμΌλ¬ μ§λ¨(Compiler diagnostics): Build with
-Wall -Wextra -Wuninitialized(GCC/Clang). For C++ code,clang-tidy -checks=cppcoreguidelines-init-variableswill auto-fix many cases to zero-init and is handy to spot missed locals during audit. - λμ λꡬ(Dynamic tools):
-fsanitize=memory(MSan) in Clang or Valgrindβs--track-origins=yesreliably flag reads of uninitialized stack bytes during fuzzing. ν μ€νΈ νλμ€μ μ΄ λꡬλ€μ μ μ©νλ©΄ λ―ΈμΈν padding leaksλ₯Ό λλ¬λΌ μ μμ΅λλ€. - Grepping ν¨ν΄(Grepping patterns): μ½λ 리뷰 μ μ 체 ꡬ쑰체λ₯Ό λμμΌλ‘ νλ
copy_to_user/writeνΈμΆμ΄λ, ꡬ쑰체μ μΌλΆλ§ μ€μ λ μνμμμmemcpy/sendκ°μ stack λ°μ΄ν° μ μ‘μ κ²μνμΈμ. μ΄κΈ°νκ° κ±΄λλ°μ΄μ§λ μλ¬ κ²½λ‘μ νΉν μ£ΌμνμΈμ.
ARM64 μμ
ARM64μμλ λ‘컬 λ³μκ° μμ stackμμ κ΄λ¦¬λλ―λ‘ μ ν λ¬λΌμ§μ§ μμ΅λλ€. μ΄λ check this exampleμμ νμΈν μ μμ΅λλ€.
References
- CONFIG_INIT_STACK_ALL_PATTERN documentation
- GHSL-2024-197: GStreamer uninitialized stack variable leading to function pointer overwrite
Tip
AWS ν΄νΉ λ°°μ°κΈ° λ° μ°μ΅νκΈ°:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ν΄νΉ λ°°μ°κΈ° λ° μ°μ΅νκΈ°:HackTricks Training GCP Red Team Expert (GRTE)
Azure ν΄νΉ λ°°μ°κΈ° λ° μ°μ΅νκΈ°:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks μ§μνκΈ°
- ꡬλ κ³ν νμΈνκΈ°!
- **π¬ λμ€μ½λ κ·Έλ£Ή λλ ν λ κ·Έλ¨ κ·Έλ£Ήμ μ°Έμ¬νκ±°λ νΈμν° π¦ @hacktricks_liveλ₯Ό νλ‘μ°νμΈμ.
- HackTricks λ° HackTricks Cloud κΉνλΈ λ¦¬ν¬μ§ν 리μ PRμ μ μΆνμ¬ ν΄νΉ νΈλ¦μ 곡μ νμΈμ.


