Secure the Mosquitto broker using authenticated connections and audit your firmware and hardware interfaces.
Domain
IoT Cybersecurity
Difficulty
⭐⭐⭐⭐☆ (Advanced)
Course Module
IoT Security
Deliverables
Hardening Checklist, Secured Firmware
1. Threat Model & Authentication Interface
Local sandbox setups often bypass security features for convenience. However, exposing anonymous ports on your local network introduces significant vulnerabilities. The diagram below contrasts the unsecure setup with a hardened, password-authenticated environment. Enabling client authentication blocks rogue publishers from sending fake command payloads and prevents unauthorized subscribers from monitoring sensitive data channels.
2. Part 1: Step-by-Step Individual Security Configuration Commands
Follow these detailed steps to generate a password database inside your VM guest, restrict Mosquitto settings, and test connections.
STEP 1
Generate Mosquitto Password Database File
Open a terminal window inside the Ubuntu VM. Use the `mosquitto_passwd` tool to generate an encrypted password file.
We open `/etc/mosquitto/mosquitto.conf` in the nano editor with root privileges to modify the access rules.
STEP 5
Update Configuration Settings in Nano
Scroll down to the bottom of the configuration file. Replace the old settings with the updated authentication rules.
Replace configuration text (use arrow keys, delete the old rule, paste the text below, press Ctrl+O, Enter, Ctrl+X):
listener 1883 0.0.0.0
allow_anonymous false
password_file /etc/mosquitto/passwd
We update the listener config, setting `allow_anonymous` to `false` and referencing the password file to enforce authentication for all incoming connections.
STEP 6
Restart the Mosquitto Service to Apply Settings
Reload the system service to apply the modified configuration rules.
ubuntu@iot-vm:~$ sudo systemctl restart mosquitto
We run `systemctl restart` to restart the Mosquitto daemon, forcing the process to read the new configuration rules.
STEP 7
Verify Unauthenticated Connections are Blocked
Attempt to subscribe to a topic without providing credentials. The broker should reject the connection.
Securing an IoT node requires hardening multiple layers of the system. The checklist pipeline below outlines the key validation stages to secure edge devices before deploying them to production.
4. Part 2: Hardened Firmware Code & Security Checklist Template
Below is the updated C++ code that includes username and password parameters in the MQTT connection loop, followed by a security audit checklist.
const char* mqtt_user = "user1";: Stores the authorized username to authenticate connection requests.
const char* mqtt_pass = "securePass123";: Stores the connection password. In production, this should be stored in secure non-volatile storage (like ESP32 NVS) rather than hardcoded in plaintext.
client.connect("ESP32_SecureNode", mqtt_user, mqtt_pass, ...): Establishes a connection to the broker, passing the authentication credentials alongside the client ID and LWT status parameters.