Skip to main content

Bastion Egress

Egress allowlisting decides on a shared bastion host where each user can connect, so one bastion can replace a chain of jump hosts. Each egress knoc blocks its destinations there, and the agent lets a user's local account through only while that user holds a grant for the knoc, so Alice can reach the PCI database and Bob the development network from the same bastion host without gaining the other's access.

The agent runs on the bastion and enforces this with the host's own firewall, matching each outbound connection on the account that opened it. No credentials or remote API access are involved.

Egress allowlisting controls where people on the bastion can go. Getting to the bastion can be part of the same knoc: see Letting Users In Too. Or protect its SSH or RDP port with a separate nftables, IPSet, pf or Windows knoc as usual.

How It Works

Every firewall checks an outbound connection in the same order. On pf the agent sets this up for you. On nftables, iptables and Windows you write part of it yourself, and the nftables, iptables and Windows guides show you how:

  1. Established connections and loopback traffic pass.
  2. Root, the agent's own account and any accounts you exempt are never blocked. Use exempt accounts for break-glass access, or for a service that needs to reach a blocked destination. Windows is different: only the exceptions on the rule's Local principals tab are never blocked. A rule you create blocks SYSTEM unless you add it there, and a rule Knocknoc creates blocks every account, including SYSTEM and services.
  3. An account with a grant is let through to that knoc's destinations.
  4. Everyone else is refused.

A destination is blocked as soon as you add it to an egress knoc.

If two knocs list the same destination, a grant on either lets the user through. iptables is the exception: the knoc whose jump comes first in OUTPUT decides, so keep a destination on one knoc.

After a reboot the blocks are back before the agent reconnects, but the grants aren't. Users with a grant have to wait until the agent has synced with the Knocknoc server. There is one exception: on iptables with an ACCEPT policy, a knoc's destinations are open after a reboot or a reload until the agent writes its chain back.

When you delete an egress knoc, its destinations are open to everyone again. Your own rules, chains and sets are left alone. On Windows, a Block rule you created keeps blocking everyone, and rules the agent created are deleted.

The agent needs to be running and connected to clean up after a deleted knoc:

  • If the agent is cut off from the server when you delete the knoc, it cleans up once it reconnects. With several servers this can take up to an hour.
  • If the server is older than the agent, the block stays until you restart the agent.
  • If the agent is stopped when you delete the knoc, nothing removes the block later. You need to remove it by hand, and each firewall's guide says how.

Who Is Blocked

On pf you choose who each knoc blocks, with Fence applies to:

Fence applies to Who is refused at the knoc's destinations without a grant
Everyone (default) Every account except the ones in step 2 of How It Works
User accounts Only people's accounts: uid 1000 and up. System accounts can reach the destinations freely

Choose User accounts when a service on the bastion needs the destination too.

On the other firewalls your own rules decide:

  • nftables: the reject is your rule. To block only people's accounts, match them with meta skuid >= 1000.
  • iptables: the knoc's reject blocks every account without a grant. To leave system accounts free, turn the knoc's reject off and write your own with -m owner --uid-owner 1000-4294967294.
  • Windows: the rule's Local principals tab decides who it applies to.

Requirements

You need:

  • A bastion running the Knocknoc agent, with one of the firewalls in Choosing a Firewall.
  • One local account per person. Access is given to accounts, so everyone sharing a login such as ec2-user gets the same access. On Linux, accounts can be local or come from a directory through SSSD. On Windows they can be local, Active Directory domain or Microsoft Entra ID accounts.
  • No sudo or root for users. Root can rewrite the firewall, so it's never blocked.
  • On Windows, users who aren't local administrators. An administrator is blocked like anyone else, but can turn Windows Firewall off or change the rule.
  • Accounts with a uid of 1000 or higher, which is where useradd and most directories start. The agent won't grant a lower uid, or nobody (uid 65534). On Windows the same limit applies to the RID, the last number of the account's SID, which rules out Administrator, Guest and the other built-in accounts. A username made only of digits is treated as an account name, never as a uid.

Creating the Knoc

