<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>alexalmazan.com: IPTables and libwrap</title>
    <link>http://alexalmazan.com/articles/2008/05/03/iptables</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>atypical typo tech rhelish</description>
    <item>
      <title>IPTables and libwrap</title>
      <description>&lt;p&gt;&lt;strong&gt;&lt;span style="color:green;"&gt;Secruing Services&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;&lt;span style="color:red;"&gt;&lt;span class="caps"&gt;NOTE&lt;/span&gt;&lt;/span&gt; choose the easiest otions first such as TCPwrappers to control service connections.&lt;/p&gt;


&lt;pre&gt;
/etc/hosts.allow
/etc/hosts.deny
&lt;/pre&gt;

	&lt;p&gt;man page syntax identical:&lt;/p&gt;


basic syntax is :
&lt;daemon&gt;: &lt;client&gt;

ex. to allow ssh connections for &lt;span class="caps"&gt;SSH&lt;/span&gt;
&lt;pre&gt;
sshd: 192.168.2.200
&lt;/pre&gt;

	&lt;p&gt;These files are parse in the following order:&lt;/p&gt;


	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;/etc/hosts.allow&lt;/td&gt;
			&lt;td&gt;If the configuration of this file permits the requested connection, the connection is immediately allowed&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;/etc/hosts.deny&lt;/td&gt;
			&lt;td&gt;If the configuration of this file does not permit the requested connection, the connection is immediately refused.&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;




	&lt;p&gt;&lt;span style="color:red;"&gt;&lt;span class="caps"&gt;TCP&lt;/span&gt; wrappers&lt;/span&gt; can &lt;span style="color:red;"&gt;only&lt;/span&gt; be run on packages &lt;span style="color:red;"&gt;compiled agains libwrap&lt;/span&gt;. &lt;span style="color:blue;"&gt;ldd&lt;/span&gt; can
be used to check if it has been compiled against libwrap.&lt;/p&gt;


	&lt;p&gt;example for checking  with &amp;#8216;ldd&amp;#8217;&lt;/p&gt;


&lt;pre&gt;
[root@station home]# ldd /usr/sbin/sendmail.sendmail |grep wrap
        libwrap.so.0 =&amp;gt; /usr/lib/libwrap.so.0 (0x006a8000)
&lt;/pre&gt;

portmap is another service , but it is a bit convoluted:
&lt;pre&gt;
[root@station home]# strings /sbin/portmap |grep hosts
hosts_access_verbose
hosts_allow_table
hosts_deny_table
/etc/hosts.allow
/etc/hosts.deny
&lt;/pre&gt;

	&lt;p&gt;here are others configured against libwrap:&lt;/p&gt;


	&lt;p&gt;ssh,sendmail,xinetd,vsftpd,stunnel
&lt;del&gt;-&lt;/del&gt;
Two choices when configuring hosts.allow|deny in the following example.
permit connections for vsftp from 10.1.1.1, but block from
10.0.0.0/255.0.0.0&lt;/p&gt;


	&lt;p&gt;you can make seperate entries in both:&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;hosts.deny&lt;/strong&gt;&lt;/p&gt;


&lt;pre&gt;
vsftpd: 10.0.0.0/255.0.0.0
&lt;/pre&gt;

	&lt;p&gt;&lt;strong&gt;hosts.allow&lt;/strong&gt;&lt;/p&gt;


&lt;pre&gt;
vsftpd: 10.1.1.1
&lt;/pre&gt;

	&lt;p&gt;Or you can add this to just the deny file as suggested with the keyword&lt;/p&gt;


	&lt;p&gt;&amp;#8216;EXCEPT&amp;#8217;&lt;/p&gt;


&lt;pre&gt;
# 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!
#vsftpd: 10.0.0.0/255.0.0.0
vsftpd: 10.0.0.0/255.0.0.0 EXCEPT 10.1.1.1
&lt;/pre&gt;

	&lt;p&gt;&amp;#8216;ALL&amp;#8217; can be used in the config as the service or the connecting source:&lt;/p&gt;


&lt;pre&gt;
ALL:ALL EXCEPT 192.168.1.1 
&lt;/pre&gt;

	&lt;p&gt;In /etc/hosts.deny to block all service affected by libwrap, but open to the one source or&lt;/p&gt;


&lt;pre&gt;
ALL:ALL 
&lt;/pre&gt;
In hosts.deny, with explicit service permissions in /etc/hosts.allow.

	&lt;p&gt;&amp;#8216;DenyHosts is a script from sourceforge that you can run from cron to
