Iptables Örnekleri
Iptables kullanımı hakkında güzel örneklerin olduğu bir site
27,036 Okunma Henüz yorum yapılmamış 06/06/2011 11:56:23 24/10/2014 11:57:25
Linux comes with a host based firewall called Netfilter. According to the official project site:
netfilter is a set of hooks inside the Linux kernel that allows kernel modules to register callback functions with the network stack. A registered callback function is then called back for every packet that traverses the respective hook within the network stack.
This Linux based firewall is controlled by the program called iptables to handles filtering for IPv4, and ip6tables handles filtering for IPv6. I strongly recommend that you first read our quick tutorial that explains how to configure a host-based firewall called Netfilter (iptables) under CentOS / RHEL / Fedora / Redhat Enterprise Linux. This post list most common iptables solutions required by a new Linux user to secure his or her Linux operating system from intruders.
Type the following command as root:
# iptables -L -n -v
Sample outputs:
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
Above output indicates that the firewall is not active. The following sample shows an active firewall:
# iptables -L -n -v
Sample outputs:
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
Where,
# iptables -n -L -v --line-numbers
Sample outputs:
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
You can use line numbers to delete or insert new rules into the firewall.
# iptables -L INPUT -n -v
# iptables -L OUTPUT -n -v --line-numbers
If you are using CentOS / RHEL / Fedora Linux, enter:
# service iptables stop
# service iptables start
# service iptables restart
You can use the iptables command itself to stop the firewall and delete all 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
Where,
To display line number along with other information for existing rules, enter:
# 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
You will get the list of IP. Look at the number on the left, then use number to delete it. For example delete line number 4, enter:
# iptables -D INPUT 4
OR find source IP 202.54.1.1 and delete from rule:
# iptables -D INPUT -s 202.54.1.1 -j DROP
Where,
To insert one or more rules in the selected chain as the given rule number use the following syntax. First find out line numbers, enter:
# iptables -L INPUT -n --line-numbers
Sample outputs:
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
To insert rule between 1 and 2, enter:
# iptables -I INPUT 2 -s 202.54.1.2 -j DROP
To view updated rules, enter:
# iptables -L INPUT -n --line-numbers
Sample outputs:
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
To save firewall rules under CentOS / RHEL / Fedora Linux, enter:
# service iptables save
In this example, drop an IP and save firewall rules:
# iptables -A INPUT -s 202.5.4.1 -j DROP
# service iptables save
For all other distros use the iptables-save command:
# iptables-save > /root/my.active.firewall.rules
# cat /root/my.active.firewall.rules
To restore firewall rules form a file called /root/my.active.firewall.rules, enter:
# iptables-restore < /root/my.active.firewall.rules
To restore firewall rules under CentOS / RHEL / Fedora Linux, enter:
# service iptables restart
To drop all 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
To drop all incoming / forwarded packets, but allow outgoing traffic, enter:
# 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
IP spoofing is nothing but to stop the following IPv4 address ranges for private networks on your public interfaces. Packets with non-routable source addresses should be rejected using the following syntax:
# 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
To block an attackers ip address called 1.2.3.4, enter:
# iptables -A INPUT -s 1.2.3.4 -j DROP
# iptables -A INPUT -s 192.168.0.0/24 -j DROP
To block all service requests on port 80, enter:
# iptables -A INPUT -p tcp --dport 80 -j DROP
# iptables -A INPUT -i eth1 -p tcp --dport 80 -j DROP
To block port 80 only for an ip address 1.2.3.4, enter:
# 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
To block outgoing traffic to a particular host or domain such as cyberciti.biz, enter:
# host -t a cyberciti.biz
Sample outputs:
cyberciti.biz has address 75.126.153.206
Note down its ip address and type the following to block all outgoing traffic to 75.126.153.206:
# iptables -A OUTPUT -d 75.126.153.206 -j DROP
You can use a subnet as follows:
# iptables -A OUTPUT -d 192.168.1.0/24 -j DROP
# iptables -A OUTPUT -o eth1 -d 192.168.1.0/24 -j DROP
First, find out all ip address of facebook.com, enter:
# host -t a www.facebook.com
Sample outputs:
www.facebook.com has address 69.171.228.40
Find CIDR for 69.171.228.40, enter:
# whois 69.171.228.40 | grep CIDR
Sample outputs:
CIDR: 69.171.224.0/19
To prevent outgoing access to www.facebook.com, enter:
# iptables -A OUTPUT -p tcp -d 69.171.224.0/19 -j DROP
You can also use domain name, enter:
# iptables -A OUTPUT -p tcp -d www.facebook.com -j DROP
# iptables -A OUTPUT -p tcp -d facebook.com -j DROP
From the iptables man page:
... 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 ...
Type the following to log and block IP spoofing on public interface called 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
By default everything is logged to /var/log/messages file.
# tail -f /var/log/messages
# grep --color 'IP SPOOF' /var/log/messages
The -m limit module can limit the number of log entries created per time. This is used to prevent flooding your log file. To log and drop spoofing per 5 minutes, in bursts of at most 7 entries .
# 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
Use the following syntax:
# 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
Type the following command to block ICMP ping requests:
# iptables -A INPUT -p icmp --icmp-type echo-request -j DROP
# iptables -A INPUT -i eth1 -p icmp --icmp-type echo-request -j DROP
Ping responses can also be limited to certain networks or hosts:
# iptables -A INPUT -s 192.168.1.0/24 -p icmp --icmp-type echo-request -j ACCEPT
The following only accepts limited type of ICMP requests:
### ** 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
Use the following syntax to open a range of ports:
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 7000:7010 -j ACCEPT
Use the following syntax to open a range of IP address:
## 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
When you restart the iptables service it will drop established connections as it unload modules from the system under RHEL / Fedora / CentOS Linux. Edit, /etc/sysconfig/iptables-config and set IPTABLES_MODULES_UNLOAD as follows:
IPTABLES_MODULES_UNLOAD = no
Use the crit log level to send messages to a log file instead of console:
iptables -A INPUT -s 1.2.3.4 -p tcp --destination-port 80 -j LOG --log-level crit
The following shows syntax for opening and closing common TCP and UDP ports:
Replace ACCEPT with DROP to block port: ## open port ssh tcp port 22 ## iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 22 -j ACCEPT ## open cups (printing service) udp/tcp port 631 for LAN users ## iptables -A INPUT -s 192.168.1.0/24 -p udp -m udp --dport 631 -j ACCEPT iptables -A INPUT -s 192.168.1.0/24 -p tcp -m tcp --dport 631 -j ACCEPT ## allow time sync via NTP for lan users (open udp port 123) ## iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p udp --dport 123 -j ACCEPT ## open tcp port 25 (smtp) for all ## iptables -A INPUT -m state --state NEW -p tcp --dport 25 -j ACCEPT # open dns server ports for all ## iptables -A INPUT -m state --state NEW -p udp --dport 53 -j ACCEPT iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT ## open http/https (Apache) server port to all ## iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT ## open tcp port 110 (pop3) for all ## iptables -A INPUT -m state --state NEW -p tcp --dport 110 -j ACCEPT ## open tcp port 143 (imap) for all ## iptables -A INPUT -m state --state NEW -p tcp --dport 143 -j ACCEPT ## open access to Samba file server for lan users only ## iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 137 -j ACCEPT iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 138 -j ACCEPT iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 139 -j ACCEPT iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 445 -j ACCEPT ## open access to proxy server for lan users only ## iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 3128 -j ACCEPT ## open access to mysql server for lan users only ## iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
You can use connlimit module to put such restrictions. To allow 3 ssh connections per client host, enter:
# iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 3 -j REJECT
Set HTTP requests to 20:
# iptables -p tcp --syn --dport 80 -m connlimit --connlimit-above 20 --connlimit-mask 24 -j DROP
Where,
For more information about iptables, please see the manual page by typing man iptables from the command line:
$ man iptables
You can see the help using the following syntax too:
# iptables -h
To see help with specific commands and targets, enter:
# iptables -j DROP -h
Find out if ports are open or not, enter:
# netstat -tulpn
Find out if tcp port 80 open or not, enter:
# netstat -tulpn | grep :80
If port 80 is not open, start the Apache, enter:
# service httpd start
Make sure iptables allowing access to the port 80:
# iptables -L INPUT -v -n | grep 80
Otherwise open port 80 using the iptables for all users:
# iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
# service iptables save
Use the telnet command to see if firewall allows to connect to port 80:
$ telnet www.cyberciti.biz 80
Sample outputs:
Trying 75.126.153.206... Connected to www.cyberciti.biz. Escape character is '^]'. ^] telnet> quit Connection closed.
You can use nmap to probe your own server using the following syntax:
$ nmap -sS -p 80 www.cyberciti.biz
Sample outputs:
Starting Nmap 5.00 ( http://nmap.org ) at 2011-12-13 13:19 IST Interesting ports on www.cyberciti.biz (75.126.153.206): PORT STATE SERVICE 80/tcp open http Nmap done: 1 IP address (1 host up) scanned in 1.00 seconds
I also recommend you install and use sniffer such as tcpdupm and ngrep to test your firewall settings.
This post only list basic rules for new Linux users. You can create and build more complex rules. This requires good understanding of TCP/IP, Linux kernel tuning via sysctl.conf, and good knowledge of your own setup. Stay tuned for next topics:
Kaynak
Iptables kullanımı hakkında güzel örneklerin olduğu bir site
27,036 Okunma Henüz yorum yapılmamış 06/06/2011 11:56:23 24/10/2014 11:57:25
Özellikle bir yerel ağı internet için filitrelemek istiyorsanız Ipcop aradığınız çözümlerden biri olabilir.
24,543 Okunma 1 Yorum 27/04/2011 11:07:41
Internete bağlanan her sistem potansiyel saldiri tehlikelerine gebedir.Bir saldirgan direkt olarak veya kendini gizleyerek sisteme sızmak isteyebilir.Güncel Linux çekirdeklerinde kullanılan firewall'larin son gözdesi iptables, paket filtrelemesi yaparak erişim yetkisini belli bir seviyede tutar.Bilinmelidir ki sistem kurulmaya başladigi andan itibaren, yönetim politikasinin belirlenmeye başlamasi ve firewall'un bu politika üzerine inşası şarttır.Netfilter ( iptables ) kurulmadan önce, kullanıcının gereksiz servisleri çalıştırmaması ve gerekli olanlarda daha seçici davranması gerekir.
21,825 Okunma Henüz yorum yapılmamış 06/06/2011 09:32:30
Açık Kaynak Kodlu Firewall Projesi 3.0 Kurulum Bilgileri
21,506 Okunma Henüz yorum yapılmamış 13/10/2011 22:23:16 24/10/2014 11:50:48
Dosya, Kelime, Site, Url, İp bazlı erişim ve yasaklama işlemleri
21,090 Okunma Henüz yorum yapılmamış 13/08/2012 11:51:41 20/01/2014 14:37:04
Transparent modda 443 portunu squid acl leri ile engelleyemezsiniz. Iptables kullanarak kısıtlma yapabilirsiniz. Bu işi iptables ile yapabilirsiniz.
19,707 Okunma Henüz yorum yapılmamış 18/05/2011 11:57:30 24/10/2014 11:51:18
18,495 Okunma Henüz yorum yapılmamış 31/05/2011 11:56:10
smootwall ile ağ üzerinde kullanılan Remote access, File transfer, Email and News, Instant Messaging, Multimedia, Gaming, Web gibi protokollerden dilediğinizi yasaklayabilirsiniz.
18,254 Okunma Henüz yorum yapılmamış 10/08/2012 12:10:56 24/10/2014 12:06:40
SmoothWall Express Menüleri ve Kullanımı
18,102 Okunma Henüz yorum yapılmamış 13/10/2011 13:04:34 24/01/2014 13:06:24
IPCop tan daha iyi verim alabilmek için addon server kurup diğer addonları (eklentileri) kurmanız gerekiyor.
17,994 Okunma Henüz yorum yapılmamış 02/05/2011 11:44:35
Genel olarak transparent proxy kullanılsada, transparent olmayan proxy daha gelişmiş içerik filitrelemesi yapmaktadır. Aynı zamanda UltraSurf gibi HTTPS üzerinden webe çıkan uygulamalarda engellenmektedir.
17,736 Okunma Henüz yorum yapılmamış 17/10/2011 12:51:44 24/10/2014 11:52:42
İçerisinde Clamav ve Fprot virüs tarayıcıları olan ve spam maillere kadar filitreleyen bir ipcop eklentisi
17,400 Okunma Henüz yorum yapılmamış 31/05/2011 14:39:30
Açık Kaynak Kodlu Firewall Projesi SmoothWall Express 3.1 Kurulum Bilgileri
17,366 Okunma Henüz yorum yapılmamış 13/01/2014 11:29:29 20/01/2014 11:32:27
SmoothWall ve IpCop için kurul aynı sadece kaynak dosyalar farklıdır...
17,103 Okunma Henüz yorum yapılmamış 31/05/2011 15:44:45
Smootwall ve URLFilter eklentisi ile filitrelediğiniz sistemde bazı bilgisayarların filitreleme dışında kalmasını isterseniz aşağıdakileri yapmalısınız.
15,180 Okunma Henüz yorum yapılmamış 04/04/2013 17:31:54 04/04/2013 17:32:13
Ultrasurf, Hotspotshild gibi https üzerinden veri çekebilen ve böylelikle web filitrelerini aşan yazılımlara karşı smoothwall ile başa çıkmanız mümkün.
15,161 Okunma Henüz yorum yapılmamış 04/04/2013 12:52:14 24/10/2014 11:54:49
Toplam internet hızınızı var olan bilgisayarlarınıza adil olarak paylaştırmak için smoowwall Download throttling ayarlarını kullanabilirsiniz.
13,558 Okunma Henüz yorum yapılmamış 24/01/2014 11:23:13 11/04/2014 11:25:07
iptables ile youtube.com, facebook.com gibi bazı özel adresleri nasıl engelleyebilirsiniz?
13,438 Okunma Henüz yorum yapılmamış 20/02/2014 13:11:40 24/10/2014 11:48:37
smoothwall web filitre Sunucunuz varsa web kullanıcılarınıza kullanıcı adı ve şifre karşılığı web hizmeti verebilirsiniz.
13,375 Okunma Henüz yorum yapılmamış 24/01/2014 15:41:11 24/01/2014 15:42:57
Çeşitli smoothwall sorunları için çözümler
12,485 Okunma Henüz yorum yapılmamış 11/02/2014 14:54:15 02/12/2015 14:54:34
IP Tables, güvenlik duvarı için Linux'ta kullanılan temel araçlardan birisidir. Linux'un çekirdek uzayındaki güvenlik duvarı gerçekleştirimi olan netfilter ile etkileşime giren, kullanıcı uzayında çalışan bir takım araçların bir araya gelmesinden oluşmuştur.
8,149 Okunma Henüz yorum yapılmamış 16/02/2016 00:15:15
Httpsli URLleri engelemek için bu sitelerin ipsini yasaklamak yeterli.
2,686 Okunma Henüz yorum yapılmamış 05/03/2020 13:50:44 05/03/2020 13:51:17
Sistem güvenliği ve kullanıcıların kontrol altında tutulması için piyasada bir çokfirewall ve benzeri gerek donanımsal gerekse yazılımsal olarak bir çok ürün bulunmaktadır. Pfsense ?de BSD tabanlıyazılımsal bir firewalldır. Ürününmsn yada program yasklama,web içerik filtreleme,kullanıcı kısıtlama ve vpn gibi çok kullanışlı özellikleri bulunmaktadır.
2,447 Okunma Henüz yorum yapılmamış 05/02/2020 11:17:43 06/02/2020 14:37:06
Makalemizin bu kısmında sizlere web filtreleme, kullanıcı bazlı kısıtlama yapabileceğimiz squid ve squidguard paketlerinin kurulumu ve yapılandırması hakkında bilgi vereceğim. Makale serimizin önceki bölümlerinde Pfsense hakkında yeteri kadar açıklama ve ön bilgi verdiğim için lafı fazla uzatmadan konun anlatımına geçmek istiyorum.
2,395 Okunma Henüz yorum yapılmamış 07/01/2020 10:56:37 07/01/2020 10:57:07
2,377 Okunma Henüz yorum yapılmamış 07/01/2020 10:54:45
Pfsense konulu makalemizin serimizin bu bölümünde 5651 yasalı kanun gereği kullanıcılarımız internet log ?larının kayıt altına alınması konusunu inceleyeceğiz.
2,300 Okunma Henüz yorum yapılmamış 07/01/2020 10:53:20