Submitted by ramez.hanna on Mon, 12/07/2004 - 14:12.
( categories: Howtos )

LINUX FIREWALLS

IPATBLES

by ramez.hanna

This is not a reference to IPTABLES in any way it is just a start that i gathered from several other documents with my humble knowledge and experience and i hope it could get you started with the firewalls configuration. This document is far from complete and I'll be adding more as soon as i have the time

General

Before we mention anything about firewalls i need to point out a critical point “a firewall’s power lies within the configuration” meaning that no matter what the firewall application power is, a weak configuration will weaken it.And also remember that security is not just a firewall. Think of a firewall as just a first line of defense you need to secure your applications and keep your system updated and patched for any exploits to secure your applications Mainly a firewall is used to block or allow certain traffic based upon the network needs, it may block all incoming requests or it may allow requests to go through to the mail server within the local network The firewall in Linux sits in the kernel waiting for network traffic from the NICs and handles it as mentioned in the configuration file.
It works on layer 3 and 4 of the OSI network model this mean it operates on the IP and TCP/UDP level thus it cannot filter upon the content of the packet and hence cannot be used to check mails for viruses or block unwanted content in web pages

How IPTABLES is configured?

IPTABLES configuration file (/etc/sysconfig/iptables) consists of a set of rules, each line contains one rule
The IPTABLES daemon goes through the configuration file line by line, so it is important to pay attention for the order of the rules. When a packet is processed by the daemon for a certain match criteria it is directed to a certain destination (REJECT/DROP/ACCEPT) the next time the same criteria is met the rule is ignored because this specific packet has been already filtered, only when you use the LOG destination it is logged and then the same criteria can be used again
There are two ways of creating the config file either by editing the config file directly or by using the #iptables command so here is this document I'll edit the file directly (no reason just that i used to do it both ways are good but some people prefer using the command as it is more flexible)
IPTABLES has 3 tables :

  • FILTER: where the filtering of the packets takes place
  • NAT: packet modification such as NAT/PAT and IP msquerading
  • MANGLE: for setting packet options such type of service


THE FILTER TABLE The filter table has 3 default chains

  • INPUT: any traffic directed to the local machine
  • FORWARD: any traffic directed from one network to another such as web pages requested by the clients on the local network
  • OUTPUT: any traffic generated from the local machine
  • user defined chains
  • How a rule is built?

    A rule is a match criteria applied to the packets in a certain chain that reach the firewall machine. These match criteria are define packet properties and information such as source IP, destination IP, etc . . .
    I'll start by giving you the quick steps and some quick notes first then I'll go through them one by one

    • Add/Insert/Delete the rule
    • Define the chain
    • Create your match
    • Determine the action

    So lets go into more details
    "Add/Insert/Delete the rule” as mentioned before the rules are placed in order so you just define -A to append a new rule, -I to insert a rule in a certain place, -D to delete a certain rule
    -A
    "Define the chain” define which chain you are dealing with
    -A INPUT
    "Create your match” will talk about this in details later
    -A INPUT some match criteria
    "Determine the action” tell the daemon what to do
    -A INPUT some match criteria -j ACCEPT

    So how a match is built?

    Generally the matches that can be defined are:

    • -p : to define a protocol which can be TCP, UDP or ICMP
    • --sport : to define the source port of the packet
    • --dport : to define the destination port of the packet
    • -m : use state matches
    • state
    • --state : to define the connection state NEW, RELATED, ESTABLISHED, INVALID
    • --syn : to define that the packet contains a syn request equivalent to --state NEW
    • -i : to define the incoming interface
    • -o : to define the outgoing interface

    Examples

    -A INPUT -p tcp --dport 110 -m state --state NEW -j ACCEPT

    The above example rule will accept incoming pop requests to the local server

    -A OUTPUT -p tcp --dport 80 -m --syn -j ACCEPT 

    The above example rule will accept outgoing web requests
    here is an example configuration file
    The first line determines which table we are using
    The following three lines set the default behavior
    Then comes the rules
    Then at the end to apply these rules you add COMMIT

    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    -A INPUT -i lo -j ACCEPT
    -A FORWARD -i lo -j ACCEPT
    -A INPUT -p icmp --icmp-type any -j ACCEPT
    -A FORWARD -p icmp --icmp-type any -j ACCEPT
    -A INPUT -p 50 -j ACCEPT
    -A FORWARD -p 50 -j ACCEPT
    -A INPUT -p 51 -j ACCEPT
    -A FORWARD -p 51 -j ACCEPT
    -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
    -A INPUT -j REJECT --reject-with icmp-host-prohibited
    -A FORWARD -j REJECT --reject-with icmp-host-prohibited
    COMMIT
    

    OneOfOne's picture
    Submitted by OneOfOne on Sat, 07/08/2004 - 15:18.

    Nice article, only one thing, in gentoo its : /var/lib/iptables/rules-save (configurable from /etc/conf.d/iptables)

    *filter
    :INPUT DROP [403:19667]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [156593:40054420]
    -A INPUT -i lo -j ACCEPT
    -A INPUT -m state --state INVALID -j DROP
    -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    -A INPUT -p tcp -m tcp --dport 113 -j REJECT --reject-with icmp-host-unreachable
    -A INPUT -p tcp -m tcp --dport 5154 -j ACCEPT
    -A INPUT -p udp -m udp --dport 5154 -j ACCEPT
    -A INPUT -p tcp -m tcp --sport 60100:60200 -j ACCEPT
    -A INPUT -p tcp -m tcp --dport 60100:60200 -j ACCEPT
    -A OUTPUT -m state --state INVALID -j DROP
    COMMIT
    

    ^^^^ example from my file, drop all input, allow all forward/output, open some ports, reject 113 to make irc connect faster and allow the computer to be ping'ed.

    peace


    -OneOfOne kernel patching monkey/BORG drone.


    Pronco's picture
    Submitted by Pronco on Sat, 28/08/2004 - 07:05.

    GFCC is a graphical interface tool for managing ipchains/iptables rules


    Comment viewing options

    Select your preferred way to display the comments and click "Save settings" to activate your changes.