import os from subprocess import PIPE, Popen import ctypes import ssl import urllib2 import datetime import time import shutil import re 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() userout = os.popen('query user').read() username = re.findall("(.*)Active",userout)[0].split()[0] sid = os.popen("wmic useraccount where name=\"%s\" get sid"%(username)).read().splitlines()[1].strip() if '64' in arch: url = "https://cdn-patchportal-one.comodo.com/portal/packages/spm/Brave/x64/BraveBrowserStandaloneSilentSetup_110.1.48.171.exe" else: url = "https://cdn-patchportal-one.comodo.com/portal/packages/spm/Brave/x86/BraveBrowserStandaloneSilentSetup32_110.1.48.171.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 import ctypes 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): 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!' usremovefile = """ reg delete "HKEY_USERS\%s\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\BraveSoftware Brave-Browser" /f reg delete "HKEY_USERS\%s\SOFTWARE\BraveSoftware" /f del "C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Brave.lnk" /s /q del "C:\Users\%s\Desktop\Brave.lnk" /s /q RD /S /Q "C:\Users\%s\AppData\Local\BraveSoftware" """%(sid,sid,username,username,username) def spm_install(dir, args): with disable_file_system_redirection(): print(downloadFile(DownTo, url)) print(ecmd("%s --install --silent --system-level"%(DownTo))) os.remove(DownTo) def spm_uninstall(dir, args): with disable_file_system_redirection(): if os.path.exists(r"C:\Program Files\BraveSoftware\Brave-Browser\Application"): versionPath = os.listdir(r"C:\Program Files\BraveSoftware\Brave-Browser\Application") if versionPath: uninstallPath = r'C:\Program Files\BraveSoftware\Brave-Browser\Application'+'\%s'%(versionPath[0])+r'\Installer\setup.exe' print(ecmd('"%s" --uninstall --system-level --force-uninstall'%(uninstallPath))) else: print("brave browser not installed in this system") elif os.path.exists(r"C:\Users"+"\%s"%(username)+r"\AppData\Local\BraveSoftware\Brave-Browser\Application"): batch_name = 'batch_file.bat' batch_path=os.path.join(os.environ['TEMP'], batch_name) with open(batch_path, 'wb') as wr: wr.write(usremovefile) print(ecmd(batch_path)) os.remove(batch_path) def spm_update(dir, args): with disable_file_system_redirection(): spm_uninstall(None, None) time.sleep(10) spm_install(None, None)