parse /var/log/secure to review those that have attempted multiple brute force attacks&amp;#8217;
(&amp;#8216;swatch&amp;#8217;-&amp;#8216;pamABL&amp;#8217; are similar scripts, but modify iptables)&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;&lt;span style="color:green;"&gt;IPTables&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;Permits packet filterring at the kernel level. Netfilter is the kernel module that does the dirty work, iptables helps define the rules/chains to permit/deny through the kernels ip stack.&lt;/p&gt;


	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;strong&gt;&lt;span class="caps"&gt;INPUT&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;responsible for inbound destined for the server&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;strong&gt;&lt;span class="caps"&gt;OUTPUT&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;responsible for outbound traffic leaving the server&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;strong&gt;&lt;span class="caps"&gt;FORWARD&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;across the servers interfaces&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;




&lt;pre&gt;
iptables -L 
&lt;/pre&gt;

	&lt;p&gt;lists or prints the current rules in place&lt;/p&gt;


&lt;pre&gt;
iptables -L -n -v -t nat
&lt;/pre&gt;

	&lt;p&gt;&lt;span style="color:blue;"&gt;iptables-save&lt;/span&gt; writes to /etc/sysconfig/iptables. (keeping rules persistent,they must be apart of this file)&lt;/p&gt;


	&lt;p&gt;Configuration parsed from top to bottom. IPTables will response based on the first match. If there is no specific match, the chain policy will apply.&lt;/p&gt;


	&lt;p&gt;IPtables uses targets to determine what action will be taken if traffic matches an existing rule.&lt;/p&gt;


	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;strong&gt;&lt;span class="caps"&gt;DROP&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;will drop package and send no info to the user&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;strong&gt;&lt;span class="caps"&gt;REJECT&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;will send a connection refused notice back to the sender&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;strong&gt;&lt;span class="caps"&gt;ACCEPT&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;will permit the connection&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;strong&gt;&lt;span class="caps"&gt;LOG&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;will log the connection attempt&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;




	&lt;p&gt;IPTables syntax rule formulation help:-&lt;/p&gt;


	&lt;p&gt;What chain will the rule apply to?&lt;/p&gt;


&lt;pre&gt;
-A INPUT
&lt;/pre&gt;
What patterns(s) would you like to check for?

&lt;pre&gt;
-s 192.168.2.100
&lt;/pre&gt;

	&lt;p&gt;To make the rule active, you can add the following info to
/etc/sysconfig/iptables&lt;/p&gt;


&lt;pre&gt;
-A INPUT -s 192.168.2.100 -j REJECT
&lt;/pre&gt;

	&lt;p&gt;You can also configure the rule from &lt;span class="caps"&gt;CLI&lt;/span&gt; with&lt;/p&gt;


&lt;pre&gt;
iptables -A INPUT -s 192.168.2.100 -j REJECT
&lt;/pre&gt;

	&lt;p&gt;What should IPTables do when a matching pattern is found?&lt;/p&gt;


&lt;pre&gt;
-j REJECT
&lt;/pre&gt;

	&lt;p&gt;You can also match on the following criteria:&lt;/p&gt;


	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;strong&gt;-i&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;incoming interface&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;strong&gt;-p&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;protocol&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;strong&gt;-s&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;source ip address&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;strong&gt;-d&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;destination ip address&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;strong&gt;-dport&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;destination port&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;




	&lt;p&gt;Saving the rules &lt;span style="color:blue;"&gt;service iptables-save&lt;/span&gt;&lt;/p&gt;


&lt;pre&gt;
iptables -D INPUT 3 
&lt;/pre&gt;

	&lt;p&gt;This command will delete the third rule in the &lt;span class="caps"&gt;INPUT CHAIN&lt;/span&gt;
A quick means of identifying line number is:&lt;/p&gt;


&lt;pre&gt;
iptables -L --line-numbers
&lt;/pre&gt;

&lt;pre&gt;
iptables -D INPUT &amp;lt;rule&amp;gt; 
&lt;/pre&gt;

	&lt;p&gt;This command will delete the specific rule from
the &lt;span class="caps"&gt;INPUT&lt;/span&gt; chain.&lt;/p&gt;


&lt;pre&gt;
iptables -F 
&lt;/pre&gt;

	&lt;p&gt;This command will flush the IPTables rulesets&lt;/p&gt;


	&lt;p&gt;&lt;span style="color:blue;"&gt;system-config-securitylevel&lt;/span&gt;  utility used to create the following config&lt;/p&gt;


&lt;pre&gt;
[root@station sysconfig]# cat iptables
# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT                             
---(permit the loopback)
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT                             
---(for ipsec)
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT                             
---(for ipsec)
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
---(multicast/Avahi)
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT            ---(for cups)
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT            ---(for cups)
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j
ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j
ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j
ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m udp -p udp --dport 137 -j
ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m udp -p udp --dport 138 -j
ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 139 -j
ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 445 -j
ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j
ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j
ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 110 -j
ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 143 -j
ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 837 -j
ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 993 -j
ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 995 -j
ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
&lt;/pre&gt;

created the following chain to apply the rules in /etc/sysconfig/iptables:
&lt;pre&gt;
RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
&lt;/pre&gt;
This and the allowed outlined rules above where automagically put in place.

Considered a mostly closed configuration based on the &amp;#8216;ACCEPT&amp;#8217; policy. If you need to use the rulesets generated by &amp;#8216;system&amp;#8217; utlities comment out the last rule to keep from failing the test 
&lt;pre&gt;
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
&lt;/pre&gt;
&lt;span style="color:red;"&gt;service iptables panic&lt;/span&gt; changes to a default drop policy
&lt;pre&gt;
[root@station sysconfig]# service iptables panic
Flushing firewall rules:                                   [  OK  ]
Setting chains to policy DROP: filter                      [  OK  ]
[root@station13 sysconfig]# iptables -L -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source              
destination

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source              
destination

Chain OUTPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source              
destination
&lt;/pre&gt;

Put these back in place to permit connections
&lt;pre&gt;
[root@station sysconfig]# iptables -P INPUT ACCEPT
[root@station sysconfig]# iptables -P OUTPUT ACCEPT
&lt;/pre&gt;

&lt;pre&gt;
[root@station sysconfig]# iptables -L -v
Chain INPUT (policy ACCEPT 1 packets, 78 bytes)
 pkts bytes target     prot opt in     out     source              
destination

Chain FORWARD (policy DROP 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
&lt;/pre&gt;

	&lt;p&gt;configure your mail server not to accept connections from the
192.168.1.0/24 network, &lt;span class="caps"&gt;EXCEPT&lt;/span&gt; for the 192.168.1.2 host:&lt;/p&gt;


	&lt;p&gt;iptables example&lt;/p&gt;


&lt;pre&gt;
[root@station etc]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  10.0.0.0/8           anywhere            reject-with
icmp-port-unreachable
ACCEPT     tcp  --  192.168.1.2          anywhere            tcp dpt:smtp
REJECT     tcp  --  192.168.1.0/24       anywhere            tcp dpt:smtp
reject-with icmp-port-unreachable

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
&lt;/pre&gt;

&lt;pre&gt;
[root@station etc]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.3.5 on Thu Aug  9 16:08:07 2007
*filter
:INPUT ACCEPT [134:32679]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [47:4569]
-A INPUT -s 10.0.0.0/255.0.0.0 -j REJECT --reject-with icmp-port-unreachable
-A INPUT -s 192.168.1.2/255.255.255.255 -p tcp -m tcp --dport 25 -j ACCEPT
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp -m tcp --dport 25 -j REJECT
--reject-with icmp-port-unreachable
&lt;/pre&gt;

	&lt;p&gt;tcp wrapper example ( since this said sendmail, it must be the &lt;span class="caps"&gt;MTA&lt;/span&gt; running, not postfix, as postfix is not compiled against libwrap)&lt;/p&gt;


	&lt;p&gt;alternatives&amp;#8212;config mta&lt;/p&gt;


	&lt;p&gt;then /etc/hosts.deny:&lt;/p&gt;


&lt;pre&gt;
[root@station etc]# cat /etc/hosts.deny
#
# 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!
#vsftpd: 10.0.0.0/255.0.0.0
#vsftpd: 10.0.0.0/255.0.0.0 EXCEPT 10.1.1.1
sendmail: 192.168.1.0/24 EXCEPT 192.168.1.2
&lt;/pre&gt;
followed with rules for governing port 110/143:
&lt;del&gt;-&lt;/del&gt;
&lt;pre&gt;[root@station etc]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.3.5 on Thu Aug  9 16:08:07 2007
*filter
:INPUT ACCEPT [134:32679]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [47:4569]
-A INPUT -s 192.168.1.2/255.255.255.255 -p tcp -m tcp --dport 25 -j ACCEPT
-A INPUT -s 192.168.1.2/255.255.255.255 -p tcp -m tcp --dport 143 -j ACCEPT
-A INPUT -s 192.168.1.2/255.255.255.255 -p tcp -m tcp --dport 993 -j ACCEPT
-A INPUT -s 192.168.1.2/255.255.255.255 -p tcp -m tcp --dport 110 -j ACCEPT
-A INPUT -s 192.168.1.2/255.255.255.255 -p tcp -m tcp --dport 995 -j ACCEPT
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp -m tcp --dport 25 -j REJECT --reject-with icmp-port-unreachable
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp -m tcp --dport 143 -j REJECT --reject-with icmp-port-unreachable
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp -m tcp --dport 993 -j REJECT --reject-with icmp-port-unreachable
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp -m tcp --dport 110 -j REJECT --reject-with icmp-port-unreachable
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp -m tcp --dport 995 -j REJECT --reject-with icmp-port-unreachable
&lt;/pre&gt;</description>
      <pubDate>Sat, 03 May 2008 01:02:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:1d7ebdc0-72e4-4f1d-805f-8b28387233a9</guid>
      <author>aalmazan@rackspace.com (alex almazan)</author>
      <link>http://alexalmazan.com/articles/2008/05/03/iptables</link>
      <category>iptables</category>
      <category>libwrap</category>
    </item>
  </channel>
</rss>
