BAT=r'''
@echo off
rmdir "C:\Program Files\Bitdefender Antivirus Free\bdagent.exe" /s /q
reg delete "HKEY_CURRENT_USER\SOFTWARE\Bitdefender\Bitdefender Antivirus Free" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Bitdefender\Bitdefender Antivirus Free" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1FCCF41D-5F00-4FE2-9653-162D0486C8B4}" /f
reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\updatesrv" /f
reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\vsserv" /f
rmdir "C:\ProgramData\Bitdefender\Bitdefender Antivirus Free" /s /q
rmdir "C:\Program Files\Bitdefender Antivirus Free" /s /q
rmdir "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Bitdefender Antivirus Free" /s /q
del "C:\Users\Public\Desktop\Bitdefender Antivirus Free.Ink" /s /q
'''
import os
import sys
import platform
import subprocess
import ctypes

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)

path=os.environ['programdata']+"\Sample.bat"
with open(path,"w") as f:
    f.write(BAT)
    
with disable_file_system_redirection():
	print "Excuting Bat File"
	process = subprocess.Popen([path],stdout=subprocess.PIPE)
	stdout = process.communicate()[0]
	print "Bitdefender antivirus free edition uninstall sucessfully"
	print stdout

if os.path.exists(path):
	try:
		os.remove(path)
	except:
		pass
	
import socket
import _winreg
import os
import shutil
import time
import ssl
import time
import ctypes
import urllib2
import getpass
def os_platform():
    true_platform = os.environ['PROCESSOR_ARCHITECTURE']
    try:
            true_platform = os.environ["PROCESSOR_ARCHITEW6432"]
    except KeyError:
            pass
            #true_platform not assigned to if this does not exist
    return true_platform

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)

ps_content=r'''

Get-CimInstance -Namespace root/cis AvProfile -Filter "Name = 'Full Scan'" | Invoke-CimMethod -MethodName StartScan
'''


def ecmd(command):
    import ctypes
    from subprocess import PIPE, Popen

    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

file_name='powershell_file.ps1'
file_path=os.path.join(os.environ['TEMP'], file_name)
with open(file_path, 'wb') as wr:
    wr.write(ps_content)

def Download1(Download_URL,Download_Path):
    print 'Downloading required Comodo Client Security'
    fileName = Download_URL.split('/')[-1]
    DownTo = os.path.join(Download_Path, fileName)
    try:
        context = ssl._create_unverified_context()
        f=urllib2.urlopen(Download_URL,context=context)
    except:
        f=urllib2.urlopen(Download_URL)
    data=f.read()
    print f.getcode()
    with open(DownTo, "wb") as code:
        code.write(data)
    print 'Comodo Client security has been downloaded successfully here '+DownTo
    print 'Installing Comodo Client Security'
    return DownTo


ki=os_platform()
archi=int(filter(str.isdigit, ki))


if archi==64:
	Download_URL ="https://download.comodo.com/itsm/release/win/endpoint_security/latest/ces_64.msi"
	path="CIS_x64"
elif archi==86:
    Download_URL ="https://download.comodo.com/itsm/release/win/endpoint_security/latest/ces_86.msi"
    path="CIS_x86"
fileName = Download_URL.split('/')[-1]
Download_Path=os.environ['PROGRAMDATA']
path=Download1(Download_URL,Download_Path)
if archi==64:
    command1='msiexec /i  "'+path+'"  /quiet REBOOT=ReallySuppress CESMCONTEXT=1 MAKE_CESM_DEFAULT_CONFIG=1 CES_SANDBOX=1 CES_FIREWALL=1 CES_ANTIVIRUS=1  '
    command2='"C:\Program Files\COMODO\Comodo ITSM\ITSMService.exe" -c 4'
elif archi==86:
    command1='msiexec /i  "'+path+'"  /quiet REBOOT=ReallySuppress CESMCONTEXT=1 MAKE_CESM_DEFAULT_CONFIG=1 CES_SANDBOX=1 CES_FIREWALL=1 CES_ANTIVIRUS=1  '
    command2='"C:\Program Files (x86)\COMODO\Comodo ITSM\ITSMService.exe" -c 4'
command=os.popen(command1).read()
time.sleep(300)
command=os.popen(command2).read()
os.remove(path)
print 'Comodo Client Security installed Successfully'
ecmd('powershell "Set-ExecutionPolicy RemoteSigned"')
print ecmd('powershell "%s"'%file_path)
print "Successfully started the Full scan in CCS..."
os.remove(file_path)
