import os import sys import smtplib import datetime from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText from ..web.models import db class Report: def __init__(self, test_id=None, results=None): self.test_id = test_id self.results = results if results is not None: self.report = self._prepare_report() else: self.report = None def _prepare_report(self): r = "" for l in self.results: if l is list: for el in l: print " %s\n" % el r += " %s\n" % el else: print "%s\n" % l r += "%s\n" % l return r def send_mail(self): if self.report is None: return False try: msg = MIMEMultipart() msg["Subject"] = "AV Monitor" msg["From"] = "avmonitor@hackingteam.com" msg["To"] = "olli@hackingteam.com,zeno@hackingteam.com" body = MIMEText(self.report) msg.attach(body) smtp = smtplib.SMTP("mail.hackingteam.com", 25) #smtp.sendmail(msg["From"], msg["To"].split(","), msg.as_string()) smtp.sendmail(msg["From"], msg["To"], msg.as_string()) smtp.quit() return True except Exception as e: print "[report:send mail] Impossible to send report via mail. Exception: %s" % e return False def _build_mail_body(self, url_dir): hresults = [] hcolumns = ['name'] host = "172.20.20.167" port = "8000" report_file = "http://%s:%s/report/%s" % (host, port, self.test_id) sortedresults = sorted(self.results, key=lambda x: x[0][0]) print "DBG sorted %s" % sortedresults for av in sortedresults: name = av[0].split(",")[0] #k = len(av) hres = [] hres.append(name) for ares in av: r = ares.split(", ") hres.append(r[-1]) if r[1] not in hcolumns: hcolumns.append(r[1]) hresults.append(hres) print "DBG hresults %s" % hresults style = """""" ''' style = """""" header_st = "\n \n" header_en = " \n" linestart = " " linetoken = " \n" lineend = " \n" legend = "
%s
\n

Legend:

\n
SUCCESS
FAILED
ERROR
BLACKLISTED
\n" footer = "

View full report" % report_file ''' header_st = "" header_en = "" linestart = "" linetoken = "" lineend = "" legend = "
%s

Legend:

SUCCESS
FAILED
ERROR
BLACKLISTED
" footer = "

View full report" % report_file content = style header = header_st for col in hcolumns: header += "%s" % col # col.replace("exploit_","") header += header_en content += header for res in hresults: rd = dict(zip(hcolumns, res)) print "DBG rd %s" % rd #rd['name'], rd['silent'], rd['melt'], rd['exploit'] avname = rd['name'] l = linestart % avname for col in hcolumns[1:]: link = "http://%s:%s/report/%s/result/%s/%s" % ( host, port, self.test_id, avname, col) found = False for kind in ["FAILED", "BLACKLISTED", "SUCCESS", "ERROR"]: print "DBG parsing rd[%s]" % col if kind in rd[col]: l += linetoken % (kind.lower()[0:3], link) found = True break elif "STARTED" in rd[col]: # or rd[col] == "n": print "DBG found line STARTED" # l += linetoken % ("error", link) l += linetoken % ("err", link) found = True break if not found: print "DBG found nothing. assuming ERROR" # l += linetoken % ("error", link) l += linetoken % ("err", link) l += lineend content += l content += legend content += footer return content def send_report_color_mail(self, url_dir): content = self._build_mail_body(url_dir) try: msg = MIMEMultipart() msg["Subject"] = "AV Monitor Results" msg["From"] = "avmonitor@hackingteam.com" msg["To"] = "olli@hackingteam.com,zeno@hackingteam.com,alor@hackingteam.com,g.landi@hackingteam.com" #msg["To"] = "olli@hackingteam.com,zeno@hackingteam.com" #msg["To"] = "olli@hackingteam.com" print "CONTENT LENGTH: %s" % len(content) body = MIMEText(content, 'html') msg.attach(body) smtp = smtplib.SMTP("mail.hackingteam.com", 25) smtp.sendmail(msg["From"], msg["To"].split(","), msg.as_string()) smtp.quit() return True except Exception as e: print "[report:send mail] Impossible to send report via mail. Exception: %s" % e return False .