Set Up ParseDMARC on Ubuntu 20.04 to Analyze DMARC Reports

ParseDMARC is an open-source, self-hosted DMARC report analyzer. For those who don’t know, DMARC is an email security standard that can protect your domain name from email spoofing and also identify incoming spoofed emails to protect end users. In a previous article, we discussed how to create DMARC DNS record for your domain name and used a third-party tool called PostMark to analyze DMARC reports. In this tutorial, we are going to set up ParseDMARC on Ubuntu 20.04 server so we can analyze DMARC reports without having to share the data with a third-party.

Prerequisites

It is assumed that you have created a DMARC DNS record for your domain name.

To generate visualized data, ParseDMARC relies on Elasticsearch and Kibana, both of which are RAM hungry. They will use about 2G RAM after installation and require more RAM to process data. So you need a server with at least 3G RAM. It’s also recommended that you use a server with at least 4 CPU cores.

You can click this special link to get $100 free credit on DigitalOcean. (For new users only). If you are already a DigitalOcean user, then you can click this special link to get $50 free credit on Vultr (for new users only). Once you have an account at DigitalOcean or Vultr, install Ubuntu 20.04 on your server and follow the instructions below.

Installing ParseDMARC on Ubuntu 20.04 Server

ParseDMARC is a Python program and can be installed from the PyPI (Python Package Index) software repository. First, we need to install the Python package installer on Ubuntu 20.04. In the following command, we install the python3-pip package because ParseDMARC can only work with Python 3. The geoipupdate package is used to update the MaxMind GeoIP database.

sudo apt install python3-pip geoipupdate

Then install the latest stable version of ParseDMARC with the following command.

sudo -H pip3 install -U parsedmarc

To check information about the parsedmarc package, you can run

pip3 show -f parsedmarc

To see command line options, run

parsedmarc --help

Installing Elasticsearch and Kibana on Ubuntu 20.04

ParseDMARC is a command-line program, which produces hard-to-read output. If you want to see visualized DMARC reports in a web-based interface, you need to install two other open-source programs: Elasticsearch and Kibana. Elasticsearch is a search and analytics engine and Kibana allows users to visualize data with charts and graphs in Elasicsearch.

We can install Elasticsearch and Kibana from the official repository with the following commands. Elasticsearch is written in Java programming language, so we also install the default-jre-headless (Java runtime environment) package in the last command.

sudo apt-get install -y apt-transport-https

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list

sudo apt-get update

sudo apt-get install -y default-jre-headless elasticsearch kibana

By default, the Elasticsearch systemd service is disabled. You can start and enable it with the following commands.

sudo systemctl start elasticsearch

sudo systemctl enable elasticsearch

Then check the status.

systemctl status elasticsearch

We can see that it’s now enabled and running, and it uses 1.3G RAM. (Hint: If this command doesn’t quit immediately, press Q to make it quit.)

parsedmarc-elasticsearch-ubuntu 20.04

We also need to do the same for Kibaba.

sudo systemctl start kibana

sudo systemctl enable kibana

Check the status.

systemctl status kibana

parsedmarc kibana ubuntu 20.04

Setting Up Nginx Reverse Proxy for Elastic

Elastic web server listens on 127.0.0.1:5601. We can use Nginx to set up reverse proxy to allow for remote access and also protect Elastic web interface. Run the following command to install Nginx from Ubuntu 20.04 repository.

sudo apt install nginx

Create a Nginx virtual host file for Elastic.

sudo nano /etc/nginx/conf.d/elastic.conf

Put the following lines into the file. Replace the placeholder as necessary and you should create a DNS A record for the sub-domain.

