
Analyzing Email Headers For Phishing Investigation
- 466 installs
- 27.3k repo stars
- Updated August 2, 2026
- mukul975/anthropic-cybersecurity-skills
analyzing-email-headers-for-phishing-investigation is a cybersecurity skill that parses raw email headers, traces SMTP hops, and checks SPF, DKIM, and DMARC alignment so developers investigating suspected phishing can ve
About
analyzing-email-headers-for-phishing-investigation is version 1.0 of a digital-forensics skill in mukul975/anthropic-cybersecurity-skills authored under Apache-2.0 license. The workflow parses raw email headers, traces SMTP relay hops, and validates SPF, DKIM, and DMARC alignment to spot spoofing during phishing triage. Tags include forensics, email-analysis, phishing, and header-analysis, with mappings to ATLAS technique AML.T0052 and NIST CSF controls RS.AN-01, RS.AN-03, DE.AE-02, and RS.MA-01. Developers and security engineers reach for this skill when a suspicious message needs origin tracing beyond reading the visible From field.
- Workflow for extracting raw headers from EML/MSG, Gmail, and Outlook
- SPF, DKIM, and DMARC validation with DNS lookups (dig/nslookup)
- SMTP path tracing and relay identification for spoofing cases
- Python-oriented automated parsing aligned with MHA-style header review
- Threat-intel hooks for IP and domain reputation during phishing IR
Analyzing Email Headers For Phishing Investigation by the numbers
- 466 all-time installs (skills.sh)
- +30 installs in the week ending Aug 4, 2026 (Skillselion tracking)
- Ranked #515 of 2,203 Security skills by installs in the Skillselion catalog
- Security screen: HIGH risk (skills.sh audit)
- Data as of Aug 5, 2026 (Skillselion catalog sync)
npx skills add https://github.com/mukul975/anthropic-cybersecurity-skills --skill analyzing-email-headers-for-phishing-investigationAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 466 |
|---|---|
| repo stars | ★ 27.3k |
| Security audit | 2 / 3 scanners passed |
| Last updated | August 2, 2026 |
| Repository | mukul975/anthropic-cybersecurity-skills ↗ |
How do you investigate phishing from raw email headers?
Investigate suspected phishing by parsing raw headers, tracing SMTP hops, and checking SPF, DKIM, and DMARC alignment.
Who is it for?
Developers and security engineers triaging suspected phishing emails who need structured header forensics and authentication alignment checks.
Skip if: Teams needing malware sandbox detonation, endpoint EDR response, or automated SOAR playbooks should use dedicated incident-response tooling beyond this header-analysis skill.
When should I use this skill?
The user investigates a suspected phishing email and needs raw header parsing with SPF, DKIM, and DMARC validation.
What you get
Header parse report, SMTP hop trace, and SPF/DKIM/DMARC alignment findings documenting spoofing indicators.
By the numbers
- Skill version 1.0 under Apache-2.0 license
- Maps to 4 NIST CSF controls: RS.AN-01, RS.AN-03, DE.AE-02, RS.MA-01
- Tags include spf, dkim, dmarc, phishing, and header-analysis
Files
Analyzing Email Headers for Phishing Investigation
When to Use
- When investigating a suspected phishing email to determine its true origin
- For verifying sender authenticity and detecting email spoofing
- During incident response when a user has clicked a phishing link
- When tracing the delivery path and relay servers of a suspicious email
- For validating SPF, DKIM, and DMARC alignment to identify forgery
Prerequisites
- Raw email headers from the suspicious message (EML or MSG format)
- Understanding of SMTP protocol and email header fields
- Access to DNS lookup tools (dig, nslookup) for SPF/DKIM/DMARC verification
- Email header analysis tools (MHA, emailheaders.net concepts)
- Python with email parsing libraries for automated analysis
- Access to threat intelligence platforms for IP/domain reputation
Workflow
Step 1: Extract Raw Email Headers
# Export from Outlook: Open email > File > Properties > Internet Headers
# Export from Gmail: Open email > Three dots > Show original
# Export from Thunderbird: View > Message Source
# If working with EML file from forensic image
cp /mnt/evidence/Users/suspect/AppData/Local/Microsoft/Outlook/phishing_email.eml \
/cases/case-2024-001/email/
# If working with PST file, extract individual messages
pip install pypff
python3 << 'PYEOF'
import pypff
pst = pypff.file()
pst.open("/cases/case-2024-001/email/outlook.pst")
root = pst.get_root_folder()
def extract_messages(folder, path=""):
for i in range(folder.get_number_of_sub_messages()):
msg = folder.get_sub_message(i)
headers = msg.get_transport_headers()
subject = msg.get_subject()
if headers:
filename = f"/cases/case-2024-001/email/msg_{i}_{subject[:30]}.txt"
with open(filename, 'w') as f:
f.write(headers)
for i in range(folder.get_number_of_sub_folders()):
extract_messages(folder.get_sub_folder(i))
extract_messages(root)
PYEOFStep 2: Parse the Email Header Chain
# Parse headers using Python email library
python3 << 'PYEOF'
import email
from email import policy
with open('/cases/case-2024-001/email/phishing_email.eml', 'r') as f:
msg = email.message_from_file(f, policy=policy.default)
print("=== KEY HEADER FIELDS ===")
print(f"From: {msg['From']}")
print(f"To: {msg['To']}")
print(f"Subject: {msg['Subject']}")
print(f"Date: {msg['Date']}")
print(f"Message-ID: {msg['Message-ID']}")
print(f"Reply-To: {msg['Reply-To']}")
print(f"Return-Path: {msg['Return-Path']}")
print(f"X-Mailer: {msg['X-Mailer']}")
print(f"X-Originating-IP: {msg['X-Originating-IP']}")
print("\n=== RECEIVED HEADERS (bottom-up = chronological) ===")
received_headers = msg.get_all('Received')
if received_headers:
for i, header in enumerate(reversed(received_headers)):
print(f"\nHop {i+1}: {header.strip()}")
print("\n=== AUTHENTICATION RESULTS ===")
auth_results = msg.get_all('Authentication-Results')
if auth_results:
for result in auth_results:
print(result)
print(f"\nARC-Authentication-Results: {msg.get('ARC-Authentication-Results', 'Not present')}")
print(f"Received-SPF: {msg.get('Received-SPF', 'Not present')}")
print(f"DKIM-Signature: {msg.get('DKIM-Signature', 'Not present')}")
PYEOFStep 3: Validate SPF, DKIM, and DMARC Records
# Extract the envelope sender domain
SENDER_DOMAIN="example-corp.com"
# Check SPF record
dig TXT $SENDER_DOMAIN +short | grep "v=spf1"
# Example: "v=spf1 include:_spf.google.com include:sendgrid.net ~all"
# Check DKIM record (selector from DKIM-Signature header, e.g., "s=selector1")
DKIM_SELECTOR="selector1"
dig TXT ${DKIM_SELECTOR}._domainkey.${SENDER_DOMAIN} +short
# Check DMARC record
dig TXT _dmarc.${SENDER_DOMAIN} +short
# Example: "v=DMARC1; p=reject; rua=mailto:dmarc@example-corp.com; pct=100"
# Verify the sending IP against SPF
# Extract IP from first Received header
SENDING_IP="203.0.113.45"
# Manual SPF check using python
python3 << 'PYEOF'
import spf # pip install pyspf
result, explanation = spf.check2(
i='203.0.113.45',
s='sender@example-corp.com',
h='mail.example-corp.com'
)
print(f"SPF Result: {result}")
print(f"Explanation: {explanation}")
# Results: pass, fail, softfail, neutral, none, temperror, permerror
PYEOF
# Check if sending IP is in known malicious IP lists
# Query AbuseIPDB or VirusTotal
curl -s "https://api.abuseipdb.com/api/v2/check?ipAddress=${SENDING_IP}" \
-H "Key: YOUR_API_KEY" -H "Accept: application/json" | python3 -m json.toolStep 4: Analyze Sender Domain and Infrastructure
# WHOIS lookup on sender domain
whois $SENDER_DOMAIN | grep -iE '(registrar|creation|expiration|registrant|nameserver)'
# Check domain age (recently registered domains are suspicious)
# DNS record investigation
dig A $SENDER_DOMAIN +short
dig MX $SENDER_DOMAIN +short
dig NS $SENDER_DOMAIN +short
# Reverse DNS on sending IP
dig -x $SENDING_IP +short
# Check for lookalike/typosquatting domains
# Compare with legitimate domain using visual similarity
python3 << 'PYEOF'
import Levenshtein # pip install python-Levenshtein
legitimate = "microsoft.com"
suspicious = "micr0soft.com"
distance = Levenshtein.distance(legitimate, suspicious)
ratio = Levenshtein.ratio(legitimate, suspicious)
print(f"Edit distance: {distance}")
print(f"Similarity ratio: {ratio:.2%}")
if ratio > 0.8:
print("WARNING: Likely typosquatting/lookalike domain!")
PYEOF
# Check domain reputation on VirusTotal
curl -s "https://www.virustotal.com/api/v3/domains/${SENDER_DOMAIN}" \
-H "x-apikey: YOUR_VT_API_KEY" | python3 -m json.tool
# Check if the Reply-To differs from From (common phishing indicator)
python3 -c "
import email
with open('/cases/case-2024-001/email/phishing_email.eml') as f:
msg = email.message_from_file(f)
from_addr = email.utils.parseaddr(msg['From'])[1]
reply_to = email.utils.parseaddr(msg.get('Reply-To', msg['From']))[1]
if from_addr != reply_to:
print(f'WARNING: From ({from_addr}) != Reply-To ({reply_to})')
else:
print('From and Reply-To match')
"Step 5: Examine Email Body and Attachments
# Extract URLs from email body
python3 << 'PYEOF'
import email
import re
from email import policy
with open('/cases/case-2024-001/email/phishing_email.eml', 'r') as f:
msg = email.message_from_file(f, policy=policy.default)
body = msg.get_body(preferencelist=('html', 'plain'))
if body:
content = body.get_content()
urls = re.findall(r'https?://[^\s<>"\']+', content)
print("=== URLs FOUND IN EMAIL BODY ===")
for url in set(urls):
print(f" {url}")
# Check for URL obfuscation (display text != href)
href_pattern = re.findall(r'<a[^>]*href=["\']([^"\']+)["\'][^>]*>(.*?)</a>', content, re.DOTALL)
print("\n=== HYPERLINK ANALYSIS ===")
for href, text in href_pattern:
display_url = re.findall(r'https?://[^\s<]+', text)
if display_url and display_url[0] != href:
print(f" MISMATCH: Display='{display_url[0]}' -> Actual='{href}'")
# Extract and hash attachments
print("\n=== ATTACHMENTS ===")
for part in msg.walk():
if part.get_content_disposition() == 'attachment':
filename = part.get_filename()
content = part.get_payload(decode=True)
import hashlib
sha256 = hashlib.sha256(content).hexdigest()
print(f" File: {filename}, Size: {len(content)}, SHA-256: {sha256}")
with open(f'/cases/case-2024-001/email/attachments/{filename}', 'wb') as af:
af.write(content)
PYEOF
# Submit attachment hashes to VirusTotal
# Submit URLs to URLhaus or PhishTank for reputation checkKey Concepts
| Concept | Description |
|---|---|
| SPF (Sender Policy Framework) | DNS record specifying authorized mail servers for a domain |
| DKIM (DomainKeys Identified Mail) | Cryptographic signature verifying email content integrity |
| DMARC | Policy framework combining SPF and DKIM for sender authentication |
| Received headers | Server-added headers showing each hop in the delivery chain (read bottom to top) |
| Return-Path | Envelope sender address used for bounce messages; may differ from From |
| Message-ID | Unique identifier assigned by the originating mail server |
| X-Originating-IP | Original sender IP address (added by some mail services) |
| Header forgery | Attackers can forge From, Reply-To, and other headers but not Received chains |
Tools & Systems
| Tool | Purpose |
|---|---|
| MXToolbox | Online email header analyzer and DNS lookup |
| dig/nslookup | DNS record queries for SPF, DKIM, DMARC verification |
| pyspf | Python SPF record validation library |
| dkimpy | Python DKIM signature verification library |
| PhishTool | Specialized phishing email analysis platform |
| VirusTotal | URL and file reputation checking service |
| AbuseIPDB | IP address reputation database |
| whois | Domain registration information lookup |
Common Scenarios
Scenario 1: CEO Fraud / Business Email Compromise The email claims to be from the CEO but Reply-To points to a Gmail address, SPF fails because the sending IP is not authorized for the spoofed domain, DKIM is missing, and the From domain is a lookalike (ceo-company.com vs company.com).
Scenario 2: Credential Harvesting Phishing Email contains a link that displays "login.microsoft.com" but href points to a lookalike domain, the attachment is an HTML file containing a fake login page with credential exfiltration JavaScript, the sending domain was registered 3 days ago.
Scenario 3: Malware Delivery via Attachment Email with an Office document attachment containing macros, the sender domain passes SPF but the account was compromised, DKIM signature is valid (sent from legitimate infrastructure), attachment SHA-256 matches known malware on VirusTotal.
Scenario 4: Spear Phishing with Legitimate Service Attacker uses a legitimate email marketing service to send phishing, SPF and DKIM pass because the service is authorized, the phishing is in the content not the infrastructure, requires URL and content analysis rather than header authentication checks.
Output Format
Email Header Analysis Report:
Subject: "Urgent: Invoice Payment Required"
From: accounting@examp1e-corp.com (SPOOFED)
Reply-To: payments.urgent@gmail.com (MISMATCH)
Return-Path: <bounce@mail-server.xyz>
Date: 2024-01-15 09:23:45 UTC
Delivery Path (4 hops):
Hop 1: mail-server.xyz [203.0.113.45] -> relay1.isp.com
Hop 2: relay1.isp.com -> mx.target-company.com
Hop 3: mx.target-company.com -> internal-filter.target.com
Hop 4: internal-filter.target.com -> mailbox
Authentication:
SPF: FAIL (203.0.113.45 not authorized for examp1e-corp.com)
DKIM: NONE (no signature present)
DMARC: FAIL (p=none, no enforcement)
Indicators of Phishing:
- Lookalike domain (examp1e-corp.com vs example-corp.com, 96% similar)
- From/Reply-To mismatch
- Domain registered 2 days before email sent
- URL in body points to credential harvesting page
- Attachment: invoice.xlsm (SHA-256: a3f2...) - Known malware on VT
Risk Level: HIGH
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to the Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by the Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding any notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. Please do not remove or change
the license header comment from a contributed file except when
necessary.
Copyright 2026 mukul975
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
API Reference: Email Header Analysis Tools
Python email Module
Parsing EML Files
import email
from email import policy
with open("phishing.eml", "r") as f:
msg = email.message_from_file(f, policy=policy.default)
msg["From"] # From header
msg["To"] # To header
msg["Subject"] # Subject line
msg["Message-ID"] # Unique message identifier
msg["Reply-To"] # Reply-To address
msg["Return-Path"] # Envelope sender
msg.get_all("Received") # All Received headers (list)
msg.get_all("Authentication-Results") # Auth resultsBody and Attachment Extraction
body = msg.get_body(preferencelist=("html", "plain"))
content = body.get_content()
for part in msg.walk():
if part.get_content_disposition() == "attachment":
filename = part.get_filename()
data = part.get_payload(decode=True)dig - DNS Record Lookup
SPF Record
dig TXT example.com +short
# Output: "v=spf1 include:_spf.google.com ~all"DKIM Record
dig TXT selector1._domainkey.example.com +shortDMARC Record
dig TXT _dmarc.example.com +short
# Output: "v=DMARC1; p=reject; rua=mailto:dmarc@example.com"pyspf - SPF Validation (Python)
Syntax
import spf
result, explanation = spf.check2(
i="203.0.113.45", # Sending IP
s="sender@example.com", # Envelope sender
h="mail.example.com" # HELO hostname
)
# Results: pass, fail, softfail, neutral, none, temperror, permerrordkimpy - DKIM Verification (Python)
Syntax
import dkim
with open("email.eml", "rb") as f:
message = f.read()
result = dkim.verify(message)
# Returns True/FalseAbuseIPDB - IP Reputation
API Endpoint
curl -G "https://api.abuseipdb.com/api/v2/check" \
-H "Key: YOUR_API_KEY" \
-H "Accept: application/json" \
-d "ipAddress=203.0.113.45" -d "maxAgeInDays=90"Response Fields
| Field | Description |
|---|---|
abuseConfidenceScore | 0-100 confidence of abuse |
totalReports | Number of abuse reports |
countryCode | Source country |
isp | Internet service provider |
VirusTotal - Domain/URL Reputation
Domain Lookup
curl -H "x-apikey: YOUR_KEY" \
"https://www.virustotal.com/api/v3/domains/suspicious.com"URL Scan
curl -X POST "https://www.virustotal.com/api/v3/urls" \
-H "x-apikey: YOUR_KEY" \
-d "url=http://suspicious-url.com/login"whois - Domain Registration
Syntax
whois suspicious-domain.comKey Fields
Registrar- Domain registrarCreation Date- When domain was registeredRegistrant- Domain owner infoName Server- Authoritative DNS servers
#!/usr/bin/env python3
"""Email header analysis agent for phishing investigation and sender verification."""
import email
import email.utils
import re
import hashlib
import os
import sys
import subprocess
from email import policy
def parse_email_file(eml_path):
"""Parse an EML file and extract key header fields."""
with open(eml_path, "r", errors="replace") as f:
msg = email.message_from_file(f, policy=policy.default)
headers = {
"from": str(msg["From"] or ""),
"to": str(msg["To"] or ""),
"subject": str(msg["Subject"] or ""),
"date": str(msg["Date"] or ""),
"message_id": str(msg["Message-ID"] or ""),
"reply_to": str(msg["Reply-To"] or ""),
"return_path": str(msg["Return-Path"] or ""),
"x_mailer": str(msg["X-Mailer"] or ""),
"x_originating_ip": str(msg["X-Originating-IP"] or ""),
}
return msg, headers
def extract_received_chain(msg):
"""Extract and parse the Received header chain (bottom-up = chronological)."""
received_headers = msg.get_all("Received") or []
hops = []
ip_pattern = re.compile(r"\[?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]?")
for i, header in enumerate(reversed(received_headers)):
ips = ip_pattern.findall(header)
hops.append({
"hop": i + 1,
"header": header.strip()[:200],
"ips": ips,
})
return hops
def extract_authentication_results(msg):
"""Extract SPF, DKIM, and DMARC results from Authentication-Results headers."""
auth_results = msg.get_all("Authentication-Results") or []
received_spf = str(msg.get("Received-SPF", ""))
dkim_sig = str(msg.get("DKIM-Signature", ""))
results = {
"spf": "unknown",
"dkim": "unknown",
"dmarc": "unknown",
"raw_authentication_results": [],
"received_spf": received_spf,
"has_dkim_signature": bool(dkim_sig),
}
for ar in auth_results:
results["raw_authentication_results"].append(ar.strip())
ar_lower = ar.lower()
if "spf=" in ar_lower:
spf_match = re.search(r"spf=(\w+)", ar_lower)
if spf_match:
results["spf"] = spf_match.group(1)
if "dkim=" in ar_lower:
dkim_match = re.search(r"dkim=(\w+)", ar_lower)
if dkim_match:
results["dkim"] = dkim_match.group(1)
if "dmarc=" in ar_lower:
dmarc_match = re.search(r"dmarc=(\w+)", ar_lower)
if dmarc_match:
results["dmarc"] = dmarc_match.group(1)
return results
def check_from_replyto_mismatch(headers):
"""Detect mismatch between From and Reply-To addresses."""
from_addr = email.utils.parseaddr(headers["from"])[1].lower()
reply_to = headers["reply_to"]
if reply_to:
reply_addr = email.utils.parseaddr(reply_to)[1].lower()
if reply_addr and from_addr != reply_addr:
return True, from_addr, reply_addr
return False, from_addr, None
def extract_urls(msg):
"""Extract all URLs from the email body."""
body = msg.get_body(preferencelist=("html", "plain"))
urls = []
if body:
content = body.get_content()
urls = list(set(re.findall(r"https?://[^\s<>\"']+", content)))
return urls
def detect_url_mismatch(msg):
"""Detect hyperlinks where display text differs from actual href."""
body = msg.get_body(preferencelist=("html",))
mismatches = []
if body:
content = body.get_content()
href_pattern = re.findall(
r'<a[^>]*href=["\']([^"\']+)["\'][^>]*>(.*?)</a>', content, re.DOTALL
)
for href, text in href_pattern:
display_urls = re.findall(r"https?://[^\s<]+", text)
if display_urls:
for display_url in display_urls:
if display_url.rstrip("/") != href.rstrip("/"):
mismatches.append({
"display_url": display_url,
"actual_url": href,
})
return mismatches
def extract_attachments(msg, output_dir=None):
"""Extract and hash all email attachments."""
attachments = []
for part in msg.walk():
if part.get_content_disposition() == "attachment":
filename = part.get_filename() or "unnamed_attachment"
content = part.get_payload(decode=True)
if content:
sha256 = hashlib.sha256(content).hexdigest()
md5 = hashlib.md5(content).hexdigest()
att_info = {
"filename": filename,
"size": len(content),
"sha256": sha256,
"md5": md5,
"content_type": part.get_content_type(),
}
if output_dir:
os.makedirs(output_dir, exist_ok=True)
filepath = os.path.join(output_dir, filename)
with open(filepath, "wb") as f:
f.write(content)
att_info["saved_to"] = filepath
attachments.append(att_info)
return attachments
def dns_lookup(domain, record_type="TXT"):
"""Perform DNS lookup for SPF/DKIM/DMARC records."""
stdout, _, rc = subprocess.run(
["dig", record_type, domain, "+short"],
capture_output=True, text=True, timeout=10
).stdout, "", 0
return stdout.strip() if stdout else ""
def check_domain_spf(domain):
"""Look up the SPF record for a domain."""
return dns_lookup(domain, "TXT")
def check_domain_dmarc(domain):
"""Look up the DMARC record for a domain."""
return dns_lookup(f"_dmarc.{domain}", "TXT")
def generate_phishing_indicators(headers, auth, hops, url_mismatches, attachments):
"""Compile a list of phishing indicators from the analysis."""
indicators = []
mismatch, from_addr, reply_addr = check_from_replyto_mismatch(headers)
if mismatch:
indicators.append(f"From/Reply-To mismatch: {from_addr} vs {reply_addr}")
if auth["spf"] in ("fail", "softfail"):
indicators.append(f"SPF {auth['spf']}")
if auth["dkim"] == "fail" or not auth["has_dkim_signature"]:
indicators.append("DKIM failed or missing")
if auth["dmarc"] in ("fail", "none"):
indicators.append(f"DMARC {auth['dmarc']}")
if url_mismatches:
indicators.append(f"{len(url_mismatches)} URL display/href mismatches detected")
for att in attachments:
if any(att["filename"].endswith(ext) for ext in [".exe", ".scr", ".vbs", ".js",
".docm", ".xlsm", ".bat", ".ps1", ".hta"]):
indicators.append(f"Suspicious attachment: {att['filename']}")
return indicators
if __name__ == "__main__":
print("=" * 60)
print("Email Header Phishing Analysis Agent")
print("SPF/DKIM/DMARC validation, URL analysis, attachment extraction")
print("=" * 60)
eml_file = sys.argv[1] if len(sys.argv) > 1 else None
if eml_file and os.path.exists(eml_file):
print(f"\n[*] Analyzing: {eml_file}")
msg, headers = parse_email_file(eml_file)
print(f" From: {headers['from']}")
print(f" To: {headers['to']}")
print(f" Subject: {headers['subject']}")
print(f" Date: {headers['date']}")
hops = extract_received_chain(msg)
print(f"\n[*] Delivery path: {len(hops)} hops")
for hop in hops:
print(f" Hop {hop['hop']}: IPs={hop['ips']}")
auth = extract_authentication_results(msg)
print(f"\n[*] Authentication: SPF={auth['spf']} DKIM={auth['dkim']} DMARC={auth['dmarc']}")
urls = extract_urls(msg)
print(f"\n[*] URLs found: {len(urls)}")
url_mismatches = detect_url_mismatch(msg)
for m in url_mismatches:
print(f" [!] MISMATCH: Display='{m['display_url']}' Actual='{m['actual_url']}'")
attachments = extract_attachments(msg)
print(f"\n[*] Attachments: {len(attachments)}")
for att in attachments:
print(f" {att['filename']} ({att['size']} bytes) SHA256={att['sha256'][:16]}...")
indicators = generate_phishing_indicators(headers, auth, hops, url_mismatches, attachments)
if indicators:
print(f"\n[!] PHISHING INDICATORS:")
for ind in indicators:
print(f" - {ind}")
else:
print(f"\n[DEMO] Usage: python agent.py <email.eml>")
print("[*] Provide an EML file for phishing analysis.")
Related skills
FAQ
What authentication checks does analyzing-email-headers-for-phishing-investigation run?
analyzing-email-headers-for-phishing-investigation validates SPF, DKIM, and DMARC alignment while parsing raw headers and tracing SMTP hops. The skill helps determine whether the visible sender matches authenticated mail paths or shows spoofing indicators.
Which frameworks does analyzing-email-headers-for-phishing-investigation reference?
analyzing-email-headers-for-phishing-investigation version 1.0 lists NIST CSF controls RS.AN-01, RS.AN-03, DE.AE-02, and RS.MA-01 plus ATLAS technique AML.T0052. The skill is tagged for digital-forensics email-analysis workflows under Apache-2.0 license.
Is Analyzing Email Headers For Phishing Investigation safe to install?
skills.sh reports 2 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.