0% found this document useful (0 votes)
18 views3 pages

Handy Linux Iptables Firewall Script

This document provides a script for setting up a simple iptables firewall on a Linux web server, including instructions for creating necessary configuration files for blacklisting and whitelisting IP addresses and allowed ports. It includes flood detection settings for TCP-SYN and ping floods, as well as logging capabilities for potential attacks. Users are advised to rerun the script after making changes to the configuration files to apply the new rules.

Uploaded by

nideham547
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Handy Linux Iptables Firewall Script

This document provides a script for setting up a simple iptables firewall on a Linux web server, including instructions for creating necessary configuration files for blacklisting and whitelisting IP addresses and allowed ports. It includes flood detection settings for TCP-SYN and ping floods, as well as logging capabilities for potential attacks. Users are advised to rerun the script after making changes to the configuration files to apply the new rules.

Uploaded by

nideham547
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

[Link]

php- RAID help

Handy Linux iptables script

by Hersey on Apr.23, 2009, under My Notes, Networking, Tools

Here is a script I put together some years ago to create a simple


Iptables firewall on my linux webserver. Some of the code was
borrowed from a linux security book but I do not remember which
one. Anyway it is a pretty handy script to give you some control and
protection.

First create three files in /usr/local/etc:

[Link] – this file contains a list of ip addresses you want to


blacklist. One ip or subnet per line.

Example:

[Link]
[Link]/8

[Link] – this file contains a list of ip addresses that you allow


unrestricted access (Be careful with this). One ip or subnet per line.
Make sure you add localhost to this file.

Example:

localhost
[Link] #Home IP Address

[Link] – this file contains a list of ports you allow.

Example:

22 #SSH
25 #SMTP
53 #DNS/Domain
80 #HTTPD
443 #HTTPS

Add this [Link] script to /usr/local/sbin


#!/bin/sh

#Iptables for webserver

IPTABLES=/sbin/iptables
WHITELIST=/usr/local/etc/[Link]
BLACKLIST=/usr/local/etc/[Link]
PORTSLIST=/usr/local/etc/[Link]

#—-Flood Variables—–#

# Overall Limit for TCP-SYN-Flood detection


TCPSYNLIMIT=”5/s”
# Burst Limit for TCP-SYN-Flood detection
TCPSYNLIMITBURST=”10″

# Overall Limit for Loggging in Logging-Chains


LOGLIMIT=”2/s”
# Burst Limit for Logging in Logging-Chains
LOGLIMITBURST=”10″

# Overall Limit for Ping-Flood-Detection


PINGLIMIT=”5/s”

# Burst Limit for Ping-Flood-Detection


PINGLIMITBURST=”10″

#Clear any current filters


$IPTABLES -F

#Process Whitelist
for x in `grep -v ^# $WHITELIST | awk ‘{print $1}’`; do
echo “Permitting $x…”
$IPTABLES -A INPUT -t filter -s $x -j ACCEPT
done

#Process Blacklist
for x in `grep -v ^# $BLACKLIST | awk ‘{print $1}’`; do
echo “Blocking $x…”
#$IPTABLES -A INPUT -t filter -s $x -j LOG
$IPTABLES -A INPUT -t filter -s $x -j DROP
done

#Allow Ports list


for port in `grep -v ^# $PORTSLIST | awk ‘{print $1}’`; do
echo “Accepting port $port…”
$IPTABLES -A INPUT -t filter -p tcp –dport $port -j ACCEPT
done

$IPTABLES -A INPUT -t filter -p tcp –syn -j DROP

#ICMP TIMESTAMP REQUEST AND REPLY


$IPTABLES -A INPUT -p icmp –icmp-type timestamp-request -j DROP
$IPTABLES -A FORWARD -p icmp –icmp-type timestamp-request -j
DROP

#Logging of possible TCP-SYN-Floods


$IPTABLES -N LSYNFLOOD
$IPTABLES -A LSYNFLOOD -m limit –limit $LOGLIMIT –limit-burst
$LOGLIMITBURST -j LOG –log-prefix “fp=SYNFLOOD:1 a=DROP ”
$IPTABLES -A LSYNFLOOD -j DROP

#INVALID SYN packets


