diff --git a/examples/retrieve_high_priorities_cve.py b/examples/retrieve_high_priorities_cve.py new file mode 100644 index 0000000..5a6fb13 --- /dev/null +++ b/examples/retrieve_high_priorities_cve.py @@ -0,0 +1,260 @@ +import os +import glob +import smtplib +import json +import ssl +from email.mime.text import MIMEText +from configparser import ConfigParser +from datetime import datetime, timedelta +from cyberwatch_api import Cyberwatch_Pyhelper + +############################################################ +# CONFIGURATION - USE THIS SECTION TO CONFIGURE SCRIPT +############################################################ + +# Add the following block to api.conf and set variables in smtp_settings: +# [cyberwatch] #Configure API acess +# api_key = +# secret_key = +# url = +# +# [SMTP] #Configure SMTP server to send mail +# smtp_server = +# login = +# password = + +SENDER_EMAIL = "" +RECEIVER_EMAILS = "" +SUBJECT = "Cyberwatch - Rapport 'CVEs prioritaires'" + +############################################################ + +def send_email(html): + """Sends an email using smtp specified in the file api.conf""" + + conf = ConfigParser() + conf.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'api.conf')) + + smtp_settings = { + "server": conf.get('SMTP', 'smtp_server'), + "port": 587, + "username": conf.get('SMTP', 'login'), + "password": conf.get('SMTP', 'password'), + "sender": SENDER_EMAIL, + "recipient": RECEIVER_EMAILS + } + + print("[*] Trying the SMTP server..") + context = ssl.create_default_context() + smtpserver = smtplib.SMTP(smtp_settings["server"], smtp_settings["port"]) + smtpserver.starttls(context=context) # Secure the connection + smtpserver.login(smtp_settings["username"], smtp_settings["password"]) + print("[+] SMTP server connected !") + + today = datetime.now().strftime("%d-%m-%Y") + msg = MIMEText(html, 'html', 'utf-8') + msg['Subject'] = SUBJECT + " - " + today + msg['From'] = smtp_settings["sender"] + msg['To'] = smtp_settings["recipient"] + smtpserver.send_message(msg) + + smtpserver.quit() + +def build_email(cve_list): + """Send email with report""" + conf = ConfigParser() + conf.read(os.path.join(os.path.abspath( + os.path.dirname(__file__)), 'api.conf')) + api_url = conf.get('cyberwatch', 'url') + yesterday = datetime.today() - timedelta(days=1) + + html_start = """ + + + + + + + + + + + +