threat hunting

Hunting For Spearphishing Indicators

Hunt for spearphishing campaign indicators across email logs, endpoint telemetry, and network data to detect targeted email attacks.

email-securityinitial-accessmitre-attackproactive-detectionspearphishingt1566threat-hunting
Install this skill
npx skills add mukul975/Anthropic-Cybersecurity-Skills
Framework mappings

When to Use

  • When proactively hunting for indicators of hunting for spearphishing indicators in the environment
  • After threat intelligence indicates active campaigns using these techniques
  • During incident response to scope compromise related to these techniques
  • When EDR or SIEM alerts trigger on related indicators
  • During periodic security assessments and purple team exercises

Prerequisites

  • EDR platform with process and network telemetry (CrowdStrike, MDE, SentinelOne)
  • SIEM with relevant log data ingested (Splunk, Elastic, Sentinel)
  • Sysmon deployed with comprehensive configuration
  • Windows Security Event Log forwarding enabled
  • Threat intelligence feeds for IOC correlation

Workflow

  1. Formulate Hypothesis: Define a testable hypothesis based on threat intelligence or ATT&CK gap analysis.
  2. Identify Data Sources: Determine which logs and telemetry are needed to validate or refute the hypothesis.
  3. Execute Queries: Run detection queries against SIEM and EDR platforms to collect relevant events.
  4. Analyze Results: Examine query results for anomalies, correlating across multiple data sources.
  5. Validate Findings: Distinguish true positives from false positives through contextual analysis.
  6. Correlate Activity: Link findings to broader attack chains and threat actor TTPs.
  7. Document and Report: Record findings, update detection rules, and recommend response actions.

Key Concepts

Concept Description
T1566.001 Spearphishing Attachment
T1566.002 Spearphishing Link
T1566.003 Spearphishing via Service

Tools & Systems

Tool Purpose
CrowdStrike Falcon EDR telemetry and threat detection
Microsoft Defender for Endpoint Advanced hunting with KQL
Splunk Enterprise SIEM log analysis with SPL queries
Elastic Security Detection rules and investigation timeline
Sysmon Detailed Windows event monitoring
Velociraptor Endpoint artifact collection and hunting
Sigma Rules Cross-platform detection rule format

Common Scenarios

  1. Scenario 1: Macro-enabled Excel executing PowerShell downloader
  2. Scenario 2: HTML smuggling delivering ISO with LNK payload
  3. Scenario 3: Credential harvesting link as SharePoint notification
  4. Scenario 4: QR code phishing in PDF attachment

Output Format

Hunt ID: TH-HUNTIN-[DATE]-[SEQ]
Technique: T1566.001
Host: [Hostname]
User: [Account context]
Evidence: [Log entries, process trees, network data]
Risk Level: [Critical/High/Medium/Low]
Confidence: [High/Medium/Low]
Recommended Action: [Containment, investigation, monitoring]
Source materials

References and resources

Everything below is rendered for inspection. Script files are read-only and never run.

References 3

api-reference.md1.8 KB

API Reference: Hunting for Spearphishing Indicators

Email Header Analysis

import email
from email import policy
 
msg = email.message_from_file(open("suspect.eml"), policy=policy.default)
print(msg["From"], msg["Return-Path"], msg["Received"])
print(msg["Authentication-Results"])  # SPF/DKIM/DMARC

Suspicious Attachment Types

Extension Risk Technique
.exe, .scr, .dll CRITICAL T1566.001
.xlsm, .docm HIGH T1566.001 (macros)
.iso, .img, .lnk HIGH T1566.001 (MOTW bypass)
.html, .htm HIGH HTML Smuggling
.zip, .rar MEDIUM Archive with payload

Splunk SPL - Phishing Detection

index=email sourcetype=exchange
| where match(attachment_name, "(?i)\.(exe|scr|iso|lnk|docm|xlsm|hta)$")
| stats count by sender, recipient, attachment_name, subject
| where count > 3