Set the bastion up first, following the guide for its firewall Choosing a Firewall. Then:

  1. In the Knoc wizard, choose the Bastion egress card with the logo of the bastion's operating system (Linux, Windows or OpenBSD) and select the agent on the bastion. On Linux, pick the Firewall: nftables or iptables.

    If you can't choose Bastion egress for the agent, run knocker status egress on the bastion. On Windows, check the agent's service runs as LocalSystem.

  2. Fill in the knoc:

    Field Example Notes
    Account mapping Microsoft Entra ID, users become alice.smith How a user becomes a local account name. Choose Set up mapping, say where the account name comes from, pick your identity provider and answer its questions. See Local Accounts.
    Destinations 10.20.0.0/24, 10.30.1.15:22 The addresses and networks to block, IPv4 or IPv6, each with optional ports. On Windows you only fill this in when Knocknoc creates the rules. See Destinations and Ports.
    Fence applies to Everyone pf only. Who is refused at the destinations without a grant. See Who Is Blocked.
    Users set inet:filter:knocknoc_egress_users nftables only. The set of uids your rules accept for this knoc's destinations. See nftables egress.
    Chain KNOCKNOC_EGRESS_1 iptables only. The chain in your ruleset that the agent fills for this knoc. See iptables egress.
    Rule kk_bastion_egress Windows only. With Firewall rules on Use an existing firewall rule, this is the outbound Block rule you created. Create a new firewall rule asks for the destinations instead. See Windows egress.
  3. Choose Verify under Verify knoc against device to check the bastion is ready, then save.

  4. Assign users and/or groups to the Knoc.

  5. Log in to Knocknoc as a test user and request access. Then check the grant on the bastion with knocker status egress, or on Windows as Checking What Is Granted describes.

  6. As that user on the bastion, connect to a destination. Then try the same destination as an account with no grant. It should be refused.

Letting Users In Too

An egress knoc can also let its users in to the bastion, so you don't need a second knoc for that. The knoc's Settings have a section for each direction, Egress (Out from this host) and Ingress (In to this host). Ingress is on by default. Fill in what the firewall needs, or turn it off to use a different knoc, or no knoc if your bastion reachable without knocknoc.

Egress firewall Lets users in through What you fill in
iptables ipset The IPv4 and IPv6 sets your rules accept, as for an IPSet knoc
nftables nftables The sets your rules accept, as for an nftables knoc
pf (OpenBSD) pf The table your rules pass, as for a pf knoc
Windows Windows Firewall The inbound rule to admit addresses through, as for a Windows knoc

Users see one knoc, and one grant opens both directions. Knocknoc keeps the ingress part as a second knoc under the egress knoc, with its own integration. If the agent hasn't reported the ingress firewall, you can't turn ingress on, and the section tells you which firewall it's waiting for.

A few things to know:

  • If you add ingress to a knoc that's already in use, every user with a grant gets ingress straight away.
  • If you turn ingress off on a saved knoc, its grants are revoked and its integration is deleted.
  • You can't move the knoc to an agent that lacks something ingress needs, such as its firewall rule.
  • A failed egress grant doesn't stop the ingress grant. A user with no local account can still reach the bastion's login, and is refused there. The knoc's status shows which half failed.
  • You can't make a manual grant from the admin portal for either part (see What Is and Is Not Covered).

Destinations and Ports

A destination is an address or a network. You can follow it with the port or port range to block:

Destination What the knoc blocks
10.20.0.5 or 10.20.0.5/32 Every port on the address
10.20.0.0/24 Every port on the network
10.20.0.5:22 TCP and UDP port 22 only
10.20.1.0/24:8000-8100 Ports 8000 to 8100
[2001:db8::1]:22 Port 22 on an IPv6 address. Bracket an IPv6 address or network when it has ports.

Ports you don't list stay open to everyone. That lets one host carry two knocs: one blocking port 22 for administrators and another blocking 443 for everyone else.

You can't list overlapping destinations in one knoc, or the same address twice with different ports, so a knoc blocks one port or range per address. A knoc can hold up to 1024 destinations.

