T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/send.py:12
- Finding
- SMTP STARTTLS Does Not Explicitly Enforce Certificate and Hostname Verification<![CDATA[ ## Vulnerability Details **File Location**: `scripts/send.py`, lines 12-14 **Vulnerability Type**: Improper TLS server authentication **Risk Level**: Medium ### Vulnerable Code ```python with smtplib.SMTP(smtp_server, smtp_port) as smtp: smtp.starttls() smtp.login(smtp_user, smtp_pass) ``` ### Technical Analysis The SMTP connection is upgraded with `starttls()` without supplying a hardened `ssl.SSLContext`. This does not explicitly guarantee certificate-chain and hostname verification across supported Python runtimes and configurations. Encryption without reliable server authentication does not prevent an attacker from impersonating the SMTP server. The application subsequently transmits the SMTP username and password through this connection. An attacker capable of intercepting or redirecting network traffic could present an untrusted certificate and impersonate the configured SMTP endpoint where the runtime does not enforce verification. ### Attack Path 1. An attacker obtains a network interception position or manipulates DNS resolution for the configured SMTP hostname. 2. The script connects to the attacker-controlled SMTP endpoint. 3. The endpoint advertises STARTTLS and presents an untrusted or hostname-mismatched certificate. 4. If the active runtime accepts the certificate because verification is not explicitly enforced, the script completes the TLS negotiation. 5. The script calls `smtp.login(smtp_user, smtp_pass)`, disclosing the SMTP credentials to the impersonated endpoint. 6. The attacker may reuse the credentials to access or send email within the permissions granted to the SMTP account. ### Impact Assessment Successful exploitation could disclose the SMTP username, application password, recipient information, subject, and message body. The attacker would obtain the privileges associated with the compromised SMTP credentials, potentially including sending email as the account and any additional mailbox access permitted by ...[truncated 156 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions Create and pass a verified default TLS context explicitly: ```python import ssl context = ssl.create_default_context() with smtplib.SMTP(smtp_server, smtp_port, timeout=30) as smtp: smtp.ehlo() smtp.starttls(context=context) smtp.ehlo() smtp.login(smtp_user, smtp_pass) smtp.send_message(msg) ``` Keep `check_hostname` enabled and `verify_mode` set to `ssl.CERT_REQUIRED`. Do not use unverified contexts or suppress certificate errors. Consider restricting the allowed SMTP server to an approved hostname and adding a connection timeout to limit hangs against hostile endpoints. ]]>
