Linux firewall




Дата канвертавання22.04.2016
Памер74.85 Kb.

LINUX Firewall

iptables


Originally, the most popular firewall/NAT package running on Linux was ipchains. To address a number of shortcomings, the Netfilter organization created a new product called iptables . Considered a faster and more secure alternative to ipchains, iptables has become the default firewall package installed under RedHat and Fedora Linux with the following improvements:

  • Better integration with the Linux kernel with the capability of loading iptables-specific kernel modules designed for improved speed and reliability.

  • Stateful packet inspection. This means that the firewall keeps track of each connection passing through it and in certain cases will view the contents of data flows in an attempt to anticipate the next action of certain protocols. This is an important feature in the support of active FTP and DNS, as well as many other network services.

  • Filtering packets based on a MAC address and the values of the flags in the TCP header. This is helpful in preventing attacks using malformed packets and in restricting access from locally attached servers to other networks in spite of their IP addresses.

  • System logging that provides the option of adjusting the level of detail of the reporting.

  • Better network address translation.

  • Support for transparent integration with such Web proxy programs as Squid.

  • A rate limiting feature that helps iptables block some types of denial of service (DoS) attacks.

Iptables Package Installation and Startup


The iptables RPM filename usually starts with the software package name by a version number, as in iptables-1.2.9-1.0.i386.rpm and the associated Gnome configuration front-end – system-config-firewall.

You can start, stop, and restart iptables after booting by using the commands:



service iptables start

service iptables stop

service iptables restart
To get iptables configured to start at boot, use the chkconfig command:

chkconfig iptables on

You can determine whether iptables is running or not via the service iptables status command. Fedora Core will give a simple status message. For example



service iptables status
You can determine current ipttables rule status with the following command:
iptables –L
The iptables application requires you to load certain kernel modules to activate some of its functions. Whenever any type of NAT is required, the iptable_nat module needs to be loaded. The ip_conntrack_ftp module needs to be added for FTP support and should always be loaded with the ip_conntrack module which tracks TCP connection states. As most scripts probably will keep track of connection states, the ip_conntrack module will be needed in any case. The ip_nat_ftp module also needs to be loaded for FTP servers behind a NAT firewall. /etc/sysconfig/iptables file doesn't support the loading of modules, so you'll have to add these statements to your /etc/rc.local file. For example
# File: /etc/rc.local

# Module to track the state of connections

modprobe ip_conntrack

# Load the iptables active FTP module, requires ip_conntrack

modprobe ip_conntrack_ftp

# Load iptables NAT module when required

modprobe iptable_nat

# Module required for active an FTP server using NAT

modprobe ip_nat_ftp

Packet Processing In iptables


All packets inspected by iptables pass through a sequence of built-in tables (queues) for processing. Each of these queues is dedicated to a particular type of packet activity and is controlled by an associated packet transformation/filtering chain.
There are three tables in total.
- The first table is the mangle table which is responsible for the alteration of quality of service bits in the TCP header. This is not used in a general firewall environmentt.

- The second table is the filter queue which is responsible for packet filtering. It has three built-in chains in which you can place your firewall policy rules. These are the:



  • Forward chain: Filters packets to servers protected by the firewall.

  • Input chain: Filters packets destined for the firewall.

  • Output chain: Filters packets originating from the firewall.

- The third table is the nat queue which is responsible for network address translation. It has two built-in chains; these are:

  • Pre-routing chain: NATs packets when the destination address of the packet needs to be changed.

  • Post-routing chain: NATs packets when the source address of the packet needs to be changed
Processing For Packets Routed By The Firewall

Queue

Type

Queue

Function

Packet transformation chain in Queue

Chain Function

Filter

Packet filtering

FORWARD

Filters packets to servers accessible by another NIC on the firewall.

 

 

INPUT

Filters packets destined to the firewall.

 

 

OUTPUT

Filters packets originating from the firewall

Nat

Network Address Translation

PREROUTING

Address translation occurs before routing. Facilitates the transformation of the destination IP address to be compatible with the firewall's routing table. Used with NAT of the destination IP address, also known as destination NAT or DNAT.

 

 

POSTROUTING

Address translation occurs after routing. This implies that there was no need to modify the destination IP address of the packet as in pre-routing. Used with NAT of the source IP address using either one-to-one or many-to-one NAT. This is known as source NAT, or SNAT.

 

 

OUTPUT

