import os from subprocess import PIPE, Popen import ctypes import ssl import urllib2 import datetime import time 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(): 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() url="https://script-downloads.itarian.com/signal_files/signal-desktop-win-5.62.0.exe" regUrl ="https://script-downloads.itarian.com/signal_files/Signal_62.reg" Down_path=os.environ['TEMP'] fileName = url.split('/')[-1] DownTo = os.path.join(Down_path, fileName) RegName = regUrl.split('/')[-1] RegDownTo = os.path.join(Down_path, RegName) uninstall_name = 'uninstall.bat' uninstall_path=os.path.join(r"C:\Program Files\signal-desktop", uninstall_name) 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!' batchfile = """ %s /S xcopy C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Programs\signal-desktop "C:\Program Files\signal-desktop" /E /H /C /I "C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Programs\signal-desktop\Uninstall Signal.exe" /currentuser /S RD /S /Q C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Programs\signal-desktop reg delete "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Uninstall\\7d96caee-06e6-597c-9f2f-c7bb2e0948b4" /f reg import %s """%(DownTo,RegDownTo) ps_content = """ $TargetFile = "C:\Program Files\signal-desktop\Signal.exe" $ShortcutFile = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Signal.lnk" $WScriptShell = New-Object -ComObject WScript.Shell $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile) $Shortcut.TargetPath = $TargetFile $Shortcut.Save() $TargetFile = "C:\Program Files\signal-desktop\Signal.exe" $ShortcutFile = "C:\Users\Public\Desktop\Signal.lnk" $WScriptShell = New-Object -ComObject WScript.Shell $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile) $Shortcut.TargetPath = $TargetFile $Shortcut.Save() """ usremovefile = """ reg delete "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\\7d96caee-06e6-597c-9f2f-c7bb2e0948b4" /f del "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Signal.lnk" /s /q del "C:\Users\Public\Desktop\Signal.lnk" /s /q RD /S /Q "C:\Program Files\signal-desktop" """ usersignalremove = """ RD /S /Q C:\Users\%s\AppData\Local\Programs\signal-desktop reg delete "HKEY_USERS\%s\Software\Microsoft\Windows\CurrentVersion\Uninstall\\7d96caee-06e6-597c-9f2f-c7bb2e0948b4" /f reg delete "HKEY_USERS\%s\Software\\7d96caee-06e6-597c-9f2f-c7bb2e0948b4" /f del "C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Signal.lnk" /s /q del "C:\Users\%s\Desktop\Signal.lnk" /s /q """%(username,sid,sid,username,username) def spm_install(dir, args): with disable_file_system_redirection(): ps_name='powershell_file.ps1' ps_path=os.path.join(os.environ['TEMP'], ps_name) with open(ps_path, 'wb') as wr: wr.write(ps_content) batch_name = 'batch_file.bat' batch_path=os.path.join(os.environ['TEMP'], batch_name) with open(batch_path, 'wb') as wr: wr.write(batchfile) print(downloadFile(DownTo, url)) print(downloadFile(RegDownTo, regUrl)) print(ecmd(batch_path)) ecmd('powershell "Set-ExecutionPolicy RemoteSigned"') print ecmd('powershell "%s"'%ps_path) with open(uninstall_path, 'wb') as wr: wr.write(usremovefile) os.remove(DownTo) os.remove(RegDownTo) os.remove(ps_path) os.remove(batch_path) def spm_uninstall(dir, args): with disable_file_system_redirection(): 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(): if os.path.exists("C:\Users\%s\AppData\Local\Programs\signal-desktop\Uninstall Signal.exe"%(username)): batch_name = 'batch_file.bat' batch_path=os.path.join(os.environ['TEMP'], batch_name) with open(batch_path, 'wb') as wr: wr.write(usersignalremove) print(ecmd(batch_path)) os.remove(batch_path) if os.path.exists("C:\Program Files\signal-desktop"): spm_uninstall(None, None) time.sleep(20) spm_install(None, None)