EG254S: IoT System Project - IoT Security IoT Security

Testing the TLS/SSL Secure Communication

We shall test the TLS/SSL encryption layer over TCP/IP connection between ESP8266 nodes (MQTT client) in the mesh network and the MQTT Broker on the Raspberry Pi. If all goes well you should be able to publish and subscribe to topics as normal, but now the connection between client and broker is encrypted using the TLS protocol.

Unfortunately, there is no easy way of seeing this. Hence, we shall use Wireshark, a network packet analyser, to capture network packets for analysis.

Installing Wireshark on Raspberry Pi

On the Raspberry Pi, open a new terminal and type the following command:

pi@raspberrypi:~ $ sudo apt-get install wireshark

Reading package lists... Done

Building dependency tree      

Reading state information... Done

The following packages were automatically installed and are no longer required:

  coinor-libipopt1 dns-root-data dnsmasq-base libboost-filesystem1.55.0 libboost-program-options1.55.0 libboost-regex1.55.0 libffi5

  libgmime-2.6-0 libmnl0 libmumps-seq-4.10.0 libnetfilter-conntrack3 liboauth0 libraw10 wolframscript

Use 'apt-get autoremove' to remove them.

The following extra packages will be installed:

  liblua5.2-0 libpcap0.8 libsmi2ldbl libwireshark-data libwireshark5 libwiretap4 libwsutil4 wireshark-common

Suggested packages:

  snmp-mibs-downloader wireshark-doc

The following NEW packages will be installed:

  liblua5.2-0 libpcap0.8 libsmi2ldbl libwireshark-data libwireshark5 libwiretap4 libwsutil4 wireshark wireshark-common

0 upgraded, 9 newly installed, 0 to remove and 252 not upgraded.

Need to get 11.0 MB of archives.

After this operation, 54.5 MB of additional disk space will be used.

Do you want to continue? [Y/n] y

Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main liblua5.2-0 armhf 5.2.3-1.1 [64.8 kB]

Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main libpcap0.8 armhf 1.6.2-2 [121 kB]

Get:3 http://mirrordirector.raspbian.org/raspbian/ jessie/main libsmi2ldbl armhf 0.4.8+dfsg2-10 [108 kB]

Err http://mirrordirector.raspbian.org/raspbian/ jessie/main libwsutil4 armhf 1.12.1+g01b65bf-4+deb8u14

  404  Not Found [IP: 93.93.128.193 80]

Err http://mirrordirector.raspbian.org/raspbian/ jessie/main libwiretap4 armhf 1.12.1+g01b65bf-4+deb8u14

  404  Not Found [IP: 93.93.128.193 80]

Err http://mirrordirector.raspbian.org/raspbian/ jessie/main libwireshark-data all 1.12.1+g01b65bf-4+deb8u14

  404  Not Found [IP: 93.93.128.193 80]

Err http://mirrordirector.raspbian.org/raspbian/ jessie/main libwireshark5 armhf 1.12.1+g01b65bf-4+deb8u14                                   

  404  Not Found [IP: 93.93.128.193 80]

Err http://mirrordirector.raspbian.org/raspbian/ jessie/main wireshark-common armhf 1.12.1+g01b65bf-4+deb8u14                               

  404  Not Found [IP: 93.93.128.193 80]

Err http://mirrordirector.raspbian.org/raspbian/ jessie/main wireshark armhf 1.12.1+g01b65bf-4+deb8u14                                      

  404  Not Found [IP: 93.93.128.193 80]

Fetched 294 kB in 7s (37.3 kB/s)                                                                                                             

E: Failed to fetch http://mirrordirector.raspbian.org/raspbian/pool/main/w/wireshark/libwsutil4_1.12.1+g01b65bf-4+deb8u14_armhf.deb  404  Not Found [IP: 93.93.128.193 80]

 

E: Failed to fetch http://mirrordirector.raspbian.org/raspbian/pool/main/w/wireshark/libwiretap4_1.12.1+g01b65bf-4+deb8u14_armhf.deb  404  Not Found [IP: 93.93.128.193 80]

 

E: Failed to fetch http://mirrordirector.raspbian.org/raspbian/pool/main/w/wireshark/libwireshark-data_1.12.1+g01b65bf-4+deb8u14_all.deb  404  Not Found [IP: 93.93.128.193 80]

 

E: Failed to fetch http://mirrordirector.raspbian.org/raspbian/pool/main/w/wireshark/libwireshark5_1.12.1+g01b65bf-4+deb8u14_armhf.deb  404  Not Found [IP: 93.93.128.193 80]

 

