0Pricing
Secure Coding & OWASP Top 10 for Backend · บทเรียน

การแทรกคำสั่งและโค้ด

เรียนรู้การระบุและลดช่องโหว่ที่เกี่ยวข้องกับการแทรกคำสั่ง OS และการเรียกใช้โค้ดตามอำเภอใจในระบบแบ็กเอนด์

การแทรกคำสั่งและโค้ด เป็นบทเรียน Secure Coding & OWASP Top 10 for Backend ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Secure Coding & OWASP Top 10 for Backend และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Secure Coding & OWASP Top 10 for Backend มีบทเรียนทั้งหมด 4 บทเรียน

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

What is Command & Code Injection?

Welcome! In this lesson, we'll explore Command and Code Injection, two critical backend vulnerabilities.

These attacks trick your application into executing unintended system commands or application-level code, leading to severe security breaches.

  • Command Injection: Executes OS commands.
  • Code Injection: Executes application programming language code.

OS Command Injection Basics

OS Command Injection occurs when an attacker can run arbitrary operating system commands on the server hosting your application.

This happens when an application passes unsanitized user-supplied input to a system shell, often through functions that execute external programs.

Attackers can then:

  • Read, write, or delete files.
  • Execute malicious scripts.
  • Gain full control over the server.

Vulnerable OS Command Example

Let's see a vulnerable Java example. Imagine userInput comes directly from a web form.

The attacker can use command separators (like ; or &) to append new commands to the original one.

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class CommandInjectionDemo {
  public static void main(String[] args) {
    // Imagine 'userInput' comes from a web request parameter
    String userInput = "my_file.txt; echo HELLO_INJECTED_COMMAND"; // Malicious input example
    // For Windows: "my_file.txt & echo HELLO_INJECTED_COMMAND"
    
    // VULNERABLE: Direct concatenation of user input into a system command
    String command = "ls -l " + userInput; // Linux example command
    // For Windows: String command = "dir " + userInput;
    
    System.out.println("Executing: " + command);
    
    try {
      Process p = Runtime.getRuntime().exec(command);
      BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
      String line;
      while ((line = reader.readLine()) != null) {
        System.out.println(line);
      }
      p.waitFor(); // Wait for the command to finish
    } catch (Exception e) {
      System.err.println("Error executing command: " + e.getMessage());
    }
  }
}

How the Injection Works

In the previous example, the string ls -l my_file.txt; echo HELLO_INJECTED_COMMAND is passed to the shell.

The semicolon (;) is a command separator on Linux/Unix-like systems. This tells the shell to execute the first command, then the second.

Even if my_file.txt doesn't exist, the echo command will still run, demonstrating the injection.

Mitigation: Input Validation

The primary defense against OS Command Injection is strict input validation.

  • Whitelisting: This is the safest approach. Define exactly what characters, formats, or values are allowed for user input. Reject anything that doesn't match.
  • Avoid Blacklisting: Trying to block known malicious characters (like ;, &) is often bypassable due to different shell interpretations or encoding tricks.

Mitigation: Preferring Safe APIs

Instead of concatenating user input directly into a command string, use APIs that pass arguments as separate elements to the command.

In Java, ProcessBuilder is safer than Runtime.getRuntime().exec() when used correctly, as it doesn't invoke a shell by default to interpret arguments.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class SafeCommandExec {
  public static void main(String[] args) {
    // Imagine 'userInput' comes from a web request parameter
    String userInput = "my_file.txt; echo HELLO_INJECTED_COMMAND"; // Malicious input attempt
    
    // SECURE: Pass arguments as separate strings to ProcessBuilder
    // The shell will NOT interpret '; echo HELLO_INJECTED_COMMAND' as a new command
    ProcessBuilder pb = new ProcessBuilder("ls", "-l", userInput);
    // For Windows: ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", "dir", userInput);
    
    System.out.println("Executing: " + String.join(" ", pb.command()));
    
    try {
      Process p = pb.start(); // Start the process
      BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
      String line;
      while ((line = reader.readLine()) != null) {
        System.out.println(line);
      }
      p.waitFor();
    } catch (Exception e) {
      System.err.println("Error executing command: " + e.getMessage());
    }
  }
}

Understanding Code Injection

Code Injection is similar to Command Injection, but instead of OS commands, it executes arbitrary code in the application's programming language.

This can happen when an application:

  • Evaluates user-supplied input as executable code (e.g., using eval() in scripting languages, or Expression Languages in Java).
  • Deserializes untrusted data that can construct malicious objects.
  • Uses vulnerable templating engines.

Code Injection Scenarios

Unlike OS Command Injection which targets the operating system shell, Code Injection targets the application's runtime environment.

  • Dynamic Evaluation: If your application takes user input and uses it in a function like eval() (common in Python, PHP, JavaScript), an attacker can inject their own code.
  • Insecure Deserialization: When an application deserializes untrusted data without proper validation, an attacker can craft malicious serialized objects that execute code upon deserialization.

Java applications are particularly vulnerable to insecure deserialization.

Preventing Code Injection

Mitigating Code Injection requires careful design and strict data handling:

  • Avoid Dynamic Code Execution: Never use functions that evaluate user-supplied input as code.
  • Validate Deserialized Data: Only deserialize data from trusted sources. Implement strict type checks and object validation for any deserialized input.
  • Use Safe Template Engines: Ensure your template engines automatically escape user-provided data to prevent injection.
  • Least Privilege: Run your application with the minimum necessary permissions.

Test Your Knowledge

It's time for a quick check! Select the most effective practices to prevent injection vulnerabilities.

Recap: Injection Defenses

Great job! You've learned about Command and Code Injection.

  • Command Injection allows attackers to run OS commands.
  • Code Injection allows attackers to run application-level code.
  • Key defenses include strict input validation (whitelisting), using safe APIs (like ProcessBuilder with separate arguments), and avoiding dynamic code execution from untrusted sources.

Always treat user input as untrusted data!

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

บทเรียน “การแทรกคำสั่งและโค้ด” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแทรกคำสั่งและโค้ด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Secure Coding & OWASP Top 10 for Backend ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Secure Coding & OWASP Top 10 for Backend มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแทรกคำสั่งและโค้ด”

เรียนรู้การระบุและลดช่องโหว่ที่เกี่ยวข้องกับการแทรกคำสั่ง OS และการเรียกใช้โค้ดตามอำเภอใจในระบบแบ็กเอนด์ คุณปฏิบัติ Secure Coding & OWASP Top 10 for Backend ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Secure Coding & OWASP Top 10 for Backend หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Secure Coding & OWASP Top 10 for Backend บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การแทรกคำสั่งและโค้ด” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Secure Coding & OWASP Top 10 for Backend นี้ได้ไหม

ได้ บทเรียน Secure Coding & OWASP Top 10 for Backend ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การป้องกัน SQL Injection
  2. การแทรกคำสั่งและโค้ด
  3. Cross-Site Scripting (XSS) ในแบ็กเอนด์
  4. การป้องกันการแทรกคำสั่ง XML และ LDAP
← กลับไปที่ Secure Coding & OWASP Top 10 for Backend