Difference between pages "Apache" and "IPTables"

From Wikislax
(Difference between pages)
Jump to: navigation, search
(Created page with "{{RightTOC}} == What is Apache ? == [http://httpd.apache.org Apache] is the most widespread and powerful Open Source HTTP server. Apache is included in the base Slackware d...")
 
(Iptables Filtering)
 
Line 1: Line 1:
 
{{RightTOC}}
 
{{RightTOC}}
  
== What is Apache ? ==
+
Packet filtering affords opening access only to these services you have decided to open. The TCP or UDP packets include a piece of information called the port number, that is used to identify the type of service. Secure ports were defined as SSL counterparts of the native ports but were superseded by [https://en.wikipedia.org/wiki/Transport_Layer_Security TLS] and are now deprecated due to security weaknesses in the SSL protocol. SSL should not be used any longer. Instead, use TLS. Current version is v1.2.
  
[http://httpd.apache.org Apache] is the most widespread and powerful Open Source HTTP server.
+
{| {{thead}}
 +
|-
 +
! {{chead}} width="100" | Protocol
 +
! {{chead}} | Port #
 +
! {{chead}} | Secure Protocol
 +
! {{chead}} | Secure Port #
 +
! {{chead}} | Service
 +
|-
 +
|SMTP||25||SMTPS||465||Mail exchange
 +
|-
 +
|HTTP||80||HTTPS||443||Web browsing
 +
|-
 +
|POP3||110||POP3S||995||Mail retrieval
 +
|-
 +
|NTTP||119||NTTPS||563||News exchange
 +
|-
 +
|IMAP||143||IMAPS||993||Mail retrieval
 +
|-
 +
|LDAP||389||LDAPS||636||Ldap Directory
 +
|}
  
Apache is included in the base Slackware distribution so it is possible to skip installation alltogether and proceed to [[Apache#Configuring Apache|Configuring Apache]] below. As part of Slackware, Apache benefits from Slackware security advisories.
+
<br clear=all>
  
== Installing Apache ==
+
On server side, the services are provided by applications that may have vulnerabilities and be attacked. Examples of attacks are buffer overflow or format string attacks, that afford getting full access on the target machine by crafting special strings sent to it. An attacker could then obtain any information present there or modify or destroy the system.
  
[http://httpd.apache.org/download.cgi Download] Apache and untar under /usr/local. The [http://httpd.apache.org/docs/2.2 documentation] is available online. By default apache is installed in '''/usr/local/apache2'''. Here we split apache in directories '''/etc''', '''/usr/local''' and '''/var/www'''. '''--enable-authnz-ldap''' and '''--enable-ldap''' enable LDAP based authentication, '''--enable-ssl''' enables SSL/TLS support, '''--with-openssl''' specifies the location of the openssl source and '''--enable-so''' is required for '''PHP''', to be installed at the next step.
+
To reduce the number of possible attacks, the number of services authorized, or who can access the system, must be restricted. This is known as packet filtering. It is only an aspect of security (obviously, the applications on the server side must also be secured ...), but it is important. <u>Never *** ever *** connect to the network a computer not protected by a packet filter !</u>
  
# groupadd apache
+
To illustrate, let's configure our two-interfaces computer to be its own firewall. '''eth0''' is the Internet interface, it uses network 192.168.0.x, the gateway is an ADSL router/switch at 192.168.0.254. '''eth1''' is the (Intranet) interface to the internal network 192.168.1.x.
# useradd -g apache apache
 
# tar -C /usr/local -xvf httpd-x.y.z.txz
 
# cd /usr/local/httpd*
 
# ./configure --prefix=/var/www --bindir=/usr/local/bin \
 
--sbindir=/usr/local/sbin --sysconfdir=/etc/httpd \
 
--libdir=/usr/local/lib64 --includedir=/usr/local/include \
 
--datarootdir=/usr/local/share --mandir=/usr/local/man \
 
--enable-authnz-ldap --enable-ldap --enable-ssl \
 
--with-openssl=/usr/local/openssl-1.0.2a \
 
--enable-so --enable-mime-magic
 
# make
 
# removepkg /var/log/packages/httpd-x.y.z
 
# mv /etc/httpd /etc/httpd.0
 
# make install
 
# make clean
 
# cd /var
 
# chown -R apache:apache www
 
  
== Configuring Apache ==
+
== Iptables Filtering ==
  
Edit '''/var/www/conf/httpd.conf''' to change the '''User''' and '''Group''' to '''apache''', enter the '''ServerAdmin''' e-mail address. To prevent apache from displaying version information, specify '''ServerSignature Off''' and '''ServerTokens prod'''. To prevent users from viewing directory content, remove the '''Indexes''' option in section '''<Directory "/var/www/htdocs">'''. Check section '''<IfModule dir_module>''' to make sure which file names will be served if a directory is requested (by default '''index.html''').
+
Since Linux 2.4, packet filtering is effected inside the kernel, and configuration effected by the '''iptables''' user-space program. In addition to rules for incoming and outgoing packets, iptables affords defining rules for routing between the interfaces. The '''iptables''' command affords entering the rules '''one by one'''. Using a script affords entering all the rules. '''iptable -L -v''' affords viewing the current rules.
  
  User '''apache'''
+
For more information, see the [http://www.netfilter.org/ netfilter] official site. This site has links to various documents, including a simple introduction to packet filtering in this [http://www.netfilter.org/documentation/HOWTO/packet-filtering-HOWTO.html HOWTO].
Group '''apache'''
+
 
  . . .
+
In Slackware, the script used is <tt>'''/etc/rc.d/rc.firewall'''</tt>. It is called automatically when the system starts or stops, using commands <tt>'''./rc.firewall start'''</tt> or <tt>'''./rc.firewall stop'''</tt>.
  ServerAdmin '''postmaster@inner'''
+
 
 +
  #! /bin/sh
 +
#
 +
# startup script for local packet filter
 +
#
 +
fw_start () {
 +
echo "Loading packet filter rules"
 +
 
 +
The flush command affords deleting all the active nat and filtering rules:
 +
 
 +
# flush old rules
 +
iptables -t nat --flush
 +
iptables -flush
 +
 
 +
The -P option affords defining the default policy. A good practise is to forbid by default everything not authorized. This is done here for packets incoming, outgoing, and routed between the interfaces:
 +
 
 +
# drop by default
 +
iptables -P INPUT DROP
 +
iptables -P FORWARD DROP
 +
iptables -P OUTPUT DROP
 +
 
 +
Connections already established are authorized to continue:
 +
 
 +
# accept packets that are part of previously OK'ed sessions
 +
iptables -A INPUT -j ACCEPT -m conntrack --ctstate ESTABLISHED,RELATED
 +
iptables -A OUTPUT -j ACCEPT -m conntrack --ctstate ESTABLISHED,RELATED
 +
iptables -A FORWARD -j ACCEPT -m conntrack --ctstate ESTABLISHED,RELATED
 +
 
 +
The -A option affords adding a rule. Here all the packets on the loopback interface are accepted:
 +
 
 +
  # INBOUND POLICY
 
   
 
   
  '''ServerSignature Off'''
+
  # pass all traffic for network 127.0.0.0/8 on loopback interface
  '''ServerTokens prod'''
+
  iptables -A INPUT -i lo -s 127.0.0.0/8 -d 127.0.0.0/8 -j ACCEPT
 +
 
 +
Addresses of RFC 1918 private networks are not routable on the Internet. So packets with such addresses are not expected on the internal network. However as anti-spoofing is ensured by Internet box we do not need to introduce anti-spoofing rules here:
 
   
 
   
  <Directory "/var/www/htdocs">
+
  # anti-spoofing done by Internet box so not needed here
  . . .
+
# iptables -A INPUT -s 10.0.0.0/8 -j LOG --log-prefix "INPUT spoofed IP "
  Options '''Indexes''' FollowSymLinks
+
# iptables -A INPUT -s 10.0.0.0/8 -j DROP
  . . .
+
  # . . .
</Directory>
+
 
+
The protocols corresponding to services offered or used externally are accepted:
<IfModule dir_module>
+
 
    DirectoryIndex '''index.html index.htm index.php'''
+
# services SMTP HTTP HTTPS
  </IfModule>
+
# iptables -A INPUT -p tcp -j ACCEPT --dport 25 -m conntrack --ctstate NEW
 +
  iptables -A INPUT -p tcp -j ACCEPT --dport 80 -m conntrack --ctstate NEW
 +
  # iptables -A INPUT -p tcp -j ACCEPT --dport 143 -m conntrack --ctstate NEW
 +
iptables -A INPUT -p tcp -j ACCEPT --dport 443 -m conntrack --ctstate NEW
 +
 
 +
The protocols corresponding to services offered on the local network are accepted:
 +
 
 +
  # services on local network FTP DNS BOOTP NNTP SUBMIT VNC
 +
  iptables -A INPUT -p tcp -j ACCEPT --dport 20 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p tcp -j ACCEPT --dport 21 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p udp -j ACCEPT --dport 53 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p tcp -j ACCEPT --dport 53 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p udp -j ACCEPT --dport 69 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p tcp -j ACCEPT --dport 119 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p tcp -j ACCEPT --dport 587 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  # iptables -A INPUT -p tcp -j ACCEPT --dport 5900:5912 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
 
 +
We accept X-Window traffic on the local network:
 +
 
 +
  # SSH-tunnelled X-Window output appears as input on interface lo
 +
  iptables -A INPUT -p udp -j ACCEPT --dport 177 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p tcp -j ACCEPT --dport 6000:6063 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -i lo -p tcp -j ACCEPT --dport 6000:6063 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
 
 +
We accept NFS on the local network and fix the NFS ports:
 +
 
 +
  # NFS ports
 +
  iptables -A INPUT -p udp -j ACCEPT --dport 111 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p tcp -j ACCEPT --dport 111 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p udp -j ACCEPT --dport 2049 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p tcp -j ACCEPT --dport 2049 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p udp -j ACCEPT --dport 32764:32769 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p tcp -j ACCEPT --dport 32764:32769 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
 
 +
We accept samba traffic on the local network:
 +
 
 +
  # samba ports
 +
  iptables -A INPUT -p udp -j ACCEPT --dport 135 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p tcp -j ACCEPT --dport 135 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p udp -j ACCEPT --dport 137 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p tcp -j ACCEPT --dport 137 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p udp -j ACCEPT --dport 138 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p tcp -j ACCEPT --dport 139 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p udp -j ACCEPT --dport 445 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
  iptables -A INPUT -p tcp -j ACCEPT --dport 445 -m conntrack --ctstate NEW -s 192.168.53.0/24
 +
 
 +
Broadcast traffic is also OK:
 +
 
 +
  # broadcast traffic
 +
  iptables -A INPUT -p udp -s 0.0.0.0 -sport 67:68 -d 255.255.255.255 -j ACCEPT
 +
 
 +
We accept pings on the local network:
 +
 
 +
  # accept some icmp packets
 +
  iptables -A INPUT -p icmp --icmp-type echo-request -s 192.168.53.0/24 -j ACCEPT
 +
  iptables -A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT
 +
  iptables -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT
 +
 
 +
We could log anything not accepted above:
 +
 
 +
  # log anything not accepted above
 +
# iptables -A INPUT -j LOG --log-prefix "INPUT bad traffic "
  
== Running Apache ==
+
We accept all outbound packets, which would for example afford using a network scanner. In a production environment, there would be a stricter policy:
  
To automatically launch apache at startup, edit '''/etc/rc.d/rc.httpd''' and update the paths to reflect installation of the software under '''/usr/local/sbin'''. Give the file execution rights, launch the software using '''/etc/rc.d/rc.httpd start''', check if it's running by a '''ps -ef | grep httpd''', point your browser to http://localhost which should load the apache test page with the message '''It works!''', then open port 80 on the firewall.
+
# OUTBOUND POLICY
 +
 +
# accept all outbound packets
 +
iptables -A OUTPUT -j ACCEPT
 +
}
 +
 +
After the fw_start() function ends, the fw_stop() function is defined to authorize everything:
  
  # '''vi /etc/rc.d/rc.httpd'''
+
fw_stop () {
  . . .
+
  echo "Unloading all packet filter rules"
 +
  iptables -t nat --flush
 +
  iptables -flush
 +
 +
  # accept by default
 +
  iptables -P INPUT ACCEPT
 +
  iptables -P FORWARD ACCEPT
 +
  iptables -P OUTPUT ACCEPT
 +
  }
 +
   
 
  case "$1" in
 
  case "$1" in
  'start')
+
‘start’)
    /usr'''/local/sbin'''/apachectl -k start
+
  fw_start
 
   ;;
 
   ;;
  'stop')
+
’stop’)
    /usr'''/local/sbin'''/apachectl -k stop
+
  fw_stop
    killall httpd
 
    rm -f /var/www/logs/httpd/*.pid
 
 
   ;;
 
   ;;
  'restart')
+
’restart’)
    /usr'''/local/sbin'''/apachectl -k restart
+
  fw_start
 
   ;;
 
   ;;
  'graceful')
+
*)
    /usr'''/local/sbin'''/apachectl -k graceful
+
   echo "usage $0 start | stop | restart"
  ;;
 
  'graceful-stop')
 
    /usr'''/local/sbin'''/apachectl -k graceful-stop
 
   ;;
 
  *)
 
    echo "Usage: $0 {start|stop|restart|graceful|graceful-stop}"
 
  ;;
 
esac
 
''':x'''
 
# '''chmod u+x /etc/rc.d/rc.httpd'''
 
# '''/etc/rc.d/rc.httpd start'''
 
# '''ps -ef | grep httpd'''
 
root      9875    1  1 07:52 ?        00:00:00 /usr/local/sbin/httpd -k start
 
apache    9876  9875  0 07:52 ?        00:00:00 /usr/local/sbin/httpd -k start
 
apache    9877  9875  0 07:52 ?        00:00:00 /usr/local/sbin/httpd -k start
 
apache    9878  9875  0 07:52 ?        00:00:00 /usr/local/sbin/httpd -k start
 
apache    9879  9875  0 07:52 ?        00:00:00 /usr/local/sbin/httpd -k start
 
apache    9880  9875  0 07:52 ?        00:00:00 /usr/local/sbin/httpd -k start
 
root      9882  3198  0 07:52 pts/1    00:00:00 grep httpd
 
# '''vi /etc/rc.d/rc.firewall'''
 
. . .
 
iptables -A INPUT -p tcp -j ACCEPT --dport 80 -m state --state NEW
 
. . .
 
''':x'''
 
# '''/etc/rc.d/rc.firewall restart'''
 
  
== Using encryption ==
+
== Testing the firewall ==
  
As we use HTTP applications that require login with passwords, we configure httpd.conf with SSL/TLS. This is described simply in the Apache documentation [https://httpd.apache.org/docs/2.4/en/ssl/ssl_howto.html SSL/TLS Strong Encryption: How-To]. At least the directives below are required in '''/etc/httpd/httpd.conf'''. The '''SSLCipherSuite''' directive enables only the strongest ciphers. '''apache.mtacert.pem.unsecure''' is a copy of your server certificate owned by '''apache:apache''' :
+
Use '''nmap -sU hostname''' (UDP) and '''nmap -sT hostname''' (TCP) to make sure what ports are visible locally and do the same from the outside.
  
LoadModule ssl_module modules/mod_ssl.so
+
== Download example ==
 
Listen 443
 
<VirtualHost *:443>
 
    ServerName inner.studioware.com
 
    SSLEngine on
 
    SSLCertificateFile "/etc/ssl/certs/mtacert.pem"
 
    SSLCertificateKeyFile "/etc/ssl/private/apache.mtacert.pem.unsecure"
 
    SSLCipherSuite HIGH:!aNULL:!MD5
 
</VirtualHost>
 
  
For this example to work in Firefox with your self-signed CA certificate, you need to import it using the "Preferences" "Advanced" "Certificates" "View certificates" "Authorities" "Import" menu. We have chosen to encrypt all the site but it is possible to restrict directives to specific areas. More details can be found in the [https://httpd.apache.org/docs/2.4/en/mod/mod_ssl.html Apache Module mod_ssl] and [https://httpd.apache.org/docs/2.4/en/ Apache] documentations.
+
[{{SERVER}}/wikislax/download/rc.firewall Download file rc.firewall]
  
 
<br/>
 
<br/>
  
{{pFoot|[[MySQL]]|[[Main Page]]|[[PHP]]}}
+
{{pFoot|[[Configuration files]]|[[Main Page]]|[[X11 configuration]]}}

Revision as of 13:22, 26 March 2026

Packet filtering affords opening access only to these services you have decided to open. The TCP or UDP packets include a piece of information called the port number, that is used to identify the type of service. Secure ports were defined as SSL counterparts of the native ports but were superseded by TLS and are now deprecated due to security weaknesses in the SSL protocol. SSL should not be used any longer. Instead, use TLS. Current version is v1.2.

Protocol Port # Secure Protocol Secure Port # Service
SMTP 25 SMTPS 465 Mail exchange
HTTP 80 HTTPS 443 Web browsing
POP3 110 POP3S 995 Mail retrieval
NTTP 119 NTTPS 563 News exchange
IMAP 143 IMAPS 993 Mail retrieval
LDAP 389 LDAPS 636 Ldap Directory


On server side, the services are provided by applications that may have vulnerabilities and be attacked. Examples of attacks are buffer overflow or format string attacks, that afford getting full access on the target machine by crafting special strings sent to it. An attacker could then obtain any information present there or modify or destroy the system.

To reduce the number of possible attacks, the number of services authorized, or who can access the system, must be restricted. This is known as packet filtering. It is only an aspect of security (obviously, the applications on the server side must also be secured ...), but it is important. Never *** ever *** connect to the network a computer not protected by a packet filter !

To illustrate, let's configure our two-interfaces computer to be its own firewall. eth0 is the Internet interface, it uses network 192.168.0.x, the gateway is an ADSL router/switch at 192.168.0.254. eth1 is the (Intranet) interface to the internal network 192.168.1.x.

Iptables Filtering

Since Linux 2.4, packet filtering is effected inside the kernel, and configuration effected by the iptables user-space program. In addition to rules for incoming and outgoing packets, iptables affords defining rules for routing between the interfaces. The iptables command affords entering the rules one by one. Using a script affords entering all the rules. iptable -L -v affords viewing the current rules.

For more information, see the netfilter official site. This site has links to various documents, including a simple introduction to packet filtering in this HOWTO.

In Slackware, the script used is /etc/rc.d/rc.firewall. It is called automatically when the system starts or stops, using commands ./rc.firewall start or ./rc.firewall stop.

#! /bin/sh
#
# startup script for local packet filter
#
fw_start () {
echo "Loading packet filter rules"

The flush command affords deleting all the active nat and filtering rules:

# flush old rules
iptables -t nat --flush
iptables -flush

The -P option affords defining the default policy. A good practise is to forbid by default everything not authorized. This is done here for packets incoming, outgoing, and routed between the interfaces:

# drop by default
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

Connections already established are authorized to continue:

# accept packets that are part of previously OK'ed sessions
iptables -A INPUT -j ACCEPT -m conntrack --ctstate ESTABLISHED,RELATED
iptables -A OUTPUT -j ACCEPT -m conntrack --ctstate ESTABLISHED,RELATED
iptables -A FORWARD -j ACCEPT -m conntrack --ctstate ESTABLISHED,RELATED 

The -A option affords adding a rule. Here all the packets on the loopback interface are accepted:

# INBOUND POLICY

# pass all traffic for network 127.0.0.0/8 on loopback interface
iptables -A INPUT -i lo -s 127.0.0.0/8 -d 127.0.0.0/8 -j ACCEPT

Addresses of RFC 1918 private networks are not routable on the Internet. So packets with such addresses are not expected on the internal network. However as anti-spoofing is ensured by Internet box we do not need to introduce anti-spoofing rules here:

# anti-spoofing done by Internet box so not needed here
# iptables -A INPUT -s 10.0.0.0/8 -j LOG --log-prefix "INPUT spoofed IP "
# iptables -A INPUT -s 10.0.0.0/8 -j DROP
# . . .

The protocols corresponding to services offered or used externally are accepted:

# services SMTP HTTP HTTPS
# iptables -A INPUT -p tcp -j ACCEPT --dport 25 -m conntrack --ctstate NEW
iptables -A INPUT -p tcp -j ACCEPT --dport 80 -m conntrack --ctstate NEW
# iptables -A INPUT -p tcp -j ACCEPT --dport 143 -m conntrack --ctstate NEW
iptables -A INPUT -p tcp -j ACCEPT --dport 443 -m conntrack --ctstate NEW

The protocols corresponding to services offered on the local network are accepted:

 # services on local network FTP DNS BOOTP NNTP SUBMIT VNC
 iptables -A INPUT -p tcp -j ACCEPT --dport 20 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p tcp -j ACCEPT --dport 21 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p udp -j ACCEPT --dport 53 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p tcp -j ACCEPT --dport 53 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p udp -j ACCEPT --dport 69 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p tcp -j ACCEPT --dport 119 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p tcp -j ACCEPT --dport 587 -m conntrack --ctstate NEW -s 192.168.53.0/24
 # iptables -A INPUT -p tcp -j ACCEPT --dport 5900:5912 -m conntrack --ctstate NEW -s 192.168.53.0/24

We accept X-Window traffic on the local network:

 # SSH-tunnelled X-Window output appears as input on interface lo
 iptables -A INPUT -p udp -j ACCEPT --dport 177 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p tcp -j ACCEPT --dport 6000:6063 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -i lo -p tcp -j ACCEPT --dport 6000:6063 -m conntrack --ctstate NEW -s 192.168.53.0/24

We accept NFS on the local network and fix the NFS ports:

 # NFS ports
 iptables -A INPUT -p udp -j ACCEPT --dport 111 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p tcp -j ACCEPT --dport 111 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p udp -j ACCEPT --dport 2049 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p tcp -j ACCEPT --dport 2049 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p udp -j ACCEPT --dport 32764:32769 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p tcp -j ACCEPT --dport 32764:32769 -m conntrack --ctstate NEW -s 192.168.53.0/24

We accept samba traffic on the local network:

 # samba ports
 iptables -A INPUT -p udp -j ACCEPT --dport 135 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p tcp -j ACCEPT --dport 135 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p udp -j ACCEPT --dport 137 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p tcp -j ACCEPT --dport 137 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p udp -j ACCEPT --dport 138 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p tcp -j ACCEPT --dport 139 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p udp -j ACCEPT --dport 445 -m conntrack --ctstate NEW -s 192.168.53.0/24
 iptables -A INPUT -p tcp -j ACCEPT --dport 445 -m conntrack --ctstate NEW -s 192.168.53.0/24

Broadcast traffic is also OK:

 # broadcast traffic
 iptables -A INPUT -p udp -s 0.0.0.0 -sport 67:68 -d 255.255.255.255 -j ACCEPT

We accept pings on the local network:

 # accept some icmp packets
 iptables -A INPUT -p icmp --icmp-type echo-request -s 192.168.53.0/24 -j ACCEPT
 iptables -A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT
 iptables -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT

We could log anything not accepted above:

# log anything not accepted above
# iptables -A INPUT -j LOG --log-prefix "INPUT bad traffic "

We accept all outbound packets, which would for example afford using a network scanner. In a production environment, there would be a stricter policy:

# OUTBOUND POLICY

# accept all outbound packets
iptables -A OUTPUT -j ACCEPT
}

After the fw_start() function ends, the fw_stop() function is defined to authorize everything:

fw_stop () {
  echo "Unloading all packet filter rules"
  iptables -t nat --flush
  iptables -flush

# accept by default
  iptables -P INPUT ACCEPT
  iptables -P FORWARD ACCEPT
  iptables -P OUTPUT ACCEPT
  }

case "$1" in
‘start’)
  fw_start
  ;;
’stop’)
  fw_stop
  ;;
’restart’)
  fw_start
  ;;
*)
  echo "usage $0 start | stop | restart"

Testing the firewall

Use nmap -sU hostname (UDP) and nmap -sT hostname (TCP) to make sure what ports are visible locally and do the same from the outside.

Download example

Download file rc.firewall


Configuration files Main Page X11 configuration