One place for hosting & domains

      Secure

      How To Secure Nginx with NAXSI on Ubuntu 16.04


      The author selected The OWASP Foundation to receive a donation as part of the Write for DOnations program.

      Introduction

      Nginx is a popular, open-source HTTP server and reverse proxy known for its stability, simple configuration, and frugal resource requirements. You can greatly increase the security of your Nginx server by using a module like NAXSI. NAXSI (Nginx Anti XSS & SQL Injection) is a free, third-party Nginx module that provides web application firewall features. NAXSI analyzes, filters, and secures the traffic that comes to your web application, and acts like a DROP-by-default firewall, which means that it blocks all the traffic coming its way unless instructed to specifically allow access.

      The simplicity with which a user can manipulate access is a key feature that differentiates NAXSI from other web application firewalls (WAF) with similar functionality like ModSecurity. Although ModSecurity comes with a rich feature set, it is more difficult to maintain than NAXSI. This makes NAXSI a simple and adaptable choice that provides readily available rules that work well with popular web applications such as WordPress.

      In this tutorial, you will use NAXSI to secure Nginx on your Ubuntu 16.04 server. Since the NAXSI module doesn’t come with the Nginx package by default, you will need to compile Nginx from source with NAXSI. By the end of this tutorial, you will know what kinds of attacks NAXSI can block and how to configure NAXSI rules.

      Prerequisites

      To complete this tutorial, you will need:

      Step 1 — Installing Nginx and NAXSI

      Most of the Nginx modules are not available through repositories, and NAXSI is no exception. Because of this, you will have to manually download and compile Nginx from source with NAXSI.

      First, download Nginx using the following command.

      Note: This tutorial uses version 1.14 of Nginx. To use a more recent version, you can visit the download page and replace the highlighted text in the preceding command with an updated version number. It is recommended to use the latest stable version.

      • wget http://nginx.org/download/nginx-1.14.0.tar.gz

      Next, download NAXSI from the stable 0.56 release on Github.

      Note: This tutorial uses version 0.56 of NAXSI. You can find more recent releases at the NAXSI Github page. It is recommended to use the latest stable version.

      • wget https://github.com/nbs-system/naxsi/archive/0.56.tar.gz -O naxsi

      As you may have noticed, the Nginx repository is a tar archive. You first need to extract it to be able to compile and install it, which you can do by using the tar command.

      • tar -xvf nginx-1.14.0.tar.gz

      In the preceding command, -x specifies the extract utility, -v makes the utility run in verbose mode, and -f indicates the name of the archive file to extract.

      Now that you have extracted the Nginx files, you can move on to extract the NAXSI files using the following command:

      You now have the folders naxsi-0.56 and nginx-1.14.0 in your home directory. Using the files that you just downloaded and extracted, you can compile the Nginx server with NAXSI. Move into your nginx-1.14.0 directory

      In order to compile Nginx from source, you will need the C compiler gcc, the Perl Compatible Regular Expressions library libpcre3-dev, and libssl-dev, which implements the SSL and TLD cryptographic protocols. These dependencies can be added with the apt-get command.

      First, run the following command to make sure you have an updated list of packages.

      Then install the dependencies:

      • sudo apt-get install build-essential libpcre3-dev libssl-dev

      Now that you have all your dependencies, you can compile Nginx from source. In order to prepare Nginx to be compiled from source on your system, execute the following script, which will create the Makefile that shows where to find all necessary dependencies.

      • ./configure
      • --conf-path=/etc/nginx/nginx.conf
      • --add-module=../naxsi-0.56/naxsi_src/
      • --error-log-path=/var/log/nginx/error.log
      • --http-client-body-temp-path=/var/lib/nginx/body
      • --http-fastcgi-temp-path=/var/lib/nginx/fastcgi
      • --http-log-path=/var/log/nginx/access.log
      • --http-proxy-temp-path=/var/lib/nginx/proxy
      • --lock-path=/var/lock/nginx.lock
      • --pid-path=/var/run/nginx.pid
      • --user=www-data
      • --group=www-data
      • --with-http_ssl_module
      • --without-mail_pop3_module
      • --without-mail_smtp_module
      • --without-mail_imap_module
      • --without-http_uwsgi_module
      • --without-http_scgi_module
      • --prefix=/usr

      Each of the lines of the preceding command defines a parameter for the Nginx web server. The most important of these are the --add-module=../naxsi-0.56/naxsi_src/ parameter, which connects the NAXSI module with Nginx, and the --user=www-data and --group=www-data paramaters, which make Nginx run with the user and group privileges of a dedicated user/group called www-data that comes with your Ubuntu 16.04 server. The --with-http_ssl_module parameter enables the Nginx server to use SSL cryptography, and the --without-mail_pop3_module, --without-mail_smtp_module, and --without-mail_imap_module parameters turn off the unneeded mail protocols that would otherwise be automatically included. For further explanation of these parameters, see the official Nginx docs.

      After using the ./configure command, run the make command to enact a series of tasks defined in the Makefile you just created to build the program from the source code.

      When Nginx is built and ready to run, use the make install command as a superuser to copy the built program and its libraries to the correct location on your server.

      Once this succeeds, you will have a compiled version of Nginx with the NAXSI module. In order to get NAXSI to start blocking unwanted traffic, you now need to establish a set of rules that NAXSI will act upon by creating a series of configure files.

      Step 2 — Configuring NAXSI

      The most important part of a firewall's functioning is its rules, which determine how requests are blocked from the server. The basic set of rules that comes by default with NAXSI are called core rules. These rules are meant to search for patterns in parts of a request and to filter out ones that may be attacks. NAXSI core rules are applied globally to the server for signature matching.

      To configure Nginx to use these core rules, copy the naxsi_core.rules file to Nginx config directory.

      • sudo cp ~/naxsi-0.56/naxsi_config/naxsi_core.rules /etc/nginx/

      Now that the core rules are established, add the basic Naxsi rules, which enable and implement the core rules on a per location basis and assign actions for the server to take when a URL request does not satisfy the core rules. Create a file called naxsi.rules inside the /etc/nginx/ directory. To do so, use the following command to open the file in the text editor called nano, or use your text editor of choice.

      • sudo nano /etc/nginx/naxsi.rules

      Add the following block of code that defines some basic firewall rules.

      /etc/nginx/naxsi.rules

       SecRulesEnabled;
       DeniedUrl "/error.html";
      
       ## Check for all the rules
       CheckRule "$SQL >= 8" BLOCK;
       CheckRule "$RFI >= 8" BLOCK;
       CheckRule "$TRAVERSAL >= 4" BLOCK;
       CheckRule "$EVADE >= 4" BLOCK;
       CheckRule "$XSS >= 8" BLOCK;
      

      The preceding code defines the DeniedUrl, which is the URL NAXSI will redirect to when a request is blocked. The file also enables a checklist of different kinds of attacks that NAXSI should block, including SQL injection, cross-site scripting (XSS), and remote file inclusion (RFI). Once you have added the preceding code to the file, save and exit the text editor.

      Since you redirected blocked requests to /error.html, you can now create an error.html file inside /usr/html directory to provide this destination with a landing page. Open up the file in your text editor:

      • sudo nano /usr/html/error.html

      Next, add the following HTML code to the file to make a web page that lets the user know that their request was blocked:

      /usr/html/error.html

      <html>
        <head>
          <title>Blocked By NAXSI</title>
        </head>
        <body>
          <div style="text-align: center">
            <h1>Malicious Request</h1>
            <hr>
            <p>This Request Has Been Blocked By NAXSI.</p>
          </div>
        </body>
      </html>
      

      Save the file and exit the editor.

      Next, open up the Nginx configuration file /etc/nginx/nginx.conf in your text editor.

      • sudo nano /etc/nginx/nginx.conf

      To add the NAXSI configuration files to Nginx's configuration so that the web server knows how to use NAXSI, insert the highlighted lines of code into the http section of the nginx.conf file:

      /etc/nginx/nginx.conf

      . . .
      http {
          include       mime.types;
          include /etc/nginx/naxsi_core.rules;
          include /etc/nginx/conf.d/*.conf;
          include /etc/nginx/sites-enabled/*;
      
      
          default_type  application/octet-stream;
      . . .
      

      Then in the server section of the same file, add the following highlighted line:

      /etc/nginx/nginx.conf

      . . .
          server {
              listen       80;
              server_name  localhost;
      
              #charset koi8-r;
      
              #access_log  logs/host.access.log  main;
      
              location / {
              include /etc/nginx/naxsi.rules;
                  root   html;
                  index  index.html index.htm;
              }
      . . .
      

      Now that you have configured Nginx with the core and basic rules for NAXSI, the firewall will block matching malicious requests when you start the web server. Next, you can write a startup script to ensure that Nginx starts up when you reboot the server.

      Step 3 — Creating the Startup Script for Nginx

      Since you installed Nginx manually, the next step is to create a startup script to make the web server autostart on system reloads.

      This tutorial uses the Systemd software suite to make the script. To do this, you will create a Unit File (see Understanding Systemd Units and Unit Files for further study) to configure how Systemd should start and manage the Nginx service.

      Make a file called nginx.service and open it up in your text editor:

      • sudo nano /lib/systemd/system/nginx.service

      Add the following lines to the file:

      /lib/systemd/system/nginx.service

      [Unit]
      Description=The NGINX HTTP and reverse proxy server
      After=syslog.target network.target remote-fs.target nss-lookup.target
      
      [Service]
      Type=forking
      PIDFile=/run/nginx.pid
      ExecStartPre=/usr/sbin/nginx -t
      ExecStart=/usr/sbin/nginx
      ExecReload=/usr/sbin/nginx -s reload
      ExecStop=/bin/kill -s QUIT $MAINPID
      PrivateTmp=true
      
      [Install]
      WantedBy=multi-user.target
      

      The [Unit] section defines the program that you are configuring, [Service] describes how Nginx should behave on startup, and [Install] provides information about unit installation. Once you add these lines to the nginx.service file, systemd will know how to start Nginx.

      Next, Nginx needs a folder to temporarily store incoming request data before processing it in the event that your server doesn't have enough memory. Since you installed Nginx from source, you will need to create a directory that Nginx can use to store this data. Make a directory called body inside /var/lib/nginx:

      • sudo mkdir -p /var/lib/nginx/body

      With the startup script set up, you will now be able to start the Nginx server.

      Use the following command to start the server.

      • sudo systemctl start nginx

      To check that your server is active, run the following command:

      • sudo systemctl status nginx

      You will see the following output in your terminal stating that the server has started successfully:

      Output

      ● nginx.service - The NGINX HTTP and reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; disabled; vendor preset: enabled) Active: active (running) since Mon 2018-11-05 13:59:40 UTC; 1s ago Process: 16199 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 16194 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Main PID: 16201 (nginx) Tasks: 2 Memory: 1.3M CPU: 17ms CGroup: /system.slice/nginx.service ├─16201 nginx: master process /usr/sbin/ngin └─16202 nginx: worker proces . . .

      You now have a running Nginx server secured by NAXSI. The next step is to run a simulated XSS and SQL injection attack to ensure that NAXSI is protecting your server effectively.

      Step 4 — Testing NAXSI

      To test that Nginx is up and running with the NAXSI module enabled, you will try hitting the server with malicious HTTP requests and analyze the responses.

      First, copy the Public IP of your server and use the curl command to make malicious request the Nginx server.

      • curl 'http://your_server_ip/?q="><script>alert(0)</script>'

      This URL includes the XSS script "><script>alert(0)</script> in the q parameter and should be rejected by the server. According to the NAXSI rules that you set up earlier, you will be redirected to the error.html file and receive the following response:

      Output

      <html> <head> <title>Blocked By NAXSI</title> </head> <body> <div style="text-align: center"> <h1>Malicious Request</h1> <hr> <p>This Request Has Been Blocked By NAXSI.</p> </div> </body> </html>

      The NAXSI firewall has blocked the request.

      Now, verify the same using the Nginx log by tailing the Nginx server log using the following command:

      • tail -f /var/log/nginx/error.log

      In the log, you will see that the XSS request from the remote IP address is getting blocked by NAXSI:

      Output

      2018/11/07 17:05:05 [error] 21356#0: *1 NAXSI_FMT: ip=your_server_ip&server=your_server_ip&uri=/&learning=0&vers=0.56&total_processed=1&total_blocked=1&block=1&cscore0=$SQL&score0=8&cscore1=$XSS&score1=8&zone0=ARGS&id0=1001&var_name0=q, client: your_server_ip, server: localhost, request: "GET /?q="><script>alert(0)</script> HTTP/1.1", host: "your_server_ip"

      Press CTRL-C to exit tail and stop the output of the error log file.

      Next, try another URL request, this time with a malicious SQL Injection query.

      • curl 'http://your_server_ip/?q=1" or "1"="1"'

      The or "1"="1" part of the preceding URL can expose a user's data in a database, and will be blocked by NAXSI. It should produce the same response in the terminal:

      Output

      <html> <head> <title>Blocked By NAXSI</title> </head> <body> <div style="text-align: center"> <h1>Malicious Request</h1> <hr> <p>This Request Has Been Blocked By NAXSI.</p> </div> </body> </html>

      Now use tail to follow the server log again:

      • tail -f /var/log/nginx/error.log

      In the log file you'll see the blocked entry for the SQL Injection attempt:

      Output

      2018/11/07 17:08:01 [error] 21356#0: *2 NAXSI_FMT: ip=your_server_ip&server=your_server_ip&uri=/&learning=0&vers=0.56&total_processed=2&total_blocked=2&block=1&cscore0=$SQL&score0=40&cscore1=$XSS&score1=40&zone0=ARGS&id0=1001&var_name0=q, client: your_server_ip, server: localhost, request: "GET /?q=1" or "1"="1" HTTP/1.1", host: "your_server_ip"

      Press CTRL-C to exit the log.

      NAXSI has now successfully blocked an XSS and SQL injection attack, which proves that NAXSI has been configured correctly and that your Nginx web server is secure.

      Conclusion

      You now have a basic understanding of how to use NAXSI to protect your web server from malicious attacks. To learn more about setting up Nginx, see How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 16.04. If you'd like to continue studying security on web servers, check out How To Secure Nginx with Let's Encrypt on Ubuntu 16.04 and How To Create a Self-Signed SSL Certificate for Nginx in Ubuntu 16.04.



      Source link

      How to Install and Secure the Mosquitto MQTT Messaging Broker on Ubuntu 18.04


      Introduction

      MQTT is a machine-to-machine messaging protocol, designed to provide lightweight publish/subscribe communication to “Internet of Things” devices. It is commonly used for geo-tracking fleets of vehicles, home automation, environmental sensor networks, and utility-scale data collection.

      Mosquitto is a popular MQTT server (or broker, in MQTT parlance) that has great community support and is easy to install and configure.

      In this tutorial, we’ll install Mosquitto and set up our broker to use SSL to secure our password-protected MQTT communications.

      Prerequisites

      Before starting this tutorial, you will need:

      Step 1 — Installing Mosquitto

      Ubuntu 18.04 has a fairly recent version of Mosquitto in its default software repository, so we can install it from there.

      First, log in using your non-root user and update the package lists using apt update:

      Now, install Mosquitto using apt install:

      • sudo apt install mosquitto mosquitto-clients

      By default, Ubuntu will start the Mosquitto service after install. Let's test the default configuration. We'll use one of the Mosquitto clients we just installed to subscribe to a topic on our broker.

      Topics are labels that you publish messages to and subscribe to. They are arranged as a hierarchy, so you could have sensors/outside/temp and sensors/outside/humidity, for example. How you arrange topics is up to you and your needs. Throughout this tutorial we will use a simple test topic to test our configuration changes.

      Log in to your server a second time, so you have two terminals side-by-side. In the new terminal, use mosquitto_sub to subscribe to the test topic:

      • mosquitto_sub -h localhost -t test

      -h is used to specify the hostname of the MQTT server, and -t is the topic name. You'll see no output after hitting ENTER because mosquitto_sub is waiting for messages to arrive. Switch back to your other terminal and publish a message:

      • mosquitto_pub -h localhost -t test -m "hello world"

      The options for mosquitto_pub are the same as mosquitto_sub, though this time we use the additional -m option to specify our message. Hit ENTER, and you should see hello world pop up in the other terminal. You've sent your first MQTT message!

      Enter CTRL+C in the second terminal to exit out of mosquitto_sub, but keep the connection to the server open. We'll use it again for another test in Step 5.

      Next, we'll secure our installation using password-based authentication.

      Step 2 — Configuring MQTT Passwords

      Let's configure Mosquitto to use passwords. Mosquitto includes a utility to generate a special password file called mosquitto_passwd. This command will prompt you to enter a password for the specified username, and place the results in /etc/mosquitto/passwd.

      • sudo mosquitto_passwd -c /etc/mosquitto/passwd sammy

      Now we'll open up a new configuration file for Mosquitto and tell it to use this password file to require logins for all connections:

      • sudo nano /etc/mosquitto/conf.d/default.conf

      This should open an empty file. Paste in the following:

      /etc/mosquitto/conf.d/default.conf

      allow_anonymous false
      password_file /etc/mosquitto/passwd
      
      

      Be sure to leave a trailing newline at the end of the file.

      allow_anonymous false will disable all non-authenticated connections, and the password_file line tells Mosquitto where to look for user and password information. Save and exit the file.

      Now we need to restart Mosquitto and test our changes.

      • sudo systemctl restart mosquitto

      Try to publish a message without a password:

      • mosquitto_pub -h localhost -t "test" -m "hello world"

      The message should be rejected:

      Output

      Connection Refused: not authorised. Error: The connection was refused.

      Before we try again with the password, switch to your second terminal window again, and subscribe to the 'test' topic, using the username and password this time:

      • mosquitto_sub -h localhost -t test -u "sammy" -P "password"

      It should connect and sit, waiting for messages. You can leave this terminal open and connected for the rest of the tutorial, as we'll periodically send it test messages.

      Now publish a message with your other terminal, again using the username and password:

      • mosquitto_pub -h localhost -t "test" -m "hello world" -u "sammy" -P "password"

      The message should go through as in Step 1. We've successfully added password protection to Mosquitto. Unfortunately, we're sending passwords unencrypted over the internet. We'll fix that next by adding SSL encryption to Mosquitto.

      Step 3 — Configuring MQTT SSL

      To enable SSL encryption, we need to tell Mosquitto where our Let's Encrypt certificates are stored. Open up the configuration file we previously started:

      • sudo nano /etc/mosquitto/conf.d/default.conf

      Paste in the following at the end of the file, leaving the two lines we already added:

      /etc/mosquitto/conf.d/default.conf

      . . .
      listener 1883 localhost
      
      listener 8883
      certfile /etc/letsencrypt/live/mqtt.example.com/cert.pem
      cafile /etc/letsencrypt/live/mqtt.example.com/chain.pem
      keyfile /etc/letsencrypt/live/mqtt.example.com/privkey.pem
      
      

      Again, be sure to leave a trailing newline at the end of the file.

      We're adding two separate listener blocks to the config. The first, listener 1883 localhost, updates the default MQTT listener on port 1883, which is what we've been connecting to so far. 1883 is the standard unencrypted MQTT port. The localhost portion of the line instructs Mosquitto to only bind this port to the localhost interface, so it's not accessible externally. External requests would have been blocked by our firewall anyway, but it's good to be explicit.

      listener 8883 sets up an encrypted listener on port 8883. This is the standard port for MQTT + SSL, often referred to as MQTTS. The next three lines, certfile, cafile, and keyfile, all point Mosquitto to the appropriate Let's Encrypt files to set up the encrypted connections.

      Save and exit the file, then restart Mosquitto to update the settings:

      • sudo systemctl restart mosquitto

      Update the firewall to allow connections to port 8883.

      Output

      Rule added Rule added (v6)

      Now we test again using mosquitto_pub, with a few different options for SSL:

      • mosquitto_pub -h mqtt.example.com -t test -m "hello again" -p 8883 --capath /etc/ssl/certs/ -u "sammy" -P "password"

      Note that we're using the full hostname instead of localhost. Because our SSL certificate is issued for mqtt.example.com, if we attempt a secure connection to localhost we'll get an error saying the hostname does not match the certificate hostname (even though they both point to the same Mosquitto server).

      --capath /etc/ssl/certs/ enables SSL for mosquitto_pub, and tells it where to look for root certificates. These are typically installed by your operating system, so the path is different for Mac OS, Windows, etc. mosquitto_pub uses the root certificate to verify that the Mosquitto server's certificate was properly signed by the Let's Encrypt certificate authority. It's important to note that mosquitto_pub and mosquitto_sub will not attempt an SSL connection without this option (or the similar --cafile option), even if you're connecting to the standard secure port of 8883.

      If all goes well with the test, we'll see hello again show up in the other mosquitto_sub terminal. This means your server is fully set up! If you'd like to extend the MQTT protocol to work with websockets, you can follow the final step.

      Step 4 — Configuring MQTT Over Websockets (Optional)

      In order to speak MQTT using JavaScript from within web browsers, the protocol was adapted to work over standard websockets. If you don't need this functionality, you may skip this step.

      We need to add one more listener block to our Mosquitto config:

      • sudo nano /etc/mosquitto/conf.d/default.conf

      At the end of the file, add the following:

      /etc/mosquitto/conf.d/default.conf

      . . .
      listener 8083
      protocol websockets
      certfile /etc/letsencrypt/live/mqtt.example.com/cert.pem
      cafile /etc/letsencrypt/live/mqtt.example.com/chain.pem
      keyfile /etc/letsencrypt/live/mqtt.example.com/privkey.pem
      
      

      Again, be sure to leave a trailing newline at the end of the file.

      This is mostly the same as the previous block, except for the port number and the protocol websockets line. There is no official standardized port for MQTT over websockets, but 8083 is the most common.

      Save and exit the file, then restart Mosquitto.

      • sudo systemctl restart mosquitto

      Now, open up port 8083 in the firewall.

      To test this functionality, we'll use a public, browser-based MQTT client. There are a few out there, but the Eclipse Paho JavaScript Client is simple and straightforward to use. Open the Paho client in your browser. You'll see the following:

      Paho Client Screen

      Fill out the connection information as follows:

      • Host should be the domain for your Mosquitto server, mqtt.example.com.
      • Port should be 8083.
      • ClientId can be left to the default value, js-utility-DI1m6.
      • Path can be left to the default value, /ws.
      • Username should be your Mosquitto username; here, we used sammy.
      • Password should be the password you chose.

      The remaining fields can be left to their default values.

      After pressing Connect, the Paho browser-based client will connect to your Mosquitto server.

      To publish a message, navigate to the Publish Message pane, fill out Topic as test, and enter any message in the Message section. Next, press Publish. The message will show up in your mosquitto_sub terminal.

      Conclusion

      We've now set up a secure, password-protected and SSL-secured MQTT server. This can serve as a robust and secure messaging platform for whatever projects you dream up. Some popular software and hardware that work well with the MQTT protocol include:

      • OwnTracks, an open-source geo-tracking app you can install on your phone. OwnTracks will periodically report position information to your MQTT server, which you could then store and display on a map, or create alerts and activate IoT hardware based on your location.
      • Node-RED is a browser-based graphical interface for 'wiring' together the Internet of Things. You drag the output of one node to the input of another, and can route information through filters, between various protocols, into databases, and so on. MQTT is very well supported by Node-RED.
      • The ESP8266 is an inexpensive wifi microcontroller with MQTT capabilities. You could wire one up to publish temperature data to a topic, or perhaps subscribe to a barometric pressure topic and sound a buzzer when a storm is coming!

      These are just a few popular examples from the MQTT ecosystem. There is much more hardware and software out there that speaks the protocol. If you already have a favorite hardware platform, or software language, it probably has MQTT capabilities. Have fun getting your "things" talking to each other!



      Source link

      How to Install and Secure the Mosquitto MQTT Messaging Broker on Ubuntu 18.04 [Quickstart]


      Introduction

      MQTT is a machine-to-machine messaging protocol, designed to provide lightweight publish/subscribe communication to “Internet of Things” devices. Mosquitto is a popular MQTT server (or broker, in MQTT parlance) that has great community support and is easy to install and configure.

      In this condensed quickstart tutorial we’ll install and configure Mosquitto, and use Let’s Encrypt SSL certificates to secure our MQTT traffic. If you need more in-depth coverage of any of the steps, please review the following tutorials:

      Prerequisites

      Before starting this tutorial, you will need:

      • An Ubuntu 18.04 server with a non-root, sudo-enabled user and basic firewall set up, as detailed in this Ubuntu 18.04 server setup tutorial
      • A domain name pointed at your server. This tutorial will use the placeholder mqtt.example.com throughout
      • Port 80 must be unused on your server. If you’re installing Mosquitto on a machine with a web server that occupies this port, you’ll need to use a different method to fetch certificates, such as Certbot’s webroot mode.

      Step 1 — Installing the Software

      First we will install a custom software repository to get the latest version of Certbot, the Let’s Encrypt client:

      • sudo add-apt-repository ppa:certbot/certbot

      Press ENTER to accept, then install the software packages for Mosquitto and Certbot:

      • sudo apt install certbot mosquitto mosquitto-clients

      Next we’ll fetch our SSL certificate.

      Step 2 — Downloading an SSL Certificate

      Open up port 80 in your firewall:

      Then run Certbot to fetch the certificate. Be sure to substitute your server's domain name here:

      • sudo certbot certonly --standalone --preferred-challenges http -d mqtt.example.com

      You will be prompted to enter an email address and agree to the terms of service. After doing so, you should see a message telling you the process was successful and where your certificates are stored.

      We'll configure Mosquitto to use these certificates next.

      Step 3 — Configuring Mosquitto

      First we'll create a password file that Mosquitto will use to authenticate connections. Use mosquitto_passwd to do this, being sure to substitute your own preferred username:

      • sudo mosquitto_passwd -c /etc/mosquitto/passwd your-username

      You will be prompted twice for a password.

      Now open up a new configuration file for Mosquitto:

      • sudo nano /etc/mosquitto/conf.d/default.conf

      This will open an empty file. Paste in the following:

      /etc/mosquitto/conf.d/default.conf

      allow_anonymous false
      password_file /etc/mosquitto/passwd
      
      listener 1883 localhost
      
      listener 8883
      certfile /etc/letsencrypt/live/mqtt.example.com/cert.pem
      cafile /etc/letsencrypt/live/mqtt.example.com/chain.pem
      keyfile /etc/letsencrypt/live/mqtt.example.com/privkey.pem
      
      listener 8083
      protocol websockets
      certfile /etc/letsencrypt/live/mqtt.example.com/cert.pem
      cafile /etc/letsencrypt/live/mqtt.example.com/chain.pem
      keyfile /etc/letsencrypt/live/mqtt.example.com/privkey.pem
      

      Be sure to substitute the domain name you used in Step 2 for mqtt.example.com. Save and close the file when you are finished.

      This file does the following:

      • Disables anonymous logins
      • Uses our password file to enable password authentication
      • Sets up a unsecured listener on port 1883 for localhost only
      • Sets up a secure listener on port 8883
      • Sets up a secure websocket-based listener on port 8083

      Restart Mosquitto to pick up the configuration changes:

      • sudo systemctl restart mosquitto

      Check to make sure the service is running again:

      • sudo systemctl status mosquitto

      Output

      ● mosquitto.service - LSB: mosquitto MQTT v3.1 message broker Loaded: loaded (/etc/init.d/mosquitto; generated) Active: active (running) since Mon 2018-07-16 15:03:42 UTC; 2min 39s ago Docs: man:systemd-sysv-generator(8) Process: 6683 ExecStop=/etc/init.d/mosquitto stop (code=exited, status=0/SUCCESS) Process: 6699 ExecStart=/etc/init.d/mosquitto start (code=exited, status=0/SUCCESS) Tasks: 1 (limit: 1152) CGroup: /system.slice/mosquitto.service └─6705 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

      The status should be active (running). If it's not, check your configuration file and restart again. Some more information may be available in Mosquitto's log file:

      • sudo tail /var/log/mosquitto/mosquitto.log

      If all is well, use ufw to allow the two new ports through the firewall:

      • sudo ufw allow 8883
      • sudo ufw allow 8083

      Now that Mosquitto is set up, we'll configure Certbot to restart Mosquitto after renewing our certificates.

      Step 4 — Configuring Certbot Renewals

      Certbot will automatically renew our SSL certificates before they expire, but it needs to be told to restart the Mosquitto service after doing so.

      Open the Certbot renewal configuration file for your domain name:

      • sudo nano /etc/letsencrypt/renewal/mqtt.example.com.conf

      Add the following renew_hook option on the last line:

      /etc/letsencrypt/renewal/mqtt.example.com.conf

      renew_hook = systemctl restart mosquitto
      

      Save and close the file, then run a Certbot dry run to make sure the syntax is ok:

      • sudo certbot renew --dry-run

      If you see no errors, you're all set. Let's test our MQTT server next.

      Step 5 – Testing Mosquitto

      We installed some command line MQTT clients in Step 1. We can subscribe to the topic test on the localhost listener like so:

      • mosquitto_sub -h localhost -t test -u "your-user" -P "your-password"

      And we can publish with mosquitto_pub:

      • mosquitto_pub -h localhost -t test -m "hello world" -u "your-user" -P "your-password"

      To subscribe using the secured listener on port 8883, do the following:

      • mosquitto_sub -h mqtt.example.com -t test -p 8883 --capath /etc/ssl/certs/ -u "your-username" -P "your-password"

      And this is how you publish to the secured listener:

      • mosquitto_pub -h mqtt.example.com -t test -m "hello world" -p 8883 --capath /etc/ssl/certs/ -u "your-username" -P "your-password"

      Note that we're using the full hostname instead of localhost. Because our SSL certificate is issued for mqtt.example.com, if we attempt a secure connection to localhost we'll get an error saying the hostname does not match the certificate hostname.

      To test the websocket functionality, we'll use a public, browser-based MQTT client. Open the Eclipse Paho javascript client utility in your browser and fill out the connection information as follows:

      • Host is the domain for your Mosquitto server, mqtt.example.com
      • Port is 8083
      • ClientId can be left to the default randomized value
      • Path can be left to the default value of /ws
      • Username is your Mosquitto username from Step 3
      • Password is the password you chose in Step 3

      The remaining fields can be left to their default values.

      After pressing Connect, the client will connect to your server. You can publish and subscribe using the Subscribe and Publish Message panes below the Connection pane.

      Conclusion

      We've now set up and tested a secure, password-protected and SSL-encrypted MQTT server. This can serve as a robust and secure messaging platform for your IoT, home automation, or other projects.



      Source link