Each firewall has its own limits:

  • nftables: destinations with ports need Linux 5.6 or later on the bastion. On an older kernel knocker enable egress doesn't create sets for them.
  • iptables: each port range costs one rule for TCP and one for UDP per granted account, and a knoc's destinations can cost at most 256 rules per account.
  • Windows: with your own rule, the ports are whatever you set on it. When the agent creates the rules, it makes one for every destination without ports, and a TCP and a UDP rule for each distinct set of ports, at most 32 per knoc.

Local Accounts

A grant is for a Knocknoc user, but the bastion works with local accounts. The knoc's mapping tells Knocknoc which local account each user gets. The agent then checks that the account exists, has a uid (on Windows a RID) of 1000 or higher, and isn't the agent's own.

To set it up, choose Set up mapping under Account mapping on the knoc, or Edit mapping once the knoc has one. As you edit, the dialog shows what your identity provider's example users would get. To check a real user, type their username into Test a username and choose Check on host. This tells you whether the account exists on the bastion with a uid or RID of 1000 or higher.

You can start from another egress knoc's mapping. Choose the copy button beside the mapping on the knoc, or Copy from knoc at the top of the dialog, and pick the knoc. Check it before you save:

  • The host question is asked again for this bastion. A Windows mapping's domain is dropped on Linux, and a Linux mapping copied to Windows asks where its accounts live.
  • Rules you edited by hand are copied as written, so they may not fit this host.

Where the Account Comes From

The dialog asks this first. Tick one or more sources. They're tried in this order, whatever order you tick them:

  1. Account name from the IdP. A SAML attribute holding the user's account on the bastion, such as localAccount, used as it is. Put the attribute's name in IdP attribute holding the account name.
  2. Another IdP attribute, mapped. A SAML attribute holding a username, run through the rules. Use it when the NameID is an opaque ID but another claim, such as an email, can be mapped. Put the attribute's name in IdP attribute to map.
  3. Knocknoc username, mapped. The SAML NameID, the LDAP login or the local user's name, run through the rules.

The first ticked source that has a value for the user decides. If no rule maps that value, the grant is refused and the next source isn't tried. This stops a person landing on a different account depending on which attributes their provider sent that day.

If you tick only Account name from the IdP there's nothing to map, so the dialog asks nothing more.

Choosing the Identity Provider

Pick the identity provider your users sign in through: Microsoft Entra ID, Okta, Google Workspace, Active Directory, OpenLDAP, Keycloak or FreeIPA, Auth0, or another provider. If all your user identity providers are the same kind and Knocknoc recognises it, a new mapping starts on that provider.

The dialog then asks a few questions, one at a time. Each shows a username the provider sends and what it could become. Name questions turn the username into the person's name. A host question then says how this bastion spells that name. You can save once every question is answered:

Identity provider Name questions Host question on Linux Host question on Windows
Microsoft Entra ID [email protected] becomes alice.smith, [email protected], asmith or alice_smith. A B2B guest is refused, or becomes guest_henry or henry None Local or domain account
Okta, Google Workspace Logins, then whether [email protected] is the same person as [email protected] or is refused None Local or domain account
Active Directory None How SSSD names the account: asmith, [email protected] or CORP\asmith None: the account is CORP\asmith
OpenLDAP None: an LDAP DN gives its uid, and a plain login is used as it is None None
Keycloak or FreeIPA None How SSSD names the account: alice or [email protected] Local or domain account
Auth0 samlp|corp-sso|[email protected] becomes alice.smith or [email protected]. Database and social IDs need an attribute None Local or domain account

Your answers write the rules, which you can see under Advanced configuration. Edit them there if no answer fits. The dialog marks rules you've edited by hand and offers to regenerate them from your answers. If no provider fits, choose Another provider and write the rules yourself. You can't save a mapping that uses the rules until you've chosen a provider.

When the mapping gives no usable account, the grant fails with EGR053 and the reason. The admin portal's health panel shows a warning while this has happened in the last 24 hours, with links to the knocs that failed. When you save a changed mapping the knoc's grants are resent, so a live grant moves to its new account at once.