$IPTABLES -A INPUT -i eth0 -p tcp –tcp-flags ALL ACK,RST,SYN,FIN -
j DROP
$IPTABLES -A INPUT -i eth0 -p tcp –tcp-flags SYN,FIN SYN,FIN -j
DROP
$IPTABLES -A INPUT -i eth0 -p tcp –tcp-flags SYN,RST SYN,RST -j
DROP

#Logging of possible Ping-Floods


$IPTABLES -N LPINGFLOOD
$IPTABLES -A LPINGFLOOD -m limit –limit $LOGLIMIT –limit-burst
$LOGLIMITBURST -j LOG –log-prefix “fp=PINGFLOOD:1 a=DROP ”
$IPTABLES -A LPINGFLOOD -j DROP

Add /usr/local/sbin/[Link] to [Link] so that it runs when the


machine starts up.

Anytime you make changes to the [Link], [Link], or [Link]


files rerun the [Link] script to apply the rules.

The script also applies iptable rules to help protect against ping
floods, SYN flood, and invalid SYN packets.

Common questions

Powered by AI

The script uses iptables logging for both TCP SYN and ping flood detection by creating distinct chains (LSYNFLOOD and LPINGFLOOD) that log potentially harmful activities with specific log prefixes, such as "fp=SYNFLOOD:1 a=DROP." This logging is crucial because it enables administrators to monitor attempts at network exploitation, analyze traffic patterns, and respond to security incidents promptly .

The whitelist ensures only trusted IP addresses have direct access to specified network services, minimizing attack vectors from unauthorized sources. The blacklist blocks known malicious or untrusted IP addresses, adding an additional layer of security to preempt attacks. This access control mechanism is essential for protecting critical resources and limiting exposure to cyber threats .

The script employs a special chain (LSYNFLOOD) in iptables to log and drop TCP SYN flood packets. It uses rate limiting with parameters such as TCPSYNLIMIT and TCPSYNLIMITBURST to control the rate of SYN packets allowed, which helps detect and mitigate potential flood attacks by logging and dropping excess traffic .

Whitelisting 'localhost' is crucial because many applications and services running on a Linux server rely on loopback communication through the localhost address (127.0.0.1). Blocking this could disrupt these services. Ensuring localhost access guarantees that these internal communications remain uninterrupted and secure .

The 'burst' parameter in flood detection settings specifies the maximum number of packets that can be handled in a short burst beyond the defined rate limit. By configuring this, the system allows for temporary spikes in traffic without dropping packets immediately. This ensures normal operations aren’t interrupted by transient load increases, and only persistent unusual traffic is disregarded, thus enhancing resilience to genuine or false attack signals .

The iptables script manages network traffic by utilizing whitelist and blacklist files to filter IP addresses, only allowing listed IP addresses to access the server. It also defines port rules based on a ports list, specifying which service ports are open for communication, such as SSH (port 22) and HTTPS (port 443). Additionally, the script implements measures against network attacks by setting limits on TCP SYN, ping floods, and logging suspicious activity .

The script drops ICMP timestamp requests and replies that could be used in a ping flood attack. It defines a chain (LPINGFLOOD) and sets a limit on ICMP packets processed per second (PINGLIMIT), as well as the burst limit (PINGLIMITBURST), to prevent and log excessive ICMP traffic, thereby blocking potential flood attacks .

Updates to whitelist, blacklist, and ports files should be managed through a consistent change management process that includes vetting changes for security implications, testing updates in isolated environments if feasible, and tracking changes in version control with appropriate documentation. Automation tools could also be employed to evaluate and deploy these changes securely across multiple environments, ensuring the security policy remains coherent and effective .

Automating the iptables script using rc.local ensures that the firewall and network security rules are applied each time the server boots. This automatic enforcement of security settings helps maintain consistent security posture, reduces the risk of human error in manual setups, and ensures protection is always in place, even after a restart or power failure .

Specific ports are managed via a ports list that specifies which service ports are open for TCP traffic. Each port in the list is allowed through the iptables firewall using the -A INPUT rule, which makes essential services accessible while keeping other ports closed. This configuration impacts service accessibility by ensuring only intended communication is possible, minimizing the risk of unauthorized access or exploitation .

You might also like