ail-project / ail-framework

AIL framework - Analysis Information Leak framework
GNU Affero General Public License v3.0
561 stars 80 forks source link

How to Fix Send Mail Addresses With SSL Certificate #157

Open darkcode357 opened 1 year ago

darkcode357 commented 1 year ago

Name: Send Mail Addresses With SSL Certificate

Steps to reproduce

How'd you do it?

image

[Notifications] ail_domain = 'https://xpto:7000' sender = 'xpto@gmail.com' sender_user = 'xpto@gmail.com' sender_host = 'smtp.gmail.com' sender_port = '465' sender_pw = 'masterpass'

Were you following a specific guide/tutorial or reading documentation?

I am not using any guide/tutorial or documentation

Expected behavior

What should happen?

┌──(XPTO㉿NULL)-[~]
└─$ python3 /home/XPTO/a.py XPTO@gmail.com
Send notification: _mail test_ to XPTO@gmail.com

Current behavior

What happens instead?

^CTraceback (most recent call last):
  File "/home/XPTO/a.py", line 71, in <module>
    sendEmailNotification('XPTO@gmail.com', '_mail test_', 'Success.')
  File "/home/XPTO/a.py", line 38, in sendEmailNotification
    smtp_server = smtplib.SMTP(sender_host, sender_port)
  File "/usr/lib/python3.10/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python3.10/smtplib.py", line 343, in connect
    (code, msg) = self.getreply()
  File "/usr/lib/python3.10/smtplib.py", line 398, in getreply
    line = self.file.readline(_MAXLINE + 1)
  File "/usr/lib/python3.10/socket.py", line 705, in readinto
    return self._sock.recv_into(b)
KeyboardInterrupt

AIL version

Run the command to get my version git log -1 --pretty=oneline

01f459109fa36473f66cc9c400c27a4cebf77858 (origin/master, origin/HEAD) chg: [feeder] rename bgp_monitor

t3st0n1 commented 1 year ago

Hi @darkcode357,

This is an unofficial workaround.

I went through the same situation and to create a workaround I modified the file /ail-framework/bin/NotificationHelper.py adding the following pyhton library: SSL

import ssl

In the line which contains:

smtp_server = smtplib.SMTP(sender_host, sender_port) 

I changed it to:

smtp_server = smtplib.SMTP_SSL(sender_host, sender_port,context=ssl_context)

Adding the following line after the imports:

ssl_context = ssl.creat_default_context()

And remove this line if you don't use TLS and config parser:

smtp_server.starttls()
sender = config_loader.get_config_str("Notifications", "sender")
sender_user = config_loader.get_config_str("Notifications", "sender_user")
sender_host = config_loader.get_config_str("Notifications", "sender_host")
sender_port = config_loader.get_config_int("Notifications", "sender_port")
sender_pw = config_loader.get_config_str("Notifications", "sender_pw")

That way you will see that my file is static and is not using the environment variable,

In this way it was possible to send the email with SSL support.

My file:


import os
import sys

import argparse
import traceback
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import ssl 
ssl_context = ssl.create_default_context()

def sendEmailNotification(recipient, mail_subject, mail_body):

    sender = 'EMAIL@test.com'
    sender_user = 'EMAIL@test.com'
    sender_host = 'smtp.gmail.com'
    sender_port = 465
    sender_pw = 'Password'
    if sender_pw == 'None':
        sender_pw = None

    # raise an exception if any of these is None
    if (sender is None or
        sender_host is None or
        sender_port is None
        ):
        raise Exception('SMTP configuration (host, port, sender) is missing or incomplete!')

    try:
        if sender_pw is not None:
            try:
                ##add ssl suporte
                smtp_server = smtplib.SMTP_SSL(sender_host, sender_port,context=ssl_context)
            except smtplib.SMTPNotSupportedError:
                print("The server does not support the STARTTLS extension.")
                smtp_server = smtplib.SMTP_SSL(sender_host, sender_port)

            smtp_server.ehlo()
            if sender_user is not None:
                smtp_server.login(sender_user, sender_pw)
            else:
                smtp_server.login(sender, sender_pw)
        else:
            smtp_server = smtplib.SMTP(sender_host, sender_port)

        mime_msg = MIMEMultipart()
        mime_msg['From'] = sender
        mime_msg['To'] = recipient
        mime_msg['Subject'] = mail_subject

        mime_msg.attach(MIMEText(mail_body, 'plain'))

        smtp_server.sendmail(sender, recipient, mime_msg.as_string())
        smtp_server.quit()
        print('Send notification: ' + mail_subject + ' to '+recipient)

    except Exception as err:
        traceback.print_tb(err.__traceback__)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Test notification sender.')
    parser.add_argument("addr", help="Test mail 'to' address")
    args = parser.parse_args()
    sendEmailNotification('EMAIL@email.com', '_mail test_', 'Success.')

If you need more help, let me know.