diff --git a/examples/email_report_filters.md b/examples/email_report_filters.md new file mode 100644 index 0000000..15c183c --- /dev/null +++ b/examples/email_report_filters.md @@ -0,0 +1,219 @@ +"""Generate a HTML email with the summary""" + +import os +import smtplib +import ssl +from configparser import ConfigParser +from datetime import datetime +from email.mime.text import MIMEText +from cbw_api_toolbox.cbw_api import CBWApi + +CONF = ConfigParser() +CONF.read(os.path.join(os.path.abspath( + os.path.dirname(__file__)), '..', 'api.conf')) + +############################################################ +# CONFIGURATION - USE THIS SECTION TO CUSTOMIZE YOUR REPORTS +############################################################ + +# Filters to use, please comment unused parameters +# ( ["group"] or ["groupA", "groupB", "groupC"]...) +GROUPS = ["GROUPE_1","GROUP_2"] + +# Add the following block to api.conf and set variables in SMTP_SETTINGS: +# [SMTP] +# server = +# login = +# password = + +SMTP_SETTINGS = { + "server": CONF.get('SMTP', 'server'), + "port": 587, + "username": CONF.get('SMTP', 'login'), + "password": CONF.get('SMTP', 'password'), + "sender": "", + "recipient": "" +} + +############################################################ + +print("! Testing communication with Cyberwatch API") +CLIENT = CBWApi(CONF.get('cyberwatch', 'url'), CONF.get( + 'cyberwatch', 'api_key'), CONF.get('cyberwatch', 'secret_key')) +API_URL = CONF.get('cyberwatch', 'url') + +CLIENT.ping() + + +def find_groups_details(GROUPS): + '''d''' + groups_id = [] + all_groups = CLIENT.groups() + for group in GROUPS: + for group_detail in all_groups: + if group_detail.name == group: + groups_id.append(group_detail.id) + return groups_id + + +def get_servers_details(): + '''Find servers with group filters''' + server_details = [] + for group_id in find_groups_details(GROUPS): + filters = {"group_id": group_id} + servers_with_groups = CLIENT.servers(filters) + for server in servers_with_groups: + server_details.append(CLIENT.server(str(server.id))) + return server_details + + +def build_data(): + '''dd''' + + data_for_mail = [] + + critical_cve_details_list = {} + + for server in get_servers_details(): + data = { + 'server_id':server.id, + 'hostname':server.hostname, + 'cve_count': 0, + 'critical_count': 0, + 'critical_with_exploit': 0, + 'critical_issues': 0, + 'major_issues': 0, + } + for cve in server.cve_announcements: + data['cve_count'] += 1 + + if cve.score is not None and cve.score >= 9: + data['critical_count'] += 1 + if cve.cve_code not in critical_cve_details_list: + # faire un appel a l'api pour les details CVEs et stocker dans "critical_cve_details_list" pour eviter de dupliquer les appels + # ensuite calculer "critical_with_exploit" + critical_cve_details_list[cve.cve_code] = CLIENT.cve_announcement(str(cve.cve_code)) + if critical_cve_details_list[cve.cve_code].exploitable : + data['critical_with_exploit'] += 1 + else: + # Si deja dans la liste, juste faire le calcul de "critical_with_exploit" + if critical_cve_details_list[cve.cve_code].exploitable : + data['critical_with_exploit'] += 1 + print("") + + # Ensuite faire le calcul pour "critical_issues" et "major_issues" avant de build_email avec les donnees et envoyer le mail + for security_issue in server.security_issues: + if security_issue.level == "level_critical": + data['critical_issues'] += 1 + if security_issue.level == "level_major": + data['major_issues'] += 1 + + data_for_mail.append(data) + return data_for_mail + + +def build_email(cve_counts): + '''Build HTML for email''' + html_start = """ + + +
+ + + +
+
+
+
+
+
+
+
+
+
+
+Veille sécurité Cyberwatch +
+
+
+
+Liste des serveurs des groupes "{}" vulnérables :
+
+
+
+
+Message généré automatiquement par Cyberwatch. +Toute notre équipe vous remercie pour votre confiance. Pour toute question, n'hésitez pas à contacter support@cyberwatch.fr + |
+
Aucun serveur avec une CVE critique, une CVE critique avec exploit ou un défaut de sécurité critique/majeur correspondant aux critères définis n'a été remonté
" + data = html_start + html + html_end + return data + + html_start += """ + +