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: values, to . Well-known ports - (servers), registered -, ephemeral - (clients).
Example. An SMTP server listens on port 25; a client uses an ephemeral source port, e.g. 51034. Rules say "source port " for clients.
Reminder: TCP opening. Three-way handshake: SYN (client), SYN+ACK (server), ACK (client, possibly with data, piggyback). With the propagation delay of a segment, the opening time is without piggyback and with it; with ms: ms or ms. Closing: three-way or four-way with half closing. The first segment (SYN) has ACK ; 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 ).
| rule | direction | source | dest. | proto | src port | dst port | ACK | action |
|---|---|---|---|---|---|---|---|---|
| A | in | external | internal | TCP | 25 | allow | ||
| B | out | internal | external | TCP | allow | |||
| C | out | internal | external | TCP | 25 | allow | ||
| D | in | external | internal | TCP | 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 , 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 . 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 ) towards ports is no longer possible.
Numeric example. From 203.0.113.9:40000 to the mail server port 25, SYN (ACK ): 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 : D requires ACK , A requires port 25, so rule E denies.
Limit: a crafted packet with ACK 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: 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 | 80 | NEW | allow | |
| B | out | internal | external | TCP | 80 | NEW | allow | |
| C | out | internal | external | UDP | 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à →.
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 connectionsConnection 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
ESTABLISHEDrule (stateful). - Wrong rule order: the first match wins; "deny all" goes last.
- Thinking "destination port " is harmless: it admits X11.
- Thinking ACK proves a real connection.
- Saying NAT is a firewall: it has no policy and does not filter outgoing connections.
Versione ripasso
- Definition. Stops unauthorised traffic between networks (hardware, software or both). Requirements: all traffic passes through it; only authorised traffic passes; immune to intrusion. Incoming/outgoing traffic.
- Policies. User control, service control (socket), direction control (inbound/outbound).
- Outcomes. Accepted, denied (silent drop), rejected (denied + ICMP message, which reveals the firewall).
- Information. L2: network protocol, MAC. L3: addresses, fragmentation flags, transport protocol; source routing discarded. L4: ports, SYN/ACK/FIN flags. Ports: well-known -, registered -, ephemeral -.
- TCP. SYN, SYN+ACK, ACK; (or with piggyback), e.g. 60 ms or 40 ms with ms. SYN has ACK . 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 →) can hide fields.
- Packet filter (stateless). Per-packet; ingress and egress; first matching rule wins; "deny all" last.
- SMTP example. A: in, dst port 25; B: out, dst port ; C and D for sending; E denies all. Spoofed internal source falls in E; Telnet blocked; X11 (6000) passes by mistake.
- Fix 1: source port 25 in B and D (an attacker can still use 25). Fix 2: ACK yes in B and D, so new outgoing connections (SYN, ACK ) are blocked. Limit: crafted ACK packets pass.
- Stateful. Tracks connections; states NEW, ESTABLISHED, RELATED, INVALID; follows UDP and ICMP pseudo-sessions; limits new connections per second, timeouts. Web/DNS example: stateless 6 rules + deny, stateful 3 NEW + 1 ESTABLISHED + deny. DNS reply
8.8.8.8:53to192.168.1.10:51034is ESTABLISHED. - Application firewall (proxy). Impersonates the destination, separate connection, application-layer inspection; safe but slow.
- netfilter. Hooks
PRE_ROUTING,LOCAL_IN,FORWARD,LOCAL_OUT,POST_ROUTING. VerdictsNF_ACCEPT,NF_DROP,NF_QUEUE,NF_STOLEN,NF_REPEAT. - iptables. Tables = purpose, chains = hooks. See 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à →.
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; "" admits X11; ACK is no proof; NAT is not a firewall.