Advanced Configuration: Rules

Each rule has a condition, a template and transforms. The value a mapped source holds goes through the rules top to bottom, and the first rule whose condition matches gives the account.

Condition Matches
Any username Every username
Domain is A username whose domain is the one given or a subdomain of it, such as corp.com for [email protected]
Format is A username of one kind, as the preview labels it: email, Kerberos principal, DOMAIN\user, Entra guest, LDAP DN, provider|id, opaque ID or plain name
Matches A username a regular expression matches

The template is the account name, with tokens in braces that the username fills in. Text outside the braces is kept as it is, so ext_{short} gives ext_alice.

Token Is For [email protected]
{username} The whole username [email protected]
{localpart} Before the first @ alice.smith
{domain} After the last @ corp.com
{domain.1} The domain's first label corp
{short} The name without its domain, from user@domain, DOMAIN\user or provider|user alice.smith (and asmith for CORP\asmith)
{netbios} DOMAIN from DOMAIN\user, otherwise the domain's first label in capitals CORP
{id} After the last |, for Auth0's provider|id [email protected]
{dn.uid} The uid of an LDAP DN asmith for uid=asmith,ou=people,dc=corp,dc=com
{dn.cn} The cn of an LDAP DN Alice Smith for CN=Alice Smith,OU=Eng,DC=corp,DC=com
{guest} An Entra guest's own email [email protected] for henry_fabrikam.com#EXT#@contoso.onmicrosoft.com
{guest.localpart} Its local part henry

A token that is empty for a username refuses the grant rather than giving a shorter name: {dn.uid} on an email address gives nothing, so it is refused.

The transforms run in this order, each one optional:

  1. Strip accents: josé becomes jose
  2. Drop +tag: [email protected] becomes [email protected]
  3. Write dots as underscores or spaces: alice.smith becomes alice_smith, or alice smith for a Windows local account named for the person
  4. Lowercase

Patterns

A pattern is a regular expression in RE2 syntax, matched without regard to case. It has no lookahead or backreferences. Its groups become tokens: {match.1} for the first group, or {match.name} for a group named (?<name>...).

Pattern Template [email protected] gives
^(?<f>.)[^.]*\.(?<l>[^@]+)@ {match.f}{match.l} asmith
^svc-(.+)@corp\.com$ svc_{match.1} [email protected] gives svc_backup

What the Account Name May Hold

