Rabu, 29 Juli 2009
Mematikan akses SSH untuk Root
1. SSH Ke server yang mau di seting disable root login
2. Edit /etc/ssh/sshd_config
3. cari Protocol 2,1(Tanpa tanda kutip) dan rubah menjadi Protocol 2
4. selanjutnya cari PermitRootLogin yes dan rubah menjadi PermitRootLogin no
5. simpan dan keluar
6. Lalu restart ssh
/etc/rc.d/init.d/sshd restart
Selasa, 28 Juli 2009
Permanently IPTABLES
Nah, untuk menangani hal tersebut apa yang harus dilakukan....?
Ada 2 cara yang pernah saya lakukan untuk menangani hal tersebut di atas dan sebenarnya ini sangat mudah bahkan bagi newbie (user baru di linux) seperti saya. Dari 2 cara itu memang ada perbedaan dikarenakan ada 2 distro yaitu redhat dan opensuse. Caranya adalah sbb;
* Redhat dan turunannya...
Pertama masuk sebagai user root via console, lalu ketikkan perintah iptablesnya dan simpan di /etc/sysconfig/iptabes, misal:
[margie@server ~]$ su -
Password:
[root@server ~]# iptables -I INPUT -p tcp -s 192.168.1.100 --dport 3306 -j DROP
[root@server ~]# iptables-save > /etc/sysconfig/iptables
nah dari perintah di atas, iptables sudah tersimpan dan akan menjadi permanen sehingga ketika komputer melakukan restart maka hasil settingan tidak akan hilang. Kita dapat melihatnya dengan perintah
[root@server ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- 192.168.1.100 0.0.0.0/0 tcp dpt:3306
RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain RH-Firewall-1-INPUT (2 references)
target prot opt source destination
*Opensuse dan turunannya...
Berbeda dengan redhat, perintah iptables di opensuse tidak bisa dilakukan dengan cara seperti di atas. Maksudnya adalah kalau dilakukan dengan cara di atas maka hasil settingan masih tidak permanen atau akan hilang setelah komputer melakukan restart.
Cara yang digunakan memang sedikit agak berbeda yaitu karena perintah security termasuk iptables masuk di SuSEfirewall2. Maka sebelum kita melakukan perubahan kita stop dulu SuSEfirewall2nya dengan cara
"SuSEfirewall2 stop". Kemudian
Edit file /etc/sysconfig/SuSEFirewall2, lalu aktifkan bagianSecara default settingannya adalah FW_CUSTOMRULES=" ", jadi harus kita aktifkan. Kita bisa menggunakan editor vi atau yang lainnya.
FW_CUSTOMRULES="/etc/sysconfig/scripts/SuSEfirewall2-custom"
vi /etc/sysconfig/SuSEFirewall2 dan lakukan perubahan seperti ini (harus dengan user root);
# Do you want to load customary rules from a file?
#
# This is really an expert option. NO HELP WILL BE GIVEN FOR THIS!
# READ THE EXAMPLE CUSTOMARY FILE AT /etc/sysconfig/scripts/SuSEfirewall2-custom
#
FW_CUSTOMRULES="/etc/sysconfig/scripts/SuSEfirewall2-custom"
#FW_CUSTOMRULES=""
Setelah itu tekan tombol escape, lalu ketik :wq
Setelah itu kita lakukan perubahan atau penambahan di /etc/sysconfig/scripts/SuSEfirewall2-custom, pada baris di bawah tulisan "fw_custom_before_antispoofing()"
vi /etc/sysconfig/scripts/SuSEfirewall2-custom
fw_custom_before_antispoofing() {
# these rules will be loaded before any anti spoofing rules will be
# loaded. Effectively the only filter lists already effective are
# 1) allow any traffic via the loopback interface, 2) allow DHCP stuff,
# 3) allow SAMBA stuff [2 and 3 only if FW_SERVICE_... are set to "yes"]
# You can use this hook to prevent logging of uninteresting broadcast
# packets or to allow certain packet through the anti-spoofing mechanism.
#example: allow incoming multicast packets for any routing protocol
#iptables -A INPUT -j ACCEPT -d 224.0.0.0/24
iptables -I INPUT -p tcp -s 192168.1.100 --dport 3306 -j DROP
lalu tekan tombol escape, ketik :wq
Setelah tersimpan dengan wq, kita lakukan restart terhadap SuSEfirewall2 dengan perintah
SuSEfirewall2 start
Ok, kita tinggal cek dengan iptables -L -n
Security dengan IPTables
August 5, 2003
Tulisan ini ditujukan untuk memberikan pengetahuan dasar mengenai pemfilteran paket menggunakan IPTables pada Linux. Tulisan ini bersifat general yang menjelaskan secara umum bagaimana sintaks IPTables dibuat. Beberapa (banyak?) bagian dari tulisan diambil dari official site IPTables. Tidak ada copyright apapun dalam dokumen ini, anda bebas menyalin, mencetak, maupun memodifikasi (dengan menyertakan nama penulis asli). Kritik, koreksi, saran dan lain-lain silahkan dialamatkan ke email tersebut di atas. Semoga bermanfaat.
1. Persiapan
Sebelum mulai, diharapkan pembaca sudah memiliki pengetahuan dasar mengenai TCP/IP karena hal ini merupakan dasar dari penggunaan IPTables. Ada (sangat) banyak resource yang mendokumentasikan konsep dasar tentang TCP/IP, baik itu secara online maupun cetak. Silahkan googling untuk mendapatkannya.
Hal berikutnya yang harus anda persiapkan adalah sebuah komputer yang terinstall Linux. Akan lebih baik jika komputer anda memiliki 2 buah network interface card, sebab bisa menjalankan fungsi packet forwarding. Disarankan anda menggunakan linux dengan kernel 2.4 ke atas, karena (setahu saya) linux dengan kernel 2.4 ke atas sudah memiliki dukungan IPTables secara default, sehingga anda tidak perlu mengkompilasi ulang kernel anda. Bagi anda yang menggunakan kernel 2.2 atau sebelumnya, anda harus melakukan kompilasi kernel untuk memasukkan dukungan IPTables. Silahkan lihat tutorial Kompilasi kernel 2.4.x di Linux oleh mas Asfik.
2. Pendahuluan
IPTables memiliki tiga macam daftar aturan bawaan dalam tabel penyaringan, daftar tersebut dinamakan rantai firewall (firewall chain) atau sering disebut chain saja. Ketiga chain tersebut adalah INPUT, OUTPUT dan FORWARD.
Pada diagram tersebut, lingkaran menggambarkan ketiga rantai atau chain. Pada saat sebuah paket sampai pada sebuah lingkaran, maka disitulah terjadi proses penyaringan. Rantai akan memutuskan nasib paket tersebut. Apabila keputusannnya adalah DROP, maka paket tersebut akan di-drop. Tetapi jika rantai memutuskan untuk ACCEPT, maka paket akan dilewatkan melalui diagram tersebut.
Sebuah rantai adalah aturan-aturan yang telah ditentukan. Setiap aturan menyatakan “jika paket memiliki informasi awal (header) seperti ini, maka inilah yang harus dilakukan terhadap paket”. Jika aturan tersebut tidak sesuai dengan paket, maka aturan berikutnya akan memproses paket tersebut. Apabila sampai aturan terakhir yang ada, paket tersebut belum memenuhi salah satu aturan, maka kernel akan melihat kebijakan bawaan (default) untuk memutuskan apa yang harus dilakukan kepada paket tersebut. Ada dua kebijakan bawaan yaitu default DROP dan default ACCEPT.
Jalannya sebuah paket melalui diagram tersebut bisa dicontohkan sebagai berikut:
Perjalanan paket yang diforward ke host yang lain
1. Paket berada pada jaringan fisik, contoh internet.
2. Paket masuk ke interface jaringan, contoh eth0.
3. Paket masuk ke chain PREROUTING pada table Mangle. Chain ini berfungsi untuk me-mangle (menghaluskan) paket, seperti merubah TOS, TTL dan lain-lain.
4. Paket masuk ke chain PREROUTING pada tabel nat. Chain ini berfungsi utamanya untuk melakukan DNAT (Destination Network Address Translation).
5. Paket mengalami keputusan routing, apakah akan diproses oleh host lokal atau diteruskan ke host lain.
6. Paket masuk ke chain FORWARD pada tabel filter. Disinlah proses pemfilteran yang utama terjadi.
7. Paket masuk ke chain POSTROUTING pada tabel nat. Chain ini berfungsi utamanya untuk melakukan SNAT (Source Network Address Translation).
8. Paket keluar menuju interface jaringan, contoh eth1.
9. Paket kembali berada pada jaringan fisik, contoh LAN.
Perjalanan paket yang ditujukan bagi host lokal
1. Paket berada dalam jaringan fisik, contoh internet.
2. Paket masuk ke interface jaringan, contoh eth0.
3. Paket masuk ke chain PREROUTING pada tabel mangle.
4. Paket masuk ke chain PREROUTING pada tabel nat.
5. Paket mengalami keputusan routing.
6. Paket masuk ke chain INPUT pada tabel filter untuk mengalami proses penyaringan.
7. Paket akan diterima oleh aplikasi lokal.
Perjalanan paket yang berasal dari host lokal
1. Aplikasi lokal menghasilkan paket data yang akan dikirimkan melalui jaringan.
2. Paket memasuki chain OUTPUT pada tabel mangle.
3. Paket memasuki chain OUTPUT pada tabel nat.
4. Paket memasuki chain OUTPUT pada tabel filter.
5. Paket mengalami keputusan routing, seperti ke mana paket harus pergi dan melalui interface mana.
6. Paket masuk ke chain POSTROUTING pada tabel NAT.
7. Paket masuk ke interface jaringan, contoh eth0.
8. Paket berada pada jaringan fisik, contoh internet.
3. Sintaks IPTables
iptables [-t table] command [match] [target/jump]
1. Table
IPTables memiliki 3 buah tabel, yaitu NAT, MANGLE dan FILTER. Penggunannya disesuaikan dengan sifat dan karakteristik masing-masing. Fungsi dari masing-masing tabel tersebut sebagai berikut :
- NAT : Secara umum digunakan untuk melakukan Network Address Translation. NAT adalah penggantian field alamat asal atau alamat tujuan dari sebuah paket.
- MANGLE : Digunakan untuk melakukan penghalusan (mangle) paket, seperti TTL, TOS dan MARK.
- FILTER : Secara umum, inilah pemfilteran paket yang sesungguhnya.. Di sini bisa dintukan apakah paket akan di-DROP, LOG, ACCEPT atau REJECT
2. Command
Command pada baris perintah IPTables akan memberitahu apa yang harus dilakukan terhadap lanjutan sintaks perintah. Umumnya dilakukan penambahan atau penghapusan sesuatu dari tabel atau yang lain.
Command | Keterangan |
-A | Perintah ini menambahkan aturan pada akhir chain. Aturan akan ditambahkan di akhir baris pada chain yang bersangkutan, sehingga akan dieksekusi terakhir |
-D | Perintah ini menghapus suatu aturan pada chain. Dilakukan dengan cara menyebutkan secara lengkap perintah yang ingin dihapus atau dengan menyebutkan nomor baris dimana perintah akan dihapus. |
-R | Penggunaannya sama seperti --delete, tetapi command ini menggantinya dengan entry yang baru. |
-I | Memasukkan aturan pada suatu baris di chain. Aturan akan dimasukkan pada baris yang disebutkan, dan aturan awal yang menempati baris tersebut akan digeser ke bawah. Demikian pula baris-baris selanjutnya. |
-L | Perintah ini menampilkan semua aturan pada sebuah tabel. Apabila tabel tidak disebutkan, maka seluruh aturan pada semua tabel akan ditampilkan, walaupun tidak ada aturan sama sekali pada sebuah tabel. Command ini bisa dikombinasikan dengan option –v (verbose), -n (numeric) dan –x (exact). |
-F | Perintah ini mengosongkan aturan pada sebuah chain. Apabila chain tidak disebutkan, maka semua chain akan di-flush. |
-N | Perintah tersebut akan membuat chain baru. |
-X | Perintah ini akan menghapus chain yang disebutkan. Agar perintah di atas berhasil, tidak boleh ada aturan lain yang mengacu kepada chain tersebut. |
-P | Perintah ini membuat kebijakan default pada sebuah chain. Sehingga jika ada sebuah paket yang tidak memenuhi aturan pada baris-baris yang telah didefinisikan, maka paket akan diperlakukan sesuai dengan kebijakan default ini. |
-E | Perintah ini akan merubah nama suatu chain. |
3. Option
Option digunakan dikombinasikan dengan command tertentu yang akan menghasilkan suatu variasi perintah.
Option | Command Pemakai | Keterangan |
-v | --list | Memberikan output yang lebih detail, utamanya digunakan dengan --list. Jika digunakan dengan |
-x | --list | Memberikan output yang lebih tepat. |
-n | --list | Memberikan output yang berbentuk angka. Alamat IP dan nomor port akan ditampilkan dalam bentuk angka dan bukan hostname ataupun nama aplikasi/servis. |
--line-number | --list | Akan menampilkan nomor dari daftar aturan. Hal ni akan mempermudah bagi kita untuk melakukan modifikasi aturan, jika kita mau meyisipkan atau menghapus aturan dengan nomor tertentu. |
--modprobe | All | Memerintahkan IPTables untuk memanggil modul tertentu. Bisa digunakan bersamaan dengan semua command. |
4. Generic Matches
Generic Matches artinya pendefinisian kriteria yang berlaku secara umum. Dengan kata lain, sintaks generic matches akan sama untuk semua protokol. Setelah protokol didefinisikan, maka baru didefinisikan aturan yang lebih spesifik yang dimiliki oleh protokol tersebut. Hal ini dilakukan karena tiap-tiap protokol memiliki karakteristik yang berbeda, sehingga memerlukan perlakuan khusus.
Match | Keterangan |
-p | Digunakan untuk mengecek tipe protokol tertentu. Contoh protokol yang umum adalah TCP, UDP, ICMP dan ALL. Daftar protokol bisa dilihat pada /etc/protocols. Tanda inversi juga bisa diberlakukan di sini, misal kita menghendaki semua protokol kecuali icmp, maka kita bisa menuliskan --protokol ! icmp yang berarti semua kecuali icmp. |
-s | Kriteria ini digunakan untuk mencocokkan paket berdasarkan alamat IP asal. Alamat di sini bisa berberntuk alamat tunggal seperti 192.168.1.1, atau suatu alamat network menggunakan netmask misal 192.168.1.0/255.255.255.0, atau bisa juga ditulis 192.168.1.0/24 yang artinya semua alamat 192.168.1.x. Kita juga bisa menggunakan inversi. |
-d | Digunakan untuk mecocokkan paket berdasarkan alamat tujuan. Penggunaannya sama dengan match –src |
-i | Match ini berguna untuk mencocokkan paket berdasarkan interface di mana paket datang. Match ini hanya berlaku pada chain INPUT, FORWARD dan PREROUTING |
-o | Berfungsi untuk mencocokkan paket berdasarkan interface di mana paket keluar. Penggunannya sama dengan |
5. Implicit Matches
Implicit Matches adalah match yang spesifik untuk tipe protokol tertentu. Implicit Match merupakan sekumpulan rule yang akan diload setelah tipe protokol disebutkan. Ada 3 Implicit Match berlaku untuk tiga jenis protokol, yaitu TCP matches, UDP matches dan ICMP matches.
a. TCP matches
Match | Keterangan |
--sport | Match ini berguna untuk mecocokkan paket berdasarkan port asal. Dalam hal ini kia bisa mendefinisikan nomor port atau nama service-nya. Daftar nama service dan nomor port yang bersesuaian dapat dilihat di /etc/services. --sport juga bisa dituliskan untuk range port tertentu. Misalkan kita ingin mendefinisikan range antara port 22 sampai dengan 80, maka kita bisa menuliskan --sport 22:80. Jika bagian salah satu bagian pada range tersebut kita hilangkan maka hal itu bisa kita artikan dari port 0, jika bagian kiri yang kita hilangkan, atau 65535 jika bagian kanan yang kita hilangkan. Contohnya --sport :80 artinya paket dengan port asal nol sampai dengan 80, atau --sport 1024: artinya paket dengan port asal 1024 sampai dengan 65535.Match ini juga mengenal inversi. |
--dport --destination-port | Penggunaan match ini sama dengan match --source-port. |
--tcp-flags | Digunakan untuk mencocokkan paket berdasarkan TCP flags yang ada pada paket tersebut. Pertama, pengecekan akan mengambil daftar flag yang akan diperbandingkan, dan kedua, akan memeriksa paket yang di-set 1, atau on. Pada kedua list, masing-masing entry-nya harus dipisahkan oleh koma dan tidak boleh ada spasi antar entry, kecuali spasi antar kedua list. Match ini mengenali SYN,ACK,FIN,RST,URG, PSH. Selain itu kita juga menuliskan ALL dan NONE. Match ini juga bisa menggunakan inversi. |
--syn | Match ini akan memeriksa apakah flag SYN di-set dan ACK dan FIN tidak di-set. Perintah ini sama artinya jika kita menggunakan match --tcp-flags SYN,ACK,FIN SYN Paket dengan match di atas digunakan untuk melakukan request koneksi TCP yang baru terhadap server |
b. UDP Matches
Karena bahwa protokol UDP bersifat connectionless, maka tidak ada flags yang mendeskripsikan status paket untuk untuk membuka atau menutup koneksi. Paket UDP juga tidak memerlukan acknowledgement. Sehingga Implicit Match untuk protokol UDP lebih sedikit daripada TCP.
Ada dua macam match untuk UDP:
--sport atau --source-port
--dport atau --destination-port
c. ICMP Matches
Paket ICMP digunakan untuk mengirimkan pesan-pesan kesalahan dan kondisi-kondisi jaringan yang lain. Hanya ada satu implicit match untuk tipe protokol ICMP, yaitu :
--icmp-type
6. Explicit Matches
a. MAC Address
Match jenis ini berguna untuk melakukan pencocokan paket berdasarkan MAC source address. Perlu diingat bahwa MAC hanya berfungsi untuk jaringan yang menggunakan teknologi ethernet.
iptables –A INPUT –m mac –mac-source 00:00:00:00:00:01
b. Multiport Matches
Ekstensi Multiport Matches digunakan untuk mendefinisikan port atau port range lebih dari satu, yang berfungsi jika ingin didefinisikan aturan yang sama untuk beberapa port. Tapi hal yang perlu diingat bahwa kita tidak bisa menggunakan port matching standard dan multiport matching dalam waktu yang bersamaan.
iptables –A INPUT –p tcp –m multiport --source-port 22,53,80,110
c. Owner Matches
Penggunaan match ini untuk mencocokkan paket berdasarkan pembuat atau pemilik/owner paket tersebut. Match ini bekerja dalam chain OUTPUT, akan tetapi penggunaan match ini tidak terlalu luas, sebab ada beberapa proses tidak memiliki owner (??).
iptables –A OUTPUT –m owner --uid-owner 500
Kita juga bisa memfilter berdasarkan group ID dengan sintaks --gid-owner. Salah satu penggunannya adalah bisa mencegah user selain yang dikehendaki untuk mengakses internet misalnya.
d. State Matches
Match ini mendefinisikan state apa saja yang cocok. Ada 4 state yang berlaku, yaitu NEW, ESTABLISHED, RELATED dan INVALID. NEW digunakan untuk paket yang akan memulai koneksi baru. ESTABLISHED digunakan jika koneksi telah tersambung dan paket-paketnya merupakan bagian dari koneki tersebut. RELATED digunakan untuk paket-paket yang bukan bagian dari koneksi tetapi masih berhubungan dengan koneksi tersebut, contohnya adalah FTP data transfer yang menyertai sebuah koneksi TCP atau UDP. INVALID adalah paket yang tidak bisa diidentifikasi, bukan merupakan bagian dari koneksi yang ada.
iptables –A INPUT –m state --state RELATED,ESTABLISHED
7. Target/Jump
Target atau jump adalah perlakuan yang diberikan terhadap paket-paket yang memenuhi kriteria atau match. Jump memerlukan sebuah chain yang lain dalam tabel yang sama. Chain tersebut nantinya akan dimasuki oleh paket yang memenuhi kriteria. Analoginya ialah chain baru nanti berlaku sebagai prosedur/fungsi dari program utama. Sebagai contoh dibuat sebuah chain yang bernama tcp_packets. Setelah ditambahkan aturan-aturan ke dalam chain tersebut, kemudian chain tersebut akan direferensi dari chain input.
iptables –A INPUT –p tcp –j tcp_packets
Target | Keterangan |
-j ACCEPT | Ketika paket cocok dengan daftar match dan target ini diberlakukan, maka paket tidak akan melalui baris-baris aturan yang lain dalam chain tersebut atau chain yang lain yang mereferensi chain tersebut. Akan tetapi paket masih akan memasuki chain-chain pada tabel yang lain seperti biasa. |
-j DROP | Target ini men-drop paket dan menolak untuk memproses lebih jauh. Dalam beberapa kasus mungkin hal ini kurang baik, karena akan meninggalkan dead socket antara client dan server. Paket yang menerima target DROP benar-benar mati dan target tidak akan mengirim informasi tambahan dalam bentuk apapun kepada client atau server. |
-j RETURN | Target ini akan membuat paket berhenti melintasi aturan-aturan pada chain dimana paket tersebut menemui target RETURN. Jika chain merupakan subchain dari chain yang lain, maka paket akan kembali ke superset chain di atasnya dan masuk ke baris aturan berikutnya. Apabila chain adalah chain utama misalnya INPUT, maka paket akan dikembalikan kepada kebijakan default dari chain tersebut. |
-j MIRROR | Apabila kompuuter A menjalankan target seperti contoh di atas, kemudian komputer B melakukan koneksi http ke komputer A, maka yang akan muncul pada browser adalah website komputer B itu sendiri. Karena fungsi utama target ini adalah membalik source address dan destination address. Target ini bekerja pada chain INPUT, FORWARD dan PREROUTING atau chain buatan yang dipanggil melalui chain tersebut. |
Beberapa target yang lain biasanya memerlukan parameter tambahan:
a. LOG Target
Ada beberapa option yang bisa digunakan bersamaan dengan target ini. Yang pertama adalah yang digunakan untuk menentukan tingkat log. Tingkatan log yang bisa digunakan adalah debug, info, notice, warning, err, crit, alert dan emerg.Yang kedua adalah -j LOG --log-prefix yang digunakan untuk memberikan string yang tertulis pada awalan log, sehingga memudahkan pembacaan log tersebut.
iptables –A FORWARD –p tcp –j LOG --log-level debug
iptables –A INPUT –p tcp –j LOG --log-prefix “INPUT Packets”
b. REJECT Target
Secara umum, REJECT bekerja seperti DROP, yaitu memblok paket dan menolak untuk memproses lebih lanjut paket tersebut. Tetapi, REJECT akan mengirimkan error message ke host pengirim paket tersebut. REJECT bekerja pada chain INPUT, OUTPUT dan FORWARD atau pada chain tambahan yang dipanggil dari ketiga chain tersebut.
iptables –A FORWARD –p tcp –dport 22 –j REJECT --reject-with icmp-host-unreachable
Ada beberapa tipe pesan yang bisa dikirimkan yaitu icmp-net-unreachable, icmp-host-unreachable, icmp-port-unreachable, icmp-proto-unrachable, icmp-net-prohibited dan icmp-host-prohibited.
c. SNAT Target
Target ini berguna untuk melakukan perubahan alamat asal dari paket (Source Network Address Translation). Target ini berlaku untuk tabel nat pada chain POSTROUTING, dan hanya di sinilah SNAT bisa dilakukan. Jika paket pertama dari sebuah koneksi mengalami SNAT, maka paket-paket berikutnya dalam koneksi tersebut juga akan mengalami hal yang sama.
iptables –t nat –A POSTROUTING –o eth0 –j SNAT
--to-source 194.236.50.155-194.236.50.160:1024-32000
d. DNAT Target
Berkebalikan dengan SNAT, DNAT digunakan untuk melakukan translasi field alamat tujuan (Destination Network Address Translation) pada header dari paket-paket yang memenuhi kriteria match. DNAT hanya bekerja untuk tabel nat pada chain PREROUTING dan OUTPUT atau chain buatan yang dipanggil oleh kedua chain tersebut.
iptables –t nat –A PREROUTING –p tcp –d 15.45.23.67 --dport 80 –j DNAT
--to-destination 192.168.0.2
e. MASQUERADE Target
Secara umum, target MASQUERADE bekerja dengan cara yang hampir sama seperti target SNAT, tetapi target ini tidak memerlukan option --to-source. MASQUERADE memang didesain untuk bekerja pada komputer dengan koneksi yang tidak tetap seperti dial-up atau DHCP yang akan memberi pada kita nomor IP yang berubah-ubah.
Seperti halnya pada SNAT, target ini hanya bekerja untuk tabel nat pada chain POSTROUTING.
iptables –t nat –A POSTROUTING –o ppp0 –j MASQUERADE
f. REDIRECT Target
Target REDIRECT digunakan untuk mengalihkan jurusan (redirect) paket ke mesin itu sendiri. Target ini umumnya digunakan untuk mengarahkan paket yang menuju suatu port tertentu untuk memasuki suatu aplikasi proxy, lebih jauh lagi hal ini sangat berguna untuk membangun sebuah sistem jaringan yang menggunakan transparent proxy. Contohnya kita ingin mengalihkan semua koneksi yang menuju port http untuk memasuki aplikasi http proxy misalnya squid. Target ini hanya bekerja untuk tabel nat pada chain PREROUTING dan OUTPUT atau pada chain buatan yang dipanggil dari kedua chain tersebut.
iptables –t nat –A PREROUTING –i eth1 –p tcp --dport 80 –j REDIRECT --to-port 3128
Tutuorial Squid bisa dilihat di Instalasi Squid, Banner Filter, Porn Filter, Limit Bandwith, Transparan Proxy bikinan mas Hanny.
4. Penutup
Demikian dasar-dasar dari IPTables beserta komponen-komponennya. Mungkin anda masih agak bingung tentang implementasi dari apa yang telah dijelaskan di atas. Insya Allah dalam tulisan yang akan datang, saya akan memberikan beberapa contoh kasus jaringan yang menggunakan IPTables. Yea.. may I have enough power to do it :)
5. Change Log
5 Agustus 2003
- Penulisan pertama dokumen ini
6. Referensi
- www.netfilter.org
- Manual page iptables
- Beberapa sumber yang lain, tapi saya lupa :)
Apache ssl php mysql
Apache with OpenSSL, MySQL, PHP on OpenSuSE 11.0
The goal of this guide is to setup a minimal OpenSUSE 11.0 system and take advantage of the tools that come shipped with it.
This may also serve as a primer for a myriad of web enabled applications available out there that make use of Apache, MySQL, and PHP. Parts of this document could be used as independent guides for installing and/or configuring each core service.
The document is just a rough guide not a sacred text. Do not blindly execute commands here without understanding them. Also, please keep in mind that your journey doesn’t end here. Observe best practices in tuning and securing your machine after performing the steps discussed in this document.
All steps were done using root via terminal interface. This may work using sudo with administrative privileges and even on a full GUI workstation environment. All packages will be installed and configured using the default shell of OpenSuSE; Bourne Again shell (bash).
Contents |
Pre-requisites and additional assumptions
- Successfully installed, configured, updated base installation of OpenSuSe 11.0
- Most of the procedures here are OpenSuSE-centric (e.g. Yast and Zypper)
- Basic knowledge of Unix and Unix-like environment
- Basic knowledge of computer networks
- Will make use of a dummy SSL certificate
- Actual commands are preceded by the host name and its present working directory e.g.:
linux-iifk:~ #
- To facilitate comparison (confusion), an actual screen output will be shown whenever a command is executed.
- System information
Linux 2.6.25.18-0.2-pae #1 SMP 2008-10-21 16:30:26 +0200 i686 athlon i386
Linux version 2.6.25.18-0.2-pae (geeko@buildhost)
(gcc version 4.3.1 20080507 (prerelease) [gcc-4_3-branch revision 135036]
(SUSE Linux) ) #1 SMP 2008-10-21 16:30:26 +0200
MySQL
Install the MySQL database:
linux-iifk:~ # yast2 -i mysql
Let’s configure our MySQL service to start automatically on boot-up:
linux-iifk:~ # chkconfig --add mysql
mysql 0:off 1:off 2:on 3:on 4:off 5:on 6:off
Verify if run levels 3 and 5 are toggled “on” as shown above.
Start the Mysql Service:
linux-iifk:~ # rcmysql start
Creating MySQL privilege database...
Installing MySQL system tables...
OK
Filling help tables...
OK
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h linux-iifk.site password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
Updating MySQL privilege database...
Looking for 'mysql' in: /usr/bin/mysql
Looking for 'mysqlcheck' in: /usr/bin/mysqlcheck
Running 'mysqlcheck'...
mysql.columns_priv OK
mysql.db OK
mysql.func OK
mysql.help_category OK
mysql.help_keyword OK
mysql.help_relation OK
mysql.help_topic OK
mysql.host OK
mysql.proc OK
mysql.procs_priv OK
mysql.tables_priv OK
mysql.time_zone OK
mysql.time_zone_leap_second OK
mysql.time_zone_name OK
mysql.time_zone_transition OK
mysql.time_zone_transition_type OK
mysql.user OK
Running 'mysql_fix_privilege_tables'...
OK
Starting service MySQL done
Let’s use netstat to check if the MySQL process is running:
linux-iifk:~ # netstat -lpt | grep "mysql"
tcp 0 0 *:mysql *:* LISTEN 2962/mysqld
As shown earlier, you can install and configure your MySQL Database manually or using a script. For this example we’re going to use mysql_secure_installation script. Also, please think of a strong password that contains at least 10 mixed case alpha-numeric characters for your MySQL root password.
linux-iifk:~ # /usr/bin/mysql_secure_installation
A message like this will appear:
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
Since this is a fresh installation just press enter.
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
At this point, press enter to confirm the suggested answer (written on caps) for each question. Read them carefully as this may not be applicable for you. For this installation, the suggested answers were all accepted.
Set root password? [Y/n]
New password: (type your password, should be more than 10 characters)
Re-enter new password: (re-type your password)
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n]
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n]
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n]
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MySQL installation should now be secure.
Thanks for using MySQL!
For this setup mysql root is not allowed to login remotely. Let’s verify our database installation and root account using a localhost login.
linux-iifk:~ # mysql -u root -p
Enter password:
A successful login will yield this output:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.0.51a SUSE MySQL RPM
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
To return to your shell, either type \q or exit and press enter. MySQL monitor will bid you farewell with a message “Bye”. :)
mysql> \q
PHP
Installing PHP is simple:
linux-iifk:~ # yast2 -i php5
Install additional modules for PHP (MySQL for this installation):
linux-iifk:~ # yast2 -i php5-mysql
Note: If you need to add other PHP5 modules see additional notes section.
Apache
In this step, Apache 2.2 Server MPM Prefork will be installed.
linux-iifk:~ # yast2 -i apache2
Install PHP module for Apache:
linux-iifk:~ # yast2 -i apache2-mod_php5
As with most Linux distributions, package layout varies. Also, check your config file if the APACHE_MODULES line contains php5:
linux-iifk:~ # less /etc/sysconfig/apache2
Your output should have a line similar to this:
APACHE_MODULES="actions alias auth_basic authn_file authz_host authz_groupfile authz_default authz_user authn_dbm autoindex cgi dir env expires include log_config mime negotiation setenvif ssl suexec userdir php5"
Let’s configure Apache to automatically start at boot time:
linux-iifk:~ # chkconfig --add apache2
apache2 0:off 1:off 2:off 3:on 4:off 5:on 6:off
Verify if run levels 3 and 5 are toggled “on” as shown above.
Starting your httpd server Apache:
linux-iifk:~ # rcapache2 start
Starting httpd2 (prefork) done
To check what version of Apache is installed and important files/directories are located for this box:
linux-iifk:~ # httpd2 -V
Server version: Apache/2.2.8 (Linux/SUSE)
Server built: Sep 25 2008 11:59:55
Server's Module Magic Number: 20051115:11
Server loaded: APR 1.2.12, APR-Util 1.2.12
Compiled using: APR 1.2.12, APR-Util 1.2.12
Architecture: 32-bit
Server MPM: Prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APACHE_MPM_DIR="server/mpm/prefork"
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=128
-D HTTPD_ROOT="/srv/www"
-D SUEXEC_BIN="/usr/sbin/suexec2"
-D DEFAULT_PIDLOG="/var/run/httpd2.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_LOCKFILE="/var/run/accept.lock"
-D DEFAULT_ERRORLOG="/var/log/apache2/error_log"
-D AP_TYPES_CONFIG_FILE="/etc/apache2/mime.types"
-D SERVER_CONFIG_FILE="/etc/apache2/httpd.conf"
As shown earlier, not only the module of php5 is enabled but ssl as well; “ssl_module (shared)”. The next step would be enabling ssl module with Apache with a bash script called a2enflag.
linux-iifk:~ # a2enflag SSL
The command above modifies your /etc/sysconfig/apache2 file from:
APACHE_SERVER_FLAGS=""
to
APACHE_SERVER_FLAGS=" SSL"
Alternatively, you can use your favorite text editor and type the " SSL" at the APACHE_SERVER_FLAGS line.
Let’s restart Apache:
linux-iifk:~ # rcapache2 restart
Syntax OK
Shutting down httpd2 (waiting for all children to terminate) done
Starting httpd2 (prefork) done
Check if the machine listens for http and https traffic
linux-iifk:~ # netstat -lpt | grep "http"
tcp 0 0 *:www-http *:* LISTEN 3212/httpd2-prefork
tcp 0 0 *:https *:* LISTEN 3212/httpd2-prefork
Configure firewall (Netfilter)
The configuration of Netfilter has been done by most via command line using Iptables. For this installation, the Yast utility will be used for configuring the packet filtering instead of creating a custom iptables script.
linux-iifk:~ # yast2
The blue Yast2 Control Center console appears on your screen. (to navigate just use arrow keys and tab button)
Press arrow down to select Security and Users:
Press tab to select Firewall at the right pane and press enter.
Press arrow down to highlight Allowed Services and press enter. Kindly check if the entry Apache 2 is present on Service to Allow portion.
Press tab (7 times) until you reach Advanced then press enter.
Press tab (2 times) until you you reach the TCP Ports and type 80 443.
Press tab (4 times) until you reach OK and press enter.
Press tab (3 times) until you reach Next.
A configuration summary indicating your changes to your system. Kindly check if:
+ TCP Ports: 443, 80 is shown on Open Services, Ports and Protocol
Press enter or F10 to confirm changes and go back to the main menu of Yast2 Control Center.
Press tab twice again and press enter to quit (F9).
To verify if your system allows http (80) and https (443) traffic to pass through your box:
linux-iifk:~ # iptables -L | less
Check for lines similar to this:
ACCEPT tcp -- anywhere anywhere tcp dpt:https
ACCEPT tcp -- anywhere anywhere tcp dpt:http
Enabling Secure Socket Layer for Apache
Generate a dummy SSL keys for this server using a bash script gensslcert. Please take note that this command will place a file /srv/www/htdocs named CA.crt
linux-iifk:~ # gensslcert
comment mod_ssl server certificate
name
C XY
ST unknown
L unknown
U web server
O SuSE Linux Web Server
CN linux-iifk.site
email webmaster@linux-iifk.site
srvdays 730
CAdays 2190
creating CA key ...
710687 semi-random bytes loaded
Generating RSA private key, 2048 bit long modulus
..................+++
...........+++
e is 65537 (0x10001)
creating CA request/certificate ...
`/etc/apache2/ssl.crt/ca.crt' -> `/srv/www/htdocs/CA.crt'
creating server key ...
710687 semi-random bytes loaded
Generating RSA private key, 1024 bit long modulus
.................++++++
...++++++
e is 65537 (0x10001)
creating server request ...
creating server certificate ...
Signature ok
subject=/C=XY/ST=unknown/L=unknown/O=SuSE Linux Web Server/OU=web server/CN=linux-iifk.site/emailAddress=webmaster@linux-iifk.site
Getting CA Private Key
Verify: matching certificate & key modulus
Verify: matching certificate signature
/etc/apache2/ssl.crt/server.crt: OK
Proceed to Apache’s virtual hosts directory:
linux-iifk:~ # cd /etc/apache2/vhosts.d/
Copy the vhost-ssl.template file to that that of your machine name. In this example it’s linux-iifk:
linux-iifk:/etc/apache2/vhosts.d # cp vhost-ssl.template linux-iifk.conf
Restart Apache service again
linux-iifk:~ # rcapache2 restart
Syntax OK
Shutting down httpd2 (waiting for all children to terminate) done
Starting httpd2 (prefork) done
Test the server for http connections
Let’s create a test.php file to test our installation
linux-iifk:~ # echo "" >> /srv/www/htdocs/test.php
Open your favorite browser to check your server if it accepts http connection:
http://ip.address.ofyour.box/test.php
The output on your browser will be similar to this:
Please scroll down the output and look for for the MySQL section.
PHP Version 5.2.6
System Linux linux-iifk 2.6.25.18-0.2-pae #1 SMP 2008-10-21 16:30:26 +0200 i686
Build Date Aug 20 2008 18:19:14
--snip --
mysql
MySQL Support enabled
Active Persistent Links 0
Active Links 0
Client API version 5.0.51a
MYSQL_MODULE_TYPE external
MYSQL_SOCKET /var/lib/mysql/mysql.sock
MYSQL_INCLUDE -I/usr/include/mysql
MYSQL_LIBS -L/usr/lib -lmysqlclient
Test server for https connections
https://ip.address.ofyour.box/test.php
Since we have just made a dummy certificate, it should not come as a surprise if you receive warnings when you access the test page.
Below are messages encountered for several browsers and their suggested actions:
a) Firefox: The certificate is not trusted because the issuer certificate is unknown. (Error code: sec_error_unknown_issuer)
-- Click “Add an exception”, “Get Certificate”, “Confirm Security Exception”
b) Opera: The server’s certificate chain is incomplete and the signer(s) are not registered. Accept?
-- Click “Approve”
c) Lynx: SSL error: unable to get local issuer certificate - Continue ? (y)
-- Just press enter until you get the same output as http connection
The output should be the same as the one you saw using http connection.
Worth noting is the inclusion of the hardened php project :)
This server is protected with the Suhosin Patch 0.9.6.2 http://www.hardened-php.net/
Additional notes
- Remove test.php on the www directory after you have tested the system.
- Always restart Apache if you have loaded/removed modules by issuing:
linux-iifk:~ # rcapache2 restart
- This is also true for changes in PHP
- Should you need other modules for PHP like gd just replace php5-mysql in the install command. The command should be:
linux-iifk:~ # yast2 -i php5-gd
- The config file for PHP is /etc/php5/apache2/php.ini
- Log files for Apache are located at /var/log/apache2
- Default data directory for Apache is located at /srv/www/htdocs
You can check which shared modules are present by displaying all the Apache 2 modules by:
linux-iifk:~ # httpd2 -M
Loaded Modules:
core_module (static)
mpm_prefork_module (static)
http_module (static)
so_module (static)
actions_module (shared)
alias_module (shared)
auth_basic_module (shared)
authn_file_module (shared)
authz_host_module (shared)
authz_groupfile_module (shared)
authz_default_module (shared)
authz_user_module (shared)
authn_dbm_module (shared)
autoindex_module (shared)
cgi_module (shared)
dir_module (shared)
env_module (shared)
expires_module (shared)
include_module (shared)
log_config_module (shared)
mime_module (shared)
negotiation_module (shared)
setenvif_module (shared)
ssl_module (shared)
suexec_module (shared)
userdir_module (shared)
php5_module (shared)
Syntax OK
- The config file for MySQL is in /etc/my.cnf
- Log and data files are stored for mysql are stored at /var/lib/mysql
- Acessing the MySQL server remotely should have tcp port 3306 open on your firewall
- The command “zypper install” can be used instead of "yast2 -i"
- Please do not forget to lock down your system.
- Many,many,many thanks to OpenSuSE, Apache, PHP, Hardened PHP, MySQL, OpenSSL, Opera, Mozilla, Lynx, Vi, KDE, GNU and all Linux folks.
Further reading
For Yast 2 in text mode: http://www.novell.com/documentation/sled10/sled_deployment_sp1/index.html?page=/documentation/sled10/sled_deployment_sp1/data/cha_yast2.html
For tips/guide using zypper: http://en.opensuse.org/Zypper/Usage
For guides on how to configure/use Apache Version 2.2: http://httpd.apache.org/docs/2.2/
For guides on how to configure MySQL: http://dev.mysql.com/doc/refman/5.0/en/
For info about PHP: http://www.php.net/manual/en/
For info about packet filtering on GNU/Linux: http://www.netfilter.org/
For info about OpenSSL: http://httpd.apache.org/docs/2.2/ssl/ http://www.openssl.org/
Backup dan Restore di MySQL
I. Pendahuluan
II. Merencanakan dan Mempersiapkan backup
a. Menentukan seberapa sering backup
b. Menamakan file backup
c Menyimpan File Backup
III. Macam-Macam Backup
a. Backup Satu Database
# mysqldump -u [username] -p[password] [nama_database] > [nama-file].sql # mysqldump -u root -p123456 latihan > latihan.sql b. Backup Tabel Database
# mysqldump -u [username] -p[password] [nama_database] [table1] [table2] ... > [nama-file].sql # mysqldump -u root -p123456 latihan tes1 tes2 > duatabel.sql c. Backup Dua Atau Lebih Database
# mysqldump -u [username] -p[password] --database [database1] [database2] ... > [namafile].sql # mysqldump -u root -p123456 --database latihan teman > gabung.sql d. Backup Seluruh Database
# mysqldump -u [username] -p[password] --all-databases > [namafile].sql # mysqldump -u root -p123456 --all-databases > gabungan.sql e. Backup Struktur Data
# mysqldump -u [username] -p[password] --no-data [database] > [namafile].sql # mysqldump -u [username] -p[password] --no-data --databases [database1] [database2] > [namafile].sql f. Backup Kompress Database
# mysqldump -u [username] -p[password] [database] | bzip2 -c > [namafile].sql.bz2 # mysqldump -u [username] -p[password] [database] | gzip -c > [namafile].sql.gz # mysqldump -u root -p123456 latihan | bzip2 -c > latihan.sql.bz2 # mysqldump -u root -p123456 latihan | gzip > latihan.sql.gzip Jika kita ingin melakukan variasi backup seperti backup seluruh database, backup beberapa tabel saja atau yang lainnya maka hanya diubah sebelum tanda | dengan pola mengikuti format sebelumnya. Misalnya kita ingin membackup seluruh database mysql dan hasilnya berupa kompresan dengan gabungan maka perintahnya:
# mysqldump -u root -p123456 --all-databases | gzip > gabungan.sql.gzip # mysqldump -u root -p123456 --all-databases | bzip2 -c > gabungan.sql.bz2 g. Skrip Untuk Backup
#!/bin/sh mysqldump --all-databases | gzip > /var/backup/backup-`date -I`.sql.gz IV. Macam-Macam Restore
a. Restore Database
# mysql -u [username] -p[password] [database] < [file_backup].sql # mysql -u root -p123456 testing <> b. Restore Database Hasil Kompres
# gunzip < [file_backup] | mysql -u [username] -p[password] [database] # gunzip <> # bunzip2 < [file_backup] | mysql -u [username] -p[password] [database] # bunzip2 <> c. Backup dan Restore di Server yang lain
# mysqldump -u [username] -p[password] [database] | mysql -u [username] -p[password] --host=[alamat_ip] -C [database] > create database monitoring_baru; > grant all on testing.* to
susan@192.16This e-mail address is being protected from spambots, you need JavaScript enabled to view it .1.3 identified by 'password'; > flush privilleges; # mysqldump -u root -p123456 monitoring | mysql -u susan -ppassword --host=192.168.1.2 -C monitoring_baru >use monitoring_baru; > show tables; Jika tidak ada error, maka file hasil backup database monitoring akan berada di database monitoring_baru
Referensi
www.devshed.comhttp://mrothouse.wordpress.com/

