Single Packet Authorization and Amazon's Elastic Cloud (EC2) Service
08 November, 2008
Recently on the fwknop mailing list,
Mark V asked whether Single Packet Authorization
is compatible with virtual Linux instances in Amazon's
Elastic Cloud (EC2) computing
service. After signing up for an account and running a few tests, it turns out that
fwknop can function properly on an EC2 instance and wrap an SPA hardening layer around SSHD.
The Amazon online documentation on how to
access Linux instances
over SSH recommends adding new rules for each IP source address you want to
use to access SSHD, which can become tiresome if you are on a network running DHCP. With
fwknop, you don't need to do any such thing - you can access SSHD via SPA from anywhere,
and anyone else scanning for SSHD on the instance can't even tell that it is listening.
To re-create the steps listed in this blog post, it is necessary to have signed up for an EC2 account and to have created an SSH keypair so that instances can be launched from the command tools provided by Amazon. For this post, all commands on the fwknop client side are executed on an Ubuntu 8.04 system to gain access into the Amazon cloud.
First, let's launch a new virtual instance of Fedora 8 (the command ec2-describe-images -o self -o amazon can be used to find instance identifiers for Fedora 8 systems):
[ubuntu]$ ec2-run-instances ami-abcd1234 -k cdyne-keypair RESERVATION r-abcd1234 111111114274 default INSTANCE i-abcd1234 ami-abcd1234 pending cdyne-keypair 0 m1.small 2008-11-07T20:18:43+0000 us-east-1c aki-abcd1234 ari-abcd1234 [ubuntu]$ ec2-describe-instances i-abcd1234 RESERVATION r-abcd1234 111111114274 default INSTANCE i-abcd1234 ami-abcd1234 ec2-70-100-249-20.compute-1.amazonaws.com domU-12-31-00-00-00-00.compute-1.internal running cdyne-keypair 0 m1.small 2008-11-07T20:18:43+0000 us-east-1c aki-abcd1234 ari-abcd1234Now, we allow SSH access temporarily from the Ubuntu IP address (which will be managed by fwknop after we get it installed and set up instead). We also allow all UDP packets over port 62201 through from any IP since this is the default port and protocol used by fwknop to send SPA packets, and we need such packets to reach far enough into the cloud so that it will be seen by the fwknopd sniffer running on the Fedora 8 instance:
[ubuntu]$ ec2-authorize default -p 22 -s 123.1.1.1/32 GROUP default PERMISSION default ALLOWS tcp 22 22 FROM CIDR 123.1.1.1/32 [ubuntu]$ ec2-authorize default -P udp -p 62201 GROUP default PERMISSION default ALLOWS udp 62201 62201We can now log into the instance as root:
[ubuntu]$ ssh -i cdyne-keypair root@ec2-70-100-249-20.compute-1.amazonaws.com
The authenticity of host 'ec2-70-100-249-20.compute-1.amazonaws.com
(70.100.249.20)' can't be established.
RSA key fingerprint is 2a:aa:aa:c9:6f:aa:aa:aa:a0:48:7d:db:d6:aa:aa:aa.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'ec2-70-100-249-20.compute-1.amazonaws.com,
70.100.249.20' (RSA) to the list of known hosts.
__| __|_ ) Fedora 8
_| ( / 32-bit
___|\___|___|
Welcome to an EC2 Public Image
:-)
Base
--[ see /etc/ec2/release-notes ]--
We have a shiny new EC2 instance running Fedora 8. For fwknop to run properly, we'll
need the perl-devel and libpcap-devel packages installed, and then we'll
install fwknop (some output below has been abbreviated) and configure the
/etc/fwknop/access.conf file for SPA access to SSHD. We also add
iptables rules to allow packets that are part of established connection through, and
drop all other attempts to communicate with SSHD. Finally, we start fwknopd:
[root@domU-12-31-00-00-00-00 ~]# yum install perl-devel libpcap-devel
[root@domU-12-31-00-00-00-00 ~]# cd /usr/local/src/
[root@domU-12-31-00-00-00-00 src]# wget http://www.cipherdyne.org/fwknop/download/fwknop-1.9.9-pre7.tar.gz
[root@domU-12-31-00-00-00-00 src]# tar xfz fwknop-1.9.9-pre7.tar.gz
[root@domU-12-31-00-00-00-00 src]# cd fwknop-1.9.9-pre7
[root@domU-12-31-00-00-00-00 fwknop-1.9.9-pre7]# ./install.pl
[...]
[+] It appears that the following network interfaces are attached to the
system:
eth0
lo
Which network interface would you like fwknop to sniff packets from? eth0
[root@domU-12-31-00-00-00-00 ~]# cat > /etc/fwknop/access.conf
SOURCE: ANY;
OPEN_PORTS: tcp/22;
KEY: thisisatestkey;
FW_ACCESS_TIMEOUT: 30;
[root@domU-12-31-00-00-00-00 ~]# iptables -F
[root@domU-12-31-00-00-00-00 ~]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@domU-12-31-00-00-00-00 ~]# iptables -A INPUT -p tcp --dport 22 -j DROP
[root@domU-12-31-00-00-00-00 ~]# /etc/init.d/fwknop start
Starting fwknopd: [ OK ]
In the installation of fwknop above, we selected eth0 as the Ethernet interface
that fwknopd sniffs for SPA packets, and then we configured the /etc/fwknop/access.conf
file with a symmetric key for SPA access to SSHD over TCP port 22. Also, with
iptables now deployed to drop SSH communications, we cannot even see that SSHD is
listening under an nmap scan:
[ubuntu]$ nmap -P0 -p 22 -sT ec2-70-100-249-20.compute-1.amazonaws.com Starting Nmap 4.53 ( http://insecure.org ) at 2008-11-08 01:33 EST Interesting ports on ec2-70-100-249-20.compute-1.amazonaws.com (70.100.249.20): PORT STATE SERVICE 22/tcp filtered sshThis is where fwknop comes in. After running the fwknop client and typing the shared encryption key, we can now access the Fedora 8 instance with SSH:
[ubuntu]$ fwknop -A tcp/22 -a 123.1.1.1
-D ec2-70-100-249-20.compute-1.amazonaws.com
[+] Starting fwknop client (SPA mode)...
[+] Resolving hostname: ec2-70-100-249-20.compute-1.amazonaws.com
[+] Enter an encryption key. This key must match a key in the file
/etc/fwknop/access.conf on the remote system.
Encryption Key:
[+] Building encrypted Single Packet Authorization (SPA) message...
[+] Packet fields:
Random data: 9189958885117496
Username: mbr
Timestamp: 1226119984
Version: 1.9.9-pre7
Type: 1 (access mode)
Access: 123.1.1.1,tcp/22
SHA256 digest: EPOjHE2ANS3Fe3MHxDpok13qHDGbYziFiohv6CyHQXs
[+] Sending 161 byte message to 70.100.249.20 over udp/62201...
[ubuntu]$ ssh -i cdyne-keypair root@ec2-70-100-249-20.compute-1.amazonaws.com
__| __|_ ) Fedora 8
_| ( / 32-bit
___|\___|___|
Welcome to an EC2 Public Image
:-)
Base
In his original post, Mark had suggested a tighter level of integration with
Amazon's EC2 service by building in some notion of instance identifiers into
SPA communications. With the basic network access now validated and SPA in
its raw form demonstrated to be compatible with EC2, additional integration
points can be achieved. Incidentally, port knocking would also be compatible
with the EC2 service, but I would not recommend using it because port knocking
would look like a port scan to any IDS that Amazon may have deployed to
monitor the cloud network. With SPA, only a single packet with an encrypted
payload is involved, and this is unlikely to trigger an IDS alarm.