The result must start with a letter, digit or underscore, and hold only letters, digits, spaces and _ . @ \ -, up to 256 characters. It may not end with a space. The preview warns about names that a host may refuse even though Knocknoc accepts them: longer than 32 characters (adduser's limit), uppercase, or an ID rather than a name.

With SSSD's fully qualified names the account is [email protected], which {username} gives. With full_name_format = %2$s\%1$s it is CORP\alice, which {netbios}\{short} gives.

On Windows, an Active Directory account is CORP\alice, which {netbios}\{short} gives, or its UPN [email protected], which {username} gives. A Microsoft Entra ID account uses its UPN too. A local account is its plain name, alice.

What Each Identity Provider Sends

A SAML user's Knocknoc username is the assertion's NameID, and Knocknoc asks for the persistent format.

Identity provider The username What to use
Microsoft Entra ID A pairwise ID for a persistent NameID, unless the app sends the UPN or email. Guests look like henry_fabrikam.com#EXT#@contoso.onmicrosoft.com The UPN through the rules. A pairwise ID: an account name from the IdP
Okta The application username, usually the email The username through the rules. A user ID: an account name from the IdP
Google Workspace The primary email The username through the rules
AD FS What the claim rule issues: UPN, email or CORP\asmith The username through the rules
Active Directory over LDAP What the user typed: asmith, [email protected] or CORP\asmith The username through the rules, which give one account for all three
OpenLDAP, X.509 An LDAP DN The DN's uid
Keycloak The username or the email The username through the rules
FreeIPA A Kerberos principal such as [email protected] The username through the rules
Auth0 provider|id, or samlp|connection|attribute for enterprise SAML samlp: the username through the rules. Database and social IDs: an account name from the IdP
Knocknoc local users What the admin typed The username through the rules

SAML Attributes

Give each attribute source the attribute's name as your identity provider spells it. Case doesn't matter. Knocknoc reads a user's single-valued attributes each time they sign in with SAML, so a new attribute takes effect from the user's next sign-in. In Entra ID, for example, you can send user.onpremisessamaccountname as localAccount for directory-synced users. Local and LDAP users have no SAML attributes, so only the Knocknoc username source applies to them.

Sources: Entra ID SAML protocol, Okta SAML reference, Auth0 user IDs, RHEL SSSD fully qualified names, adduser.conf.

What Is and Is Not Covered

  • Connections from processes on the bastion are covered. On pf this is TCP and UDP only. On Windows, the rule decides which protocols and ports are blocked.
  • Containers with their own network namespace are not covered. Their traffic is forwarded and has no owning account. Don't let users run Docker, which is root-equivalent. Rootless Podman opens its sockets as the user, so it is covered.
  • SSH jumping works without a shell. ssh -J bastion target and -L forwards open their connections from the user's own session, so they're matched to the user.
  • Only the destinations you list are blocked. An address no egress knoc lists is open to every account on the bastion.
  • Revoking doesn't cut open connections. A session opened during a grant stays up after the grant ends. New connections are refused.
  • Manual IP grants don't work on an egress knoc. A manual grant is for an address and has no user, so the agent refuses it.
  • Downstream logs see the bastion's address. To tie traffic to a person, use the agent's grant records or the bastion's own logs.

Choosing a Firewall

The bastion's own firewall does the blocking: pf on OpenBSD, Windows Firewall on Windows, and nftables or iptables on Linux. On Linux and OpenBSD you need to run knocker enable egress on the bastion to set up the ability to create egress knocs. Windows is available by default and doesn't require the setup step. Setting a firewall up doesn't block anything it just puts the plumbing in place for the knocs to be created.

Firewall Host Setup guide
nftables Linux with nftables 0.9.4 or later. Each knoc uses a set of uids that your rules accept nftables egress
iptables Linux where you manage the iptables rules yourself (Shorewall, iptables-persistent, configuration management). Each knoc uses a chain from your ruleset, which the agent fills iptables egress
pf OpenBSD pf egress
Windows Windows, with the agent running as a service under LocalSystem. Each knoc uses an outbound Block rule you create, or has the agent create the rules from its destinations Windows egress

On Linux, pick the firewall your rules are already written in. On pf, only TCP and UDP connections can be matched to an account, so other protocols aren't blocked.

If you use both nftables and iptables on one bastion, keep each destination on one of them. Otherwise a user needs a grant on both to reach it.

Troubleshooting

A User Is Refused Every Grant

Check these in order:

  1. The local account exists: id <account>, or on Windows the lookup in Checking an Account. The account name is what the knoc's mapping gives for the user. To see which source and rule decide it, type the username into the preview in Edit mapping.
  2. Its uid is 1000 or higher: id -u <account>. On Windows, the last number of its SID is 1000 or higher.
  3. It isn't the agent's own account (knocknoc-agent, or SYSTEM on Windows).

The grant's error message tells you which account the mapping gave.

Error Codes

Every egress firewall shares these codes. Each firewall's guide lists the ones only it raises.

Code Meaning
EGR000 Egress allowlisting with this firewall is not set up on the agent host
EGR001 The firewall's command is not available
EGR002 The agent cannot change the egress firewall
EGR003 The egress rules are missing and could not be restored
EGR005 The agent could not look up the local account
EGR050 There is no local account for the user
EGR052 The username maps to root, a system or built-in account (uid or RID below 1000) or the agent's own account
EGR053 The knoc's local account mapping gives no usable account
EGR054 The grant has no user
EGR055 Failed to admit the local account
EGR056 Failed to set up the knoc's egress rule
EGR100 Failed to remove the local account's egress access
EGR400 Invalid or unexpected response from the firewall