Concepts, from the beginning
Every idea ipfence relies on, explained for someone meeting it for the first time. Each section ends with why ipfence cares.
Servers, clients and services
A server is a computer that waits for others to contact it. A client is whoever does the contacting. A service is one particular thing a server offers: serving web pages, accepting remote logins, and so on. One machine can run many services.
A program that waits quietly in the background to do such work is called a daemon. ipfence runs as one.
Why ipfence cares: it protects services, and it is itself a daemon.
IP addresses
Every device on a network has an IP address, which is where packets come from and go to.
- IPv4 looks like
203.0.113.50: four numbers from 0 to 255. - IPv6 looks like
2001:db8:200::50: longer, written in hexadecimal.::means "a run of zeros here". IPv6 exists because IPv4 addresses ran out.
Some ranges have special meanings:
| Range | Name | Meaning |
|---|---|---|
127.0.0.1, ::1 |
loopback | this computer talking to itself |
10.x, 172.16–31.x, 192.168.x, fd.. |
private | inside a home, office or data-centre network |
169.254.x, fe80.. |
link-local | only reaches directly connected neighbours |
100.64–127.x |
carrier-grade NAT | shared by internet providers, and used by Tailscale |
203.0.113.x, 2001:db8:: |
documentation | reserved for examples, as in these pages |
Why ipfence cares: an address is what it bans. It never bans loopback, never bans this server's own addresses, and by default never automatically bans private or link-local ones, because those are usually you.
Ranges: CIDR notation
198.51.100.0/24 means "every address whose first 24 bits match", which here is
256 addresses, 198.51.100.0 to 198.51.100.255. The number after the slash
says how many leading bits are fixed, so a smaller number means a bigger
range:
| Written | Covers |
|---|---|
198.51.100.7/32 |
exactly one IPv4 address |
198.51.100.0/24 |
256 addresses |
10.0.0.0/8 |
about 16.7 million |
0.0.0.0/0 |
every IPv4 address |
2001:db8:1:2::/64 |
a typical IPv6 subnet |
10.0.0.5/8 is rejected by ipfence: the /8 says only the first number matters,
yet other numbers are filled in. It suggests 10.0.0.0/8 instead.
Why ipfence cares: you can ban or allow ranges. It refuses very wide manual bans so a typo cannot cut off half the internet.
Addresses are not people
One person can have many addresses (phone, home, VPN). Many people can share one (an office, a café, a mobile network). Addresses can change. A ban is therefore a blunt tool, which is why ipfence's first ban is short.
Packets and ports
Data crosses networks in small packets. Each says which address it is from, which it is to, and usually which port: a number identifying the service. SSH normally uses port 22, web traffic 80 and 443.
Why ipfence cares: a ban drops all packets from an address, on every port, not only the one that was attacked.
SSH
SSH (Secure Shell) is how administrators log into Linux servers remotely.
The server program is sshd, almost always OpenSSH. Newer versions split it
into several programs (sshd, sshd-session, sshd-auth).
Logging in uses a password or a key (a file proving who you are). Keys are far stronger than passwords.
Why ipfence cares: SSH is the most commonly attacked door. ipfence reads sshd's messages about failures. It ignores failed keys, because a person with several keys produces those while logging in normally.
Web servers
Nginx and Caddy are programs that serve websites. Each can write an access log: one line per request, with the visitor's address, what was asked for, and the answer.
Answers are numbered status codes: 200 is success, 404 not found, 403
forbidden, 401 login required.
Why ipfence cares: access logs are how it spots probes.
Probes
A probe is a request that looks for a weakness. Asking for /.git/config
(the internals of a developer's code folder) or /wp-config.php.bak (a backup of
a WordPress settings file with passwords) is something a real visitor never does.
Why ipfence cares: only these unmistakable requests count. General "this URL looks odd" rules would ban real people.
Logs and the journal
A log is a program's diary. Two kinds matter here:
- Log files, plain text such as
/var/log/nginx/access.log. - The systemd journal, a central log kept by
journald. You read it withjournalctl. Modern distributions such as Debian 12 and later may keep SSH messages only here.
The journal records facts about the sender of each message that the sender
cannot fake: which program it was, which user ran it, which service it belongs
to. Those fields begin with an underscore, such as _COMM and _UID.
Why ipfence cares: any user on a machine can write a message that looks like sshd's. The underscore fields reveal the forgery.
Rotation
Logs grow, so a tool (usually logrotate) periodically renames the old file and
starts a new one. This is rotation. The variant called copytruncate copies
the file and then empties it.
Why ipfence cares: it must follow a log across rotation without missing or double-counting lines.
Cursors
A cursor is a bookmark: "I have read up to here". The journal gives every entry a cursor; for files ipfence remembers the position.
Why ipfence cares: after a restart it continues from the bookmark.
Firewalls, netfilter and nftables
A firewall decides which packets get through. In Linux the machinery lives in
the kernel (the core of the operating system) and is called netfilter.
nftables is the modern way to configure it; the command is nft.
nftables has a hierarchy:
Diagram source
flowchart TD
T["table: a named container"] --> C["chain: an ordered list of rules"]
T --> S["set: a list of values, such as addresses"]
C --> R["rule: if a packet matches, do something"]
R -. "looks up" .-> S- A table groups related things. ipfence owns one, named
ipfence. - A set is a list that rules can check quickly, here lists of addresses.
- A rule is "if the source address is in this set, drop the packet".
- A verdict is the outcome:
acceptordrop. - A hook is the point in the packet's journey where a chain runs:
inputfor packets addressed to this machine,forwardfor packets passing through it to somewhere else. - A priority orders chains on the same hook; lower runs first.
One property shapes ipfence's design: a drop is final, but an accept is not. If any chain drops a packet it is gone. If one chain accepts it, later chains can still drop it. So a program that only ever drops cannot accidentally open something another firewall closed.
Sets can also expire their own entries: an address added "for 15 minutes" is removed by the kernel after 15 minutes, with no program involved.
Why ipfence cares: this is how bans are enforced and how they expire even if ipfence is not running.
Other firewall tools
ufw and firewalld are friendlier front ends that create their own rules. They can coexist with ipfence because ipfence stays inside its own table.
Linux users and root
root is the administrator account with full power. sudo runs one command as root.
Linux splits root's power into capabilities. Changing the firewall needs the
one called CAP_NET_ADMIN.
Why ipfence cares: it runs as root only because changing the firewall requires it, and the service settings strip most other powers away.
systemd
systemd starts and supervises services on most Linux systems. A unit file describes a service. Useful commands:
| Command | Meaning |
|---|---|
systemctl start ipfence |
start now |
systemctl enable ipfence |
start at every boot |
systemctl status ipfence |
is it running? |
journalctl -u ipfence |
what has it logged? |
A sandbox is a set of restrictions systemd places on a service, such as "may not create internet connections".
Why ipfence cares: systemd runs it, restarts it after crashes, and enforces that it cannot open network connections.
Sockets
A socket is an endpoint for communication. A Unix socket is one that never leaves the machine and appears as a file. A netlink socket is how programs talk to the kernel.
Why ipfence cares: the command you type talks to the daemon through a Unix socket, and the daemon talks to the firewall through netlink. Neither can reach the internet.
Databases and transactions
ipfence keeps its memory in SQLite, a database stored in a single file, with no separate server.
A transaction groups changes so that either all happen or none do, even if the power fails halfway.
Why ipfence cares: it saves "I counted this event" and "my bookmark is now here" together. A crash therefore cannot make it count an event twice or skip one.
Proxies, CDNs and forwarded headers
A reverse proxy receives visitors' requests and passes them to your server. A CDN (content delivery network) such as Cloudflare is a large, distributed one.
Diagram source
flowchart LR
V["Visitor 198.51.100.4"] --> P["Proxy 10.0.0.2"] --> S["Your server"]Your server's firewall sees packets from the proxy (10.0.0.2), called the
peer. The proxy may mention the visitor in a header such as
X-Forwarded-For, but anyone can write any header.
Why ipfence cares: blocking the visitor on your server would not stop them (their packets never arrive directly), and blocking the proxy would block everyone. So ipfence reports such cases and bans nothing.
NAT, containers and Docker
NAT (network address translation) rewrites addresses as packets pass. A container is an isolated program environment; Docker is the common tool for running them. When a container "publishes a port", the kernel rewrites incoming packets and forwards them to the container.
Forwarded packets go through the forward hook, not input.
Why ipfence cares: a rule that only guards input does not protect containers.
ipfence can add a forward chain if you ask (docker.mode = "forward").
False positives and lockout
A false positive is blocking someone innocent. Lockout is blocking yourself.
Why ipfence cares: most of its caution (thresholds, exemptions, short first bans, observe mode, refusing risky manual bans) exists to avoid these two.
Next: The story of an attack.