KQL - Microsoft Defender for Office 365

EmailAttachmentInfo
| where FileType in ("exe", "scr", "iso", "lnk", "docm", "xlsm")
| join kind=inner EmailEvents on NetworkMessageId
| project Timestamp, SenderFromAddress, RecipientEmailAddress, Subject, FileName

Phishing URL Patterns

patterns = [
    r"https?://bit\.ly/",           # URL shorteners
    r"https?://\d+\.\d+\.\d+\.\d+", # IP-based URLs
    r"https?://[^/]*login[^/]*\.",   # Credential harvesting
    r"https?://[^/]*\.(top|xyz)/",   # Suspicious TLDs
]

SPF/DKIM/DMARC Validation

import spf
result, _, _ = spf.check2(ip="1.2.3.4", sender="user@example.com", helo="mail.example.com")
# result: 'pass', 'fail', 'softfail', 'neutral', 'none'

References

standards.md1.6 KB

Standards and References - Hunting For Spearphishing Indicators

MITRE ATT&CK Mappings

Technique Name Description
T1566.001 Spearphishing Attachment See attack.mitre.org/techniques/T1566/001
T1566.002 Spearphishing Link See attack.mitre.org/techniques/T1566/002
T1566.003 Spearphishing via Service See attack.mitre.org/techniques/T1566/003

Detection Data Sources

Source Event ID Purpose
Sysmon 1 Process creation with command line
Sysmon 3 Network connection initiated
Sysmon 7 Image loaded (DLL)
Sysmon 10 Process access (LSASS)
Sysmon 11 File creation
Sysmon 12/13 Registry create/set
Sysmon 22 DNS query
Sysmon 25 Process tampering
Windows Security 4624 Successful logon
Windows Security 4625 Failed logon
Windows Security 4648 Explicit credential logon
Windows Security 4672 Special privileges assigned
Windows Security 4688 Process creation
Windows Security 4697 Service installed
Windows Security 4698 Scheduled task created
Windows Security 4769 Kerberos TGS requested
Windows Security 5140 Network share accessed

References

workflows.md3.0 KB

Detailed Hunting Workflow - Hunting For Spearphishing Indicators

Phase 1: Data Collection and Querying

Splunk SPL Query

index=sysmon EventCode=1
| where match(ParentImage, "(?i)(winword|excel|powerpnt|outlook)\.exe$")
| where match(Image, "(?i)(cmd|powershell|wscript|cscript|mshta|certutil)\.exe$")
| table _time Computer User ParentImage Image CommandLine

KQL Query (Microsoft Defender for Endpoint)

DeviceProcessEvents
| where InitiatingProcessFileName in~ ("winword.exe","excel.exe","powerpnt.exe","outlook.exe")
| where FileName in~ ("cmd.exe","powershell.exe","wscript.exe","mshta.exe")
| project Timestamp, DeviceName, AccountName, InitiatingProcessFileName, FileName, ProcessCommandLine

Phase 2: Baseline and Anomaly Detection

Step 2.1 - Establish Normal Behavior Baseline

  • Collect 30 days of historical data for the targeted technique
  • Document expected patterns, frequencies, and legitimate use cases
  • Identify known false positive sources and document exceptions
  • Build statistical baseline (mean, standard deviation) for key metrics

Step 2.2 - Identify Anomalies

  • Compare current activity against the 30-day baseline
  • Flag events exceeding 3 standard deviations from normal
  • Prioritize anomalies by risk score and potential business impact
  • Cross-reference with threat intelligence for known IOCs

Phase 3: Investigation and Correlation

Step 3.1 - Deep Dive Analysis

  • For each anomaly, collect full process tree context
  • Correlate with network activity, file operations, and authentication events
  • Check binary signatures, file hashes, and certificate validity
  • Review user account context and access patterns

