Archive

Articles taggués ‘cybersecurite’

iptables command in Linux

25/09/2026 Aucun commentaire

Source: geekforgeeks

iptables is a Linux command-line firewall tool used to control and secure network traffic by defining packet-filtering rules. It works with the Netfilter framework in the Linux kernel to decide whether packets are accepted, dropped, or forwarded.

  • Controls incoming, outgoing, and forwarded traffic based on IP addresses, ports, and protocols.
  • Uses tables, chains, and rules to define how network packets should be handled.
  • Performs actions such as ACCEPT, DROP, REJECT, or FORWARD when packets match rules.
  • Helps protect servers and networks by allowing trusted traffic and blocking unauthorized access.

Core Components of iptables

iptables works using a structured model consisting of tables, chains, rules, and targets.

Tables in Iptables

Tables are collections of chains that define how packets should be processed for specific purposes, iptables uses different tables for handling various types of packet processing.

  • filter: Default used table for packet filtering. It includes chains like INPUT, OUTPUT and FORWARD.
  • nat: Related to Network Address Translation. It includes PREROUTING and POSTROUTING chains.
  • mangle: For specialised packet alteration. Inbuilt chains include PREROUTING and OUTPUT.
  • raw: Configures exemptions from connection tracking. Built-in chains are PREROUTING and OUTPUT.
  • security: Used for Mandatory Access Control

Built-in Chains of Tables in Iptables

Chains are ordered lists of rules that determine what action should be taken when a packet matches certain conditions. Each table contains predefined chains

  • INPUT: A set of rules for packets destined to localhost sockets.
  • FORWARD: For packets routed through the device.
  • OUTPUT: It is locally generated packets, meant to be transmitted outside.
  • PREROUTING: It is used for modifying packets as they arrive.
  • POSTROUTING: IIt helps in modifying packets as they are leaving.

Rules

Rules are the conditions applied to packets within chains. A rule matches a packet if it meets certain criteria. Common criteria include:

  • Source IP address: Blocks or allows traffic coming from a specific computer or device. Example: Stop all traffic from 192.168.1.100.
  • Destination IP address: Blocks or allows traffic going to a specific computer or device. Example: Allow traffic only to 10.0.0.5.
  • Protocol type: Filters traffic based on the type of communication, like TCP (web), UDP, or ICMP (ping).
  • Port number: Controls access to certain services or programs on a computer. Example: Allow web traffic on port 80 but block SSH on port 22.

Targets (Actions)

Targets specify what happens when a packet matches a rule:

  • ACCEPT: Allow the packet to continue its path.
  • DROP: Silently discard the packet with no response.
  • REJECT: Discard the packet but send an error response to the sender.
  • QUEUE: Send the packet to userspace for custom processing.
  • RETURN: Stop processing the current chain and resume at the previous chain.

Example:

Imagine you want to block SSH from a specific IP but allow all other traffic:

iptables -A INPUT -p tcp --dport 22 -s 192.168.1.100 -j DROP
  • Chain: INPUT
  • Protocol: TCP
  • Destination Port: 22 (SSH)
  • Source IP: 192.168.1.100
  • Target: DROP

Output:

This rule drops incoming SSH connections from 192.168.1.100 while letting other traffic pass normally.

Syntax

iptables [options] [chain] [rule specification] [target]
  • options: Specifies the action to perform (like adding, deleting, or listing rules)
  • chain: Defines where the rule is applied (INPUT, OUTPUT, FORWARD)
  • rule specification: Conditions to match packets (protocol, port, IP address, etc.)
  • target: Action to take on matched packets (ACCEPT, DROP, REJECT)

Lire la suite…