E: Failed to fetch http://mirrordirector.raspbian.org/raspbian/pool/main/w/wireshark/wireshark-common_1.12.1+g01b65bf-4+deb8u14_armhf.deb  404  Not Found [IP: 93.93.128.193 80]

 

E: Failed to fetch http://mirrordirector.raspbian.org/raspbian/pool/main/w/wireshark/wireshark_1.12.1+g01b65bf-4+deb8u14_armhf.deb  404  Not Found [IP: 93.93.128.193 80]

 

E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

pi@raspberrypi:~ $

If unsuccessful (as shown above), do an update:

pi@raspberrypi:~ $ sudo apt-get update

And, install Wireshark again:

pi@raspberrypi:~ $ sudo apt-get install wireshark

Reading package lists... Done

Building dependency tree      

Reading state information... Done

The following packages were automatically installed and are no longer required:

  coinor-libipopt1 dns-root-data dnsmasq-base libboost-filesystem1.55.0 libboost-program-options1.55.0 libboost-regex1.55.0 libffi5

  libgmime-2.6-0 libmnl0 libmumps-seq-4.10.0 libnetfilter-conntrack3 liboauth0 libraw10 wolframscript

Use 'apt-get autoremove' to remove them.

The following extra packages will be installed:

  liblua5.2-0 libpcap0.8 libsmi2ldbl libwireshark-data libwireshark5 libwiretap4 libwsutil4 wireshark-common

Suggested packages:

  snmp-mibs-downloader wireshark-doc

The following NEW packages will be installed:

  liblua5.2-0 libpcap0.8 libsmi2ldbl libwireshark-data libwireshark5 libwiretap4 libwsutil4 wireshark wireshark-common

0 upgraded, 9 newly installed, 0 to remove and 252 not upgraded.

Need to get 10.7 MB/11.0 MB of archives.

After this operation, 54.5 MB of additional disk space will be used.

Do you want to continue? [Y/n] y

Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main libwsutil4 armhf 1.12.1+g01b65bf-4+deb8u15 [98.6 kB]

Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main libwiretap4 armhf 1.12.1+g01b65bf-4+deb8u15 [175 kB]

Get:3 http://mirrordirector.raspbian.org/raspbian/ jessie/main libwireshark5 armhf 1.12.1+g01b65bf-4+deb8u15 [8,685 kB]

Get:4 http://mirrordirector.raspbian.org/raspbian/ jessie/main libwireshark-data all 1.12.1+g01b65bf-4+deb8u15 [839 kB]                     

Get:5 http://mirrordirector.raspbian.org/raspbian/ jessie/main wireshark-common armhf 1.12.1+g01b65bf-4+deb8u15 [177 kB]                    

Get:6 http://mirrordirector.raspbian.org/raspbian/ jessie/main wireshark armhf 1.12.1+g01b65bf-4+deb8u15 [689 kB]                           

Fetched 10.7 MB in 12s (862 kB/s)                                                                                                            

Preconfiguring packages ...

Selecting previously unselected package liblua5.2-0:armhf.

(Reading database ... 111638 files and directories currently installed.)

Preparing to unpack .../liblua5.2-0_5.2.3-1.1_armhf.deb ...

Unpacking liblua5.2-0:armhf (5.2.3-1.1) ...

Selecting previously unselected package libpcap0.8:armhf.

Preparing to unpack .../libpcap0.8_1.6.2-2_armhf.deb ...

Unpacking libpcap0.8:armhf (1.6.2-2) ...

Selecting previously unselected package libsmi2ldbl:armhf.

Preparing to unpack .../libsmi2ldbl_0.4.8+dfsg2-10_armhf.deb ...

Unpacking libsmi2ldbl:armhf (0.4.8+dfsg2-10) ...

Selecting previously unselected package libwsutil4:armhf.

Preparing to unpack .../libwsutil4_1.12.1+g01b65bf-4+deb8u15_armhf.deb ...

Unpacking libwsutil4:armhf (1.12.1+g01b65bf-4+deb8u15) ...

Selecting previously unselected package libwiretap4:armhf.

Preparing to unpack .../libwiretap4_1.12.1+g01b65bf-4+deb8u15_armhf.deb ...

Unpacking libwiretap4:armhf (1.12.1+g01b65bf-4+deb8u15) ...

Selecting previously unselected package libwireshark-data.

Preparing to unpack .../libwireshark-data_1.12.1+g01b65bf-4+deb8u15_all.deb ...

Unpacking libwireshark-data (1.12.1+g01b65bf-4+deb8u15) ...

Selecting previously unselected package libwireshark5:armhf.

Preparing to unpack .../libwireshark5_1.12.1+g01b65bf-4+deb8u15_armhf.deb ...