You can see in the graph above that there are several spikes for a few character values.
These spikes correspond with the characters U2FsdGVkX1, which is the base64
encoded version of the string "Salted__". Every SPA packet in the spa_artifacts.pkts
file begins with this string, and so one would expect more of these characters on
average than the others. There is also a sharp dip to 20,000 on the right-hand
portion of the graph for the value 64, which corresponds to the = character.
This is because there is only one equals character at the end of each SPA packet in
the spa_artifacts.pkts file.
This graph shows us where the predictable bytes are. We know that each SPA
packet begins with U2FsdGVkX1 and ends with =, and the outliers (on the
left-hand side and one point on the right-hand side - all with Z-axis values of
about 20,000) in the above graph illustrate this. The X-axis represents the byte
positions of each packet, the Y-axis are the numeric character codes, and the
Z-axis is the number of times a character is counted.
Now that the invariant sections have been removed, the length of each packet is
161 characters, so if the randomness were perfect, we would expect the average
instance of each character to be 161 / 64 * 20000 = 50,300 (approximately), and
this is quite close to the value displayed by the graph.
There are still two minor spikes corresponding to the numeric values around 0
and 1, and then again around 10 and 11 on the X-axis. These bytes correspond to
the characters +, /, 8, and 9 respectively.
So, at the scale shown above, the 3D graph looks decently random - at least there
are far fewer outliers than for the first data set of 20,000 packets, and there aren't
any other obviously regular structures in the graph. However, if you look to the
left-hand portion of the graph, you will see there are
small outliers at position 0 on the X-axis - the very first byte of each SPA
packet. Looking through the spa_no_artifacts.pkts file, the graph is indeed
correct - every SPA packet begins with one of the bytes +, /, 8, or 9.

In the continual arms race that is computer security today,
having a good understanding of network communications and how to customize an
IDS rule set to an emerging protocol is an important skill. Finally, SPA
offers a compelling addition to the tools available for effective server
defense; I personally sleep more soundly knowing that arbitrary IP addresses
around the Internet cannot see that I have an SSH daemon running, and yet I can
access it from wherever I like.








