Open Source How-To

You are here: Home > 3. Ubuntu 16.04 Server > การตั้งค่า fail2ban block brute force wordpress

การตั้งค่า fail2ban block brute force wordpress

 
 

บันทึกนี้ปรับปรุงล่าสุดเมื่อวันที่ 24-08-2559
ดูแลโดย WIBOON

  • ทดสอบกับ fail2ban เวอร์ชั่น 0.9.3-1
  • การตั้งค่า fail2ban block brute force wordpress จะใช้วิธีเขียนไฟล์ filter แล้วเปิดใช้งาน filter ในไฟล์ jail.local
  • ไฟล์ filter จะอยู่ใน /etc/fail2ban/filter.d เช่น wordpress-hard.conf หรือ wp-login.conf เป็นต้น
  • อ่านเจอในอินเทอร์เน็ต พบว่ามีอยู่ 2 วิธี คือ
  • วิธีที่ 1 สร้างไฟล์ filter ตามความเข้าใจว่า access.log ที่เกิดขึ้นของ wordpress ต้องมีคำว่า wp-login และถ้าพบซ้ำ ๆ กัน 5 ครั้ง ก็ถือว่าพยายามเดา username/password
  • วิธีที่ 2 จะได้ไฟล์ filter จากการติดตั้ง wordpress plugin ชื่อว่า wp-fail2ban



มาดูรายละเอียดกัน

วิธีที่ 1 เขียน filter เอง
ขั้นตอน
1.สร้างไฟล์ filter เอง ตั้งชื่อว่า wp-login.conf ไว้ใน /etc/fail2ban/filter.d

mama@ubuntu:~$ sudo vi /etc/fail2ban/filter.d/wp-login.conf
# WP brute force attacks filter
[Definition]
failregex = ^<HOST> .* "POST .*wp-login.php
ignoreregex =

 

2.ทดสอบไวยกรณ์ของไฟล์ filter
ในตัวอย่างนี้ apache2 web server log อยู่ที่ /var/log/apache2/access.log

mama@ubuntu:~$ fail2ban-regex /var/log/apache2/access.log /etc/fail2ban/filter.d/wp-login.conf  --print-all-matched

 

3. เมื่อทดสอบผ่าน ก็ให้เพิ่มต่อท้ายไฟล์ jail.local ด้วยข้อความดังนี้

mama@ubuntu:~$ sudo vi /etc/fail2ban/jail.local
[wp-login]
enabled = true
filter = wp-login
logpath = /var/log/apache2/access.log
maxretry = 5
port = http,https

 

4.สั่ง restart fail2ban และตรวจสอบ status ว่า fail2ban สำเร็จหรือไม่

sudo systemctl restart fail2ban.service
sudo systemctl status fail2ban.service

 

Reference:
Keep WordPress Safe From Brute Force Attacks with Fail2ban
https://1fix.io/blog/2015/01/10/wordpress-brute-force-attacks-fail2ban/


วิธีที่ 2 ติดตั้ง wordpress plugin ชื่อ wp-fail2ban และ สั่ง activate ให้ทำงาน
ขั้นตอน

1.ให้ copy ไฟล์ wordpress-hard.conf ไปไว้ใน /etc/fail2ban/filter.d

mama@ubuntu:~$ sudo cp /var/www/html/wp-content/plugins/wp-fail2ban/filters.d/wordpress-hard.conf /etc/fail2ban/filter.d/

 

2.ทดสอบไวยกรณ์ของไฟล์ filter
ตัวอย่างนี้ wp fail2ban plugin นี้ จะเขียนข้อมูลการถูกเดา username/password ไว้ที่ /var/log/auth.log

mama@ubuntu:~$ fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/wordpress-hard.conf  --print-all-matched

 

3.เมื่อทดสอบผ่าน ก็ให้เพิ่มต่อท้ายไฟล์ jail.local ด้วยข้อความดังนี้

mama@ubuntu:~$ sudo vi /etc/fail2ban/jail.local
[wordpress-hard]
enabled = true
filter = wordpress-hard
logpath = /var/log/auth.log
maxretry = 5
port = http,https

 

4.สั่ง restart fail2ban และตรวจสอบ status ว่า fail2ban สำเร็จหรือไม่

sudo systemctl restart fail2ban.service
sudo systemctl status fail2ban.service

 

Reference:
How To Protect WordPress with Fail2Ban on Ubuntu 14.04
https://www.digitalocean.com/community/tutorials/how-to-protect-wordpress-with-fail2ban-on-ubuntu-14-04


ถ้าจำเป็น ในบางครั้งเราต้องการให้ server ของเรา ส่ง log ไปให้ Firewall เพื่อ block ด้วย
ให้ทำต่อโดยอ่านที่เรื่องนี้ การตั้งค่า fail2ban ส่ง log ให้กับ remote syslog server

nach oben