Unpacking libwireshark5:armhf (1.12.1+g01b65bf-4+deb8u15) ...

Selecting previously unselected package wireshark-common.

Preparing to unpack .../wireshark-common_1.12.1+g01b65bf-4+deb8u15_armhf.deb ...

Unpacking wireshark-common (1.12.1+g01b65bf-4+deb8u15) ...

Selecting previously unselected package wireshark.

Preparing to unpack .../wireshark_1.12.1+g01b65bf-4+deb8u15_armhf.deb ...

Unpacking wireshark (1.12.1+g01b65bf-4+deb8u15) ...

Processing triggers for man-db (2.7.0.2-5) ...

Processing triggers for hicolor-icon-theme (0.13-1) ...

Processing triggers for gnome-menus (3.13.3-6) ...

Processing triggers for desktop-file-utils (0.22-1) ...

Processing triggers for mime-support (3.58) ...

Processing triggers for shared-mime-info (1.3-1) ...

Setting up liblua5.2-0:armhf (5.2.3-1.1) ...

Setting up libpcap0.8:armhf (1.6.2-2) ...

Setting up libsmi2ldbl:armhf (0.4.8+dfsg2-10) ...

Setting up libwsutil4:armhf (1.12.1+g01b65bf-4+deb8u15) ...

Setting up libwiretap4:armhf (1.12.1+g01b65bf-4+deb8u15) ...

Setting up libwireshark-data (1.12.1+g01b65bf-4+deb8u15) ...

Setting up libwireshark5:armhf (1.12.1+g01b65bf-4+deb8u15) ...

Setting up wireshark-common (1.12.1+g01b65bf-4+deb8u15) ...

Setting up wireshark (1.12.1+g01b65bf-4+deb8u15) ...

Processing triggers for libc-bin (2.19-18+deb8u7) ...

pi@raspberrypi:~ $

Once that is complete, do not start Wireshark, we need to configure our lan card:

pi@raspberrypi:~ $ sudo groupadd wireshark

Add the username to the wireshark group:

pi@raspberrypi:~ $ sudo usermod -a -G wireshark pi

Change the group ownership of the dompcap to wireshark:

pi@raspberrypi:~ $ sudo chgrp wireshark /usr/bin/dumpcap

Change the mode of the file dump cap to allow execution by the group:

pi@raspberrypi:~ $ sudo chmod 750 /usr/bin/dumpcap

Grant capabilities with setcap:

pi@raspberrypi:~ $ sudo setcap cap_net_raw,cap_net_admin=eip /usr/bin/dumpcap

Verify the change:

pi@raspberrypi:~ $ sudo getcap /usr/bin/dumpcap

/usr/bin/dumpcap = cap_net_admin,cap_net_raw+eip

pi@raspberrypi:~ $

Reboot the Raspberry Pi.

Testing the Unsecured Communication

Before we test the connections between the MQTT clients (ESP8266 nodes) and the MQTT Broker (Raspberry Pi) are secured by TLS/SSL protocol, we shall first use Wireshark to examine an unsecured communication between the both.

Step 1: Mesquitto Node and Mesh Gateway Node

For the test, we will use two ESP8266 nodes (MQTT Client) from the mesh network. Power up the two Mesquitto nodes to begin publishing sensors data to the MQTT Broker using the ESP8266 mesh gateway node which has to be powered up as well.

Step 2: MQTT Gateway Node

From the previous Programming the ESP8266 MQTTGateway to use TLS/SSL, we have uploaded the program with TLS/SSL support to the ESP8266 MQTT gateway node using the Arduino IDE. Now we need to upload back the initial program to it. After uploading, power down the MQTT gateway node. We will power it up after we start capturing traffic using Wireshark.

Step 3: MQTT Broker

Boot up the Raspberry Pi.

Since we have configured the Mosquitto Broker with TLS/SSL support in the configuration file (mosquitto.conf) from our previous Activating TLS/SSL option in Mosquitto, the Raspberry Pi is booted up using the mosquitto.conf file.

Luckily, there is an easier way out instead of making changes to the mosquitto.conf file and rebooting Raspberry Pi. By default, mosquitto does not need a configuration file and will use the default values.

We shall stop the mosquitto running on the Raspberry Pi. Open a terminal and type the following:

pi@raspberrypi:~ $ sudo systemctl stop mosquitto

Restart mosquitto using verbose logging, but without loading the configuration file:

pi@raspberrypi:~ $ sudo mosquitto -v

The mosquitto will be restarted with the default values, listening only on the default port 1883.

Step 4: Wireshark

Start Wireshark.

Select wlan0 and click on Start to start capturing traffic from the wireless network interface.

