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

การป้องกัน SQL Injection

ทำความเข้าใจการทำงานของ SQL Injection และใช้การป้องกันที่แข็งแกร่งด้วยคำค้นแบบระบุพารามิเตอร์และคำสั่งที่เตรียมไว้ล่วงหน้า

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

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

What is SQL Injection?

Imagine you're talking to a database using a special language called SQL. Sometimes, bad actors can trick your application into sending unexpected SQL commands to the database.

This trick is called SQL Injection (SQLi). It happens when user input is treated as part of the SQL command itself, rather than just data.

SQLi can lead to:

  • Data theft or modification
  • Bypassing login screens
  • Gaining control over the database server

How SQLi Works: A Login Example

Let's say a login form takes your username and password. The application might build a SQL query like this to check if you exist:

SELECT * FROM users WHERE username = 'your_username' AND password = 'your_password';

What if 'your_username' isn't just a name, but also a piece of SQL code?

The Vulnerable Code

A common mistake is building SQL queries by directly combining (concatenating) user input with the query string. Here's a simplified example in Java:

public class Main {
  public static void main(String[] args) {
    String username = "admin"; // User input
    String password = "pass123"; // User input
    
    // DANGEROUS: String concatenation
    String query = "SELECT * FROM users WHERE username = '" + username + "' AND password = '" + password + "';";
    
    System.out.println("Executing query:\n" + query);
    // In a real app, this query would go to the database.
  }
}

Crafting the Malicious Input

Now, imagine a malicious user enters this as their username:

admin' OR '1'='1

And any text for the password. When combined with the vulnerable query, it becomes:

SELECT * FROM users WHERE username = 'admin' OR '1'='1' AND password = 'any_password';

The OR '1'='1' part makes the condition always true, effectively bypassing the password check and logging the attacker in!

Introducing Prepared Statements

The best defense against SQL Injection is using Prepared Statements (also known as Parameterized Queries).

They separate the SQL logic from the user-provided data. Instead of directly inserting input, you use placeholders (like ?) in your query.

The database then understands that anything provided for these placeholders is pure data, not part of the command.

Prepared Statements in Action

Here's how you'd use a prepared statement to safely query for a user by their ID. Notice the ? placeholder.

public class Main {
  public static void main(String[] args) {
    int userId = 123; // User input
    
    // Secure: Using a placeholder for the value
    String sql = "SELECT name, email FROM users WHERE id = ?;";
    
    // In a real application, you'd 'prepare' this SQL
    // and then 'set' the parameter value (userId) separately.
    System.out.println("Prepared SQL: " + sql);
    System.out.println("Parameter 1 (id): " + userId);
    // The database treats 'userId' as a value, not SQL code.
  }
}

How Parameters Prevent SQLi

When you use a prepared statement:

  • Separation of concerns: The SQL query structure is sent to the database first.
  • Data vs. Code: The database 'pre-compiles' the query. Then, when you provide values for the placeholders, it treats them strictly as data.
  • Automatic Escaping: The database automatically handles any special characters in your input, preventing them from being interpreted as SQL commands.

This ensures that malicious input like ' OR '1'='1 cannot alter the query's intent.

Securing the Login Example

Let's fix our vulnerable login example using prepared statements. Now, even if a user tries to inject SQL, it will be treated as part of their username/password, not as a command.

public class Main {
  public static void main(String[] args) {
    String username = "admin' OR '1'='1"; // Malicious input
    String password = "any_password"; // Malicious input
    
    // SECURE: Using prepared statement with placeholders
    String sql = "SELECT * FROM users WHERE username = ? AND password = ?;";
    
    // Simulating how parameters are set (conceptual)
    System.out.println("Prepared SQL: " + sql);
    System.out.println("Parameter 1 (username): " + username);
    System.out.println("Parameter 2 (password): " + password);
    
    System.out.println("\nDatabase will look for a user with the literal username 'admin' OR '1'='1' and the given password. This user likely won't exist. Attack thwarted!");
  }
}

Beyond Login: All SQL Operations

Prepared statements aren't just for SELECT queries. You should use them for ALL SQL operations that involve user input:

  • INSERT statements (e.g., adding a new user)
  • UPDATE statements (e.g., changing a user's profile)
  • DELETE statements (e.g., removing data)

Always assume user input is malicious until proven otherwise. Parameterized queries are your first line of defense.

Quick Check: Secure Query

Which of the following code snippets correctly uses parameterized queries to prevent SQL Injection when searching for a product by name?

Recap: SQL Injection Prevention

Great job! In this lesson, you learned about:

  • What SQL Injection is and its dangers.
  • How vulnerable applications can be exploited by concatenating user input directly into SQL queries.
  • The importance of using Prepared Statements (Parameterized Queries) as the primary defense mechanism.
  • How prepared statements separate data from code, preventing malicious input from altering query logic.

Always use prepared statements for any SQL operation involving user input to keep your backend secure!

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

บทเรียน “การป้องกัน SQL Injection” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การป้องกัน SQL Injection”

ทำความเข้าใจการทำงานของ SQL Injection และใช้การป้องกันที่แข็งแกร่งด้วยคำค้นแบบระบุพารามิเตอร์และคำสั่งที่เตรียมไว้ล่วงหน้า คุณปฏิบัติ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การป้องกัน SQL Injection” ใช้เวลานานแค่ไหน

บทเรียน 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