Salta al contenuto
Note per Studenti Firewalls Report

Firewalls

In questa pagina 5

See also: Transport Layer - Ports and MultiplexingThe transport layer provides logical end-to-end communication between application processes on different hosts and exists only in end hosts; the network layer delivers to the right computer (IP address) and transport to the right process (16-bit port number, 0 to 65535); an IP address plus a port is a socket and the four-tuple of IPs and ports identifies a connection, with multiplexing and demultiplexing done by UDP on the destination socket and by TCP on the whole four-tuple, and the protocols are UDP (connectionless, unreliable), TCP (connection-oriented, reliable) and SCTP.Transport Layer - Ports and Multiplexing →, TCP - Connection, Reliability and Flow ControlTCP is the connection-oriented, reliable transport protocol that turns IP's unreliable service into an ordered byte stream without errors or duplicates; the connection opens with a three-way handshake (SYN, SYN+ACK, ACK) and closes with FIN segments, bytes are numbered (sequence number = first byte of the segment, cumulative ACK = next byte expected), the sender may transmit min(rwnd, cwnd) unacknowledged bytes (rwnd, the receiver window in a 16-bit field, is flow control), errors are handled with checksum, ACKs, retransmission timeout and fast retransmit after three duplicate ACKs, and to fill the pipe the window must reach the bandwidth-delay product, with maximum throughput MSS times Wmax over RTT.TCP - Connection, Reliability and Flow Control →, NAT and Private AddressesPrivate networks (intranets) use TCP/IP with private address blocks (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) that may be reused by different intranets but are never routed in the public Internet; to reach the Internet an intranet needs an application proxy or NAT, a router that translates private to public addresses using a NAT table and per-session mappings: Basic NAT translates only the IP address one to one, NAPT also translates the port so many sessions share one public IP, and Twice NAT adds DNS support and static mappings so sessions can also start from outside.NAT and Private Addresses →, ICMP, Ping and TracerouteICMP adds to IP the error reporting and queries it lacks; its messages travel inside IP datagrams with Protocol 1 and are either errors (destination unreachable type 3, time exceeded 11, redirect 5, parameter problem 12), always sent to the original source with the original IP header plus 8 bytes of data, or queries (echo request 8 and reply 0, timestamp 13 and 14); ICMP only reports and never corrects errors, and it supports ping (RTT), path MTU discovery with the D bit (maximum payload 1500-20-8=1472 bytes) and traceroute (TTL increasing, n+1 probes for n routers), but also attacks such as smurf and fake redirects.ICMP, Ping and Traceroute →.

What a firewall is

Definition. Part of a computer system or network designed to stop unauthorised traffic passing from one network to another. It can be hardware, software or both. It filters data, redirects traffic, protects from attacks.

A well-designed firewall must satisfy three requirements: all traffic between two trust zones passes through it; only authorised traffic passes; the firewall itself is immune to intrusion. Packets can be incoming (Internet to LAN) or outgoing. An administrator configures a set of policies:

  • user control: access depends on the user's role inside the perimeter;
  • service control: access depends on the service, usually the socket address (address and port);
  • direction control: inbound or outbound flow.

Definition (outcomes). A packet can be accepted; denied (silently dropped); rejected (denied, plus an ICMP message to the source). Rejecting can help an attacker (it reveals the firewall), so silent drop is often preferred.

Information used

layer information
L2 link next physical hop, network protocol, MAC address
L3 network source and destination addresses, fragmentation flags, transport protocol (TCP, UDP, ICMP), options
L4 transport source and destination ports, TCP control flags (SYN, ACK, FIN)

Packets with source routing are usually discarded because they are used in attacks.

Property (port ranges). The port number has 16 bits: 216=65 5362^{16}=65\,536 values, 00 to 65 53565\,535. Well-known ports 00-10231023 (servers), registered 10241024-4915149151, ephemeral 4915249152-6553565535 (clients).

Example. An SMTP server listens on port 25; a client uses an ephemeral source port, e.g. 51034. Rules say "source port >1023>1023" for clients.

Reminder: TCP opening. Three-way handshake: SYN (client), SYN+ACK (server), ACK (client, possibly with data, piggyback). With τp\tau_p the propagation delay of a segment, the opening time is TCE=3τpT_{CE}=3\tau_p without piggyback and 2τp2\tau_p with it; with τp=20\tau_p=20 ms: 6060 ms or 4040 ms. Closing: three-way or four-way with half closing. The first segment (SYN) has ACK =0=0; all later ones have ACK on.

Effect of encryption. Encryption (IPsec, TLS, VPN and IPsecA VPN is a private network built over a public one: a VPN server admitted by the firewall authenticates outside users, and protected IP packets are encapsulated as the payload of other IP packets (IP tunnelling) using IPsec (AH, ESP, tunnel or transport mode, one-way Security Associations) or an SSL/TLS tunnel through TUN/TAP virtual interfaces.VPN and IPsec →) may hide fields from the firewall, depending on the layer at which the firewall works.

Types of firewall

Packet filter (stateless)

Works on each packet individually, ingress (protecting the internal network) and egress filtering. It can use source and destination IP, transport protocol, ports, TCP flags, interface. Actions: pass, drop, log, optionally send an error. Rules are read in order and the first match applies; the "deny all" rule goes last (default deny).

Example 1: allow only SMTP towards a mail server (server port 25, client port >1023>1023).

rule direction source dest. proto src port dst port ACK action
A in external internal TCP 25 allow
B out internal external TCP >1023>1023 allow
C out internal external TCP 25 allow
D in external internal TCP >1023>1023 allow
E both any any any any deny

