Blokir SSH Brute Force dengan PF
SSH brute force merupakan salah cara yang umum dipakai oleh orang-orang jahil agar dapat masuk ke dalam server kita. Orang - orang jahil tersebut akan berusaha masuk ke dalam server kita dengan cara menebak-nebak user dan password yang ada di dalam server. Bisa dibilang SSH brute force ini cara yang untung-untungan. Berhasil masuk ya alhamdulillah, ga berhasil masuk ya dicoba terus nge-brute force
Berikut tips yang saya ambil dari halaman http://home.earthlink.net/~valiantsoul/pf.html akan menjelaskan bagaimana caranya mengeblok IP address tersangka SSH brutefoce. Tips tersebut sudah saya coba dan berhasil. Server tempat saya nyoba menggunakan OS FreeBSD 8.0-RELEASE dan menggunakan firewall PF
Enable firewall PF di FreeBSD
Secara default, firewall PF di disable. Untuk menyalakan firewall yang harus dilakukan adalah me-load module kernel pf.ko dengan cara mengetikkan perintah (sebagai user root) :
# kldload pf.ko
Agar PF selalu nyala setiap kali booting, tambahkan baris berikut di /etc/rc.conf
pf_enable="YES"
pf_rules="/etc/pf.conf"
Konfigurasi file /etc/pf.conf
Tambahkan baris berikut di /etc/pf.conf
table <bruteforce> persist file "/var/db/blacklist"
block quick from <bruteforce>
pass in quick on em0 proto tcp from any to any port 22 keep state
em0 ini merupakan network interface server saya. jadi kalo misalnya network interface anda bukan em0 ya disesuaikan aja konfigurasi pf.conf nya
Buat tabel bruteforce
Buat file bruteforce di direktori /var/db/ dan set permissionnya jadi 644
#touch /var/db/bruteforce
#chmod 644 /var/db/bruteforce
Skrip untuk nge-blok IP address 'nakal'
Buatlah file, misalnya dengan nama blok.pl di direktori yang diinginkan, misalnya di /etc/
Masukkan skrip berikut ke dalam file /etc/blok.pl
#!/usr/bin/perluse strict;
my @assholes = ();
open (IN, "/var/log/auth.log");
while (<IN>) {
if ($_ =~ /Invalid user.*from ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) {
push(@assholes, $1);
}
if ($_ =~ /Did not receive identification string from ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) {
push(@assholes, $1);
}
}
close (IN);
@assholes = sort {lc($a) cmp lc($b)} @assholes;
my @allowedIPs = ();
open (IN, "/var/db/allowed-ips");
while (<IN>) {
if ($_ =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) {
push(@allowedIPs, $_);
}
}
close (IN);
chop(@allowedIPs);
my $tmp = "";
foreach my $asshole (@assholes) {
if ($asshole eq $tmp) {
$asshole = "";
} else {
if ($asshole =~ /127\.0\.0\.1/) {
$asshole = "";
}
if ($asshole =~ /192\.168\.[0-9]+\.[0-9]+/) {
$asshole = "";
}
foreach my $allowedIP (@allowedIPs) {
if ($asshole =~ /$allowedIP/) {
$asshole = "";
}
}
$tmp = $asshole;
}
}
@assholes = sort {lc($b) cmp lc($a)} @assholes;
my $popCount = 0;
foreach my $asshole (reverse @assholes) {
if ($asshole eq "") {
$popCount++;
}
}
for (my $i = 0; $i < $popCount; $i++) {
pop (@assholes);
}
my $list = "";
foreach my $asshole (@assholes) {
$list = $list . $asshole . " ";
}
exec "/sbin/pfctl -t bruteforce -T add $list";
Edit file /etc/syslog.conf
Ganti baris :
auth.info;authpriv.info /var/log/auth.log
dengan :
auth.info;authpriv.info | exec /usr/bin/perl /etc/blok.pl
Setelah itu restart syslogd dengan cara :
# /etc/rc.d/syslogd restart
Selesai dehhhh....:D
untuk melihat IP mana saja yang sudah di blok, jalankan perintah berikut :
# pfctl -t bruteforce -T show
Saat saya menulis ini, output dari perintah diatas adalah sebagai berikut :
No ALTQ support in kernel
ALTQ related functions disabled
118.129.170.103
221.7.196.68
Artinya sudah ada 2 IP address yang diblok untuk akses ssh ke server saya
LANGKAH PERTAMA






. Dulu sih pernah mau bikin blog, tapi karena suatu masalah klasik yaitu males, ya blog-nya ga jadi- jadi
. Jadi ini adalah postingan pertama saya di blog gue yang pertama pula. Semoga postingan saya akan terus bertambah terussss.