mcpField
A
analyzing-email-headers-for-phishing-investigation

analyzing-email-headers-for-phishing-investigation

github.com/mukul975安全35.2k614 次安装8 周前更新
代码仓库

关于 analyzing-email-headers-for-phishing-investigation

用于安全分析人员调查可疑邮件的流程指南,帮助追踪邮件来源、检查发件人真实性并识别伪造。内容涵盖邮件头解析、认证记录验证及正文和附件检查。

主要功能

  1. 01解析邮件头与投递链
  2. 02验证 SPF、DKIM 和 DMARC
  3. 03检查发件域名及基础设施
  4. 04提取并分析链接与附件
  5. 05生成邮件分析报告

适用场景

  1. 01调查疑似钓鱼邮件来源
  2. 02核验发件人身份及邮件伪造
  3. 03响应用户点击钓鱼链接的事件
  4. 04分析邮件附件与可疑链接

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mukul975/anthropic-cybersecurity-skills --skill analyzing-email-headers-for-phishing-investigation

安装后在 Claude Code 中输入/analyzing-email-headers-for-phishing-investigation确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

LICENSE
references/
api-reference.md
scripts/
agent.py

详细介绍

分析电子邮件标头以进行网络钓鱼调查

使用时机

  • 在调查可疑网络钓鱼电子邮件以确定其真实来源时
  • 用于验证发件人真实性并检测电子邮件欺骗
  • 在用户点击网络钓鱼链接后的事件响应期间
  • 在追踪可疑电子邮件的传递路径和中继服务器时
  • 用于验证 SPF、DKIM 和 DMARC 对齐以识别伪造

前提条件

  • 来自可疑邮件的原始电子邮件标头(EML 或 MSG 格式)
  • 了解 SMTP 协议和电子邮件标头字段
  • 可访问 DNS 查询工具(dig、nslookup)以进行 SPF/DKIM/DMARC 验证
  • 电子邮件标头分析工具(MHA、emailheaders.net 概念)
  • 带有电子邮件解析库的 Python,用于自动化分析
  • 可访问威胁情报平台以查询 IP/域信誉

工作流

第 1 步:提取原始电子邮件标头
# 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)
PYEOF
第 2 步:解析电子邮件标头链
# 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')}")
PYEOF
第 3 步:验证 SPF、DKIM 和 DMARC 记录
# 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:[email protected]; 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='[email protected]',
    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.tool
第 4 步:分析发件人域和基础设施
# 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')
"
第 5 步:检查电子邮件正文和附件
# 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 check

关键概念

概念描述
SPF(发件人策略框架)指定域授权邮件服务器的 DNS 记录
DKIM(域名密钥识别邮件)验证电子邮件内容完整性的加密签名
DMARC结合 SPF 和 DKIM 进行发件人身份验证的策略框架
Received 标头服务器添加的标头,显示传递链中的每一跳(从下往上读)
Return-Path用于退信消息的信封发件人地址;可能与 From 不同
Message-ID由原始邮件服务器分配的唯一标识符
X-Originating-IP原始发件人 IP 地址(由某些邮件服务添加)
标头伪造攻击者可以伪造 From、Reply-To 和其他标头,但不能伪造 Received 链

工具与系统

工具用途
MXToolbox在线电子邮件标头分析器和 DNS 查询
dig/nslookup用于 SPF、DKIM、DMARC 验证的 DNS 记录查询
pyspfPython SPF 记录验证库
dkimpyPython DKIM 签名验证库
PhishTool专业的网络钓鱼电子邮件分析平台
VirusTotalURL 和文件信誉检查服务
AbuseIPDBIP 地址信誉数据库
whois域注册信息查询

常见场景

场景 1:CEO 欺诈 / 商务电子邮件入侵 电子邮件声称来自 CEO,但 Reply-To 指向 Gmail 地址,SPF 失败,因为发送 IP 未获授权用于被伪造的域,DKIM 缺失,并且 From 域是相似域(ceo-company.com 与 company.com)。

场景 2:凭证收集网络钓鱼 电子邮件包含一个链接,显示为 "login.microsoft.com",但 href 指向相似域,附件是一个 HTML 文件,包含带有凭证窃取 JavaScript 的虚假登录页面,发送域在 3 天前注册。

场景 3:通过附件传递恶意软件 电子邮件带有包含宏的 Office 文档附件,发件人域通过 SPF,但账户已被入侵,DKIM 签名有效(从合法基础设施发送),附件 SHA-256 在 VirusTotal 上匹配已知恶意软件。

场景 4:利用合法服务的鱼叉式网络钓鱼 攻击者使用合法的电子邮件营销服务发送网络钓鱼,SPF 和 DKIM 通过,因为该服务已获授权,网络钓鱼在于内容而非基础设施,需要 URL 和内容分析,而不是标头身份验证检查。

输出格式

Email Header Analysis Report:
  Subject:     "Urgent: Invoice Payment Required"
  From:        [email protected] (SPOOFED)
  Reply-To:    [email protected] (MISMATCH)
  Return-Path: <[email protected]>
  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

评论

支持 Markdown