WordPress โดนแฮกบ่อยแค่ไหน
ผมดูแล WordPress sites มากกว่า 100 sites ตลอด 20 ปีที่ผ่านมาและเจอ sites ถูกแฮกมาแล้วหลายสิบครั้ง 90% ของการถูกแฮกเกิดจาก 3 สาเหตุ plugins/themes ที่ไม่ได้ update, weak passwords และ file permissions ที่ผิด WordPress เป็น CMS ที่ถูกโจมตีมากที่สุดในโลกเพราะมี market share กว่า 43% ของเว็บทั้งหมด
บทความนี้จะสอน security hardening ที่ผมใช้จริงกับทุก WordPress site ที่ดูแลทุกขั้นตอนทดสอบแล้วกับ WordPress 6.x บน Ubuntu 24.04 + Nginx + PHP 8.3
Security Keys และ Salts
// wp-config.php — เปลี่ยน Security Keys ทุก 6 เดือน
// Generate ใหม่จาก: https://api.wordpress.org/secret-key/1.1/salt/
define('AUTH_KEY', 'ใส่ค่าใหม่ที่ generate จาก WordPress.org');
define('SECURE_AUTH_KEY', 'ใส่ค่าใหม่ที่ generate จาก WordPress.org');
define('LOGGED_IN_KEY', 'ใส่ค่าใหม่ที่ generate จาก WordPress.org');
define('NONCE_KEY', 'ใส่ค่าใหม่ที่ generate จาก WordPress.org');
define('AUTH_SALT', 'ใส่ค่าใหม่ที่ generate จาก WordPress.org');
define('SECURE_AUTH_SALT', 'ใส่ค่าใหม่ที่ generate จาก WordPress.org');
define('LOGGED_IN_SALT', 'ใส่ค่าใหม่ที่ generate จาก WordPress.org');
define('NONCE_SALT', 'ใส่ค่าใหม่ที่ generate จาก WordPress.org');
Disable File Editing และ Security Settings
// ปิด theme/plugin editor ใน Dashboard
define('DISALLOW_FILE_EDIT', true);
// ปิด plugin/theme installation จาก Dashboard
define('DISALLOW_FILE_MODS', true);
// Force SSL สำหรับ admin
define('FORCE_SSL_ADMIN', true);
// เปลี่ยน database prefix (ตอนติดตั้ง)
$table_prefix = 'wp_s3cur3_';
// ซ่อน WordPress version
remove_action('wp_head', 'wp_generator');
// จำกัด post revisions
define('WP_POST_REVISIONS', 5);
// ปิด debug ใน production
define('WP_DEBUG', false);
define('WP_DEBUG_LOG', false);
define('WP_DEBUG_DISPLAY', false);
ย้าย wp-config.php ขึ้น 1 directory
# WordPress อ่าน wp-config.php จาก parent directory ได้อัตโนมัติ
mv /var/www/html/wp-config.php /var/www/wp-config.php
chmod 440 /var/www/wp-config.php
chown www-data:www-data /var/www/wp-config.php
ตั้ง File Permissions ที่ถูกต้อง
#!/bin/bash
# fix-wp-permissions.sh
WP_ROOT="/var/www/html"
WP_OWNER="www-data"
WP_GROUP="www-data"
# ตั้ง ownership
chown -R :
# Directories: 755
find -type d -exec chmod 755 {} \;
# Files: 644
find -type f -exec chmod 644 {} \;
# wp-config.php: 440 (read-only)
chmod 440 /wp-config.php 2>/dev/null
# .htaccess: 444 (read-only)
chmod 444 /.htaccess 2>/dev/null
# wp-content/uploads: 755 (writable)
chmod -R 755 /wp-content/uploads
echo "Permissions fixed for "
Nginx Security Rules
# /etc/nginx/snippets/wordpress-security.conf
# Block access to sensitive files
location ~* /(?:xmlrpc\.php|wp-trackback\.php)$ {
deny all;
return 403;
}
location ~ /\.ht {
deny all;
}
location ~ wp-config\.php {
deny all;
}
# Block PHP execution in uploads
location ~* /wp-content/uploads/.*\.php$ {
deny all;
}
# Block PHP execution in plugins/themes (ยกเว้น index.php)
location ~* /wp-content/(?:plugins|themes)/.*\.php$ {
deny all;
}
# Block access to wp-includes
location ~* /wp-includes/.*\.php$ {
deny all;
}
# Limit request methods
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 405;
}
เปลี่ยน Login URL
# ใช้ plugin WPS Hide Login หรือตั้งค่า Nginx
# เปลี่ยน /wp-admin/ และ /wp-login.php เป็น URL ลับ
# Nginx: redirect wp-login.php ไป 404
location = /wp-login.php {
# อนุญาตเฉพาะ IP ที่กำหนด
allow 10.100.0.0/24; # VPN subnet
deny all;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
Two-Factor Authentication
# ติดตั้ง plugin: Two Factor Authentication
# หรือ WP 2FA
# wp plugin install wp-2fa --activate
# กำหนดให้ทุก admin ต้องใช้ 2FA
# Settings > Two Factor Authentication
# - Force 2FA for: Administrator, Editor
# - Methods: TOTP (Google Authenticator), Email
Limit Login Attempts
# ใช้ Fail2ban สำหรับ WordPress (server level)
# /etc/fail2ban/filter.d/wordpress.conf
[Definition]
failregex = ^ .* "POST /wp-login.php
^ .* "POST /xmlrpc.php
ignoreregex =
# /etc/fail2ban/jail.d/wordpress.conf
[wordpress]
enabled = true
port = http, https
filter = wordpress
logpath = /var/log/nginx/access.log
maxretry = 5
bantime = 3600
findtime = 600
สำหรับรายละเอียด Fail2ban configuration ดูบทความ Fail2ban Security Setup ของผม
Security Headers
# /etc/nginx/snippets/security-headers.conf
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' *.google-analytics.com *.googletagmanager.com; style-src 'self' 'unsafe-inline' fonts.googleapis.com; font-src 'self' fonts.gstatic.com; img-src 'self' data: *.gravatar.com *.wp.com;" always;
# ใช้ใน server block
server {
listen 443 ssl http2;
server_name example.com;
include snippets/security-headers.conf;
include snippets/wordpress-security.conf;
# ...
}
Automated WordPress Backup
#!/bin/bash
# /usr/local/bin/wp-backup.sh
SITE_DIR="/var/www/html"
DB_NAME="wordpress"
DB_USER="wp_user"
DB_PASS="DbSecurePass2026!"
BACKUP_DIR="/backup/wordpress"
DATE=$(date +%Y%m%d_%H%M)
RETENTION=30
mkdir -p ""
# Backup database
mysqldump --single-transaction \
-u "" -p"" "" \
| gzip > "/db-.sql.gz"
# Backup files
tar czf "/files-.tar.gz" \
--exclude="/wp-content/cache" \
""
# Upload to offsite storage
rclone copy "/db-.sql.gz" b2-encrypted:wp-backup/
rclone copy "/files-.tar.gz" b2-encrypted:wp-backup/
# Cleanup old backups
find "" -name "*.gz" -mtime + -delete
echo "Backup completed: "
# Crontab: 0 3 * * * /usr/local/bin/wp-backup.sh
สำหรับ offsite backup ด้วย Rclone ดูบทความ Rclone Cloud Sync ของผม
แนวทางป้องกันภัยไซเบอร์สำหรับองค์กรไทย
ภัยคุกคามทางไซเบอร์ในปี 2026 มีความซับซ้อนมากขึ้น Ransomware ยังคงเป็นภัยอันดับหนึ่ง โดยผู้โจมตีใช้ AI ช่วยสร้าง Phishing Email ที่แนบเนียนขึ้น องค์กรควรมี Multi-Layered Security ตั้งแต่ Perimeter Defense ด้วย Next-Gen Firewall Endpoint Protection ด้วย EDR Solution และ Network Detection and Response
การฝึกอบรมพนักงานเป็นสิ่งสำคัญที่สุด เพราะ Human Error เป็นสาเหตุหลักของการรั่วไหลข้อมูล ควรจัด Security Awareness Training อย่างน้อยไตรมาสละครั้ง ทำ Phishing Simulation ทดสอบพนักงาน และมี Incident Response Plan ที่ชัดเจน ฝึกซ้อมเป็นประจำ
สำหรับกฎหมาย PDPA ของไทย องค์กรต้องมี Data Protection Officer แจ้งวัตถุประสงค์การเก็บข้อมูลอย่างชัดเจน ขอ Consent ก่อนใช้ข้อมูลส่วนบุคคล มีมาตรการรักษาความปลอดภัยที่เหมาะสม และแจ้งเหตุ Data Breach ภายใน 72 ชั่วโมง
เปรียบเทียบข้อดีและข้อเสีย
จากตารางเปรียบเทียบจะเห็นว่าข้อดีมีมากกว่าข้อเสียอย่างชัดเจน โดยเฉพาะในแง่ของประสิทธิภาพและความสามารถในการ Scale สำหรับข้อเสียส่วนใหญ่สามารถแก้ไขได้ด้วยการเรียนรู้อย่างเป็นระบบและวางแผนทรัพยากรให้เหมาะสม
Plugin security ตัวไหนดี Wordfence หรือ Sucuri?
ทั้งคู่ดีครับ Wordfence มี firewall ที่ทำงานใน PHP level มี malware scanner ที่ดีมากฟรี version ก็ใช้ได้ดี Sucuri เน้น cloud-based WAF ที่ทำงานก่อนถึง server ดีกว่าในเรื่อง DDoS protection ผมใช้ Wordfence สำหรับ sites ทั่วไปและ Sucuri สำหรับ sites ที่มี traffic สูงหรือเคยถูก DDoS
xmlrpc.php จำเป็นไหมควรปิดีไหม?
ถ้าไม่ใช้ Jetpack, WordPress mobile app หรือ pingbacks ให้ปิดเลย xmlrpc.php เป็นช่องทางที่ถูกโจมตีบ่อยมากทั้ง brute force login และ DDoS amplification ปิดง่ายที่สุดคือ block ใน Nginx/Apache หรือใช้ plugin Disable XML-RPC
WordPress ควร update อัตโนมัติไหม?
สำหรับ minor updates (เช่น 6.4.1 → 6.4.2) ควร auto-update เพราะเป็น security patches สำหรับ major updates (เช่น 6.4 → 6.5) ควร update manual หลังทดสอบบน staging ก่อนสำหรับ plugins ผมแนะนำ auto-update เฉพาะ plugins ที่ trust ได้และ monitor หลัง update เสมอ
// เปิด auto-update สำหรับ minor versions (default)
define('WP_AUTO_UPDATE_CORE', 'minor');
// เปิด auto-update สำหรับ plugins
add_filter('auto_update_plugin', '__return_true');
// เปิด auto-update สำหรับ themes
add_filter('auto_update_theme', '__return_true');
โดนแฮกแล้วทำยังไง?
ขั้นตอนที่ผมใช้คือ 1) Take site offline ทันที 2) เปลี่ยน passwords ทั้งหมด (WP admin, database, FTP, SSH) 3) Scan ด้วย Wordfence หรือ grep -r "eval(base64" /var/www/html/ 4) Compare files กับ WordPress core ด้วย wp core verify-checksums 5) Restore จาก clean backup 6) Update WordPress, plugins, themes ทั้งหมด 7) ตรวจสอบ Lynis security audit ของ server
สรุป
WordPress Security Hardening ไม่ยากแต่ต้องทำอย่างเป็นระบบเริ่มจาก wp-config.php hardening, file permissions ที่ถูกต้อง, block xmlrpc.php, จำกัด login attempts ด้วย Fail2ban, เพิ่ม security headers, บังคับ 2FA สำหรับ admin และ backup ทุกวัน
สิ่งที่สำคัญที่สุดคือ update WordPress, plugins และ themes สม่ำเสมอ 90% ของ WordPress ที่ถูกแฮกเกิดจากไม่ update