Network address translation for packets generated by the firewall. (

Mangle

TCP header modification

PREROUTING POSTROUTING OUTPUT INPUT FORWARD

Modification of the TCP packet quality of service bits before routing occurs

You need to specify the table and the chain for each firewall rule you create. There is an exception: Most rules are related to filtering, so iptables assumes that any chain that's defined without an associated table will be a part of the filter table. The filter table is therefore the default.


To look at the way packets are handled by iptables refernce the below figure:.

    • A TCP packet from the Internet arrives at the firewall's interface on Network A to create a data connection.

    • The packet is first examined by your rules in the mangle table's PREROUTING chain, if any.

    • It is then inspected by the rules in the nat table's PREROUTING chain to see whether the packet requires DNAT. It is then routed.

    • If the packet is destined for a protected network, then it is filtered by the rules in the FORWARD chain of the filter table and, if necessary, the packet undergoes SNAT in the POSTROUTING chain before arriving at Network B.

    • When the destination server decides to reply, the packet undergoes the same sequence of steps. Both the FORWARD and POSTROUTING chains may be configured to implement quality of service (QoS) features in their mangle tables.

    • If the packet is destined for the firewall itself, then it passes through the mangle table of the INPUT chain, if configured, before being filtered by the rules in the INPUT chain of the filter table before. If it successfully passes these tests then it is processed by the intended application on the firewall.

    • At some point, the firewall needs to reply. This reply is routed and inspected by the rules in the OUTPUT chain of the mangle table, if any.

    • Next, the rules in the OUTPUT chain of the nat table determine whether DNAT is required and the rules in the OUTPUT chain of the filter table are then inspected to help restrict unauthorized packets.

    • Finally, before the packet is sent back to the Internet, SNAT and QoS mangling is done by the POSTROUTING chain
Iptables Packet Flow Diagram


Each firewall rule inspects each IP packet and then tries to identify it as the target of some sort of operation. Once a target is identified, the packet needs to jump over to it for further processing.
Commonly Used Targets

Target

Description

Most common options

ACCEPT

>       iptables stops further processing.

>       The packet is handed over to the end application or the operating system for processing



N/A

DROP

>       iptables stops further processing.

>       The packet is blocked



N/A

 


LOG

>       The packet information is sent to the syslog daemon for logging

>       iptables continues processing with the next rule in the table

>       As you can't log and drop at the same time, it is common to have two similar rules in sequence. The first will log the packet, the second will drop it.


--log-prefix "string"

Tells iptables to prefix all log messages with a user defined string. Frequently used to tell why the logged packet was dropped



REJECT

>       Works like the DROP target, but will also return an error message to the host sending the packet that the packet was blocked

--reject-with qualifier

The qualifier tells what type of reject message is returned. Qualifiers include:

icmp-port-unreachable (default)

icmp-net-unreachable

icmp-host-unreachable

icmp-proto-unreachable

icmp-net-prohibited

icmp-host-prohibited

tcp-reset

echo-reply



DNAT

>       Used to do destination network address translation. ie. rewriting the destination IP address of the packet

--to-destination ipaddress

Tells iptables what the destination IP address should be



SNAT

>       Used to do source network address translation rewriting the source IP address of the packet

>       The source IP address is user defined



--to-source
[-
][:
-
]

Specifies the source IP address and ports to be used by SNAT.



MASQUERADE

 


>       Used to do Source Network Address Translation.

>       By default the source IP address is the same as that used by the firewall's interface



[--to-ports
[-
]]

Specifies the range of source ports to which the original source port can be mapped.



Each line of an iptables script not only has a jump, but they also have a number of command line options that are used to append rules to chains that match your defined packet characteristics, such the source IP address and TCP port. There are also options that can be used to just clear a chain so you can start all over again.


General Iptables Match Criteria

iptables

command

Switch

Description

-t

If you don't specify a table, then the filter table is assumed. As discussed before, the possible built-in tables include: filter, nat, mangle

-j

Jump to the specified target chain when the packet matches the current rule.

-A

Append rule to end of a chain

-F

Flush. Deletes all the rules in the selected table

-p


Match protocol. Types include, icmp, tcp, udp, and all

-s

Match source IP address

-d

Match destination IP address

-i

Match "input" interface on which the packet enters.

-o

Match "output" interface on which the packet exits

Example: iptables is being configured to allow the firewall to accept TCP packets coming in on interface eth0 from any IP address destined for the firewall's IP address of 192.168.1.1. The 0/0 representation of an IP address means any.

iptables -A INPUT -s 0/0 -i eth0 -d 192.168.1.1 -p TCP -j ACCEPT
Common TCP and UDP Match Criteria

Switch

Description

-p tcp --sport


TCP source port

Can be a single value or a range in the format:



start-port-number:end-port-number

-p tcp --dport


TCP destination port

Can be a single value or a range in the format:



starting-port:ending-port

-p tcp --syn

Used to identify a new TCP connection request

 

! --syn means, not a new connection request



-p udp --sport


UDP source port

Can be a single value or a range in the format:



starting-port:ending-port

-p udp --dport


UDP destination port

Can be a single value or a range in the format:



starting-port:ending-port

Example: iptables is being configured to allow the firewall to accept TCP packets for routing when they enter on interface eth0 from any IP address and are destined for an IP address of 192.168.1.58 that is reachable via interface eth1. The source port is in the range 1024 to 65535 and the destination port is port 80 (www/http).


iptables -A FORWARD -s 0/0 -i eth0 -d 192.168.1.58 -o eth1 -p TCP --sport 1024:65535 --dport 80 -j ACCEPT
ICMP (Ping) Match Criteria
Matches

used with

 ---icmp-type





Description

--icmp-type

The most commonly used types are echo-reply and echo-request

Example: iptables is being configured to allow the firewall to send ICMP echo-requests (pings) and in turn, accept the expected ICMP echo-replies.



iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT

iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
Example”: The limit feature in iptables specifies the maximum average number of matches to allow per second. You can specify time intervals in the format /second, /minute, /hour, or /day, or you can use abbreviations so that 3/second is the same as 3/s.
iptables -A INPUT -p icmp --icmp-type echo-request: -m limit --limit 1/s -i eth0 -j ACCEPT
Example: ICMP echo requests are restricted to no more than one per second. When tuned correctly, this feature allows you to filter unusually high volumes of traffic that characterize denial of service (DOS) attacks and Internet worms. You can expand on the limit feature of iptables to reduce your vulnerability to certain types of denial of service attack. Here a defense for SYN flood attacks was created by limiting the acceptance of TCP segments with the SYN bit set to no more than five per second.
iptables -A INPUT -p tcp --syn -m limit --limit 5/s -i eth0 -j ACCEPT
Extended Match Criteria

Switch

Description

-m multiport --sport


A variety of TCP/UDP source ports separated by commas. Unlike when -m isn't used, they do not have to be within a range.

-m multiport --dport


A variety of TCP/UDP destination ports separated by commas. Unlike when -m isn't used, they do not have to be within a range.

-m multiport --ports


A variety of TCP/UDP ports separated by commas. Source and destination ports are assumed to be the same and they do not have to be within a range.

-m --state

The most frequently tested states are:

 ESTABLISHED: The packet is part of a connection that has seen packets in both directions



NEW: The packet is the start of a new connection

RELATED: The packet is starting a new secondary connection. This is a common feature of such protocols such as an FTP data transfer, or an ICMP error.

INVALID: The packet couldn't be identified. Could be due to insufficient system resources, or ICMP errors that don't match an existing data flow.

Expansion on the previous example: Here iptables is being configured to allow the firewall to accept TCP packets to be routed when they enter on interface eth0 from any IP address destined for IP address of 192.168.1.58 that is reachable via interface eth1. The source port is in the range 1024 to 65535 and the destination ports are port 80 (www/http) and 443 (https). The return packets from 192.168.1.58 are allowed to be accepted too. Instead of stating the source and destination ports, you can simply allow packets related to established connections using the -m state and --state ESTABLISHED options.
iptables -A FORWARD -s 0/0 -i eth0 -d 192.168.1.58 -o eth1 -p TCP --sport 1024:65535 -m multiport --dport 80,443 -j ACCEPT

iptables -A FORWARD -d 0/0 -o eth0 -s 192.168.1.58 -i eth1 -p TCP -m state --state ESTABLISHED -j ACCEPT

User Defined Chains

As you may remember, you can configure iptables to have user-defined chains. This feature is frequently used to help streamline the processing of packets. For example, instead of using a single, built-in chain for all protocols, you can use the chain to determine the protocol type for the packet and then hand off the actual final processing to a user-defined, protocol-specific chain in the filter table. In other words, you can replace a long chain with a stubby main chain pointing to multiple stubby chains, thereby shortening the total length of all chains the packet has to pass through. For example
iptables -A INPUT -i eth0 -d 206.229.110.2 -j fast-input-queue

iptables -A OUTPUT -o eth0 -s 206.229.110.2 -j fast-output-queue

iptables -A fast-input-queue -p icmp -j icmp-queue-in

iptables -A fast-output-queue -p icmp -j icmp-queue-out

iptables -A icmp-queue-out -p icmp --icmp-type echo-request -m state --state NEW -j ACCEPT

iptables -A icmp-queue-in -p icmp --icmp-type echo-reply -j ACCEPT

Custom Queues Example

Chain

Description

INPUT

The regular built-in INPUT chain in iptables

OUTPUT

The regular built-in OUTPUT chain in iptables

fast-input-queue

Input chain dedicated to identifying specific protocols and shunting the packets to protocol specific chains.

fast-output-queue

Output chain dedicated to identifying specific protocols and shunting the packets to protocol specific chains.

icmp-queue-out

Output queue dedicated to ICMP

icmp-queue-in

Input queue dedicated to ICMP

iptables Scripts


There are many iptables “canned” scripts available on the Internet, so samples will nt be provided here.
The service iptables save command permanently saves the iptables configuration in the /etc/sysconfig/iptables file. When the system reboots, the iptables-restore program reads the configuration and makes it the active configuration. The format of the /etc/sysconfig/iptables file is different from the commands shown previously - initialization of built-in chains is automatic and the string "iptables" is omitted from the rule statements. Direct edit of this script is not recommended because it is always overwritten by the save command and it doesn't save any comments at all, which can also make it extremely difficult to follow. For these reasons, you're better off writing and applying a customized script and then using the service iptables save command to make the changes permanent.
A sample /etc/sysconfig/iptables configuration allowing ICMP, IPSec (ESP and AH packets), already established connections, and inbound SSH.
*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [144:12748]

: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

-A RH-Firewall-1-INPUT -p icmp -m icmp --icmp-type 255 -j ACCEPT

-A RH-Firewall-1-INPUT -p esp -j ACCEPT

-A RH-Firewall-1-INPUT -p ah -j ACCEPT

-A RH-Firewall-1-INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited

COMMIT
Fedora comes with a program called lokkit that you can use to generate a very rudimentary firewall rule set. Like the service iptables save command, lokkit saves the firewall rules in a new /etc/sysconfig/iptables file for use on the next reboot. And also a GNOME desktop configurator in the system-config-firewall package.

You can use the iptables-save and iptables-restore commands to assist you with the continued management of the server.


Unlike the service iptables save command, which actually saves a permanent copy of the firewall's active configuration in the /etc/sysconfig/iptables file, iptables-save displays the active configuration to the screen in /etc/sysconfig/iptables format. If you redirect the iptables-save screen output to a file with the > symbol, then you can edit the output and reload the updated rules when they meet your new criteria with the iptables-restore command.
iptables-save > firewall-config
After editing the firewall-config file with the commands you need, you can reload it into the active firewall rule set with the iptables-restore command.

iptables-restore < firewall-config
Finally, you should permanently save the active configuration so that it will be loaded automatically when the system reboots:
service iptables save

Troubleshooting iptables

Rules

You track packets passing through the iptables list of rules using the LOG target added to the end of any rule. The results wind up in /var/log/messages.

If you want to log only unwanted traffic, therefore, you have to add a matching rule with a DROP target immediately after the LOG rule outherwise you'll log both desired and unwanted traffic with no way of discerning between the two, because by default iptables doesn't state why the packet was logged in its log message.


iptables Won't Start

The iptables startup script expects to find the /etc/sysconfig/iptables before it starts. If none exists, then symptoms include the firewall status always being stopped and the /etc/init.d/iptables script running without the typical [OK] or [FAILED] messages. If you have just installed iptables and have never applied a policy, then you will face this problem. Unfortunately, running the service iptables save command before restarting won't help either. You have to create this file and start the service.
touch /etc/sysconfig/iptables

chmod 600 /etc/sysconfig/iptables



service iptables start

Applying iptables firewall rules: [ OK ]


База данных защищена авторским правом ©shkola.of.by 2016
звярнуцца да адміністрацыі

    Галоўная старонка