Feel banana feel the freedom

September 13, 2008

Public Key Authentication 4 Passwordless

Filed under: Linux Security — yoner @ 4:24 am

Baru dapet ilmu baru nich dari Ikhsan, mau coba langsung diterapin, ya sesuai prinsip gwe lah bisa karena biasa.

Ya dari judulnya kan dah ketauan bahwa ini adalah sarana untuk SSH, SCP session agar bisa connect tanpa perlu password, ya biasa digunakan untuk network yang sudah terpercaya.

OK, langsung ke prakteknya aja deh, contoh 2 mesin :

sebagai SSH server : sandy

client SSH : yoner

Pastikan di SSH server bisa menerima public key authentication

Sebagai root, buka file /etc/ssh/sshd_config, bisa pake nano. gedit, dll pastikan baris berikut sudah ada dan di uncomment :

PubkeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keys

Pastikan di SSH client tau dimana letak private key

Sebagai root, buka file /etc/ssh/ssh_config, bisa pake nano. gedit, dll pastikan baris berikut sudah ada dan di uncomment :

IdentityFile ~/.ssh/identity
IdentityFile ~/.ssh/id_rsa
IdentityFile ~/.ssh/id_dsa

Buat public key dan private key

Pada mesin client, jalankan command berikut :

ssh-keygen -t dsa

Jika sukses harusnya keluar output :

   yoner@libels:~/.ssh$ ssh-keygen -t dsa
   Generating public/private dsa key pair.
   Enter file in which to save the key (/home/yoner/.ssh/id_dsa):
   Enter passphrase (empty for no passphrase):
   Enter same passphrase again:
   Your identification has been saved in /home/yoner/.ssh/id_dsa.
   Your public key has been saved in /home/yoner/.ssh/id_dsa.pub.
   The key fingerprint is:
   be:4d:36:7c:29:a9:e0:8a:c0:c5:d5:65:40:fe:97:4b yoner@libels

Karena kebutuhan wa cuma password authentication aja jadi passphrase gak perlu diisi.

Catatan : private (id_dsa) dan publik (id_dsa.pub) tersimpan di direktory .ssh

   yoner@libels:~$ cd .ssh
   yoner@libels:~/.ssh$ ls
   authorized_keys  id_dsa  id_dsa.pub  id_rsa  id_rsa.pub

Copy public key ssh server :

   yoner@libels:~$ cd .ssh
   yoner@libels:~/.ssh$ scp id_dsa.pub sandy@libels2:/home/surfer/.ssh
   sandy@libels2's password:
   id_dsa.pub   

Login to ssh server sandy, dan install public key

   sandy@libels2:~$ cd .ssh
   sandy@libels2:~/.ssh$ cat id_dsa.pub >> authorized_keys

Untuk pengetesan coba dari client ssh ke server jika sukses maka bisa masuk ke server tanpa perlu password, atau bisa juga test kirim file lewat scp.

Spesial thanks to : M. Ikhsan

http://outhereinthefield.wordpress.com/


September 12, 2008

Blocking SSH-Attack

Filed under: Linux Security — yoner @ 4:13 am

Beberapa hari belakangan ini server wa diserang terus pake ssh-attack brute force.
Dasar nih cracker rese banget bikin kerjaan tambahan aja but gwe, masa tiap pagi monitor dan blocking ip addressnya CAPEE DEHHHH !!!

Akhirnya gwe tanya sama om google, akhirnya dapet pencerahan deh.

Pertama-tama yaitu dengan mengedit file /etc/ssh/sshd_config
PermitRootLogin no /* ini biar agar tidak boleh login sebagai root */

Kalau mau aman sih disable aja password authentication untuk ssh, jadi user yg ssh ke user kita melalui private key authentication aja.

Tapi berhubung server tempat gwe gak memperbolehkan itu jadinya gwe cari alternatif lain.

Cara manual :
Pantengin log messagess : tail -f /var/log/messages
Kalo ada ip yg nyerang catet ipnya, lalu block ip tsb ( cape kalo yg nyerang ratusan ip), contoh ip crackernya 192.185.2.52
Command buat block ip : route add 192.185.2.52 gw 127.0.0.1 lo
Untuk melihat list ip yang di block bisa pake command : route -n atau netstat -nr

Gwe coba nulis script, kalo punya banyak list ip yg mau di block

#!/bin/bash
BLOCKDB=”/home/yoner/ip.blocked”
IPS=$(grep -Ev “^#” $BLOCKDB)
for i in $IPS
do
route add $i 127.0.0.1 lo
done

ip.blocked : list ip address yg mau di block

Cara lain yaitu dengan mendaftarkan blacklist ip ke /etc/hosts.deny
Yaitu dengan menjalankan suatu scripts secara periodik, kalo di linux pake crontab.

contoh crontab gwe :

# m h  dom mon dow   command
# Script dijalankan setiap 5 menit
*/5      *       *       *       *       /mnt/shared/deny-ssh-host.sh


Script deny-ssh-host.sh

#!/bin/bash
#Script ini akan memonitor login failed yg mencoba login beberapa kali
#Yoner
LOGFILE=”/var/log/messages”
HOSTSDENY=”/etc/hosts.deny”
BADCOUNT=”5″
# Baca log file dan cari login yg failed
grep sshd $LOGFILE |grep “Invalid user”| awk ‘{print $NF}’|sort|uniq -c|sort -n|sed “s/[[:space:]]*//” | while read i
do
# Lihat berapa kali gagal login
count=`echo $i | cut -d” ” -f1`
# cari ip dari yg gagal login
ip=`echo $i | cut -d” ” -f2`
#check host deny apakah sudah ada atau belum
already=`grep $ip $HOSTSDENY | grep sshd`
#jika ip belum ada tambahkan ke deny host
if [ -z "$already"  ]
then
if [ "$count" -ge "$BADCOUNT" ]
then
echo “sshd: “$ip >> $HOSTSDENY
fi
fi
done

Kelemahan script deny-ssh-host.sh adalah jika si penyerang menggunakan login yg terdaftar maka script ini tidak akan menambahkan ip penyerang ke deny host

Hasil output script deny-ssh-host.sh bisa dilihat di /etc/hosts.deny
biasanya sbb :

# hosts.deny    This file describes the names of the hosts which are
#               *not* allowed to use the local INET services, as decided
#               by the ‘/usr/sbin/tcpd’ server.
#
# The portmap line is redundant, but it is left to remind you that
# the new secure portmap uses hosts.deny and hosts.allow.  In particular
# you should know that NFS uses portmap!

sshd: 194.158.130.111
sshd: 203.153.33.178
sshd: 193.19.165.25
sshd: 83.15.10.114
sshd: 222.138.229.251
sshd: 218.75.172.173
sshd: 89.32.152.35
sshd: 202.53.254.50
sshd: 219.138.207.35
sshd: 200.124.21.8
sshd: 208.42.251.52

OK mungkin ini aja yang bisa gwe jabarkan, semoga serangan ini bisa dihilangkan ya paling gak diminimalisir

Blog at WordPress.com.