parsedmarc

repository·master·Indexed 22 days ago

https://github.com/domainaware/parsedmarc

A Python package and CLI for parsing aggregate, failure, and SMTP TLS DMARC reports. It ingests email reports from sources such as IMAP and APIs, and exports them to data analysis platforms including Elasticsearch, Splunk, and Kafka.

Tokens
45.4K
Snippets
52
Records
221
Agent score
79%

What's inside parsedmarc

  1. Overview of parsedmarc

    master
    parsedmarc is a Python module and CLI utility designed for parsing DMARC (Domain-based Message Authentication, Reporting, and Conformance) reports. It serves as a self-hosted, open-source alternative to commercial DMARC processing services. It is commonly used in conjunction with Elasticsearch and Kibana (or Splunk) to visualize email authentication data through premade dashboards.
  2. Overview of parsedmarc capabilities

    master

    What is parsedmarc?

    parsedmarc is a Python module and CLI utility designed to parse DMARC reports. It serves as a self-hosted, open-source alternative to commercial DMARC processing services. When integrated with visualization tools like Kibana, Grafana, or Splunk, it provides a complete pipeline for analyzing email authentication data.

    Key Features

    • Report Parsing:
      • Aggregate/rua DMARC reports (RFC 7489 and RFC 9990).
      • Failure/ruf DMARC reports (RFC 6591 and RFC 9991).
      • SMTP TLS Reporting (TLS-RPT, RFC 8460).
    • Ingestion Methods:
      • Fetch reports from an inbox via IMAP, Microsoft Graph, or Gmail API.
      • Transparently handles .gz or .zip compressed reports.
    • Output & Integration:
      • Formats: Simple JSON and/or CSV.
      • Direct Destinations: Elasticsearch, OpenSearch, Splunk, or PostgreSQL (compatible with premade dashboards).
      • Streaming/Cloud: Apache Kafka, Amazon S3, Azure Log Analytics (Microsoft Sentinel), Graylog (GELF), syslog, or HTTP webhooks.
      • Notifications: Option to email results.
  3. Understand automatic backfilling behavior

    master

    Since the version fixing issue #169, parsedmarc automatically performs backfilling of dkim_results_combined and spf_results_combined fields on startup. This ensures that older reports (which lack these fields) appear correctly in dashboard alignment-detail tables.

    How it works:

    1. Detection: On startup, parsedmarc runs a count query against configured aggregate index patterns to find documents that have DKIM/SPF results but are missing the _combined fields.
    2. Execution: If missing fields are found, it submits a background _update_by_query task with wait_for_completion=false. This prevents startup from being blocked.
    3. Idempotency: The check is idempotent. Once an index is fully backfilled, subsequent startups will find 0 documents needing updates and will log nothing.
    4. Error Handling: Connection errors (e.g., no indexes on a fresh install) are logged as warnings and retried on the next startup rather than causing the process to abort.
    5. Targeting Logic:
      • If index_prefix_domain_map is configured (and no index_prefix is set): Every tenant prefix in the map gets its own index pattern, plus the unprefixed pattern. A SIGHUP reload re-reads the map to cover new tenants without a restart.
      • If index_prefix is set (in [elasticsearch] or [opensearch]): Only that specific prefix is targeted to avoid touching data from other deployments on a shared cluster.
      • If index_suffix is set: Both the suffixed and unsuffixed patterns are targeted.
  4. Analyze DMARC failure reports

    master

    The DMARC failure reports dashboard (formerly DMARC Forensic Samples) provides information from DMARC failure reports (also known as forensic or ruf reports). These reports contain actual samples of emails that failed to pass DMARC.

    Note on Data Availability:

    • Many recipients do not send failure/ruf reports to prevent privacy leaks.
    • Some services (e.g., certain Chinese webmail services) only provide email headers.
    • Very few providers supply the entire email body.
  5. Mailing list best practices for DMARC compliance

    master

    To prevent breaking DMARC signatures (DKIM) and causing message failures, mailing lists should follow these best practices:

    Do

    • Retain original headers: Do not remove or modify existing headers like From, Date, or Subject.
    • Use RFC 2369 headers: Add List-Unsubscribe headers instead of adding unsubscribe links to the message body.
      • Example: List-Unsubscribe: <https://list.example.com/unsubscribe-link>
    • Use RFC 2919 headers: Add List-Id headers instead of modifying the subject line.
      • Example: List-Id: Example Mailing List <list.example.com>

    Do not

    • Modify the body: Do not add or remove content from the message body, including traditional disclaimers or unsubscribe footers.
    • Modify headers: Do not remove or change any existing headers from the original message.
  6. Configure multi-tenant support with index prefixes

    master

    Multi-tenant support (available since 8.19.0) allows placing data into separate OpenSearch or Elasticsearch index prefixes based on a domain mapping.

    1. Create a YAML file where each key is a tenant name and each value is a list of domain names (strings) related to that tenant. Note: A domain cannot be in multiple tenant lists; only the first matching list is used.

      example:
        - example.com
        - example.net
      
      whalensolutions:
        - whalensolutions.com
    2. In your [general] configuration section, set index_prefix_domain_map to the path of this YAML file.

    3. Do not set an index_prefix option in the [elasticsearch] or [opensearch] sections.

    When configured, reports for domains in the map are saved to an index prefixed with {tenant_name}_.

    example:
      - example.com
      - example.net
      - example.org
    
    whalensolutions:
      - whalensolutions.com
  7. Manage records retention with daily indexes

    master
    Starting in version 5.0.0, parsedmarc implements a daily indexing strategy. Instead of a single large index, data is stored in a separate index for each day. This design facilitates compliance with data retention regulations (like GDPR) by allowing users to easily manage or delete time-based data. For implementation details on managing these indexes, refer to the official Elastic guide on managing time-based indexes efficiently.
  8. Parallelize report parsing with n_procs

    master

    You can improve performance when processing large volumes of files or messages by increasing the number of parallel worker processes using the n_procs option under the [log_file] section (or globally in some contexts).

    Important Notes:

    • Only the parsing of report files is parallelized.
    • Fetching, deduplicating, archiving/deleting, and saving/publishing remain sequential in the main process.
    • Each worker process maintains its own DNS/GeoIP cache.
    • Default value is 1.
  9. How message archiving and retries work in parsedmarc

    master

    Parsedmarc processes mailbox messages in batches of batch_size. To prevent data loss, a batch is only archived or deleted from the reports folder after it has been successfully written to all configured output destinations.

    Batch Failure and Retries

    If any destination fails (e.g., Elasticsearch outage, full disk, expired Splunk token), the entire batch remains in the reports folder to be retried. This is an all-or-nothing mechanism.

    • Retry Cap: To prevent infinite loops on broken destinations, retries are capped at max_unsaved_retries + 1 (default is 3).
    • Unsaved Messages: Once the cap is reached, messages are moved to <archive_folder>/Unsaved. Messages in this folder are never deleted, regardless of your delete settings.
    • Recovery: To recover messages from Unsaved, move them back to the reports folder or run parsedmarc with reports_folder = Archive/Unsaved.

    Behavior by Execution Mode

    • Watch Mode (Long-running): The retry cap works across multiple checks as failure counts are kept in memory.
    • One-shot Mode (Cron/Systemd): Failure counts reset every time the process starts. Messages will be retried every single run. To move unsavable messages to Unsaved immediately in one-shot mode, set max_unsaved_retries = 0.
  10. Understand parsedmarc record retention behavior

    master
    Since version 5.0.0, parsedmarc implements a daily indexing strategy. It stores data in a separate index for each individual day. This design is intended to simplify compliance with data retention regulations, such as GDPR, by allowing users to easily delete or manage specific days of data.
  11. Understand DMARC alignment requirements

    master

    DMARC ensures that SPF and DKIM authentication mechanisms are actually authenticating against the same domain that the end user sees in the message's From header. For a message to pass a DMARC check, it must pass either DKIM or SPF, and the relevant indicators must be in alignment.

    DKIM Alignment

    • Passing: The signature in the DKIM header is validated using a public key published in the DNS record of the domain name specified in the signature.
    • Alignment: The signing domain aligns with the domain in the message's From header.

    SPF Alignment

    • Passing: The mail server's IP address is listed in the SPF record of the domain in the SMTP envelope's Mail From header.
    • Alignment: The domain in the SMTP envelope's Mail From header aligns with the domain in the message's From header.
  12. Understand the Reverse DNS Mapping System

    master

    The mapping system in parsedmarc is used to identify sending sources (organizations, services, or industries) based on their reverse DNS base domains. This makes it easier to aggregate metrics, such as calculating the total volume of emails sent by a specific service like 'Intuit Mailchimp' even if they use multiple domains.

    Key Components

    • base_reverse_dns_map.csv: The primary mapping file. It contains three fields: base_reverse_dns, name, and type.
    • known_unknown_base_reverse_dns.txt: A list of domains that have been identified but cannot yet be categorized into a specific organization or industry.
    • psl_overrides.txt: A list of suffixes used to fold noisy subdomains into a single base domain (e.g., folding host01.foo.com to foo.com).

    Service Type Precedence

    When assigning a type in base_reverse_dns_map.csv, follow this order of precedence:

    1. Email Security: All email security services.
    2. Marketing: All marketing services.
    3. ISP: Telecommunications providers offering internet access.
    4. Web Host: Web hosting providers.
    5. Email Provider: Email account providers.
    6. SaaS: Legitimate Software as a Service platforms.
    7. Industry-specific: Other senders using their own domain (e.g., Finance, Healthcare, Retail).

    Note: Do not use Excel to edit CSV files, as it may corrupt Unicode characters. Use LibreOffice Calc instead.