Step 3.2 - Attack Chain Reconstruction

  • Map findings to MITRE ATT&CK kill chain stages
  • Identify initial access vector if applicable
  • Trace lateral movement and privilege escalation paths
  • Determine data access and potential exfiltration

Phase 4: Validation and Response

Step 4.1 - True/False Positive Determination

  • Verify findings with system owners and IT operations
  • Check change management records for authorized activities
  • Validate user context (authorized actions vs. compromised account)
  • Document determination rationale for each finding

Step 4.2 - Response Actions

  • For confirmed threats: initiate incident response procedures
  • For detection gaps: create or update detection rules
  • For false positives: tune existing rules and update exclusions
  • Update threat hunting playbook with lessons learned

Phase 5: Documentation and Reporting

Step 5.1 - Hunt Report

  • Summarize hypothesis, methodology, and findings
  • Include all queries executed and their results
  • Document IOCs discovered and detection rules created
  • Provide recommendations for security improvements

Step 5.2 - Knowledge Base Update

  • Add findings to threat intelligence platform
  • Update MITRE ATT&CK coverage heatmap
  • Share detection rules via Sigma format
  • Schedule follow-up hunts for related techniques

Scripts 2

agent.py6.7 KB
Display-only source. This catalog never executes bundled scripts.
#!/usr/bin/env python3
"""Agent for hunting spearphishing indicators across email and endpoint logs."""

import json
import argparse
import re
from datetime import datetime


SUSPICIOUS_EXTENSIONS = [
    ".exe", ".scr", ".bat", ".cmd", ".ps1", ".vbs", ".js", ".hta",
    ".iso", ".img", ".lnk", ".dll", ".msi", ".wsf",
]

MACRO_EXTENSIONS = [".xlsm", ".docm", ".pptm", ".xlsb"]

PHISHING_URL_PATTERNS = [
    r"https?://bit\.ly/", r"https?://tinyurl\.com/",
    r"https?://[^/]*login[^/]*\.", r"https?://[^/]*signin[^/]*\.",
    r"https?://[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}",
    r"https?://[^/]*\.top/", r"https?://[^/]*\.xyz/",
]

URGENCY_KEYWORDS = [
    "urgent", "immediate action", "account suspended", "verify your",
    "password expired", "click here immediately", "security alert",
    "unauthorized access", "confirm your identity",
]


def load_email_logs(log_path):
    """Load email logs from JSON lines file."""
    entries = []
    with open(log_path) as f:
        for line in f:
            try:
                entries.append(json.loads(line))
            except json.JSONDecodeError:
                continue
    return entries


def check_attachment_risk(filename):
    """Assess risk of email attachment by extension."""
    lower = filename.lower()
    if any(lower.endswith(ext) for ext in SUSPICIOUS_EXTENSIONS):
        return "CRITICAL"
    if any(lower.endswith(ext) for ext in MACRO_EXTENSIONS):
        return "HIGH"
    if lower.endswith(".html") or lower.endswith(".htm"):
        return "HIGH"
    if lower.endswith(".zip") or lower.endswith(".rar") or lower.endswith(".7z"):
        return "MEDIUM"
    return "LOW"


def detect_suspicious_attachments(emails):
    """Find emails with dangerous attachment types."""
    findings = []
    for email in emails:
        attachments = email.get("attachments", [])
        for att in attachments:
            name = att if isinstance(att, str) else att.get("filename", "")
            risk = check_attachment_risk(name)
            if risk in ("CRITICAL", "HIGH"):
                findings.append({
                    "subject": email.get("subject", ""),
                    "sender": email.get("from", email.get("sender", "")),
                    "recipient": email.get("to", email.get("recipient", "")),
                    "timestamp": email.get("timestamp", email.get("date", "")),
                    "attachment": name,
                    "risk": risk,
                    "category": "suspicious_attachment",
                })
    return findings


