Logo

dev-resources.site

for different kinds of informations.

Managing traffic using iptables firewall

Published at
12/11/2024
Categories
security
firewall
Author
yeshraj_b
Categories
2 categories in total
security
open
firewall
open
Author
9 person written this
yeshraj_b
open
Managing traffic using iptables firewall

iptables is a powerful firewall that allows you to set up rules to control incoming, outgoing, and forwarded traffic on your networks. It is a default firewall in many linux systems including server instances provided on oracle cloud.

In this post, we’ll cover the following topics:

  • The Basics of iptables
  • Installing and Viewing Rules
  • Common iptables Commands
  • Working with Chains and Rules
  • Best Practices

The Basics of iptables

iptables works by examining network packets and applying rules to determine how to handle them. The main component of iptables is the Chains

Chains: A chain is a list of rules that are examined in order. The three default chains in iptables are:

  • INPUT: For packets coming into our server aka incoming traffic.
  • OUTPUT: For packets going out of our server aka outgoing traffic.
  • FORWARD: For packets being routed through the server.

Installing and Viewing Rules

On Debian/Ubuntu, you can install iptables with:

sudo apt-get install iptables
Enter fullscreen mode Exit fullscreen mode

Once installed you can use this command to view it's current state:

sudo iptables -L -n -v
Enter fullscreen mode Exit fullscreen mode
  • -L: List the rules.
  • -n: Show numerical addresses instead of resolving hostnames.
  • -v: Provide verbose output.

Common iptables Commands

  • Allow input traffic on a port:
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
Enter fullscreen mode Exit fullscreen mode

This will accept incoming traffic on the port 80

  • Block input traffic on a port:
sudo iptables -A INPUT -p tcp --dport 443 -j DROP
Enter fullscreen mode Exit fullscreen mode

This will disallow input traffic on the port 443

  • Reject a Connection (Send a response):
sudo iptables -A INPUT -p tcp --dport 23 -j REJECT
Enter fullscreen mode Exit fullscreen mode
  • Flush All Rules:
sudo iptables -F
Enter fullscreen mode Exit fullscreen mode
  • Delete a Specific Rule: To delete a rule, you first need to find out its sequence number:
sudo iptables -L --line-numbers
sudo iptables -D INPUT {line_number}
Enter fullscreen mode Exit fullscreen mode

Working with Chains and Rules

Before adding or deleting a rules it's important to understand the order in which the rules of a chain are executed.

iptables evaluates rules in a chain sequentially, from top to bottom. If a rule does not match the packet, only then the packet is passed to the next rule.

When a packet arrives, it's checked against each rule in the chain, one by one, until a match is found. Once a matching rule is identified, the corresponding action (such as ACCEPT, DROP or REJECT) is applied, and processing stops. No further rules in the chain are evaluated for that packet.

So if you have a REJECT rule at line 14 which rejects all incoming traffic

REJECT     all  --  *      *       0.0.0.0/0    0.0.0.0/0      reject-with icmp-host-prohibited
Enter fullscreen mode Exit fullscreen mode

and you have a rule at line 15 which accepts incoming traffic on port 3000

ACCEPT     tcp  --  *      *       0.0.0.0/0    0.0.0.0/0      tcp dpt:3000
Enter fullscreen mode Exit fullscreen mode

there will be no incoming traffic on port 3000 as iptables will stop on line 14 and the ACCEPT rule on line 15 will not be evaluated.

  • Add Rule to End of Chain:
sudo iptables -A INPUT -s 192.168.1.2 -j ACCEPT
Enter fullscreen mode Exit fullscreen mode
  • Add a Rule at a Specific Position:
sudo iptables -I INPUT 4 -s 192.168.1.2 -j ACCEPT
Enter fullscreen mode Exit fullscreen mode

Here 4 is the line number where this rule will be added

  • Add a incoming traffic rule at Position 6 :
sudo iptables -I INPUT 6 -p tcp --dport 8000 -j ACCEPT
Enter fullscreen mode Exit fullscreen mode

Best Practices

  • Backup Your Rules: Always take a backup before making changes.
  • Test Rules: Apply and test your rules carefully, especially when implementing complex configurations.
  • Use DESCRIPTIVE Comments: Document your rules by using comments to explain the purpose of complex rules.
  • Monitor Logs: Regularly check the logs to identify any potential issues or blocked traffic.
firewall Article's
30 articles in total
Favicon
Configuring network access with Cisco ASA via minicom utility
Favicon
Configuring Cisco firewall in Linux machine with Minicom
Favicon
How to Bypass Sophos Firewall?
Favicon
How Next-Generation Firewalls Are Revolutionizing Cybersecurity🔥🛡️
Favicon
Firewall Testing 101: How to Secure Your Network and Block Cyber Threats
Favicon
Managing traffic using iptables firewall
Favicon
Homemade application firewall for Linux
Favicon
How to Become a Firewall Administrator: A Comprehensive Guide
Favicon
Debian 12 … is amazing! How to: Create your custom codehouse #4 [Security mechanisms against Network-Based attacks]
Favicon
PHP + ip2location = PHPFirewall
Favicon
How to recover and update Proxmox 8 firewall configuration in SQLite when you locked yourself out
Favicon
Setup firewall on Alpine with nftables
Favicon
Open Text Shield (OTS)
Favicon
Linux Firewall: Blocking a lot with a little
Favicon
Fortify Your Network with Optimal IPTables Rules for Cybersecurity
Favicon
Firewalls in Zero-Trust Security: Fortifying Modern Cyber Defenses
Favicon
Proxmox Network Storage: Firewall Rules
Favicon
Why SafeLine is better than traditional WAF?
Favicon
Why Choose SafeLine? Discover the Secrets of a Top Web Application Firewall
Favicon
A User-Friendly Web Security WAF Product - Safeline
Favicon
Firewalls 101: Understanding Types, Functions, and Configurations
Favicon
what happens when you type https://www.google.com in your browser
Favicon
pfSense basic firewall setup
Favicon
UFW - Quickstart
Favicon
Understanding Linux Firewalld
Favicon
Understanding Firewalls: A Comprehensive Guide
Favicon
Create new firewall rules for Azure SQL databases
Favicon
How to use efficiently IPSET with CSF Firewall
Favicon
Azure Firewall
Favicon
Understanding Firewalls: Your First Line of Cyber Defense

Featured ones: