import os import subprocess import ctypes import ssl import sys import time import _winreg def is_admin(): try: return ctypes.windll.shell32.IsUserAnAdmin() != 0 except: return False class disable_file_system_redirection: _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection def __enter__(self): self.old_value = ctypes.c_long() self.success = self._disable(ctypes.byref(self.old_value)) def __exit__(self, type, value, traceback): if self.success: self._revert(self.old_value) with disable_file_system_redirection(): arch = os.popen("wmic os get OSArchitecture").read() if '64' in arch: url = "https://cdn-patchportal-one.comodo.com/portal/packages/spm/Brave/x64/BraveBrowserStandaloneSilentSetup_140.1.82.165.exe" else: url = "https://cdn-patchportal-one.comodo.com/portal/packages/spm/Brave/x86/BraveBrowserStandaloneSilentSetup32_140.1.82.165.exe" Down_path = os.environ['TEMP'] fileName = url.split('/')[-1] DownTo = os.path.join(Down_path, fileName) def ecmd(command): from subprocess import Popen, PIPE with disable_file_system_redirection(): obj = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) out, err = obj.communicate() ret = obj.returncode if ret == 0: if out: return out.strip() else: return ret else: if err: return err.strip() else: return ret def downloadFile(DownTo, fromURL): import urllib2 headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'} context = ssl._create_unverified_context() request = urllib2.Request(fromURL, headers=headers) req = urllib2.urlopen(request, context=context) try: with open(DownTo, 'wb') as f: while True: chunk = req.read(100*1000*1000) if chunk: f.write(chunk) else: break if os.path.isfile(DownTo): return '{} - {}KB'.format(DownTo, os.path.getsize(DownTo) / 1024) except: return 'Please Check URL or Download Path!' def spm_install(dir, args): with disable_file_system_redirection(): print(downloadFile(DownTo, url)) print(ecmd('"%s" ' % (DownTo))) os.remove(DownTo) def spm_uninstall(dir, args): print("Starting uninstallation process...") registry_keys = [ r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\BraveSoftware Brave-Browser", # HKEY_CURRENT_USER r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\BraveSoftware Brave-Browser" # HKEY_LOCAL_MACHINE ] uninstall_paths = [] with disable_file_system_redirection(): if is_admin(): # Check HKEY_LOCAL_MACHINE registry (for Admin users) try: key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, registry_keys[1]) uninstall_path = _winreg.QueryValueEx(key, "UninstallString")[0] uninstall_paths.append((uninstall_path, 'HKEY_LOCAL_MACHINE')) _winreg.CloseKey(key) except WindowsError: pass # If key doesn't exist, skip # Always check HKEY_CURRENT_USER registry (for both Admin and Non-Admin users) try: key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, registry_keys[0]) uninstall_path = _winreg.QueryValueEx(key, "UninstallString")[0] uninstall_paths.append((uninstall_path, 'HKEY_CURRENT_USER')) _winreg.CloseKey(key) except WindowsError: pass # If key doesn't exist, skip # If uninstall path is found, proceed with uninstallation if uninstall_paths: print("Found uninstall paths:") for path, registry in uninstall_paths: print(path) uninstall_command = "{0} --silent --uninstall --force-uninstall".format(path) print(ecmd(uninstall_command)) else: print("Brave Browser uninstall path not found in the registry.") # Update function that uninstalls and then installs the latest version def spm_update(dir, args): with disable_file_system_redirection(): spm_uninstall(None, None) time.sleep(300) # Optional delay before reinstall spm_install(None, None)