PISA Pakistan — Community CERT

← 🚩 CTF: Getting Started

50. Email Security Analysis & Phishing Detection for CTF: Headers, SPF/DKIM/DMARC, and Malicious Payloads

⏱ 11 min

Email Security Foundations for CTF Forensics

Email phishing analysis is essential for CTF forensics and OSINT challenges. Attackers frequently hide malicious links, attachments, or exfiltration mechanisms within emails. In Pakistan, phishing campaigns impersonating NADRA, SBP, easypaisa, or JazzCash are common—understanding email security helps both in competitions and real-world threat detection.

Email Structure: An email consists of headers (metadata) and body content. CTF challenges often provide raw email files (.eml, .msg) or base64-encoded messages requiring reconstruction.

Key Email Headers to Analyze:

  • From: - Sender address (spoofable, check domain)
  • Return-Path: - Actual sender's email server
  • Received: - Mail transfer path (trace routing)
  • Authentication-Results: - SPF, DKIM, DMARC validation results
  • X-Mailer: - Email client used
  • Content-Type: - MIME type (detects HTML vs plain text)

Analyzing Email Headers with Wireshark and Command-Line Tools

Using Wireshark:

  • Open .pcap files containing SMTP traffic
  • Filter: smtp.data to see email content
  • Reassemble SMTP streams to view full email

Command-Line Analysis:

  • Extract headers from .eml files: cat email.eml | grep -i "^From:\|^To:\|^Subject:"
  • Decode MIME encoded parts: cat email.eml | munpack
  • View HTML content safely: cat email.html | sed 's/<[^>]*>//g'

Python Script for Email Parsing:

import email
from email.header import decode_header

msg = email.message_from_file(open('phishing.eml'))
print('Subject:', decode_header(msg['Subject'])[0])
print('From:', msg['From'])
for part in msg.walk():
    if part.get_content_type() == 'text/html':
        print('HTML Content:', part.get_payload())

SPF, DKIM, and DMARC Validation

Email authentication protocols determine if an email is legitimate or spoofed.

SPF (Sender Policy Framework):

  • DNS TXT record specifying authorized mail servers
  • Check via: dig TXT yourdomain.com
  • Interpretation: PASS, FAIL, SOFTFAIL, NEUTRAL

DKIM (DomainKeys Identified Mail):

  • Digital signature validating email integrity
  • Look for DKIM-Signature: header
  • Decode via online DKIM validators or opendkim-testmsg

DMARC (Domain-based Message Authentication):

  • Policy combining SPF and DKIM
  • DNS TXT record: v=DMARC1; p=reject; rua=mailto:reports@example.com
  • Flags impersonation attempts common in Pakistani banking scams

Detecting Malicious Payloads and Phishing Links

URL Analysis:

  • Extract URLs: cat email.eml | grep -oP 'http[s]?://[^\s<>"']+'
  • Check reputation: VirusTotal, URLhaus, or PhishTank
  • Identify typosquatting: paypa1.com vs paypal.com, easypakista.com

Attachment Analysis:

  • Calculate hashes: sha256sum malicious.doc
  • Submit to sandbox: ANY.RUN, Hybrid-Analysis
  • Extract macros: olevba suspicious.doc

Red Flags Specific to Pakistani Context:

  • Fake NADRA verification links: nadra-pak.com, paknadra.gov.pk-lookalike
  • SBP refund scams: sbp-org.pk, statebank-offers.com
  • OLX/Daraz transaction fraud: Requesting advance payment via easypaisa/JazzCash

CTF Challenge Scenarios

Scenario 1: Suspicious Email forensics

  • Analyze email headers to identify spoofed sender
  • Trace actual origin IP from Received headers
  • Extract hidden payload from base64-encoded attachment

Scenario 2: Email Relay and Middlebox Attacks

  • Open relay misconfiguration exploitation
  • SMTP command injection via VRFY/EXPN/RCPT TO

Scenario 3:内网 Email Exfiltration

  • Analyze captured SMTP traffic in pcap
  • Reconstruct attachments and identify data exfiltration patterns

Do / Don't

Do:

  • Always check SPF/DKIM/DMARC results in Authentication-Results header
  • Use isolated environments (VMs, sandboxes) for attachment analysis
  • Verify domain registration details for typosquatting detection
  • Report phishing to NCCIA at 1799 and relevant Pakistani CERTs

Don't:

  • Click links directly from suspicious emails—hover to preview URLs
  • Open Office documents with macros in production environments
  • Trust emails requesting urgent action or personal information
  • Share email samples without sanitizing sensitive recipient data

Takeaway

Practice analyzing a suspicious email sample from your inbox (after verifying it's not live malware). Use Wireshark to examine SMTP traffic in sample pcap files available on pisa.maahir.io. Identify at least three indicators of phishing and verify them against SPF/DKIM/DMARC records. These skills directly apply to incident response roles in Pakistani organizations handling email-based threats.

← PrevNext →