def detect_phishing_urls(emails):
    """Detect phishing URLs in email body or links."""
    findings = []
    for email in emails:
        body = email.get("body", email.get("content", ""))
        urls = email.get("urls", [])
        text = body + " " + " ".join(urls) if urls else body
        for pattern in PHISHING_URL_PATTERNS:
            matches = re.findall(pattern, text, re.IGNORECASE)
            for url in matches:
                findings.append({
                    "subject": email.get("subject", ""),
                    "sender": email.get("from", email.get("sender", "")),
                    "recipient": email.get("to", email.get("recipient", "")),
                    "url": url[:200],
                    "pattern": pattern,
                    "severity": "HIGH",
                    "category": "phishing_url",
                })
    return findings


def detect_urgency_lures(emails):
    """Detect social engineering urgency keywords."""
    findings = []
    for email in emails:
        subject = email.get("subject", "")
        body = email.get("body", email.get("content", ""))
        text = (subject + " " + body).lower()
        matched = [kw for kw in URGENCY_KEYWORDS if kw in text]
        if len(matched) >= 2:
            findings.append({
                "subject": email.get("subject", ""),
                "sender": email.get("from", email.get("sender", "")),
                "recipient": email.get("to", email.get("recipient", "")),
                "keywords_matched": matched,
                "severity": "MEDIUM",
                "category": "urgency_lure",
            })
    return findings


def detect_sender_spoofing(emails):
    """Detect display name vs envelope sender mismatches."""
    findings = []
    for email in emails:
        display_from = email.get("from", email.get("sender", ""))
        envelope = email.get("envelope_from", email.get("return_path", ""))
        if display_from and envelope:
            display_domain = re.search(r"@([\w.-]+)", display_from)
            envelope_domain = re.search(r"@([\w.-]+)", envelope)
            if display_domain and envelope_domain:
                if display_domain.group(1).lower() != envelope_domain.group(1).lower():
                    findings.append({
                        "display_from": display_from,
                        "envelope_from": envelope,
                        "recipient": email.get("to", email.get("recipient", "")),
                        "subject": email.get("subject", ""),
                        "severity": "HIGH",
                        "category": "sender_spoofing",
                    })
    return findings


def main():
    parser = argparse.ArgumentParser(description="Spearphishing Indicator Hunter")
    parser.add_argument("--email-log", required=True, help="JSON lines email log")
    parser.add_argument("--output", default="spearphishing_hunt_report.json")
    parser.add_argument("--action", choices=[
        "attachments", "urls", "urgency", "spoofing", "full_analysis"
    ], default="full_analysis")
    args = parser.parse_args()

    emails = load_email_logs(args.email_log)
    report = {"generated_at": datetime.utcnow().isoformat(), "total_emails": len(emails),
              "findings": {}}
    print(f"[+] Loaded {len(emails)} email entries")

    if args.action in ("attachments", "full_analysis"):
        f = detect_suspicious_attachments(emails)
        report["findings"]["suspicious_attachments"] = f
        print(f"[+] Suspicious attachments: {len(f)}")

    if args.action in ("urls", "full_analysis"):
        f = detect_phishing_urls(emails)
        report["findings"]["phishing_urls"] = f
        print(f"[+] Phishing URLs: {len(f)}")

    if args.action in ("urgency", "full_analysis"):
        f = detect_urgency_lures(emails)
        report["findings"]["urgency_lures"] = f
        print(f"[+] Urgency lure emails: {len(f)}")

    if args.action in ("spoofing", "full_analysis"):
        f = detect_sender_spoofing(emails)
        report["findings"]["sender_spoofing"] = f
        print(f"[+] Sender spoofing detected: {len(f)}")

    with open(args.output, "w") as f:
        json.dump(report, f, indent=2, default=str)
    print(f"[+] Report saved to {args.output}")


if __name__ == "__main__":
    main()
process.py3.5 KB
Display-only source. This catalog never executes bundled scripts.
#!/usr/bin/env python3
"""Spearphishing Detection - Analyzes logs for T1566.001 indicators."""