server {
      listen 80;
      listen [::]:80; 
      server_name dmarc.yourdomain.com;

      access_log /var/log/nginx/dmarc.access;
      error_log /var/log/nginx/dmarc.error;

      add_header X-Frame-Options SAMEORIGIN; 
      add_header X-Content-Type-Options nosniff;

      location / {
         proxy_pass http://127.0.0.1:5601;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }
}

Save and close the file. Then test Nginx configurations.

sudo nginx -t

If the test is successful, reload Nginx for the change to take effect.

sudo systemctl reload nginx

Now you can access Elastic web interface at dmarc.yourdomain.com. Click the Explore on my own button.

dmarc-report-analyzer-open-source-ubuntu-20.04

 

Enabling HTTPS

To encrypt the HTTP traffic when you visit Elastic web interface via a domain name, we can enable HTTPS by installing a free TLS certificate issued from Let’s Encrypt. Run the following command to install Let’s Encrypt client (certbot) on Ubuntu 20.04 server.

sudo apt install certbot

If you use Nginx, then you also need to install the Certbot Nginx plugin.

sudo apt install python3-certbot-nginx

Next, run the following command to obtain and install TLS certificate.

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email you@example.com -d dmarc.yourdomain.com

The certificate should now be obtained and automatically installed.

self-hosted-dmarc-analyzer-https

Restricting Access to the Elastic Web Interface

By default, Elastic doesn’t provide user authentication. To allow only trusted users to access Elastic, we can enable HTTP basic authentication in Nginx. First, we need to install the apache2-utils package which provides the htpasswd password file create tool.

sudo apt install apache2-utils

Then run the following command to create the admin user. You will need to set a password.

sudo htpasswd -c /etc/nginx/htpasswd admin

Next, edit the Elastic virtual host file.

sudo nano /etc/nginx/conf.d/elastic.conf

Add the following two lines in the listen 443 ssl server block.

auth_basic "Login required";
auth_basic_user_file /etc/nginx/htpasswd;

Save and close the file. Then test Nginx configurations.

sudo nginx -t

If the test is successful, reload Nginx for the change to take effect.

sudo systemctl reload nginx

Now if you reload the Elastic in your web browser, you will be asked to enter an username and password.

kibana HTTP basic authentication

Configuring ParseDMARC

In order to use ParseDMARC and send data to Elastisearch, we need to create a configuration file.

sudo nano /etc/parsedmarc.ini

Put the following lines in the file. Replace the palceholders as necessary.

[general]
# Save aggregate and forensic reports to Elasticsearch
save_aggregate = True
save_forensic = True

[imap]
# Log into the DMARC report email address and download data.
host = mail.yourdomain.com
port = 993
ssl = True
user = dmarc@yourdomain.com
password = your_password_here
watch = True

[elasticsearch]
# Send data to Elastichsearch, which listens on port 9200.
hosts = 127.0.0.1:9200
ssl = False

[smtp]
# For sending email
host = mail.yourdomain.com
port = 587
ssl = True
user = dmarc@yourdomain.com
password = your_password_here
from = dmarc@yourdomain.com

# send results to this address
to = admin@yourdomain.com

Save and close the file.

Running ParseDMARC as a Systemd Service

We can manually run ParseDMARC with parsedmarc -c /etc/parsedmarc.ini (Don’t run this command just yet), but it’s more convenient to run ParseDMARC as a systemd service in the background. Create a systemd service unit file for ParseDMARC with the following command.

sudo nano /etc/systemd/system/parsedmarc.service

Put the following lines into the file.

[Unit]
Description=parsedmarc mailbox watcher
Documentation=https://domainaware.github.io/parsedmarc/
Wants=network-online.target
After=network.target network-online.target elasticsearch.service

[Service]
ExecStart=/usr/local/bin/parsedmarc -c /etc/parsedmarc.ini
User=parsedmarc
Group=parsedmarc
Restart=always
RestartSec=5m

[Install]
WantedBy=multi-user.target

Save and close the file. This systemd service will run as the parsedmarc user, so we need to create the user with the following command.

sudo adduser --system --no-create-home --group parsedmarc

We also need to protect the /etc/parsedmarc.ini file so only root and parsedmarc group users can read it.

sudo chown parsedmarc /etc/parsedmarc.ini

sudo chmod 600 /etc/parsedmarc.ini

ParseDMARC will need to download MaxMind GeoIP database to /usr/share/elasticsearch/modules/ingest-geoip directory, so the parsedmarc user needs to have write permission.

sudo setfacl -R -m "u:parsedmarc:rwx" /usr/share/elasticsearch/modules/ingest-geoip/

Now we can start and enable the parsedmarc systemd service.

sudo systemctl start parsedmarc

sudo systemctl enable parsedmarc

Check status.

systemctl status parsedmarc

parsedmarc-systemd-service-ubuntu-20.04

ParseDMARC will start fetching DMARC reports from the report email address and send them to Elasticsearch for analysis. If you have lots of DMARC report emails, please be patient to let ParseDMARC finish its work.

If the parsedmarc service isn’t active (running), you can run the following command to see what’s wrong.

sudo journalctl -eu parsedmarc

Importing ParseDMARC Objects In Kibana

To use the Kibana dashboard with ParseDMARC, first you need to import the ParseDMARC objects. You can use the following command to download it on your local Linux or Mac computer.

wget https://raw.githubusercontent.com/domainaware/parsedmarc/master/kibana/export.ndjson

If you use Windows, just copy the https link and paste it in browser address bar to download the data.

Then in Elastic Home page, click Kibana to access the Kibana dashboard.

elastic kibana dashboard

Then in Kibana dashboard, click Add your data button.

add data in kibana dashboard

Next, select Saved Objects and click the Import button to import the ParseDMARC objects you downloaded. import parsedmarc kibana objects

Analyzing DMARC Reports in Kibana

Once the ParseDMARC systemd service finishes reading emails, the DMARC report data will show up in Kibana. The most useful report I think is the DMARC Summary. You can check it by going to Saved Objects -> DMARC Summary. The most useful thing you can do with DMARC summary is to identify legitimate sources that failed DMARC check.

  1. Filter the results to only show messages that didn’t pass DMARC.
  2. Then you can see the message dispositions (How the receiving email servers handle them: do nothing, put into quarantine, or reject them).
  3. Scroll down to see the SPF alignment and DKIM alignment details to identify the reason why some legitimate sources failed DMARC check.

If the sender doesn’t include DKIM signature or doesn’t have a reverse DNS record, then it’s clearly spam.

DKIM alignment details

What surprises me is that some mailbox providers uses their own domain name in the DKIM signature when forwarding my legitimate emails. Other mailbox providers uses my domain name in the DKIM signature but still failed DKIM alignment. To further investigate the cause, you can check the individual forensic report.

Conclusion

I hope this tutorial helped you set up ParseDMARC on Ubuntu 20.04 to analyze DMARC reports. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂

Rate this tutorial
[Total: 2 Average: 5]

5 Responses to “Set Up ParseDMARC on Ubuntu 20.04 to Analyze DMARC Reports

  • Michel Bisson
    2 months ago

    This titorial is excellent. I must make some remarks on 2 things:
    1 – in the result of the following command(sudo -H pip3 install -U parsedmarc) (in your tutorial) I got the follwoing error in the list of results:
    ——————————————-
    Successfully built expiringdict maxminddb
    ERROR: geoip2 4.1.0 has requirement requests=2.24.0, but you’ll have requests 2.22.0 which is incompatible.
    ——————————————
    I wonder if this will create problems.

    2 – after a clean installation of Ubuntu 20.0x the package ‘acl’ was not installed and therefore the following command could not work:
    sudo setfacl -R -m “u:parsedmarc:rwx” /usr/share/elasticsearch/modules/ingest-geoip/
    Maybe an indication that this package should be previously installed would be helpful.

    Otherwise this is one of the best tutorials I’ve come across.

  • Ken Wright
    1 month ago

    Hello Xiao!

    So I’m up to the step of running parseDMARC as a service, but I can’t get it to start. When I run journalctl I get the following output:


    Dec 08 04:30:00 grace parsedmarc[1177860]: [38B blob data]
    Dec 08 04:30:00 grace parsedmarc[1177860]: ERROR:cli.py:615:IMAP Error: [AUTHENTICATIONFAILED] Authentication failed
    Dec 08 04:30:00 grace systemd[1]: parsedmarc.service: Main process exited, code=exited, status=1/FAILURE
    Dec 08 04:30:00 grace systemd[1]: parsedmarc.service: Failed with result ‘exit-code’.

    Do I need to add a user dmarc to authenticate?

    • You need to edit the /etc/parsedmarc.ini and enter the correct password for the email address that receives DMARC reports.

      The email address is specified in your DMARC record, and you need to create this email address on your mail server if it doesn’t exist.

  • Ken Wright
    1 month ago

    That worked. Thanks!

  • Jeffery
    2 weeks ago

    Can I analysis loacal dmarc file? (Not connect dmarc@yourdomain.com)

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image.