Note
1) Run as Local System User
2) Provide URLS ( at parameters urlList ) which you want to white List.the datatype should be a list
3) Provide 1 (1 0r 0) in apndUrl if you want to add more url to the existing whitelist, 0 will create new whitelist.
# To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('parameterName') with that parameter's name
import ctypes
import os
import subprocess
urlList = itsm.getParameter('URLlist') # Provide an URLS which you want to white List.the datatype should be a list.
apndUrl = 1 # Provide 1 (1 0r 0) in apndUrl if you want to add more url to the existing whitelist, 0 will create new whitelist.
extId = "cjpalhdlnbpafiamejdnhcphjbkeiagm"
path = r'HKEY_LOCAL_MACHINE\Software\Policies\Google\Chrome\3rdparty\extensions\cjpalhdlnbpafiamejdnhcphjbkeiagm\policy'
ValueId = 'adminSettings'
getValue = r'(Get-ItemProperty -Path Registry::' + path + ' -Name ' + ValueId + ').' + ValueId
instUBlock = r'''
param(
[switch]$info
)
$extensionID = "''' + extId + r'''"
if($info){
$InformationPreference = "Continue"
}
if(!($extensionId)){
# Empty Extension
$result = "No Extension ID"
}
else{
Write-Information "ExtensionID = $extensionID"
$extensionId = "$extensionId;https://clients2.google.com/service/update2/crx"
$regKey = "HKLM:\SOFTWARE\Policies\Google\Chrome\ExtensionInstallForcelist"
if(!(Test-Path $regKey)){
New-Item $regKey -Force
Write-Information "Created Reg Key $regKey"
}
# Add Extension to Chrome
$extensionsList = New-Object System.Collections.ArrayList
$number = 0
$noMore = 0
do{
$number++
Write-Information "Pass : $number"
try{
$install = Get-ItemProperty $regKey -name $number -ErrorAction Stop
$extensionObj = [PSCustomObject]@{
Name = $number
Value = $install.$number
}
$extensionsList.add($extensionObj) | Out-Null
Write-Information "Extension List Item : $($extensionObj.name) / $($extensionObj.value)"
}
catch{
$noMore = 1
}
}
until($noMore -eq 1)
$extensionCheck = $extensionsList | Where-Object {$_.Value -eq $extensionId}
if($extensionCheck){
$result = "Extension Already Exists"
Write-Information "Extension Already Exists"
}else{
$newExtensionId = $extensionsList[-1].name + 1
New-ItemProperty HKLM:\SOFTWARE\Policies\Google\Chrome\ExtensionInstallForcelist -PropertyType String -Name $newExtensionId -Value $extensionId
$result = "Installed"
}
}
$result
'''
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 ExecuteCmd(cmd):
with disable_file_system_redirection():
obj = subprocess.Popen(["powershell", cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = obj.communicate()
return out, err
def CreateScriptFile(ps_content):
try:
file_name = 'ScriptFile.ps1'
file_path = os.path.join(os.environ['TEMP'], file_name)
with open(file_path, 'wb') as wr:
wr.write(ps_content)
wr.close()
return file_path
except:
return None
def exeSF(scriptCnt, Process, cnt):
ScriptFile = CreateScriptFile(scriptCnt)
if os.path.exists(ScriptFile):
ScriptExe = ExecuteCmd('powershell "%s"' % ScriptFile)
if (("Installed" in ScriptExe[0] or "Already Exists" in ScriptExe[0]) and cnt == 1) or (ScriptExe[1] == '' and
cnt == 2):
print 'Script execution successful [' + Process + ']'
os.remove(ScriptFile)
return True
else:
print 'Script execution failed [' + Process + ']'
os.remove(ScriptFile)
return False
else:
print 'Script file creation failed [' + Process + ']'
return False
RegVal = ExecuteCmd(getValue)[0]
if "whitelist" in RegVal and apndUrl == 1:
for url in urlList:
RegVal = RegVal.replace("]}", ',"' + url + '"]}')
else:
RegVal = '{"whitelist": ' + str(map(str, urlList)) .replace("'", '"') + '}'
RegVal = RegVal.replace('\r\n', '')
SetWhiteList = r'''
$Path01 = "HKLM:\Software\Policies\Google\Chrome\3rdparty\extensions\cjpalhdlnbpafiamejdnhcphjbkeiagm\policy"
If (-Not (Test-Path -Path $Path01)) { New-Item -Force -Path $Path01 }
# adminSettings
$adminSettings = @{
"Force" = $true
"Path" = "$Path01"
"Type" = "String"
"Name" = "adminSettings"
"Value" = ''' + "'" + RegVal + "'" + r'''
}
Set-ItemProperty @adminSettings
'''
try:
if exeSF(instUBlock, "Install UBlock origin", 1):
exeSF(SetWhiteList, "WhiteList configuration", 2)
except:
print "Script execution failed"
Comments