Privacy Tools Guide

Email forwarding is a legitimate feature that millions of users rely on daily. However, it can also be exploited maliciously to silently redirect your correspondence to an unauthorized third party. Whether you’re a developer managing corporate email or a privacy-conscious user, knowing how to detect unauthorized forwarding is essential for maintaining email security.

This guide provides practical methods to identify if your emails are being forwarded without your knowledge, with code examples and tools suitable for technical users.

Understanding Email Forwarding Mechanics

When an email is forwarded, the original message traverses multiple servers before reaching its destination. Each server adds diagnostic headers to the message, creating a trail that reveals the forwarding path. These headers become your primary investigative tool.

Email forwarding occurs in several forms:

Malicious forwarding typically happens through compromised accounts, unauthorized inbox rules, or compromised credentials. Attackers configure forwarding rules to exfiltrate sensitive communications without alerting the account owner.

Examining Email Headers

The most reliable method to detect forwarding involves analyzing raw email headers. Every email contains metadata that documents its journey through the mail system.

Finding Email Headers

Most email clients hide headers by default. Here’s how to access them:

Gmail: Open an email, click the three dots, select “Show original”

Apple Mail: View → Message → All Headers

Thunderbird: View → Headers → All

Programmatic access (Python):

import email
from email import policy
from email.parser import BytesParser

def extract_headers(raw_email):
    msg = BytesParser(policy=policy.default).parsebytes(raw_email)

    # Extract all received headers (these trace the email's path)
    received = msg.get_all('Received')

    # Check for forwarding indicators
    for header in ['X-Forwarded-For', 'X-Originating-IP', 'Received-SPF']:
        if header in msg:
            print(f"{header}: {msg[header]}")

    return received

Key Headers That Reveal Forwarding

Several headers indicate forwarding activity:

Compare the “From” address against “Delivered-To” and “Return-Path” to identify discrepancies that suggest forwarding.

Detecting Unauthorized Forwarding Rules

Email providers offer various ways to view active forwarding rules. Checking these regularly helps catch unauthorized configurations.

Gmail

Access forwarding settings at Settings → Forwarding and POP/IMAP. You’ll see all configured forwarding addresses here. For programmatic access, use the Gmail API:

from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build

def check_gmail_forwarding(credentials):
    service = build('gmail', 'v1', credentials=credentials)

    # Get forwarding settings
    settings = service.users().settings().getForwardingAddress(
        userId='me'
    ).execute()

    for forward in settings.get('forwardingEmailAddresses', []):
        print(f"Forwarding to: {forward['forwardingEmail']}")

Microsoft 365 / Outlook

Use the Exchange Online PowerShell module:

# Check forwarding rules
Get-InboxRule -Mailbox "user@domain.com" |
  Where-Object { $_.ForwardTo -or $_.RedirectTo } |
  Select-Object Name, ForwardTo, RedirectTo, Enabled

# Check mailbox forwarding settings
Get-Mailbox user@domain.com |
  Select-Object ForwardingSmtpAddress, DeliverToMailboxAndForward

Apple iCloud

iCloud doesn’t provide a web interface for viewing rules. Use AppleScript to check:

tell application "Mail"
    set forwardRules to forwarding rules of account "iCloud"
    repeat with r in forwardRules
        log (name of r & ": " & (forwarding address of r as string))
    end repeat
end tell

Server-Side Detection for Administrators

If you administer your own mail server, several methods help detect forwarding anomalies.

Postfix Logging

Check /var/log/mail.log for forwarding patterns:

# Find emails forwarded to external domains
grep -E "to=<.*@gmail\.com|to=<.*@yahoo\.com" /var/log/mail.log | \
  grep -E "from=<.*@yourdomain\.com"

# Monitor for new forwarding aliases
grep "forward" /var/log/mail.log | tail -100

Checking DMARC and SPF Failures

Forwarded emails often trigger authentication failures. Monitor these:

# Check for SPF/DKIM failures indicating forwarding
grep -E "spf=fail|dkim=fail" /var/log/mail.log | \
  awk '{print $NF}' | sort | uniq -c | sort -rn

Practical Detection Workflow

Implement a systematic detection process:

  1. Baseline your headers: Save headers from emails you know weren’t forwarded
  2. Compare new emails: Look for new “Received” chain entries or unexpected “X-Forwarded-For” values
  3. Verify forwarding rules: Check your provider’s forwarding settings weekly
  4. Monitor sent folder: Attackers forwarding your emails may use your account
  5. Set up alerts: Configure notifications for new forwarding rules

Automated Monitoring Script

Here’s a Python script that checks Gmail for suspicious forwarding:

import os
from datetime import datetime, timedelta
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build

def monitor_forwarding(credentials_path='token.json'):
    credentials = Credentials.from_authorized_user_file(credentials_path)
    service = build('gmail', 'v1', credentials=credentials)

    # Check for recent forwarding rule changes
    history = service.users().history().list(
        userId='me',
        startHistoryId=os.environ.get('LAST_HISTORY_ID', '1'),
        historyTypes=['label']
    ).execute()

    # Get current forwarding addresses
    forwarding = service.users().settings().getForwardingAddresses(
        userId='me'
    ).execute()

    known_forwarders = ['your-personal@email.com']

    for addr in forwarding.get('forwardingEmailAddresses', []):
        if addr['forwardingEmail'] not in known_forwarders:
            print(f"ALERT: Unknown forwarding address: {addr['forwardingEmail']}")

    return history.get('historyId')

if __name__ == '__main__':
    alert_history_id = monitor_forwarding()
    print(f"Monitoring from history: {alert_history_id}")

What To Do If You Detect Unauthorized Forwarding

If you discover unauthorized forwarding:

  1. Immediately remove the rule: Access your email settings and delete suspicious forwarding addresses
  2. Change your password: Assume your account may be compromised
  3. Enable two-factor authentication: Add an extra layer of security
  4. Check for other rules: Look for additional malicious rules like email deletion or marking as read
  5. Review recent account activity: Check login history for unfamiliar locations
  6. Notify contacts: Inform people who emailed you recently that your account may have been compromised

Prevention Best Practices

Detecting unauthorized email forwarding requires vigilance and understanding of email metadata. By regularly examining headers, monitoring forwarding rules, and implementing automated checks, you can protect your communications from silent exfiltration.

Built by theluckystrike — More at zovo.one