import json, csv, argparse, datetime, re
from collections import defaultdict
from pathlib import Path

DETECTION_PATTERNS = [
    r'winword\\.exe.*cmd\\.exe',
    r'excel\\.exe.*powershell',
    r'outlook\\.exe.*wscript',
    r'powerpnt\\.exe.*mshta',
]

def parse_logs(path):
    p = Path(path)
    if p.suffix == ".json":
        with open(p, encoding="utf-8") as f:
            data = json.load(f)
            return data if isinstance(data, list) else data.get("events", [])
    elif p.suffix == ".csv":
        with open(p, encoding="utf-8-sig") as f:
            return [dict(r) for r in csv.DictReader(f)]
    return []

def analyze_event(event):
    cmd = event.get("CommandLine", event.get("command_line", event.get("ProcessCommandLine", "")))
    content = event.get("Task_Content", event.get("Parameters", event.get("RawEventData", "")))
    search_text = f"{cmd} {content}"
    risk = 0
    indicators = []
    for pattern in DETECTION_PATTERNS:
        if re.search(pattern, search_text, re.IGNORECASE):
            risk += 25
            indicators.append(f"Pattern match: {pattern}")
    if not indicators:
        return None
    risk = min(risk, 100)
    return {
        "technique": "T1566.001",
        "command_line": cmd[:500] if cmd else content[:500],
        "hostname": event.get("Computer", event.get("DeviceName", event.get("hostname", "unknown"))),
        "user": event.get("User", event.get("AccountName", event.get("UserId", "unknown"))),
        "timestamp": event.get("_time", event.get("timestamp", event.get("UtcTime", event.get("Timestamp", "")))),
        "risk_score": risk,
        "risk_level": "CRITICAL" if risk >= 75 else "HIGH" if risk >= 50 else "MEDIUM" if risk >= 25 else "LOW",
        "indicators": indicators,
    }

def run_hunt(input_path, output_dir):
    print(f"[*] Spearphishing Hunt - {datetime.datetime.now().isoformat()}")
    events = parse_logs(input_path)
    findings = [f for f in (analyze_event(e) for e in events) if f]
    Path(output_dir).mkdir(parents=True, exist_ok=True)
    slug = "hunting_for_spearphi"
    with open(Path(output_dir) / f"{slug}_findings.json", "w", encoding="utf-8") as f:
        json.dump({"hunt_id": f"TH-{datetime.date.today()}", "total_events": len(events), "findings": findings}, f, indent=2)
    with open(Path(output_dir) / "hunt_report.md", "w", encoding="utf-8") as f:
        f.write(f"# Spearphishing Hunt Report\n\n")
        f.write(f"**Date**: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
        f.write(f"**Findings**: {len(findings)}\n\n")
        for finding in sorted(findings, key=lambda x: x["risk_score"], reverse=True)[:20]:
            f.write(f"### [{finding['risk_level']}] {finding['technique']}\n")
            f.write(f"- **Host**: {finding['hostname']}\n")
            f.write(f"- **Indicators**: {', '.join(finding['indicators'])}\n\n")
    print(f"[+] {len(findings)} findings written to {output_dir}")

def main():
    p = argparse.ArgumentParser(description="Spearphishing Detection")
    sp = p.add_subparsers(dest="cmd")
    h = sp.add_parser("hunt"); h.add_argument("--input", "-i", required=True); h.add_argument("--output", "-o", default="./hunting_for_spe_output")
    sp.add_parser("queries")
    args = p.parse_args()
    if args.cmd == "hunt": run_hunt(args.input, args.output)
    elif args.cmd == "queries":
        print("=== Detection Queries ===")
        print("See references/workflows.md for platform-specific queries")
    else: p.print_help()

if __name__ == "__main__": main()

Assets 1

template.mdtext/markdown · 2.6 KB
Keep exploring