ก๊วนซอฟท์แวร์ </softganz> SoftGang (Gang Software)

Web &amp; Software Developer Gang.

วิธีการตั้งค่าให้ DirectAdmin Block IP อัตโนมัติเมื่อตรวจพบการ Brute Force บน Debian 6 64-bit

by Little Bear @17 พ.ย. 54 01:43 ( IP : 122...110 ) | Tags : DirectAdmin , Debian

DirectAdmin ตั้งแต่เวอร์ชัน 1.39 เป็นต้นไป ได้เพิ่มความสามารถในการตรวจสอบการ Brute Force หรือการเดาสุ่มรหัสผ่านไปเรื่อยๆ (รายละเอียด) แต่สิ่งหนึ่งที่ DirectAdmin ไม่ได้ทำมาให้คือ ทำการแบนไอพีที่มา Brute Force เราโดยอัตโนมัติ ทำให้ DirectAdmin ทำได้เพียงแค่แจ้งเตือนว่ามีไอพีใด Brute Force เครื่องเราเรื่อยๆ โดยไม่ได้มีประโยชน์อันใดเพิ่มขึ้นมา (นอกจากทำให้รำคาญอีเมลแจ้งเตือน) เพราะฉะนั้นบทความนี้จะมาบอกถึงขั้นตอนทำให้ DirectAdmin แบนไอพีที่มา Brute Force โดยอัตโนมัติ โดยลินุกซ์ดิสโทรที่ผมใช้คือ Debian 6 64-bit ถ้าใช้ CentOS ลองทำตามขั้นตอนที่นี่ดูครับ

ขั้นแรกให้ลง iptables ก่อนด้วยคำสั่ง

apt-get install iptables

จากนั้นเราจะสร้างสคริปต์ blockip.sh ไว้ที่ /usr/local/directadmin/scripts/custom/blockip.sh โดยมีเนื้อหาตามนี้ครับ

#666666; font-style: italic;">#!/bin/sh

curriptables#7a0874; font-weight: bold;">(#7a0874; font-weight: bold;">) #7a0874; font-weight: bold;">{         #7a0874; font-weight: bold;">echo #ff0000;">"<br><br><textarea cols=160 rows=60>";         #000000; font-weight: bold;">/sbin#000000; font-weight: bold;">/iptables #660033;">-nL

        #7a0874; font-weight: bold;">echo #ff0000;">"</textarea>"; #7a0874; font-weight: bold;">}
#666666; font-style: italic;">### Make sure it's not already blocked #007800;">COUNT=#000000; font-weight: bold;"></span><span style="color: <a class="hashtag" href="/tags/c20cb9">#c20cb9</a>; font-weight: bold;">grep</span> <span style="color: <a class="hashtag" href="/tags/660033">#660033</a>;">-c</span> <span style="color: <a class="hashtag" href="/tags/007800">#007800</a>;">$ip</span> <span style="color: <a class="hashtag" href="/tags/000000">#000000</a>; font-weight: bold;">/</span>etc<span style="color: <a class="hashtag" href="/tags/000000">#000000</a>; font-weight: bold;">/</span>network<span style="color: <a class="hashtag" href="/tags/000000">#000000</a>; font-weight: bold;">/</span>iptables.save<span style="color: <a class="hashtag" href="/tags/000000">#000000</a>; font-weight: bold;">;

#000000; font-weight: bold;">if #7a0874; font-weight: bold;">[ #ff0000;">"#007800;">$COUNT" #660033;">-ne #000000;">0 #7a0874; font-weight: bold;">]; #000000; font-weight: bold;">then         #7a0874; font-weight: bold;">echo #ff0000;">"#007800;">$ip already exists in iptables (#007800;">$COUNT). Not blocking.";         curriptables         #7a0874; font-weight: bold;">exit #000000;">2;

#000000; font-weight: bold;">fi
#7a0874; font-weight: bold;">echo #ff0000;">"Adding #007800;">$ip to iptables...<br>"; #000000; font-weight: bold;">/sbin#000000; font-weight: bold;">/iptables #660033;">-I INPUT #660033;">-s #007800;">$ip #660033;">-j DROP

#000000; font-weight: bold;">/sbin#000000; font-weight: bold;">/iptables-save #000000; font-weight: bold;">> #000000; font-weight: bold;">/etc#000000; font-weight: bold;">/network#000000; font-weight: bold;">/iptables.save
#7a0874; font-weight: bold;">echo #ff0000;">"<br><br>Result:"; curriptables

#7a0874; font-weight: bold;">exit #000000;">0;




เสร็จแล้วรันคำสั่ง

chmod 700 /usr/local/directadmin/scripts/custom/block_ip.sh

ซึ่งสคริปต์นี้จะทำให้เราสามารถสั่ง block ip ผ่านหน้าจัดการใน DirectAdmin ได้ โดยให้ไปที่เมนู Brute Force Monitor แล้วกดลิงก์ IP Info ของไอพีที่เราต้องการบล็อก หลังจากนั้นให้กดปุ่ม Block This IP ที่ด้านล่างสุด

ถ้าต้องการให้ iptables ถูก restore ข้อมูลใหม่ทุกครั้งที่ reboot เครื่อง ให้ใส่

post-up iptables-restore /etc/network/iptables.save

ลงไปที่ด้านท้ายของไฟล์ /etc/network/interfaces

ถ้าต้องการให้ DirectAdmin ทำการ block ip อัตโนมัติ โดยไม่ต้องมากดปุ่ม Block This IP เอง ให้สร้างไฟล์ /usr/local/directadmin/scripts/custom/bruteforcenotice_ip.sh และใส่เนื้อหาตามนี้

#!/bin/sh
SCRIPT=/usr/local/directadmin/scripts/custom/block_ip.sh
ip=$value $SCRIPT
exit $?;

เสร็จแล้วรันคำสั่ง

chmod 700 /usr/local/directadmin/scripts/custom/brute_force_notice_ip.sh

โดยจำนวนการ Brute Force ก่อนที่จะโดน block ip นั้น สามารถตั้งได้ที่เมนู Administrator Settings > Notify Admins after an IP has และใส่จำนวนที่ต้องการลงไปครับ

ถ้าต้องการยกเลิก block ip อาจจะต้องเข้ามาพิมพ์คำสั่งยกเลิกด้วยตัวเองไปก่อนครับ ด้วยคำสั่งนี้

iptables -D INPUT -s xxx.xxx.xxx.xxx -j DROP
iptables-save > /etc/network/iptables.save

เปลี่ยน xxx.xxx.xxx.xxx เป็น ip ที่เราต้องการยกเลิก block

ที่มา: วิธีการตั้งค่าให้ DirectAdmin Block IP อัตโนมัติเมื่อตรวจพบการ Brute Force บน Debian 6 64-bit , I wish to have a block_ip.sh so I can block IPs through DirectAdmin , How to block an IP in Linux