0Pricing
Assembly Language & x86 Low-Level Systems Programming · บทเรียน

บัฟเฟอร์ล้นและเชลล์โค้ด

สำรวจกลไกของช่องโหว่บัฟเฟอร์ล้น และวิธีที่ผู้โจมตีอาจใช้ช่องโหว่นี้แทรกและเรียกใช้เชลล์โค้ดที่เป็นอันตราย

บัฟเฟอร์ล้นและเชลล์โค้ด เป็นบทเรียน Assembly Language & x86 Low-Level Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Assembly Language & x86 Low-Level Systems Programming และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Assembly Language & x86 Low-Level Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Intro: Buffer Overflows

Welcome to a critical topic in low-level security: Buffer Overflows. These are a type of software vulnerability that can allow attackers to gain control over a program.

Essentially, a buffer overflow occurs when a program tries to write more data into a fixed-size memory buffer than it was designed to hold. This excess data 'overflows' into adjacent memory regions.

What's a Buffer?

In programming, especially in languages like C or assembly, a buffer is simply a block of memory reserved for storing data. Think of it like a container with a specific capacity.

  • Buffers are often used for temporary storage, like holding user input or network data.
  • They can be declared as arrays of characters (strings) or other data types.
  • For example, char username[32]; declares a buffer that can hold up to 31 characters plus a null terminator.

Code: A Vulnerable Buffer

Let's look at a simple C program with an intentional buffer overflow vulnerability. Pay close attention to the strcpy function.

strcpy copies a string from source to destination, but it does not check the destination buffer's size. This is a common source of overflows.

/* buffer_example.c */
#include <stdio.h>
#include <string.h>

// This function is intentionally vulnerable
void vulnerable_greet(char *name_input) {
  char buffer[16]; // A small buffer, 16 bytes
  
  // This is the vulnerability! strcpy doesn't check size.
  // If name_input is longer than 15 chars (+ null terminator),
  // it will overflow 'buffer'.
  strcpy(buffer, name_input); 
  
  printf("Hello, %s!\n", buffer);
}

int main() {
  // Let's call the vulnerable function with a safe input
  vulnerable_greet("CoddyKit User"); 
  
  printf("Program finished normally.\n");
  
  return 0;
}

The Stack Frame

To understand how overflows can be exploited, we need to revisit the call stack. When a function is called, a new stack frame is created.

This stack frame typically contains:

  • Local variables for the function.
  • The saved Base Pointer (EBP/RBP).
  • Most importantly: the Return Address, which tells the CPU where to resume execution after the function finishes.

Overwriting the Return Address

When a buffer overflow occurs on the stack, the excess data doesn't just corrupt adjacent local variables. If enough data is supplied, it can overwrite the saved EBP and then the return address itself.

By changing the return address, an attacker can trick the program into jumping to an arbitrary memory location instead of returning to the legitimate caller. This is the core mechanism for many buffer overflow exploits.

Introducing Shellcode

So, where does the attacker want the program to jump? Usually, to a piece of code they control, known as shellcode.

  • Shellcode is a small, self-contained piece of machine code.
  • Its primary purpose is often to execute a 'shell' (a command prompt) on the target system.
  • It can also perform other malicious actions, like creating new users, downloading files, or connecting to a remote server.

Anatomy of Simple Shellcode

Shellcode is typically written in assembly language to be as compact and efficient as possible. It avoids null bytes (\x00) because many string functions stop copying at the first null byte.

A common goal is to call the execve system call (on Linux) to launch /bin/sh.

; Example (conceptual) Linux x86 shellcode snippet: ; mov eax, 0x0b ; Syscall number for execve ; mov ebx, addr_of_sh ; Pointer to '/bin/sh' string ; mov ecx, 0 ; Arg 2 (argv) = NULL ; mov edx, 0 ; Arg 3 (envp) = NULL ; int 0x80 ; Invoke kernel (syscall)

Injecting Shellcode

How does the shellcode get into the program? The attacker includes it as part of the malicious input that causes the buffer overflow.

When the buffer overflows, the shellcode is written into the program's memory, usually on the stack, alongside the overwritten return address.

Exploit Structure: NOP Sled

Attackers often use a NOP sled (No Operation sled) to increase the reliability of their exploit.

  • A NOP sled is a sequence of 'No Operation' (NOP) instructions (e.g., \x90 in x86).
  • If the attacker overwrites the return address to point anywhere within the NOP sled, the CPU will simply execute NOPs until it 'slides' into the actual shellcode.
  • This compensates for slight inaccuracies in guessing the exact memory address of the shellcode.

Defenses & Mitigations

Operating systems and compilers have developed several defenses against buffer overflows:

  • ASLR (Address Space Layout Randomization): Randomizes memory addresses to make guessing the return address or shellcode location harder.
  • DEP/NX Bit (Data Execution Prevention/No-Execute): Marks memory regions (like the stack) as non-executable, preventing shellcode from running there.
  • Stack Canaries: Compiler-generated random values placed on the stack; if overwritten, the program detects tampering and aborts.
  • Safe Functions: Using functions like strncpy, snprintf, fgets, or C++ strings that perform bounds checking.

Check Your Understanding

A stack buffer overflow allows an attacker to write past the end of a buffer. What is the primary goal an attacker aims to achieve by carefully crafting input to overwrite the return address?

Recap: Overflows & Shellcode

In this lesson, we explored the dangerous world of buffer overflows. We learned that they occur when too much data is written into a fixed-size buffer, corrupting adjacent memory.

Crucially, if this overflow reaches the return address on the stack, an attacker can hijack program control. They achieve this by injecting shellcode—small, malicious machine code—and redirecting execution to it, often aided by a NOP sled. We also touched upon essential mitigation techniques like ASLR, DEP, and stack canaries.

คำถามที่พบบ่อย

บทเรียน “บัฟเฟอร์ล้นและเชลล์โค้ด” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “บัฟเฟอร์ล้นและเชลล์โค้ด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Assembly Language & x86 Low-Level Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Assembly Language & x86 Low-Level Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “บัฟเฟอร์ล้นและเชลล์โค้ด”

สำรวจกลไกของช่องโหว่บัฟเฟอร์ล้น และวิธีที่ผู้โจมตีอาจใช้ช่องโหว่นี้แทรกและเรียกใช้เชลล์โค้ดที่เป็นอันตราย คุณปฏิบัติ Assembly Language & x86 Low-Level Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Assembly Language & x86 Low-Level Systems Programming หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Assembly Language & x86 Low-Level Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “บัฟเฟอร์ล้นและเชลล์โค้ด” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Assembly Language & x86 Low-Level Systems Programming นี้ได้ไหม

ได้ บทเรียน Assembly Language & x86 Low-Level Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ความสอดคล้องของแคชและประสิทธิภาพ
  2. การปรับส่วนสำคัญให้เหมาะสมด้วยตนเอง
  3. บัฟเฟอร์ล้นและเชลล์โค้ด
  4. การทำนายสาขาและการทำงานแบบคาดการณ์ล่วงหน้า
← กลับไปที่ Assembly Language & x86 Low-Level Systems Programming