antiTamper = 0 # give 0 if the anti-tamper is turned off. give 1 if the anti-tamper is turned on.

# if anti-tamper is turned on, then give the passphrase for the machine in the below variable in between the double quotation. if anti-tamper is turned off, then leave the PassPhrase as it is.

PassPhrase = r"" #give here the PassPhrase if anti-tamper is turned on which is available in the management portal for the machine for example: PassPhrase = r"abcd efgh ijkl"

import os
from subprocess import PIPE, Popen
import ctypes
import ssl
import urllib2
import datetime
import time
import shutil

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)

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 uninstall():
    with disable_file_system_redirection():
        if os.path.exists(r"C:\Program Files\SentinelOne"):
            if antiTamper==0:
                folder = os.listdir(r"C:\Program Files\SentinelOne")[0]
                filepath = "C:\Program Files\SentinelOne\%s"%(folder)
                os.chdir(filepath)
                result = ecmd('uninstall.exe /uninstall /norestart /q')
                if "Agent key required" in str(result):
                    print(result)
                    print("You got to give the passphrase for this machine which is available in your sentinelone management portal. also give 1 in the antiTamper when you rerun this script on this machine with passphrase")
                else:
                    print("successfully uninstalled the sentinelone agent. please restart the system")
            else:
                folder = os.listdir(r"C:\Program Files\SentinelOne")[0]
                filepath = "C:\Program Files\SentinelOne\%s"%(folder)
                os.chdir(filepath)
                print(ecmd('uninstall.exe /uninstall /norestart /q /k="%s"'%(PassPhrase)))
                print("successfully uninstalled the sentinelone agent. please restart the system")
        else:
            print("sentinelone agent is not installed on this system")

uninstall()