No Inbound Network Services
Recently, attackers heated up the freezers in 700+ French supermarkets to +50 °C remotely. The stores had to throw away fresh and frozen food worth millions of euros. The attacks were fairly easy, because the Danfoss controllers were directly reachable from the public Internet, ran outdated firmware and had weak authentication for remote access.
Compare this with the properties that Toradex expects from a production device.
The [product] runs no network service that is reachable from outside the device. [...]
Its only network connections are outbound [...]
If Danfoss had heeded Torizon's advice, it would most likely have avoided the French freezer fiasco. Without any inbound network services, the attack on the freezers would have been so much harder. The attackers would probably have looked for an easier target.
A crucial step in the CRA risk assessment is to find all open TCP and UDP ports with inbound network services. The best mitigation is to eliminate these services. Most of the time, this is easily possible. If elimination is not an option, we must ensure that the device is only reachable from the local network and not from the public Internet.
Additionally, we should add some security measures so that
- only authenticated users can establish the network services,
- the inbound service request is confirmed by a second person, preferably physically present at the device,
- the service using the port is not running by default and is started only when the communication is established, and
- the communication persists only for a short time before it is automatically closed.
Let's go and find the bad TCP and UDP ports.
Finding Open TCP and UDP Ports
The Scan Target: Torizon Minimal
You might be surprised that I have chosen a Toradex Verdin board running a default Torizon Minimal image on an iMX8M Mini as my scan target. Didn't I just say that Torizon disavows ports listening on inbound network services? We aren't going to find anything interesting on such a system, are we?
Not so fast! The Production Hardening Checklist for Torizon OS starts with the following warning (emphasis mine).
torizon user account that [allows sudoing into root with a password of your choice], and leaves kernel debugging interfaces available. Defaults like that [...] make the development experience smooth, but many of them are not appropriate for a production environment.I am, indeed, working with a development image. So, the image is a good example for learning how we can find open TCP and UDP ports, how attackers can exploit these vulnerabilities and how we can remediate them. Note that Toradex explains in its Network Hardening guide how to turn the development into a production image.
The Torizon Minimal image is also a realistic example, because one port is almost always open on devices in the field: the SSH port. Manufacturers think that the world will end, if they don't have an SSH terminal for remote debugging. Well, the world is still doing fine!
Scanning from the Inside
We open an SSH terminal or a serial console on the board and run the ss utility. ss is part of Yocto's iproute2-ss package, which is built by the recipe iproute2 and is included in the Torizon Minimal by default. It displays all listening TCP sockets and unconnected UDP sockets, including the processes using them.
$ sudo ss -tulpn
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
udp UNCONN 0 0 127.0.0.54:53 0.0.0.0:* users:(("systemd-resolve",pid=506,fd=15))
udp UNCONN 0 0 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=506,fd=13))
udp UNCONN 0 0 0.0.0.0:5353 0.0.0.0:* users:(("avahi-daemon",pid=539,fd=12))
udp UNCONN 0 0 [::]:5353 [::]:* users:(("avahi-daemon",pid=539,fd=13))
tcp LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=506,fd=14))
tcp LISTEN 0 4096 127.0.0.54:53 0.0.0.0:* users:(("systemd-resolve",pid=506,fd=16))
tcp LISTEN 0 4096 *:22 *:* users:(("systemd",pid=1,fd=74))The options have the following meaning:
-tenables the scan for TCP ports.-uenables the scan for UDP ports.-ldisplays the listening ports.-pshows the processes using the sockets in the last columnProcess. The complete process information is only shown toroot.-nshows the port in the columnLocal Address:Portas a number. If we runsswithout-n, we see port names instead of numbers. In our example, we'd seesshinstead of 22 andmdnsinstead of 5353.
We can ignore the entries with loopback addresses 127.0.0.*, as they are only reachable from the Verdin board itself and not from external devices. This is confirmed by the nmap scan below, which doesn't see the loopback addresses.
The avahi-daemon uses two UDP sockets:
udp 0.0.0.0:5353 (mdns)
udp [::]:5353 (mdns)The avahi-daemon listens on port 5353 on all network interfaces with an IPv4 address (first line) and with an IPv6 address (second line) for incoming mDNS requests. Multicast DNS (mDNS) translates hostnames like verdin-imx8mm-08635476.local into IP addresses like 192.168.1.31. This turns the command ssh torizon@verdin-imx8mm-08635476.local into ssh torizon@192.168.1.31. mDNS only works inside the local network - unless configured otherwise on the router.
systemd uses TCP socket 22 for incoming SSH connections:
tcp *:22 (ssh)The SSH login ssh torizon@192.168.1.31 is handled over the standard SSH port 22. The IPv6 address works, too.
We might expect that the SSH daemon sshd uses port 22. Instead, systemd (PID 1) listens for incoming SSH connections on the socket created by sshd.socket. It activates the OpenSSH per-connection daemon sshd@.service, when it receives an SSH request. Then, the OpenSSH daemon handles the authentication, encryption and session.
We can see the connections with the following command:
$ systemctl list-units 'sshd*'
UNIT LOAD ACTIVE SUB DESCRIPTION
sshd@1-192.168.1.31:22-192.168.1.222:42594.service loaded active running OpenSSH Per-Connection Daemon (192.168.1.222:42594)192.168.1.31 is the IP address of the Verdin board and 192.168.1.222 of my Linux PC. If there were multiple SSH connections to the board, we would see all of them listed by the command above.
Scanning from the Outside
As we cannot always open a terminal on the device under scrutiny, we need a way to scan the device from another computer over the network. We choose nmap for scans from the outside.
$ sudo nmap -sS -sV -p- 192.168.1.31
...
Not shown: 65534 closed ports
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 9.6 (protocol 2.0)
MAC Address: 00:14:2D:83:C4:54 (Toradex AG)The options have the following meaning:
-sSperforms a TCP SYN scan.nmapinitiates a three-way handshake, where it resets the connection in the third step instead of establishing it.-sVenables service and version detection. The result is shown in the third and fourth column.-p-scans all TCP ports from 1 to 65535.
The scan takes less than 5 seconds. The result is the same as for the internal scan. Of course, an external scan cannot show the ports used by the internal loopback address. The scan reveals some interesting pieces of information for an attacker. The device uses version 9.6 of the OpenSSH server implementing version 2.0 of the SSH protocol. An attacker could glean the MAC address and the manufacturer (Toradex AG).
$ sudo nmap -sU -sV 192.168.1.31
...
Not shown: 999 closed ports
PORT STATE SERVICE VERSION
5353/udp open|filtered zeroconf
MAC Address: 00:14:2D:83:C4:54 (Toradex AG)The command performs a UDP scan (option -sU) of the 1,000 most common UDP ports (no option -p). The scan takes roughly 20 minutes. Scanning all UDP ports (option -p-) is a lengthy affair and best done overnight.
Again, the result is not surprising. zeroconf is the external service name of avahi-daemon. If nmap sends a datagram to a closed UDP socket, it typically gets an error back. The absence of an error means that the socket is open or filtered. nmap cannot distinguish the two states, because a firewall may block the probe or response or the receiver may processes the request silently.
The device can expose different ports over IPv4 and IPv6. A service can bind a socket only to the IPv4 address, only to the IPv6 address or to both addresses. The device or firewall along the path can apply different filtering rules for IPv4 and IPv6 ports. Hence, we must scan for IPv6 ports separately by adding the option -6 to the nmap commands.
$ sudo nmap -6 -sS -sV -p- 2001:4bc9:c058:190b:cad:9e2f:9859:25db
$ sudo nmap -6 -sU -sV 2001:4bc9:c058:190b:cad:9e2f:9859:25dbFor the Verdin board, the results are the same as for the IPv4 ports.
Limiting the Attack Surfaces
The essential product property Annex I.I.2j explicitly requires manufacturers to limit the external interfaces of their products. Open TCP and UDP ports are most certainly such external interfaces. We can limit the attack surfaces of these ports in different ways. Let's get practical!
Limiting the Attack Surface of SSH
Reachability from Public, Adjacent and Local Networks
My Verdin board has a private IPv4 address 192.168.1.31. I can log in to the board from any computer in the same local network as the board with the following test commands:
$ ssh torizon@192.168.1.31
$ ssh torizon@2001:4bc9:c061:604d:c2c1:4908:785f:48feI don't have any port forwarding configured on my 5G router connecting my computers to the public Internet. So, the slightly premature conclusion would be that the SSH service is only reachable from other devices in the same local network 192.168.1.0/24. It is not that easy!
Let us assume that we segmented our network into three segments: an office LAN (192.168.2.0/24), a guest LAN (192.168.3.0/24) and a machine LAN (192.168.1.0/24) with the board. The three network segments are connected to the same router. A computer from the guest or office LAN can log in via SSH to the board in the machine LAN - unless some firewall rules forbid this explicitly.
We check the reachability of the SSH service by running the two test commands on a computer in an adjacent network. If the router filters out messages through port 22, we should see an error message like this:
% ssh torizon@192.168.1.31
ssh: connect to host 192.168.1.31 port 22: Operation timed outOtherwise, we can log in with valid credentials. This is typically undesirable. The router should be equipped with the appropriate filters.
This leaves us with checking whether we can log in to the Verdin board from a computer in the public Internet. The board's private IPv4 address is not routable on the public Internet. However, the SSH service could be made accessible through port forwarding on the router. The situation is different with IPv6, because global IPv6 addresses may make the board directly accessible.
We can check whether the SSH service is accessible from the public Internet by tethering a laptop to a phone connected to a 4G or 5G network. We then try to log in to the board with the IPv6 address. This fails for my Verdin board.
% ssh torizon@2001:4bc9:c061:604d:c2c1:4908:785f:48fe
ssh: connect to host 2001:4bc9:c061:604d:c2c1:4908:785f:48fe port 22: Operation timed outApparently, my router applies the right filters. However, we must rely on the customer to install the right filters. A heat pump, for example, is connected to the local network in the customer's home. Customers can configure the router as they see fit - and misconfigure it.
We should note down this problem in the Information and Instructions to the User (Annex II) as follows.
- We expect customers to install filters on their routers blocking IPv4 and IPv6 traffic coming in through port 22 from public and adjacent networks. We add our expectation to the specific context of the intended purpose (Annex II.4).
- We add the possible router misconfiguration as a "condition of reasonably foreseeable misuse, which may lead to significant cybersecurity risks" (Annex II.5).
This is not a disclaimer. If an attacker exploits the SSH service, the customer can be liable partially. If the manufacturer wants to get rid of the liability, the simplest way is to eliminate the SSH service completely.
Directly accessing a device from local, adjacent and public networks is a big threat. But we shouldn't forget some indirect attack paths.
An attacker could get access to a computer in the same network as our device. He could use the computer to send thousands of SSH connection requests per second to the device. Even though the requests are rejected, they can consume considerable memory and CPU time. This is a classic denial-of-service attack compromising the device's availability (Annex I.I.2h). Alternatively, the attacker could try to find out the often easy-to-guess password and to log in to the device. With root privileges, the attacker would gain full control over the device. With excessive privileges, he could do a lot of evil.
Even if the device is in a VPN, security isn't guaranteed. Once attackers gain access to the VPN, they may be able to access the device much like a computer on the local network. Attacks on the device become a lot easier. This is, however, a less likely attack path.
We have found a useful methodology whether inbound network services can be reached and exploited from local, adjacent and public networks.
nmap to discover inbound network services provided through open ports of a device. We then use a protocol-specific client like ssh to assess, whether the services can be exploited from local, adjacent and public networks.Security Measures for Inbound SSH Connections
Manufacturer: "Absolutely, yes! We need it for debugging tricky issues!"
Me: "How often have you used it in the last year?"
Manufacturer after some internal discussion: "We don't know. Maybe once."
Me: "How about eliminating it completely and improving logging instead?"
Manufacturer: "No!!! We absolutely need it!"
I've had this discussion many times. No matter which argument I put forward, the answer is always the same. This is one of the cases where the customer is wrong. Here are several security measures to mitigate the risks of remote SSH login.
The best security measure is to eliminate SSH login altogether. We exclude the SSH server from the production image. No CRA violations! Problem solved!
Internet-connected devices like freezers, heat pumps or PV inverters can use outbound network services to a dedicated cloud backend. They don't have to use inbound network services. Often, these outbound services already exist for remote monitoring, maintenance or control and are secured by TLS.
Instead of an SSH server, we could run a a dedicated diagnostic application. This application is disabled by default. Service technician can enable the application after authenticating themselves with the cloud backend. The application is terminated after, say, two hours or when the technician logs out - whichever is shorter. It has just enough privileges so that technicians can view log files and a lot more device parameters than normal users. Technicians can even change these parameters.
If the customer insists on doing the wrong thing, we can apply a couple of security measures to limit the attack surface.
- Technicians never get root privileges in the shell started by the SSH server. They get only the privileges needed to read or upload some log files.
- Technicians must authenticate with the cloud backend, preferably using passkeys or two-factor authentication. The backend can issue short-lived SSH certificates or temporary passwords for authenticating technicians with individual devices.
- The systemd service for SSH is masked by default. If a service technician wants to log in to the device, the user must explicitly confirm this request on the device or via the mobile app. Once confirmed, the device unmasks and starts the SSH server. The SSH server is stopped and masked after a predefined time - e.g., after two hours - or when the technician logs out, whichever is shorter.
- As discussed earlier, we can install filters on the routers to ensure that the device is only accessed from the local network. This is often true for machines.
The first two measures are must-haves. The third one is a should-have, which boosts security considerably. The fourth one depends on what the manufacturer wants to achieve. This is a defence-in-depth strategy, as a single measure or an unsuitable combination of measures won't provide sufficient security.
Limiting the Attack Surface of mDNS
We know that mDNS name resolution works on the same local network as the device. We use it during development all the time. Restricting multicast DNS to the local network is mandated by the protocol specification. Multicast DNS uses link-local multicast addresses. Routers do not forward packets addressed to these multicast addresses between network segments. Therefore, multicast DNS queries from adjacent and public networks normally don't work.
However, the Avahi daemon responds to unicast DNS queries sent directly to UDP port 5353. When executed on the local network, the dig command successfully resolves the hostname of the Verdin board.
$ dig -6 @2001:4bc9:c061:604d:c2c1:4908:785f:48fe \
-p 5353 verdin-imx8mm-08635476.local AAAA \
+notcp +time=3 +tries=1
...
;; QUESTION SECTION:
;verdin-imx8mm-08635476.local. IN AAAA
;; ANSWER SECTION:
verdin-imx8mm-08635476.local. 10 IN AAAA 2001:4bc9:c061:604d:c2c1:4908:785f:48fe
...When executed from the public Internet, the same command fails with the following error message.
...
;; connection timed out; no servers could be reachedApparently, my router's IPv6 firewall blocks unsolicited inbound UDP messages, just as it blocks unsolicited TCP connections. As with SSH, manufacturers cannot ensure that the router is configured correctly and notes this down in the user instructions.
Attackers have alternatives to exploit vulnerabilities in the Avahi daemon. They may compromise a computer in networks local or adjacent to the device. Then, they can launch a DoS attack against the Avahi daemon. The attack might crash the daemon or waste some CPU cycles. It might also cause an out-of-bounds write, which might open the possibility for executing malicious code. The effort may be too high relative to the potential benefit for the attackers. The threat from the open SSH port is a lot greater than from the open mDNS port.
Conclusion
My advice is slightly more drastic than Toradex's recommendation to mask the systemd units for SSH and Avahi. By removing the functionality from the device completely, we don't have to spend any time on assessing cybersecurity risks or on managing vulnerabilities for SSH and Avahi. Here is the lesson learned.