Network Address Translation pada Linux (Iptables)

Posted on Posted in Linux Networking

logo -linux

IP Forwarding

Untuk menjalankan Iptables pada sebuah Internet Gateway, yang harus dikonfigurasikan terlebih dahulu adalah kemampuan forwarding paket data.

Forwarding paket pada sistem Linux dapat dikonfigurasikan pada file /etc/sysctl.conf

[root@gateway]# cat /etc/sysctl.conf
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
 
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
 
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
 
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1
 
# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0

 Perubahan pada file ini mengharuskan sistem untuk di reboot.

 

 

 

Masquerade

Untuk menjalankan fungsi Masquerade untuk seluruh aplikasi (baik TCP, UDP dan ICMP) bagi seluruh jaringan 10.10.10.0/24 konfigurasi yang dapat digunakan adalah

 
[root@gateway ~]# iptables-save
# Generated by iptables-save v1.4.12 on Wed Aug 29 21:58:21 2012
*mangle
:PREROUTING ACCEPT [292122:195815236]
:INPUT ACCEPT [3607:780036]
:FORWARD ACCEPT [152405:97620004]
:OUTPUT ACCEPT [3087:459443]
:POSTROUTING ACCEPT [155488:98079207]
COMMIT
# Completed on Wed Aug 29 21:58:21 2012
# Generated by iptables-save v1.4.12 on Wed Aug 29 21:58:21 2012
*nat
:PREROUTING ACCEPT [56359:7782935]
:INPUT ACCEPT [1147:99782]
:OUTPUT ACCEPT [1950:151466]
:POSTROUTING ACCEPT [2056:156978]
-A POSTROUTING -s 10.10.10.0/24 –j MASQUERADE
COMMIT
# Completed on Wed Aug 29 21:58:21 2012
# Generated by iptables-save v1.4.12 on Wed Aug 29 21:58:21 2012
*filter
:INPUT ACCEPT [12:636]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [22:2312]
COMMIT
# Completed on Wed Aug 29 21:58:21 2012

 
Jika hanya ingin menjalankan fungsi Masquerade untuk layanan HTTP (Browsing Internet) bagi jaringan 10.10.10.0/24, konfigurasi file yang dapat digunakan adalah :

 [root@gateway ~]# iptables-save
# Generated by iptables-save v1.4.12 on Wed Aug 29 21:58:21 2012
*mangle
:PREROUTING ACCEPT [292122:195815236]
:INPUT ACCEPT [3607:780036]
:FORWARD ACCEPT [152405:97620004]
:OUTPUT ACCEPT [3087:459443]
:POSTROUTING ACCEPT [155488:98079207]
COMMIT
# Completed on Wed Aug 29 21:58:21 2012
# Generated by iptables-save v1.4.12 on Wed Aug 29 21:58:21 2012
*nat
:PREROUTING ACCEPT [56359:7782935]
:INPUT ACCEPT [1147:99782]
:OUTPUT ACCEPT [1950:151466]
:POSTROUTING ACCEPT [2056:156978]
-A POSTROUTING -s 10.10.10.0/24 -p tcp -m tcp –dport 80 -j MASQUERADE
-A POSTROUTING -s 10.10.10.0/24 -p tcp -m tcp –dport 443 -j MASQUERADE
COMMIT
# Completed on Wed Aug 29 21:58:21 2012
# Generated by iptables-save v1.4.12 on Wed Aug 29 21:58:21 2012
*filter
:INPUT ACCEPT [12:636]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [22:2312]
COMMIT
# Completed on Wed Aug 29 21:58:21 2012

 

Untuk menjalankan Masquerade untuk aplikasi ICMP (ping) bagi seluruh host jaringan 192.168.0.0/24, file konfigurasi yang dapat dilakukan adalah

[root@gateway ~]# iptables-save

# Generated by iptables-save v1.4.12 on Wed Aug 29 21:58:21 2012
*mangle
:PREROUTING ACCEPT [292122:195815236]
:INPUT ACCEPT [3607:780036]
:FORWARD ACCEPT [152405:97620004]
:OUTPUT ACCEPT [3087:459443]
:POSTROUTING ACCEPT [155488:98079207]
COMMIT
# Completed on Wed Aug 29 21:58:21 2012
# Generated by iptables-save v1.4.12 on Wed Aug 29 21:58:21 2012
*nat
:PREROUTING ACCEPT [56359:7782935]
:INPUT ACCEPT [1147:99782]
:OUTPUT ACCEPT [1950:151466]
:POSTROUTING ACCEPT [2056:156978]
-A POSTROUTING -s 10.10.10.0/24 -p tcp -m tcp –dport 80 -j MASQUERADE
-A POSTROUTING -s 10.10.10.0/24 -p tcp -m tcp –dport 443 -j MASQUERADE
-A POSTROUTING –s 10.10.10.0/24 –p icmp –j MASQUERADE
COMMIT
# Completed on Wed Aug 29 21:58:21 2012
# Generated by iptables-save v1.4.12 on Wed Aug 29 21:58:21 2012
*filter
:INPUT ACCEPT [12:636]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [22:2312]
COMMIT
# Completed on Wed Aug 29 21:58:21 2012

 Urutan konfigurasi pada file /etc/sysconfig/iptables sangat menentukan efektif tidaknya rule-rule yang diinginkan.

 

 

 

Logging

Iptables memiliki fasilitas untuk melakukan logging terhadap aktifitas client yang akan mengakses Internet, seperti contoh berikut :

*nat

