| FWKNOP(8) | Fwknop Client | FWKNOP(8) |
NAME
fwknop - Firewall Knock Operator
SYNOPSIS
fwknop -A <'proto/ports'> -R|-a|-s -D <'host'> [options]
DESCRIPTION
fwknop implements an authorization scheme known as Single Packet Authorization (SPA) for strong service concealment. SPA requires only a single packet which is encrypted, non-replayable, and authenticated via an HMAC in order to communicate desired access to a service that is hidden behind a firewall in a default-drop filtering stance. The main application of SPA is to use a firewall to drop all attempts to connect to services such as SSH in order to make the exploitation of vulnerabilities (both 0-day and unpatched code) more difficult. Any service that is concealed by SPA naturally cannot be scanned for with Nmap. The fwknop project natively supports four different firewalls: iptables, firewalld, PF, and ipfw across Linux, OpenBSD, FreeBSD, and Mac OS X. There is also support for custom scripts so that fwknop can be made to support other infrastructure such as ipset or nftables.
SPA is essentially next generation Port Knocking (PK), but solves many of the limitations exhibited by PK while retaining its core benefits. PK limitations include a general difficulty in protecting against replay attacks, asymmetric ciphers and HMAC schemes are not usually possible to reliably support, and it is trivially easy to mount a DoS attack against a PK server just by spoofing an additional packet into a PK sequence as it traverses the network (thereby convincing the PK server that the client doesn’t know the proper sequence). All of these limitation are solved by SPA. At the same time, SPA hides services behind a default-drop firewall policy, acquires SPA data passively (usually via libpcap or other means), and implements standard cryptographic operations for SPA packet authentication and encryption/decryption.
This is the manual page for the fwknop client which is responsible for constructing SPA packets and sending them over the network. The server side is implemented by the fwknopd daemon which sniffs the network for SPA packets and interacts with the local firewall to allow SPA authenticated connections. It is recommended to read the fwknopd(8) manual page as well. Further detailed information may be found in the tutorial “Single Packet Authorization: A Comprehensive Guide to Strong Service Concealment with fwknop” available online (see: http://www.cipherdyne.org/fwknop/docs/fwknop-tutorial.html).
SPA packets generated by fwknop leverage HMAC for authenticated encryption in the encrypt-then-authenticate model. Although the usage of an HMAC is currently optional (enabled via the --use-hmac command line switch), it is highly recommended for three reasons: 1) without an HMAC, cryptographically strong authentication is not possible with fwknop unless GnuPG is used, but even then an HMAC should still be applied, 2) an HMAC applied after encryption protects against cryptanalytic CBC-mode padding oracle attacks such as the Vaudenay attack and related trickery (like the more recent "Lucky 13" attack against SSL), and 3) the code required by the fwknopd daemon to verify an HMAC is much more simplistic than the code required to decrypt an SPA packet, so an SPA packet without a proper HMAC isn’t even sent through the decryption routines. Reason 3) is why an HMAC should still be used even when SPA packets are encrypted with GnuPG due to the fact that SPA data is not sent through libgpgme functions unless the HMAC checks out first. GnuPG and libgpgme are relatively complex bodies of code, and therefore limiting the ability of a potential attacker to interact with this code through an HMAC operation helps to maintain a stronger security stance. Generating an HMAC for SPA communications requires a dedicated key in addition to the normal encryption key, and both can be generated with the --key-gen option.
fwknop encrypts SPA packets either with the Rijndael block cipher or via GnuPG and associated asymmetric cipher. If the symmetric encryption method is chosen, then as usual the encryption key is shared between the client and server (see the fwknopd /etc/fwknop/access.conf file for details). The actual encryption key used for Rijndael encryption is generated via the standard PBKDF1 key derivation algorithm, and CBC mode is set. If the GnuPG method is chosen, then the encryption keys are derived from GnuPG key rings. SPA packets generated by fwknop running as a client adhere to the following format (before encryption and the HMAC is applied):
random data (16 digits)
username
timestamp
software version
mode (command mode (0) or access mode (1))
if command mode => command to execute
else access mode => IP,proto,port
message digest (SHA512 / SHA384 / SHA256 / SHA1 / MD5 / SHA3_256 / SHA3_512)
Each of the above fields are separated by a ":" character due to the variable length of several of the fields, and those that might contain ":" characters are base64 encoded. The message digest (SHA256 by default) is part of the data to be encrypted and is independent of the HMAC which is appended to the SPA packet data after encryption. The 16 digits of random data (about 53 bits) ensures that no two SPA packets are identical, and this is in addition to and independent of using PBKDF1 for key derivation for Rijndael in CBC mode (which uses an 8-byte random "salt" value). Because fwknopd tracks the SHA256 digest of all incoming valid SPA packets and throws out duplicates, replay attacks are not feasible against fwknop. Syslog alerts are generated if a replay is detected.
By default, the fwknop client sends authorization packets over UDP port 62201, but this can be altered with the --server-port argument (this requires fwknopd to be configured to acquire SPA data over the selected port). Also, fwknop can send the SPA packet over a random port via the --rand-port argument. See fwknopd(8) for further details. See the EXAMPLES section for example invocations of the fwknop client.
The fwknop client is quite portable, and is known to run on various Linux distributions (all major distros and embedded ones such as OpenWRT as well), FreeBSD, OpenBSD, Mac OS X, and Cygwin on Windows. There is also a library libfko that both fwknop and fwknopd use for SPA packet encryption/decryption and HMAC authentication operations. This library can be used to allow third party applications to use SPA subject to the terms of the GNU General Public License (GPL v2+).
REQUIRED ARGUMENTS
These required arguments can be specified via command-line or from within the ~/.fwknoprc file (see -n, --named-config option and the FWKNOPRC FILE section below).
-A, --access=<port list>
-D, --destination=<hostname/IP-address>
-R|-a|-s
GENERAL OPTIONS
-h, --help
-G, --get-key=<file>
--stdin
--fd=<number>
--get-hmac-key=<file>
--key-gen
--key-gen-file=<file>
--key-len=<length>
--hmac-key-len=<length>
-l, --last-cmd
-n, --named-config=<stanza name>
--key-rijndael=<key>
--key-base64-rijndael=<key>
--key-base64-hmac=<key>
--key-hmac=<key>
--rc-file=<file>
--no-rc-file
--no-home-dir
--save-rc-stanza=<stanza name>
--force-stanza
--stanza-list
--show-last
-E, --save-args-file=<file>
--no-save-args
-T, --test
-B, --save-packet=<file>
-b, --save-packet-append
--fault-injection-tag=<tag>
-v, --verbose
-V, --version
SPA OPTIONS
--use-hmac
-a, --allow-ip=<IP-address>
-g, --gpg-encryption
--hmac-digest-type=<digest>
-N, --nat-access=<internalIP:forwardPort>
--nat-local
--nat-port
--nat-rand-port
-p, --server-port=<port>
-P, --server-proto=<protocol>
-Q, --spoof-src=<IP>
-r, --rand-port
-R, --resolve-ip-https
--resolve-url <url>
--resolve-http-only
-w, --wget-cmd=<wget full path>
-s, --source-ip
-S, --source-port=<port>
--server-resolve-ipv4
-f, --fw-timeout=<seconds>
-C, --server-cmd=<command to execute>
-H, --http-proxy=<proxy-host>[:port]
-m, --digest-type=<digest>
-M, --encryption-mode=<mode>
--time-offset-plus=<time>
--time-offset-minus=<time>
-u, --user-agent=<user-agent-string>
--use-wget-user-agent
-U, --spoof-user=<user>
--icmp-type=<type>
--icmp-code=<code>
GPG OPTIONS
Note that the usage of GPG for SPA encryption/decryption can and should involve GPG keys that are signed by each side (client and server). The basic procedure for this involves the following steps after the client key has been transferred to the server and vice-versa:
[spaserver]# gpg --import client.asc
[spaserver]# gpg --edit-key 1234ABCD
Command> sign
[spaclient]$ gpg --import server.asc
[spaclient]$ gpg --edit-key ABCD1234
Command> sign
More comprehensive information on this can be found here: http://www.cipherdyne.org/fwknop/docs/gpghowto.html.
--gpg-agent
--gpg-home-dir=<dir>
--gpg-recipient=<key ID or Name>
--gpg-signer-key=<key ID or Name>
--gpg-no-signing-pw
FWKNOPRC FILE
The .fwknoprc file is used to set various parameters to override default program parameters at runtime. It also allows for additional named configuration stanzas for setting program parameters for a particular invocation.
The fwknop client will create this file if it does not exist in the user’s home directory. This initial version has some sample directives that are commented out. It is up to the user to edit this file to meet their needs.
The .fwknoprc file contains a default configuration area or stanza which holds global configuration directives that override the program defaults. You can edit this file and create additional named stanzas that can be specified with the -n or --named-config option. Parameters defined in the named stanzas will override any matching default stanza directives. Note that command-line options will still override any corresponding .fwknoprc directives.
There are directives to match most of the command-line parameters fwknop supports. Here is the current list of each directive along with a brief description and its matching command-line option(s):
SPA_SERVER <hostname/IP-address>
ALLOW_IP <IP-address>
ACCESS <port list>
SPA_SERVER_PORT <port>
SPA_SERVER_PROTO <protocol>
KEY <passphrase>
KEY_BASE64 <base64 encoded passphrase>
USE_HMAC <Y/N>
HMAC_KEY <key>
HMAC_KEY_BASE64 <base64 encoded key>
HMAC_DIGEST_TYPE <digest algorithm>
SPA_SOURCE_PORT <port>
FW_TIMEOUT <seconds>
RESOLVE_IP_HTTPS <Y/N>
RESOLVE_HTTP_ONLY <Y/N>
RESOLVE_URL <url>
WGET_CMD <wget full path>
TIME_OFFSET <time>
ENCRYPTION_MODE <mode>
DIGEST_TYPE <digest algorithm>
USE_GPG <Y/N>
USE_GPG_AGENT <Y/N>
GPG_SIGNING_PW <passphrase>
GPG_SIGNING_PW_BASE64 <base64 encoded passphrase>
GPG_SIGNER <key ID or Name>
GPG_RECIPIENT <key ID or Name>
GPG_HOMEDIR <dir>
GPG_EXE <path>
SPOOF_USER <user>
SPOOF_SOURCE_IP <IP>
RAND_PORT <Y/N>
KEY_FILE <file>
HTTP_USER_AGENT <agent string>
USE_WGET_USER_AGENT <Y/N>
NAT_ACCESS <internalIP:forwardPort>
NAT_LOCAL <Y/N>
NAT_PORT <port>
NAT_RAND_PORT <Y/N>
ENVIRONMENT
SPOOF_USER, GPG_AGENT_INFO (only used in --gpg-agent mode).
SPA PACKET SPOOFING
Because fwknop places the IP to be allowed through the firewall within the encrypted SPA payload (unless -s is used which is not recommended and can be prohibited in the fwknopd server configuration), SPA packets can easily be spoofed, and this is a good thing in this context. That is, the source IP of an SPA packet is ignored by the fwknopd daemon (when the REQUIRE_SOURCE_ADDRESS variable is set in the /etc/fwknop/access.conf file) and only the IP that is contained within an authenticated and properly decrypted SPA packet is granted access through the firewall. This makes it possible to make it appear as though, say, www.yahoo.com is trying to authenticate to a target system but in reality the actual connection will come from a seemingly unrelated IP.
EXAMPLES
The following examples illustrate the command line arguments that could be supplied to the fwknop client in a few situations:
Quick start
The most effective and easiest way to use fwknop is to have the client generate both an encryption key and an HMAC key, and then save them to the “$HOME/.fwknoprc” file along with access request specifics. The keys will also need to be transferred to the system where fwknopd is running. The also client supports a separate set of encryption and HMAC keys for each SPA destination if multiple fwknopd servers are running on different systems.
So, assuming that the IP 2.2.2.2 is the system where fwknopd is deployed and SSH is protected by the firewall on that system in a default-drop stance, run the client like so to generate encryption and HMAC keys:
$ fwknop -A tcp/22 --use-hmac -R -D 2.2.2.2 --key-gen --save-rc-stanza --verbose
[+] Wrote Rijndael and HMAC keys to rc file: /home/user/.fwknoprc
With the access request arguments and encryption and HMAC keys generated and saved in “$HOME/.fwknoprc”, the keys themselves need to be transferred to the 2.2.2.2 system where fwknopd is running. As always, this should be done via some secure means such as SSH before SPA is enabled and SSHD is blocked by the firewall. Here is what the new 2.2.2.2 stanza looks like in the ~/.fwknoprc file:
$ tail -n 8 /home/user/.fwknoprc
[2.2.2.2]
ACCESS tcp/22
SPA_SERVER 2.2.2.2
KEY_BASE64 HvUtIOramehLGKimD4ECXOzinaH4h3U8H1WXum7b54Q=
HMAC_KEY_BASE64 DLeLf93a3yBT2vhEpM+dWlirGta5GU+jdyG5uXp4461HgOtbqMem4gX0Bp2PJGzYZlbbcavcOM00UPm+0GqkXA==
USE_HMAC Y
VERBOSE Y
RESOLVE_IP_HTTPS Y
The keys are base64 encoded blobs of random data, and both the KEY_BASE64 and HMAC_KEY_BASE64 lines should be copied verbatim and placed within the /etc/fwknop/access.conf file on 2.2.2.2. Once this is done, fwknopd can be started on that system, a default-drop policy against SSH connections can be put in place, and then access to SSH is managed via fwknop. To access SSH, just use the -n argument to reference the 2.2.2.2 stanza out of the .fwknoprc file (some --verbose output is included for illustration):
$ fwknop -n 2.2.2.2
FKO Field Values:
=================
Random Value: 8950423288486978
Username: mbr
Timestamp: 1370194770
FKO Version: 2.5
Message Type: 1 (Access msg)
Message String: 1.1.1.1,tcp/22
Nat Access: <NULL>
Server Auth: <NULL>
Client Timeout: 0 (seconds)
Digest Type: 3 (SHA256)
HMAC Type: 3 (SHA256)
Encryption Type: 1 (Rijndael)
Encryption Mode: 2 (CBC)
...
$ ssh -l user 2.2.2.2
user@2.2.2.2's password:
Access mode examples
The most common usage of fwknop is to gain access to SSH running on a remote system that has the fwknopd daemon deployed along with a default-drop firewall policy. The following command illustrates this where IP 1.1.1.1 is the IP to be allowed through the firewall running on 3.3.3.3 (note that the /etc/fwknop/access.conf file consumed by fwknopd will need to have matching encryption and HMAC keys, and configuration specifics can be found in the fwknopd(8) manual page). Also, note the examples below prompt the user to supply the encryption and HMAC keys via stdin instead of writing them to disk as in the case of using the “$HOME/.fwknoprc” file in the example above. However, all of the following examples can be converted to using the ~/.fwknoprc file just by adding the --save-rc-stanza argument:
$ fwknop -A tcp/22 --use-hmac -a 1.1.1.1 -D 3.3.3.3
Enter encryption key:
Enter HMAC key:
$ ssh -l user 3.3.3.3
user@3.3.3.3's password:
If the --verbose flag is added to the command line, then some SPA packet specifics are printed to stdout (not all output is shown for brevity):
$ fwknop -A tcp/22 --use-hmac -a 1.1.1.1 -D 3.3.3.3 --verbose
Enter encryption key:
Enter HMAC key:
Random Value: 1916307060193417
Username: mbr
Timestamp: 1368498909
FKO Version: 2.5
Message Type: 1 (Access msg)
Message String: 1.1.1.1,tcp/22
Nat Access: <NULL>
Server Auth: <NULL>
Client Timeout: 0 (seconds)
Digest Type: 3 (SHA256)
HMAC Type: 3 (SHA256)
Encryption Type: 1 (Rijndael)
Encryption Mode: 2 (CBC)
Simultaneous access to multiple services is also supported, and here is an example of requesting access to both SSH and OpenVPN on 3.3.3.3:
$ fwknop -A "tcp/22,tcp/1194" --use-hmac -a 1.1.1.1 -D 3.3.3.3
There are many cases where an fwknop client is deployed on a network behind a NAT device and the externally routable IP is not known to the user. In this case, use the IP resolution service available at https://www.cipherdyne.org/cgi-bin/myip via the -R command line switch in order to derive the external client IP address. This is a safer method of acquiring the client IP address than using the -s option mentioned earlier in this manual page because the source IP is put within the encrypted packet instead of having the fwknopd daemon grant the requested access from whatever IP address the SPA packet originates (i.e. using -s opens the possibility of a MITM attack):
$ fwknop -A tcp/22 --use-hmac -R -D 3.3.3.3
Use the Single Packet Authorization mode to gain access to SSH and this time use GnuPG keys to encrypt and decrypt:
$ fwknop -A tcp/22 --use-hmac --gpg-sign ABCD1234 --gpg--recipient 1234ABCD -R -D 3.3.3.3
Instruct the fwknop server running at 3.3.3.3 to allow 1.1.1.1 to connect to SSH, but spoof the authorization packet from an IP associated with www.yahoo.com (requires root on the fwknop client OS):
# fwknop --spoof-src "www.yahoo.com" -A tcp/22 --use-hmac -a 1.1.1.1 -D 3.3.3.3
When fwknopd is running on an iptables firewall with systems deployed behind it, it is possible to take advantage of the NAT capabilities offered by iptables in order to transparently reach systems behind the firewall via SPA. Here is an example where the fwknop client is used to gain access to SSH running on the non-routable IP 192.168.10.23 that is deployed on the network behind 3.3.3.3. In this case, the SSH connection made to 3.3.3.3 is translated via NAT to the 192.168.10.2 system automatically:
$ fwknop -A tcp/22 -N 192.168.10.2:22 -R -D 3.3.3.3
BACKWARDS COMPATIBILITY
With the 2.5 release, fwknop underwent significant changes in its usage of cryptography including the addition of support for HMAC authenticated encryption for both Rijndael and GnuPG modes, ensuring the proper usage of PBKDF1 for key derivation when SPA packets are encrypted with Rijndael, and several bugs were fixed from previous versions of fwknop. In general, this implies that when Rijndael is used, SPA packets produced by the 2.5 release are incompatible with previous versions of fwknop. The GnuPG encryption mode is unaffected by these updates. However, even with Rijndael is used, backwards compatibility is supported through setting the legacy encryption mode with -M on the fwknop client command line and/or the ENCRYPTION_MODE variable in the /etc/fwknop/access.conf file. This way, a pre-2.5 server can decrypt SPA packets produced by a 2.5 and later client (set -M legacy), and a 2.5 and later server can decrypt SPA packets produced by pre-2.5 clients (set ENCRYPTION_MODE legacy in the access.conf file). Note that HMAC is only supported as of 2.5 and is an optional feature, so backwards compatibility is only for configurations that don’t use an HMAC on either side. It is strongly recommended to upgrade all fwknop clients and servers to 2.5 and use the new HMAC mode for properly authenticated SPA communications. The backwards compatibility support is used to make it easier to upgrade clients and servers with a phased approach.
For emphasis, if the fwknopd server is upgraded to 2.5 (or later), but older clients cannot be upgraded at the same time, then for each SOURCE stanza in the /etc/fwknop/access.conf file, add the following line:
ENCRYPTION_MODE legacy
In addition, if the KEY variable has an encryption key longer than 16 bytes, it will need to be truncated to 16 characters in the access.conf file in order for pre-2.5 clients to work properly. This limitation is fixed in 2.5, and provides additional motivation for upgrading all clients and servers to 2.5 or later.
Now, flipping the scenario around, if the fwknop clients are upgraded but the fwknopd server is still at a pre-2.5 version, then add the -M legacy argument to the fwknop command line:
$ fwknop -A tcp/22 -M legacy -R -D 2.2.2.2
DEPENDENCIES
The fwknop client requires libfko which is normally included with both source and binary distributions, and is a dedicated library developed by the fwknop project. Whenever the fwknopd server is used, libpcap is a required dependency. However, the upcoming 2.6 release will offer a UDP listener mode along with privilege separation support and will not require libpcap in this mode. In UDP listener mode, even though fwknopd binds to a UDP port, SPA packets are never acknowledged so from an attacker’s perspective there is no difference between fwknopd sniffing the wire passively vs. listening on a UDP socket in terms of what can be scanned for.
For GPG functionality, GnuPG must also be correctly installed and configured along with the libgpgme library.
To take advantage of all of the authentication and access management features of the fwknopd daemon/service a functioning iptables, ipfw, or pf firewall is required on the underlying operating system.
DIAGNOSTICS
The most comprehensive way to gain diagnostic information on fwknop is to run the test suite test-fwknop.pl script located in the test/ directory in the fwknop sources. The test suite sends fwknop through a large number of run time tests, has valgrind support, validates both SPA encryption and HMAC results against OpenSSL, and even has its own built in fuzzer for SPA communications (and fwknop in version 2.6.4 supports the American Fuzzy Lop (AFL) from Michal Zalewski as well). For more basic diagnostic information, fwknop can be executed with the -T (or --test) command line option. This will have fwknop simply create and print the SPA packet information, then run it through a decrypt/decode cycle and print it again. In addition, the --verbose command line switch is useful to see various SPA packet specifics printed to stdout.
SEE ALSO
fwknopd(8), iptables(8), pf(4), pfctl(8), ipfw(8), gpg(1), libfko documentation.
More information on Single Packet Authorization can be found in the paper “Single Packet Authorization with fwknop” available at http://www.cipherdyne.org/fwknop/docs/SPA.html. A comprehensive tutorial on fwknop operations and theory can be found at http://www.cipherdyne.org/fwknop/docs/fwknop-tutorial.html. This tutorial also includes information about the design of fwknop that may be worth reading for those interested in why fwknop is different from other SPA implementations.
fwknop uses the git versioning system as its source code repository along with Github for tracking of issues and milestones:
$ git clone https://github.com/mrash/fwknop.git fwknop.git
Additional commentary on Single Packet Authorization can be found via Michael Rash’s Twitter feed: http://twitter.com/michaelrash, @michaelrash
AUTHORS
The primary developers of fwknop are Michael Rash (project creator) <mbr@cipherdyne.org>, Damien Stuart <dstuart@dstuart.org>, and Jonathan Bennett <jbennett@incomsystems.biz>.
CONTRIBUTORS
This “C” version of fwknop was derived from the original Perl-based version on which many people who are active in the open source community have contributed. See the CREDITS file in the fwknop sources, or visit https://github.com/mrash/fwknop/blob/master/CREDITS to view the online list of contributors. A few contributors deserve to be singled out including: Franck Joncourt, Max Kastanas, Vlad Glagolev, Sean Greven, Hank Leininger, Fernando Arnaboldi, and Erik Gomez.
The phrase “Single Packet Authorization” was coined by MadHat and Simple Nomad at the BlackHat Briefings of 2005.
BUGS
Send bug reports to dstuart@dstuart.org or mbr@cipherdyne.org, or open a new issue on Github (see https://github.com/mrash/fwknop.git). Suggestions and/or comments are always welcome as well. Additional information may be found in the fwknop mailing list archives (see: https://lists.sourceforge.net/lists/listinfo/fwknop-discuss).
DISTRIBUTION
fwknop is distributed under the GNU General Public License (GPL v2+), and the latest version may be downloaded from http://www.cipherdyne.org.
| 08/06/2018 | Fwknop Client |