7. Firewall

What is Firewall

Requirements of a firewall

Firewall Policy

Firewall actions

Accepted: Allowed to enter the connected network/host through the firewall.

Denied: Not permitted to enter the other side of firewall.

Rejected: Similar to “Denied”, but tells the source about this decision through ICMP packet.

Type of filters

Depending on the mode of operation, there are three types of firewalls :

Packet Filter Firewall

Stateful Firewall

Application/Proxy Firewall

Building Firewall using Netfilter

Packet filter firewall implementation in Linux

Netfilter

Verdict (Return Values)

It is something you can return after analysis is done on the packet

Pasted image 20231016094137.png
#toupdate redraw, network stack is the os model, remove the header, first routing decide if its meant for local machine, second routing filter the actual packet payload. The prerouting is the function that you defined

Iptables Firewall in linux

It is a set of predefined rules for you to use

Iptables Firewall - Structure

Traversing Chains and Rule Matching

Tables and Chains: Within Netfilter, packets are processed by tables. Each table contains a series of chains, which are essentially ordered lists of rules. There are predefined chains like INPUT, FORWARD, and OUTPUT, and users can define custom chains. Each rule in a chain inspects the packet and decides an action, like accepting or dropping the packet.

There are three predefined chains in the filter table of iptables:

  1. INPUT: This chain is used to process packets that are destined for the local system.
  2. FORWARD: This chain is used for packets that are routed through the local system but aren't meant for the local system itself. Typically, this is used when the system acts as a router or gateway.
  3. OUTPUT: This chain is used for packets that originate from the local system.

Apart from the filter table, there are other tables in iptables, such as nat, mangle, and raw, and they have their own predefined chains. For example, the nat table includes:

  1. PREROUTING: For altering packets as soon as they come in.
  2. POSTROUTING: For altering packets as they are about to go out.
  3. OUTPUT: For altering locally-generated packets before routing.

Pasted image 20231016095639.png

  1. Decideds if the final destination of the packet is the local machine
  2. Packet traverses through INPUT chain
  3. Packet traverses through FORWARD chains
  4. Decides. from which of the network interface to send out outgoing packets

As a packet traverses through each chain, rules on the chain are examined to see whether there is a match or not. If there is a match, the corresponding target action is executed: ACCEPT, DROP or jumping to user-defined chain.

Example

Increase the TTL field of all packets by 5.

Add a rule to the mangle table and choose a chain provided by netfilter hooks. We choose PREROUTING chain so the changes can be applied to all packets, regardless they are for the current host or for others.

iptables -t mangle -A PREROUTING -j TTL -ttl-inc 5

Iptables Extensions

Iptables functions can be extended using modules also called as extensions.

Examples:
Conntrack: To specify rules based on connections to build stateful firewalls.

Owner: To specify rules based on user ids. Ex: To prevent user Alice from sending out telnet packets. Owner module can match packets based on the user/group id of the process that created them. This works only for OUTPUT chain (outgoing packets) as it is impossible to find the user ids for INPUT chain(incoming packets).

Stateful Firewall using Connection Tracking

Connection Tracking Framework in Linux

Example

sudo iptables -A OUTPUT -p tcp -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
This command set up firewall rule to only allow outgoing TCP packets if they belong to an ESTABLISHED TCP connection

Application/Proxy Firewall and Web Proxy

Use proxy to bypass firewall

SSH Tunneling

Work machineSSH MachineHome machineSTOPTunnelgain access through tunnelFIREWALL80002322

ssh -L 8000:work:23 sshmachine

Establish an ssh tunnel from Home to Remote machine that is in the firewall. The tunnel that is established will forward TCP data received on 8000 on Home to port 23 on work

Anonymizing Proxy

One can also use proxies to hide the origin of a network request from servers. As the servers can only see the traffic after it passes through proxies, source IP will be the proxy’s and actual origin is hidden

Revision question

What are the five netfilter hooks for IPv4 what are their purpose
NF_IP_PREROUTING to setup rules for packets before they enter the host machine
NF_IP_LOCAL_IN to setup rules for packets intended for the host
NF_IP_FORWARD to setuo rules for packets to be forwarded
NF_IP_LOCAL_OUT to setup rules for packets inteded for the host