:OUTPUT ACCEPT [0:0]
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -d www.facebook.com  -j LOG –log-prefix “FACEBOOK ACCESS”
-A POSTROUTING -p tcp –dport 80 -s 10.10.10.0/24 -j MASQUERADE
-A POSTROUTING -p tcp –dport 443 -s 10.10.10.0/24 -j MASQUERADE
-A POSTROUTING -p icmp -s 10.10.10.0/24 -j MASQUERADE
COMMIT
*mangle
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
COMMIT
*filter
:FORWARD ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
COMMIT

 

Hasil logging dapat dilihat pada file /var/log/messages, bisa dilakukan real time dengan perintah sebagai berikut :

[root@gateway ~]# tail -f /var/log/messages

 Aug 29 22:38:09 gateway kernel: [25123.593699] FACEBOOK ACCESSIN= OUT=p3p1 SRC=10.10.10.2 DST=31.13.79.4 LEN=52 TOS=0x00 PREC=0x00 TTL=127 ID=26747 DF PROTO=TCP SPT=56226 DPT=80 WINDOW=8192 RES=0x00 SYN URGP=0

Aug 29 22:38:18 gateway kernel: [25132.609546] FACEBOOK ACCESSIN= OUT=p3p1 SRC=10.10.10.2 DST=31.13.79.4 LEN=52 TOS=0x00 PREC=0x00 TTL=127 ID=26816 DF PROTO=TCP SPT=56231 DPT=80 WINDOW=8192 RES=0x00 SYN URGP=0

Aug 29 22:38:19 gateway kernel: [25134.336709] FACEBOOK ACCESSIN= OUT=p3p1 SRC=10.10.10.2 DST=31.13.79.4 LEN=52 TOS=0x00 PREC=0x00 TTL=127 ID=26864 DF PROTO=TCP SPT=56242 DPT=80 WINDOW=8192 RES=0x00 SYN URGP=0

 

 

 

Source NAT

 Masquerade digunakan untuk merubah secara otomatis IP Address pengirim dari sebuah paket data menjadi IP Address yang digunakan pada Interface Router tempat paket tersebut akan keluar. Selain menggunakan action Masquerade, dapat juga digunakan action SNAT yang masih memungkinkan memilih IP Address yang akan digunakan pada paket data tersebut.

Sebagai contoh, sebuah Internet gateway memiliki dua IP Address publik yang digunakan untuk mengakses Internet. Konfigurasi berikut memperlihatkan bahwa host yang berbeda akan menggunakan IP Address publik yang berbeda pada saat akan mengakses Internet. Contoh konfigurasinya adalah sebagai berikut :

[root@gateway ~]# iptables-save

# Generated by iptables-save v1.4.12 on Thu Aug 30 18:29:37 2012
*mangle
:PREROUTING ACCEPT [47254:17717989]
:INPUT ACCEPT [1148:299960]
:FORWARD ACCEPT [26398:8742980]
:OUTPUT ACCEPT [1058:148990]
:POSTROUTING ACCEPT [27481:8899171]
COMMIT
# Completed on Thu Aug 30 18:29:37 2012
# Generated by iptables-save v1.4.12 on Thu Aug 30 18:29:37 2012
*nat
:PREROUTING ACCEPT [8860:1378298]
:INPUT ACCEPT [249:19978]
:OUTPUT ACCEPT [731:56175]
:POSTROUTING ACCEPT [773:58359]
-A POSTROUTING -s 10.10.10.2/32 -j SNAT –to-source 60.1.1.2
-A POSTROUTING -s 10.10.10.4/32 -j SNAT –to-source 60.1.1.3
COMMIT
# Completed on Thu Aug 30 18:29:37 2012
# Generated by iptables-save v1.4.12 on Thu Aug 30 18:29:37 2012
*filter
:INPUT ACCEPT [1161:300480]
:FORWARD ACCEPT [26398:8742980]
:OUTPUT ACCEPT [1084:151926]
COMMIT
# Completed on Thu Aug 30 18:29:37 2012

 

 

 

 

 

Destination NAT

Destination NAT digunakan untuk merubah IP Address tujuan dari sebuah paket data, juga dapat digunakan untuk merubah port tujuan. Contoh yang biasa digunakan jika akan melakukan remote dari Internet ke komputer yang berada di jaringan lokal.

Contoh berikut adalah konfigurasi yang mengizinkan komputer dari jaringan publik (Internet) untuk melakukan akses dengan menggunakan Remote Desktop (port 3389) pada komputer lokal dengan IP Address 10.10.10.4.

[root@gateway ~]# iptables-save

# Generated by iptables-save v1.4.12 on Thu Aug 30 19:15:48 2012
*mangle
:PREROUTING ACCEPT [45133:36583527]
:INPUT ACCEPT [945:77940]
:FORWARD ACCEPT [21922:18231585]
:OUTPUT ACCEPT [787:62148]
:POSTROUTING ACCEPT [22709:18293733]
COMMIT
# Completed on Thu Aug 30 19:15:48 2012
# Generated by iptables-save v1.4.12 on Thu Aug 30 19:15:48 2012
*nat
:PREROUTING ACCEPT [8591:1058310]
:INPUT ACCEPT [53:5541]
:OUTPUT ACCEPT [81:6367]
:POSTROUTING ACCEPT [81:6279]
-A PREROUTING -i p3p1 -p tcp -m tcp –dport 3389 -j DNAT –to-destination 10.10.10.4
-A POSTROUTING -s 10.10.10.0/24 -j MASQUERADE
COMMIT
# Completed on Thu Aug 30 19:15:48 2012
# Generated by iptables-save v1.4.12 on Thu Aug 30 19:15:48 2012
*filter
:INPUT ACCEPT [958:78460]
:FORWARD ACCEPT [21922:18231585]
:OUTPUT ACCEPT [812:65008]
COMMIT
# Completed on Thu Aug 30 19:15:48 2012

 

End of Chapter

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *

*