Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Contoh IPTABLES Rules

#1: Melampilkan Status Firewall


Ketikkan perintah berikut dalam mode root :
# iptables -L -n -v

Contoh output nya:

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

Output di atas mengindikasikan bahwa firewall tidak aktif. Bertikut ini contoh yang
menunjukkan firewall Aktif :

# iptables -L -n -v

Contoh output nya:

Chain INPUT (policy DROP 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 state


INVALID

394 43586 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state


RELATED,ESTABLISHED

93 17292 ACCEPT all -- br0 * 0.0.0.0/0 0.0.0.0/0


1 142 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0

Chain FORWARD (policy DROP 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

0 0 ACCEPT all -- br0 br0 0.0.0.0/0 0.0.0.0/0

0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 state


INVALID

0 0 TCPMSS tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp


flags:0x06/0x02 TCPMSS clamp to PMTU

0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state


RELATED,ESTABLISHED

0 0 wanin all -- vlan2 * 0.0.0.0/0 0.0.0.0/0

0 0 wanout all -- * vlan2 0.0.0.0/0 0.0.0.0/0

0 0 ACCEPT all -- br0 * 0.0.0.0/0 0.0.0.0/0

Chain OUTPUT (policy ACCEPT 425 packets, 113K bytes)

pkts bytes target prot opt in out source destination

Chain wanin (1 references)

pkts bytes target prot opt in out source destination

Chain wanout (1 references)

pkts bytes target prot opt in out source destination

Dimana,

-L : List rules.
-v : Menampilkan informasi rinci. Pilihan ini membuat perintah list
menampilkan nama interfaces, pilihan aturan, dan masker KL.
Konter paket dan byte juga tercantum, dengan akhiran K, M atau
G untuk 1000, 1.000.000 dan 1000000000 pengganda masing-
masing.
-n : Menampilkan alamat IP dan port dalam format numerik.
#1.1: Untuk memeriksa firewall dengan nomor baris,
masukkan perintah:
# iptables -n -L -v --line-numbers

Contoh output nya:

Chain INPUT (policy DROP)

num target prot opt source destination

1 DROP all -- 0.0.0.0/0 0.0.0.0/0 state INVALID

2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED

3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0

4 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0

Chain FORWARD (policy DROP)

num target prot opt source destination

1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0

2 DROP all -- 0.0.0.0/0 0.0.0.0/0 state INVALID

3 TCPMSS tcp -- 0.0.0.0/0 0.0.0.0/0 tcp flags:0x06/0x02 TCPMSS clamp


to PMTU

4 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED

5 wanin all -- 0.0.0.0/0 0.0.0.0/0


6 wanout all -- 0.0.0.0/0 0.0.0.0/0

7 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0

Chain OUTPUT (policy ACCEPT)

num target prot opt source destination

Chain wanin (1 references)

num target prot opt source destination

Chain wanout (1 references)

num target prot opt source destination

Anda dapat menggunakan nomor baris untuk menghapus atau memasukkan


aturan baru ke dalam firewall.
#1.2: Untuk menampilkan INPUT OUTPUT atau chain
rules, masukkan perintah:
# iptables -L INPUT -n -v

# iptables -L OUTPUT -n -v --line-numbers

#2: Stop / Start / Restart Firewall


Jika Anda menggunakan CentOS / RHEL / Fedora Linux, masukkan perintah:
# service iptables stop

# service iptables start

# service iptables restart

Anda dapat menggunakan perintah iptables itu sendiri untuk menghentikan


firewall dan menghapus semua rules:
# iptables -F

# iptables -X

# iptables -t nat -F

# iptables -t nat -X

# iptables -t mangle -F
# iptables -t mangle -X

# iptables -P INPUT ACCEPT

# iptables -P OUTPUT ACCEPT

# iptables -P FORWARD ACCEPT

Dimana,
-F : Menghapus semua rules
-X : Menghapus chain.
-t table_name : Pilih tabel (disebut nas atau mangle) dan
menghapus rules.
-P : Mengatur default policy (seperti DROP, REJECT, atau
ACCEPT).

#3: Menghapus Firewall Rules


Untuk menampilkan nomor baris bersama dengan informasi lainnya untuk aturan
yang ada, masukkan perintah:
# iptables -L INPUT -n --line-numbers

# iptables -L OUTPUT -n --line-numbers

# iptables -L OUTPUT -n --line-numbers | less

# iptables -L OUTPUT -n --line-numbers | grep 202.54.1.1

Anda akan mendapatkan daftar IP. Lihat jumlah di sebelah kiri, kemudian
menggunakan nomor untuk menghapusnya. Untuk menghapus nomor baris
contoh 4, masukkan perintah:
# iptables -D INPUT 4

ATAU mencari sumber IP 202.54.1.1 dan menghapus dari rule :


# iptables -D INPUT -s 202.54.1.1 -j DROP

Dimana,

-D : Menghapus satu atau lebih aturan dari rantai yang dipilih

#4: Memasukkan Firewall Rules


Untuk menyisipkan satu atau lebih aturan dalam
rantai pilihan sebagai nomor aturan diberikan menggunakan sintaks berikut.
Pertama mengetahui nomor baris, masukkan perintah:
# Iptables-L INPUT-n - line-numbers

Contoh output:

Chain INPUT (policy DROP)

num target prot opt source destination

1 DROP all -- 202.54.1.1 0.0.0.0/0

2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state NEW,ESTABLISHED

Untuk memasukkan rule diantara 1 dan 2, masukkan perintah:


# iptables -I INPUT 2 -s 202.54.1.2 -j DROP

Untuk melihat rule yang sudah diperbarui, masukkan perintah:


# iptables -L INPUT -n --line-numbers

Contoh output:

Chain INPUT (policy DROP)

num target prot opt source destination

1 DROP all -- 202.54.1.1 0.0.0.0/0

2 DROP all -- 202.54.1.2 0.0.0.0/0

3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state NEW,ESTABLISHED

#5: Menyimpan Firewall Rules


Untuk menyimpan firewall rule pada CentOS / RHEL / Fedora Linux, masukkan
perintah:
# service iptables save
Dalam contoh ini, drop IP dan menyimpan aturan firewall:
# iptables -A INPUT -s 202.5.4.1 -j DROP

# service iptables save

Untuk semua distro lain menggunakan perintah iptables-save:


# iptables-save > /root/my.active.firewall.rules

# cat /root/my.active.firewall.rules

#6: Mengembalikan (Restore) Firewall Rules


Untuk mengembalikan firewall rule dari sebuah file yang bernama / root /
my.active.firewall.rules, masukkan perintah:
# iptables-restore < /root/my.active.firewall.rules

Untuk mengembalikan firewall rules pada CentOS / RHEL / Fedora Linux,


masukkan perintah:
# service iptables restart

#7: Menyeting Default Firewall Policies


Untuk drop semua traffic:
# iptables -P INPUT DROP

# iptables -P OUTPUT DROP

# iptables -P FORWARD DROP

# iptables -L -v -n

#### you will not able to connect anywhere as all traffic is dropped ###

# ping cyberciti.biz

# wget http://www.kernel.org/pub/linux/kernel/v3.0/testing/linux-3.2-rc5.tar.bz2

#7.1: Hanya Block Incoming Traffic


Untuk memutuskan semua paket masuk / diteruskan, namun memungkinkan lalu
lintas keluar, masukkan perintah:
# iptables -P INPUT DROP

# iptables -P FORWARD DROP

# iptables -P OUTPUT ACCEPT

# iptables -A INPUT -m state --state NEW,ESTABLISHED -j ACCEPT

# iptables -L -v -n

### *** now ping and wget should work *** ###
# ping cyberciti.biz

# wget http://www.kernel.org/pub/linux/kernel/v3.0/testing/linux-3.2-rc5.tar.bz2

#8:Drop Private Network Address Pada


Public Interface
IP spoofing tidak lain untuk menghentikan rentang alamat berikut IPv4 untuk
jaringan pribadi pada public interface Anda. Paket dengan non-routable source
address harus ditolak menggunakan sintaks berikut:
# iptables -A INPUT -i eth1 -s 192.168.0.0/24 -j DROP

# iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j DROP

#8.1: IPv4 Address Ranges Untuk Private Networks


(Pastikan Anda memblok pada public interface)
10.0.0.0/8 -j (A)
172.16.0.0/12 (B)
192.168.0.0/16 (C)
224.0.0.0/4 (MULTICAST D)
240.0.0.0/5 (E)
127.0.0.0/8 (LOOPBACK)

#9: Blocking IP Address (BLOCK IP)


Untuk memblok ip address misal 1.2.3.4, masukkan perintah :
# iptables -A INPUT -s 1.2.3.4 -j DROP

# iptables -A INPUT -s 192.168.0.0/24 -j DROP

#10: Block Incoming Port Requests (BLOCK


PORT)
Untuk Memblok semua service requests pada port 80, masukkan perintah:
# iptables -A INPUT -p tcp --dport 80 -j DROP

# iptables -A INPUT -i eth1 -p tcp --dport 80 -j DROP

Untuk memblok port 80 hanya untuk ip address trtentu misal 1.2.3.4, masukkan
perintah:
# iptables -A INPUT -p tcp -s 1.2.3.4 --dport 80 -j DROP

# iptables -A INPUT -i eth1 -p tcp -s 192.168.1.0/24 --dport 80 -j DROP


#11: Block Outgoing IP Address
Untuk memblok outgoing traffic ke host atau domain tertentu seperti cyberciti.biz,
masukkan :
# host -t a cyberciti.biz

Contoh output:

cyberciti.biz has address 75.126.153.206

Sebagai catatan alamat ip dan tipe berikut ini untuk memblokir semua lalu lintas
keluar untuk 75.126.153.206:
# iptables -A OUTPUT -d 75.126.153.206 -j DROP

Anda dapat menggunakan subnet sebagai berikut:


# iptables -A OUTPUT -d 192.168.1.0/24 -j DROP

# iptables -A OUTPUT -o eth1 -d 192.168.1.0/24 -j DROP

#11.1: Contoh Memblock Facebook.com Domain


Pertama, cari semua alamat ip facebook.com, masukkan:
# host -t a www.facebook.com

Contoh output:

www.facebook.com has address 69.171.228.40

Cari CIDR untuk 69.171.228.40, enter:


# whois 69.171.228.40 | grep CIDR

Contoh output:

CIDR: 69.171.224.0/19

Untuk mencegah akses keluar ke www.facebook.com, masukkan perintah:


# iptables -A OUTPUT -p tcp -d 69.171.224.0/19 -j DROP
Anda juga dapat menggunakan nama domainnya, masukkan perintah:
# iptables -A OUTPUT -p tcp -d www.facebook.com -j DROP

# iptables -A OUTPUT -p tcp -d facebook.com -j DROP

Dari halaman pandual iptables:

specifying any name to be resolved with a remote query such as


DNS (e.g., facebook.com is a really bad idea), a network IP address
(with /mask), or a plain IP address

menetapkan nama untuk diselesaikan dengan query jarak jauh seperti


DNS (misalnya, facebook.com adalah ide yang sangat buruk), IP
alamat jaringan (with / mask), atau alamat IP biasa

#12: Log dan Drop Packet


Ketikkan perintah berikut untuk login dan memblokir spoofing IP pada public
interface eth1
# iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j LOG --log-prefix "IP_SPOOF A: "

# iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j DROP

Secara default semua pesan log disimpan di /var/log/messages file.


# tail -f /var/log/messages

# grep --color 'IP SPOOF' /var/log/messages

#13: Log dan Drop Packet Dengan Log Entri


yang Terbatas
Batas Modul -m dapat membatasi jumlah entri log yang dibuat per waktu. Ini
digunakan untuk mencegah banjir file log anda. Untuk masuk dan drop spoofing
per 5 menit, dalam semburan paling banyak 7 entri.
# iptables -A INPUT -i eth1 -s 10.0.0.0/8 -m limit --limit 5/m --limit-burst 7 -j LOG --log-prefix

"IP_SPOOF A: "

# iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j DROP

#14: Drop atau Accept Traffic dari Mac


Address
Gunakan sintak berikut :
# iptables -A INPUT -m mac --mac-source 00:0F:EA:91:04:08 -j DROP
## *only accept traffic for TCP port # 8080 from mac 00:0F:EA:91:04:07 * ##

# iptables -A INPUT -p tcp --destination-port 22 -m mac --mac-source 00:0F:EA:91:04:07 -j ACCEPT

#15: Block atau Allow ICMP Ping Request


Ketik perintah berikut untuk memblokir permintaan ping ICMP:
# iptables -A INPUT -p icmp --icmp-type echo-request -j DROP

# iptables -A INPUT -i eth1 -p icmp --icmp-type echo-request -j DROP

Tanggapan Ping juga dapat dibatasi pada jaringan tertentu atau host:
# iptables -A INPUT -s 192.168.1.0/24 -p icmp --icmp-type echo-request -j ACCEPT

Berikut ini hanya menerima jenis permintaan ICMP terbatas :


### ** assumed that default INPUT policy set to DROP ** #############

iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT

iptables -A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT

iptables -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT

## ** all our server to respond to pings ** ##

iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT

#16: Membuka Rentang Port


Gunakan sintaks berikut untuk membuka berbagai port:
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 7000:7010 -j ACCEPT

#17: Membuka Rentang Alamat IP


Gunakan sintaks berikut untuk membuka berbagai alamat IP:
## only accept connection to tcp port 80 (Apache) if ip is between 192.168.1.100 and 192.168.1.200

##

iptables -A INPUT -p tcp --destination-port 80 -m iprange --src-range 192.168.1.100-192.168.1.200 -j

ACCEPT

## nat example ##

iptables -t nat -A POSTROUTING -j SNAT --to-source 192.168.1.20-192.168.1.25

#18: Established Connections dan Merestart


Firewall
Bila Anda restart service iptables maka enstablished connection akan di drop
karena mereload modul dari sistem pada RHEL / Fedora / CentOS Linux. Edit, /
etc / sysconfig / iptables-config dan mengatur IPTABLES_MODULES_UNLOAD
sebagai berikut:

IPTABLES_MODULES_UNLOAD = no

#19: Membantu Iptables Flooding My Server


Screen
Gunakan tingkat crit log untuk mengirim pesan ke file log bukannya konsol:
iptables -A INPUT -s 1.2.3.4 -p tcp --destination-port 80 -j LOG --log-level crit

#20: Memblok atau Membuka Port yang


Umum Digunakan
Berikut ini sintaks untuk membuka dan menutup port umum TCP dan UDP:

1.
2. Replace ACCEPT with DROP to block port:
3. ## open port ssh tcp port 22 ##
4. iptables -A INPUT -m state state NEW -m tcp -p tcp dport 22 -j
ACCEPT
5. iptables -A INPUT -s 192.168.1.0/24 -m state state NEW -p tcp
dport 22 -j ACCEPT
6.
7. ## open cups (printing service) udp/tcp port 631 for LAN users
##
8. iptables -A INPUT -s 192.168.1.0/24 -p udp -m udp dport 631 -j
ACCEPT
9. iptables -A INPUT -s 192.168.1.0/24 -p tcp -m tcp dport 631 -j
ACCEPT
10.
11. ## allow time sync via NTP for lan users (open udp port 123) ##
12. iptables -A INPUT -s 192.168.1.0/24 -m state state NEW -p udp
dport 123 -j ACCEPT
13.
14. ## open tcp port 25 (smtp) for all ##
15. iptables -A INPUT -m state state NEW -p tcp dport 25 -j ACCEPT
16.
17. # open dns server ports for all ##
18. iptables -A INPUT -m state state NEW -p udp dport 53 -j ACCEPT
19. iptables -A INPUT -m state state NEW -p tcp dport 53 -j ACCEPT
20.
21. ## open http/https (Apache) server port to all ##
22. iptables -A INPUT -m state state NEW -p tcp dport 80 -j ACCEPT
23. iptables -A INPUT -m state state NEW -p tcp dport 443 -j
ACCEPT
24.
25. ## open tcp port 110 (pop3) for all ##
26. iptables -A INPUT -m state state NEW -p tcp dport 110 -j
ACCEPT
27.
28. ## open tcp port 143 (imap) for all ##
29. iptables -A INPUT -m state state NEW -p tcp dport 143 -j
ACCEPT
30.
31. ## open access to Samba file server for lan users only ##
32. iptables -A INPUT -s 192.168.1.0/24 -m state state NEW -p tcp
dport 137 -j ACCEPT
33. iptables -A INPUT -s 192.168.1.0/24 -m state state NEW -p tcp
dport 138 -j ACCEPT
34. iptables -A INPUT -s 192.168.1.0/24 -m state state NEW -p tcp
dport 139 -j ACCEPT
35. iptables -A INPUT -s 192.168.1.0/24 -m state state NEW -p tcp
dport 445 -j ACCEPT
36.
37. ## open access to proxy server for lan users only ##
38. iptables -A INPUT -s 192.168.1.0/24 -m state state NEW -p tcp
dport 3128 -j ACCEPT
39.
40. ## open access to mysql server for lan users only ##
41. iptables -I INPUT -p tcp dport 3306 -j ACCEPT
42.

#21: Batasi Jumlah Sambungan Paralel


Untuk Server Per Client IP
Anda dapat menggunakan modul connlimit untuk menempatkan pembatasan
tersebut. Untuk memungkinkan 3 koneksi per host ssh klien, masukkan perintah:
# iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 3 -j REJECT

Set HTTP requests ke 20:


# iptables -p tcp --syn --dport 80 -m connlimit --connlimit-above 20 --connlimit-mask 24 -j DROP

Dimana,

1. connlimit-above 3 : Cocok jika jumlah koneksi yang ada di atas


3.
2. connlimit-mask 24 : Kelompok host menggunakan panjang
prefiks. Untuk IPv4, ini harus angka antara (termasuk) 0 dan 32.

#22: Cara Menggunakan IPtables seperti Pro


Untuk informasi lebih lanjut tentang iptables, silakan lihat halaman manual
dengan mengetikkan iptables manusia dari baris perintah:
$ man iptables

Anda dapat lihat bantuan menggunakan sintaks berikut juga:


# iptables -h

Untuk melihat bantuan dengan perintah dan target khusus, masukkan perintah:

# iptables -j DROP -h

#22.1: Mengetes Firewall


Cari tahu apakah port terbuka atau tidak, masukkan:
# netstat -tulpn

Cari tahu apakah tcp port 80 terbuka atau tidak, masukkan:


# netstat -tulpn | grep :80

Jika port 80 tidak terbuka, jalankan Apache, masukkan:


# service httpd start
Pastikan bahwa iptables memungkinkan akses ke port 80:
# iptables -L INPUT -v -n | grep 80

Jika tidak, buka port 80 dengan menggunakan iptables untuk semua user:
# iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT

# service iptables save

Gunakan perintah telnet untuk melihat apakah firewall memungkinkan untuk


terhubung ke port 80:
$ telnet www.cyberciti.biz 80

You might also like