เขียน PHP ให้ปลอดภัยและป้องกันการถูกโจมตีจากเหล่าร้าย

แปะๆกันลืม สำหรับท่านทั้งหลายที่กำลังจะเป็นนักพัฒนาเว็บแอพพลิเคชัน จะต้องคำนึงถึงความปลอดภัยเป็นสำคัญ เราจะเขียน PHP ยังไงให้ปลอดภัย และลดช่องโหว่ที่จะถูกโจมตีจากผู้ไม่หวังดีหรือที่เรารู้จักกันคือเหล่า Hacker นั่นเอง

แปะๆกันลืม สำหรับท่านทั้งหลายที่กำลังจะเป็นนักพัฒนาเว็บแอพพลิเคชัน จะต้องคำนึงถึงความปลอดภัยเป็นสำคัญ เราจะเขียน PHP ยังไงให้ปลอดภัย และลดช่องโหว่ที่จะถูกโจมตีจากผู้ไม่หวังดีหรือที่เรารู้จักกันคือเหล่า Hacker นั่นเอง

ตรวจสอบอะไรบ้างก่อนจะเริ่ม PHP
1) / Cross Site Scripting (XSS)
2) / Injections
    / SQL Injection
    / Directory Traversal (Path Injection)
    / Command Injection
    / Code Injection
3) / Cross Site Request Forgery (XSRF/CSRF)
4) / Public Files
5) / Passwords
6) / Uploading Files
7) / Session Hijacking
8) / Remote File Inclusion
9) / PHP Configuration
    / Error Reporting
    / Exposing PHP Version
    / Remote Files
    / open_basedir
    / Session Settings
10) / Use HTTPS
11) / Things Not Listed

Cross Site Scripting (XSS)
การโจมตีแบบ XSS เกิดขึ้นเมื่อฝั่งของผู้ใช้งานรันโค้ดที่ Hacker สามารถส่งโค้ดไปรันโดยอาศัยช่องโหว่จากเว็บเรา มักจะใช้ Javascript ในการโจมตีเหยื่อ โดยการส่งโค้ดเข้าไปผ่าน Output ของ PHP script อีกที ไปดูตัวอย่างการโจมตีแบบ Cross Site Scripting กันครับ

Cross Site Scripting
Cross Site Scripting

ใช้ ENT_QUOTES ในการดัก single และ double quotes ที่มักจะส่งมาพร้อมกับแท็ก HTML
UTF-8 ใช้ได้กับ PHP 5.4 เป็นต้นมา ซึ่งปัจจุบันถูกตั้งค่าให้เป็นค่าเริ่มต้นแล้วจ้า ในบางบราวเซอร์บางอักขระจะต้องใช้ htmlspecialchars().

Injections
SQL Injection
เมื่อเราทำการเขียนสคริปต์เพื่อติดต่อฐานข้อมูล SQL Injections คือการอาศัยช่องโหว่ส่ง SQL statement เพื่อเข้าถึงฐานข้อมูลของเรา
อ่านเพิ่มเติม “What is SQL injection and how to prevent it?

$page = $_GET['page'] ?? 'home';

require $page;
// or something like this
echo file_get_contents('../pages/'.$page.'.php');
// Checking if the string contains parent directory
if (strstr($_GET['page'], '../') !== false) {
    throw new \Exception("Directory traversal attempt!");
}

// Checking remote file inclusions
if (strstr($_GET['page'], 'file://') !== false) {
    throw new \Exception("Remote file inclusion attempt!");
}

// Using whitelists of pages that are allowed to be included in the first place
$allowed = ['home', 'blog', 'gallery', 'catalog'];
$page = (in_array($page, $allowed)) ? $page : 'home';
echo file_get_contents('../pages/'.$page.'.php');

Command Injection
โปรดใช้ความระมัดระวังเมื่อจัดการกับคำสั่งการดำเนินงานฟังก์ชั่นและข้อมูลที่คุณไม่ไว้วางใจ

exec('rm -rf '.$GET['path']);

Code Injection
Code injection จะเกิดขึ้นได้กรณีที่แฮกเกอร์สามารถส่งคำสั่งเข้ามาในฟังก์ชัน eval() ครับท่านผู้ชม

eval('include '.$_GET['path']);

อ่านเพิ่มเติมสำหรับเช็คลิสต์อื่นๆ ได้ที่
https://wwphp-fb.github.io/faq/security/php-security-issues/

บทความที่เกี่ยวข้อง
เทคนิคเพิ่มเติม SQL
sql รวมหลายๆตารางเข้าด้วยกัน
PHP ติดต่อฐานข้อมูล

Leave a Reply

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *

This site uses Akismet to reduce spam. Learn how your comment data is processed.