Now, we shall power up the MQTT Gateway node. A TCP handshake will be established between the MQTT Gateway node (ESP8266) and the MQTT Broker (Raspberry Pi).

Watch the video:

 

Observe the packets sent from the MQTT Gateway node (10.1.2.105) to the MQTT Broker (10.1.2.104) listening on port 1883. MQTT messages (containing sensors data) are published by the two Mesquitto nodes from the mesh network to the MQTT Broker, using the MQTT Gateway node.

From the Wireshark (version 2.0.5) packet details pane, we can see that packets are sent in readable form:

Since the first packet sent to the MQTT broker is in plaintext, Wireshark could identify the packet is a MQTT protocol and the data packet is a Connect Command. Furthermore, the Client ID, User Name and Password are also in plaintext!

Let’s look at the packets containing data identified as MQTT Publish messages:

From above, we can clearly identify that the MQTT Publish message was sent from Rat Bait Station 001 with the Message: 18.

Below is the MQTT Publish message sent from Rat Bait Station 002:

Here is the pcapng (PCAP Next Generation Dump File Format) file containing the unencrypted packets I had captured using Wireshark:

unsecure.pcapng

Testing the TLS Protocol Communication

Now we shall test the TLS protocol that provides security in the communication between the MQTT clients (ESP8266 nodes) and the MQTT Broker (Raspberry Pi), using Wireshark to examine the encrypted communication between the both.

Step 5: Mesquitto Node and Mesh Gateway Node

Following from the Step 1 above, allow the two Mesquitto nodes (ESP8266 nodes in the mesh network) to run and publish sensors data to the MQTT Broker with the ESP8266 mesh gateway node running as well.

Step 6: MQTT Gateway Node

Reload the program with TLS/SSL support (code: MQTTgateway.txt) to the ESP8266 MQTT gateway node using the Arduino IDE. After uploading, power down the MQTT gateway node. We will power it up after we start a new capture, this time the encrypted traffic, using Wireshark.

Step 7: MQTT Broker

Reboot the Raspberry Pi.

This time the mosquitto.conf file (with TLS option activated) will be loaded on bootup. Now the Mosquitto’s extra listener will be listening on port 8883.

This is how easy it can get without any modification done to the mosquitto.conf file in Step 3 above.

Step 8: Wireshark

Start a new capture on Wireshark.

Select wlan0 and click on Start to start capturing the encrypted traffic from the wireless network interface.

Now, we shall power up the MQTT Gateway node with TLS/SSL support. A TCP handshake will be established between the MQTT Gateway node (ESP8266) and the MQTT Broker (Raspberry Pi).

Watch the video:

 

From the Wireshark (version 2.0.5) packet details pane, we can see that data packets are sent encrypted:

Since the first packet sent to the MQTT broker was unreadable (encrypted), Wireshark could not identify the packet was a MQTT protocol except knowing from the packet header it was destined to the MQTT broker’s IP address on port 8883.

Likewise, we identify the encrypted packets sent from the MQTT client to the MQTT broker by the Source and Destination IP address, as well as the destination Port 8883. Notice the packet details pane in Wireshark, the Data of length 69 bytes was encrypted:

From our above observation, we notice the Data Length (in Bytes) is larger compared to unencrypted data packet. This is due to the TLS overhead.

Due to the encrypted data, we couldn’t identify which Rat Bait Station had sent the MQTT Publish message to the MQTT broker. Below is another encrypted packet sent to the MQTT broker:

Here is the pcapng (PCAP Next Generation Dump File Format) file containing the TLS protocol encrypted packets I had captured using Wireshark: secure.pcapng

 


Why Wireshark (version 2.0.5) cannot display TLS/SSL in the packet list pane display under the ‘Protocol’?

The data packets are sent encrypted over TCP/IP designated to the MQTT broker’s IP address on port 8883 indicated in the packet header. Because it’s not on the standard port for TLS/SSL (refer below on Protocol dependencies). Hence, we could only see “TCP” instead of “TLS/SSL” in Wireshark’s packet list pane display summary under “Protocol”.

Protocol dependencies

  • TCP: Typically, TLS uses TCP as its transport protocol.

Some well known TCP ports for TLS traffic are

  • 443 https
  • 636 ldaps
  • 989 ftps-data
  • 990 ftps
  • 992 telnets
  • 993 imaps
  • 994 ircs
  • 995 pop3s
  • 5061 sips

This completes our testing. TLS provides security on the network layer. It encrypts the following data on the client side:

Mosquitto decrypts the encrypted data on the broker side.

 

References:

http://donsthinktank.blogspot.com/2015/07/wireshark-raspberry-pi.html

https://wiki.wireshark.org/SSL