A and B receive mail; C and D send it. A packet with forged internal source is blocked (falls in E). Telnet (port 23) is blocked. X11 (server port 6000) is not blocked: rule D allows external to internal TCP towards ports >1023>1023, so X11 is admitted by mistake.

Example 2: fix with source ports. B and D require source port 25, and A and C source port >1023>1023. X traffic of internal clients is now blocked, but an attacker can still use source port 25.

Example 3: fix with ACK. B and D also require ACK = yes. A new outgoing connection (SYN, ACK =0=0) towards ports >1023>1023 is no longer possible.

Numeric example. From 203.0.113.9:40000 to the mail server port 25, SYN (ACK =0=0): rule A, allowed. The reply (source port 25, SYN+ACK): rule B, allowed. An external attacker with source port 25 trying the X server (port 6000), SYN with ACK =0=0: D requires ACK =1=1, A requires port 25, so rule E denies.

Limit: a crafted packet with ACK =1=1 still passes, because the firewall does not know whether it belongs to a real connection.

Stateful firewall

Definition. Tracks the state of traffic, controlling all interactions of a connection until closed.

It can check the protocol state (SYN, SYN+ACK, ...) and admit only valid sequences; it follows "pseudo-sessions" also for UDP. States: NEW (first packet of an unknown connection), ESTABLISHED (packet of an existing connection), RELATED (linked to an existing connection, e.g. an ICMP error), INVALID (invalid header values). It can limit new connections per second, slow abusive clients against DoS, apply timeouts to idle sessions.

Example. Allow external HTTP to the web server (TCP 80), and internal hosts starting HTTP (TCP 80) or DNS (UDP 53). Stateless: 3⋅2=63\cdot2=6 rules plus "deny all" (one per direction of each flow, with ACK == yes on the return rules). Stateful: three NEW rules plus one ESTABLISHED rule plus "deny all":

rule direction source dest. proto src port dst port state action
A in external web server TCP >1023>1023 80 NEW allow
B out internal external TCP >1023>1023 80 NEW allow
C out internal external UDP >1023>1023 53 NEW allow
D both any any any any any ESTABLISHED allow
E both any any any any any any deny

Numeric example. 192.168.1.10 sends a DNS request from :51034 to 8.8.8.8:53: rule C (NEW), allowed and recorded. The reply 8.8.8.8:53 to 192.168.1.10:51034 is ESTABLISHED: rule D. A UDP packet from 8.8.8.8:53 to another port of 192.168.1.10 has no entry, is NEW and falls in E.

Application (proxy) firewall

Controls input and output of a specific application, inspecting up to the application layer. A typical implementation is the proxy: it impersonates the destination and opens a separate connection to it. Safe but slow; it can be coupled to user authentication.

Linux firewall: netfilter and iptables

Netfilter is the kernel framework for packet processing, based on hooks: when a packet reaches a hook, the IPv4 stack calls netfilter, which calls the callback functions registered by modules; each returns a verdict.

hook when
NF_INET_PRE_ROUTING all incoming packets, before any routing decision
NF_INET_LOCAL_IN the packet is for this host
NF_INET_FORWARD the packet must be forwarded
NF_INET_LOCAL_OUT a packet generated by this host
NF_INET_POST_ROUTING the packet is leaving the host
verdict description
NF_ACCEPT the packet continues
NF_DROP drop the packet
NF_QUEUE queue the packet to user space
NF_STOLEN netfilter forgets the packet, taken by another module (e.g. fragments to reassemble)
NF_REPEAT repeat the module

iptables is the user-space program configuring the built-in firewall (Xtables): tables define the general purpose of rules, chains correspond to the hooks (PREROUTING, INPUT, FORWARD, OUTPUT, POSTROUTING). Details in Firewall Configuration with iptables in KatharàLab 6 configures a packet-filter firewall on Linux with iptables (NetFilter): tables filter, nat, mangle and raw hold chains of rules, the first matching rule wins, otherwise the default policy applies; three experiences cover a firewall protecting a host (INPUT with DROP policy plus loopback, ping, SSH and ESTABLISHED,RELATED), a firewall on a LAN router (FORWARD) and NAT with SNAT and DNAT.Firewall Configuration with iptables in Katharà →.

bash
iptables -P FORWARD DROP                                   # default policy: forwarded packets are dropped
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT              # LAN (eth1) towards Internet (eth0)
iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT   # replies of open connections

Connection tracking (nf_conntrack) stores the state of sessions: TCP with its handshake; UDP considered connected once client and server exchange packets; ICMP request-reply pairs; complex protocols such as HTTP and FTP.

Common mistakes

  • Saying the firewall encrypts or protects from internal attacks: it limits who talks to whom on which service.
  • Forgetting return traffic (stateless) or the ESTABLISHED rule (stateful).
  • Wrong rule order: the first match wins; "deny all" goes last.
  • Thinking "destination port >1023>1023" is harmless: it admits X11.
  • Thinking ACK =1=1 proves a real connection.
  • Saying NAT is a firewall: it has no policy and does not filter outgoing connections.

Versione ripasso

bash
iptables -P FORWARD DROP
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
  • Connection tracking nf_conntrack: TCP handshake, UDP when packets are exchanged, ICMP request-reply, HTTP and FTP.
  • Mistakes: firewall does not encrypt; forgetting return traffic; rule order; ">1023>1023" admits X11; ACK =1=1 is no proof; NAT is not a firewall.

Teoria collegata