You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

跨平台Python应用卸载/删除的密码保护实现咨询

针对Windows/macOS Python应用的防篡改与密码验证卸载方案

第三方工具选项

Windows

  • Advanced Installer:支持创建非管理员权限的安装包,可自定义卸载流程,添加密码验证步骤。在安装包配置中,将卸载触发逻辑绑定到密码验证界面,只有输入正确密码才会执行卸载操作,适配自动化批量部署场景。
  • InstallShield:可嵌入自定义卸载脚本,通过内置对话框组件实现密码输入验证,验证通过后才调用卸载命令,支持静默安装参数配置。

macOS

  • Packages:可视化打包工具,能在安装包中嵌入带密码验证的卸载脚本,只有验证通过才会删除应用文件。同时可配置应用目录权限,限制用户直接修改或删除。
  • AppCrypt:第三方应用加密工具,可为应用目录设置访问密码,用户无法直接删除或修改应用文件,必须输入密码才能操作,支持命令行静默批量配置。

代码实现方案

Windows 端实现

  1. 自定义卸载入口
    编写带密码验证的卸载脚本(uninstall.py),示例逻辑:

    import getpass
    import shutil
    import os
    import winreg
    
    def verify_password():
        input_pwd = getpass.getpass("请输入卸载密码:")
        # 替换为你的加密密码校验逻辑(如对比存储的哈希值)
        return input_pwd == "your_secure_password"
    
    if __name__ == "__main__":
        if verify_password():
            app_dir = os.path.dirname(os.path.abspath(__file__))
            shutil.rmtree(app_dir)
            # 移除系统卸载列表条目
            key_path = r"Software\Microsoft\Windows\CurrentVersion\Uninstall\YourAppName"
            try:
                winreg.DeleteKey(winreg.HKEY_CURRENT_USER, key_path)
            except FileNotFoundError:
                pass
        else:
            print("密码错误,卸载终止")
    

    用pyinstaller打包为单文件可执行程序:pyinstaller --onefile uninstall.py
    自动化部署时,将应用安装到用户目录,同时向注册表HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall\YourAppName添加条目,把UninstallString设为打包后的卸载程序路径。

  2. 进程与目录保护
    用psutil监控自身进程,被终止则自动重启(后台运行):

    import psutil
    import os
    import time
    
    def monitor_self():
        current_pid = os.getpid()
        while True:
            if not psutil.pid_exists(current_pid):
                os.startfile(os.path.abspath(__file__))
                break
            time.sleep(5)
    

    使用win32api修改应用目录ACL,仅赋予当前用户读取、执行权限,禁止删除操作(注:高权限用户可手动修改权限,需配合卸载密码验证)。

macOS 端实现

  1. 阻止直接拖入废纸篓卸载
    在应用包的Contents/Info.plist中添加LSUIElement并设为1,让应用后台静默运行。
    编写Python脚本监听废纸篓事件,检测到应用被拖入时验证密码,未通过则移回原目录:

    import os
    import getpass
    import time
    from AppKit import NSFileManager
    
    def watch_trash():
        app_name = "YourApp.app"
        trash_path = os.path.expanduser(f"~/.Trash/{app_name}")
        original_path = f"/Applications/{app_name}"
        file_manager = NSFileManager.defaultManager()
        
        while True:
            if os.path.exists(trash_path):
                input_pwd = getpass.getpass("请输入卸载密码:")
                if input_pwd != "your_secure_password":
                    # 移回原目录
                    file_manager.moveItemAtPath_toPath_error_(trash_path, original_path, None)
            time.sleep(3)
    
  2. 进程守护与权限控制
    创建用户级launchd配置文件(~/Library/LaunchAgents/com.yourcompany.yourapp.plist),设置进程被终止时自动重启:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.yourcompany.yourapp</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/bin/python3</string>
            <string>/Applications/YourApp.app/Contents/MacOS/your_script.py</string>
        </array>
        <key>KeepAlive</key>
        <true/>
        <key>RunAtLoad</key>
        <true/>
    </dict>
    </plist>
    

    执行launchctl load ~/Library/LaunchAgents/com.yourcompany.yourapp.plist启用守护。
    修改应用目录权限:chmod -R 555 /Applications/YourApp.app,让用户仅能读取执行,无法修改删除(注:高权限用户可改权限,需配合密码验证)。


内容的提问来源于stack exchange,提问作者Madhurikp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 14:10:36