SQL injection
When input becomes a command
- Many apps build a database query by gluing the user's input into a string. That is dangerous.
- If an attacker types SQL as their input, it can become part of the query. This is SQL injection — the most famous web attack.
เมื่ออินพุตกลายเป็นคำสั่ง
- แอปพลิเคชันหลายตัวสร้าง query ฐานข้อมูล bằng การเย็บอินพุตของผู้ใช้ เข้ากับสตริง ذلكอันตราย
- หากผู้โจมตีพิมพ์ SQL เป็นอินพุต มันอาจเป็นส่วนหนึ่งของ query นี่คือ SQL injection — การโจมตีเว็บที่มีชื่อเสียงที่สุด
See the attack
- Imagine a login that checks
... WHERE name = '<whatever you typed>'. - An attacker types
' OR '1'='1as the name. The query becomes:
'1'='1'is always true, so the database returns every user. The login is bypassed. Run it and see.
เห็นการโจมตี
- จินตนาการถึงการเข้าสู่ระบบที่ตรวจสอบ
... WHERE name = '<whatever you typed>' - ผู้โจมตีพิมพ์
' OR '1'='1เป็นชื่อ การสืบค้นจะกลายเป็น:
SELECT * FROM users WHERE name = '' OR '1'='1';
'1'='1'เป็น จริงเสมอ ดังนั้นฐานข้อมูลจึงส่งกลับผู้ใช้ ทั้งหมด การเข้าสู่ระบบถูกข้ามผ่าน ลองรันดูและสังเกตผลลัพธ์
The fix: parameterised queries
- Never glue user input into SQL. Use parameterised queries (also called prepared statements).
- The database treats the input strictly as a value, never as code — so
' OR '1'='1is just a (failed) name to look up. - Also apply least privilege: the web app's database account should only do what it needs.
วิธีแก้ไข: การสืบค้นแบบใช้พารามิเตอร์ (Parameterised queries)
- ห้าม นำข้อมูลจากผู้ใช้มาต่อกับ SQL โดยตรง ให้ใช้ การสืบค้นแบบใช้พารามิเตอร์ (หรือเรียกว่า statement ที่เตรียมไว้ล่วงหน้า/prepared statements) แทน
- ฐานข้อมูลจะ看待ข้อมูลนั้นเป็น ค่า อย่างเคร่งครัด ไม่ใช่โค้ด — ดังนั้น
' OR '1'='1จึงเป็นเพียง (ชื่อที่ไม่พบ) ที่กำลังถูกค้นหาเท่านั้น - ยังควรใช้หลักการ สิทธิ์ต่ำสุด (least privilege): บัญชีฐานข้อมูลของเว็บแอปพลิเคชันควรมีสิทธิ์ทำได้เฉพาะสิ่งที่จำเป็นเท่านั้น
Your turn
- Below, write a precise, safe query that returns only bob by his
id. That is the spirit of a parameterised lookup.
Covers: A-Level data security; web application security.
ถึงเวลาฝึกฝน
- ด้านล่างนี้ ให้เขียนการสืบค้นที่แม่นยำและปลอดภัยซึ่งส่งกลับเฉพาะ bob โดยใช้
idของเขา นั่นคือจิตวิญญาณของการค้นหาด้วยพารามิเตอร์
ครอบคลุม: ความปลอดภัยของข้อมูลระดับ A-Level; ความปลอดภัยของเว็บแอปพลิเคชัน
Common mistakes
- Never build a query by joining raw user input into the text.
- Use parameterised queries so input can never change the query.
ข้อผิดพลาดที่พบบ่อย
- ห้ามสร้างการสืบค้นโดยการนำข้อมูลดิบจากผู้ใช้มาต่อกับข้อความ
- ใช้การสืบค้นแบบใช้พารามิเตอร์เพื่อให้ข้อมูลเข้าไม่สามารถเปลี่ยนแปลงการสืบค้นได้
First, run the attack and see the damage. The app glued the attacker's input into the query, so the condition became name = '' OR '1'='1'. Complete the query exactly like that and see every user leak out. · ก่อนอื่น, รันการโจมตีเพื่อดูความเสียหาย แอปพลิเคชันนำอินพุตของผู้โจมตีมาต่อกับ query, ดังนั้นเงื่อนไขกลายเป็น name = '' OR '1'='1'. เติม query ให้ครบถ้วนตามนั้นเพื่อดูว่าผู้ใช้ ทุกคน รั่วไหลออกมา
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
A safe lookup uses a precise condition. Change the query to return only bob's row, by adding WHERE id = 2. · การค้นหาที่ปลอดภัยใช้เงื่อนไขที่แม่นยำ. เปลี่ยน query ให้แสดงเฉพาะแถวของ bob โดยเพิ่ม WHERE id = 2
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่