3.3 Enterprise Setup

Introduction

In enterprise use cases, which often involve the use of Active Directory, sysadmins might find it easier to deploy CS-DNS via the use of a startup script, rather than using the GUI installer. Powershell startup scripts are typically easier to deploy than the standard MSI file approach and give you more control and flexibility

Procedure

The following script is written in PowerShell and can be run multiple times. On each run, it does the following:

  • Checks if the necessary files are present on the system, if they are not then they are downloaded

  • Checks if any of the above files has been updated since last run, if they have been updated they are downloaded to the local system

  • Checks if the required service (cs-dns-proxy) is running and if not, attempts to start it

You are encouraged to adapt this install script to your own needs. Reach out to support if you require assistance

You are required to change the api parameter in line 5 below

# !! IMPORTANT !!!
# !! change the api value in the config.json content below !!
$configJson = @"
{
   "api": "",
   "server": "94.23.164.198"
}
"@

# Ensure admin rights
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
  Write-Warning "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
  Break
}

# Define the install path
$dirPath = "C:\Program Files (x86)\CyberSift DNS Proxy"

# Define the service name
$serviceName = "cs-dns-proxy"

# Check if the install directory exists
if (-not (Test-Path -Path $dirPath)) {
   # If the directory does not exist, create it
   Write-Host "Creating install directory $dirPath..."
   New-Item -Path $dirPath -ItemType Directory
}

# Define the config.json file path
$configFilePath = "$dirPath\config.json"

# Check if the config.json file exists
if (-not (Test-Path -Path $configFilePath)) {
  # If the file does not exist, create it with the config content
  Write-Host "Creating config.json file..."
  Set-Content -Path $configFilePath -Value $configJson
} else {
   Write-Host "config.json file already present, skipping..."
}

# Define the files to download - NB the order is important, do not change
$files = @(
   @{Url="https://csinstallscripts.blob.core.windows.net/cs-dns-client/cs-dns-proxy.exe"; LocalFile="$dirPath\cs-dns-proxy.exe"},   
   @{Url="https://csinstallscripts.blob.core.windows.net/cs-dns-client/install_service.exe"; LocalFile="$dirPath\install_service.exe"},   
   @{Url="https://csinstallscripts.blob.core.windows.net/cs-dns-client/uninstall_services.exe"; LocalFile="$dirPath\uninstall_services.exe"}
   @{Url="https://csinstallscripts.blob.core.windows.net/cs-dns-client/cybersift.crt"; LocalFile="$dirPath\cybersift.crt"}
)

foreach ($item in $files) {
   # Define the URL and the local file path
   $url = $item.Url
   $localFilePath = $item.LocalFile

   # Check if the local file exists
   if (Test-Path -Path $localFilePath -PathType Leaf) {
       # Calculate the MD5 hash of the local file and encode it in base64
       Write-Progress -Activity "Calculating MD5 hash" -Status "Processing file $localFilePath..." -PercentComplete 0
       $localHash = (Get-FileHash -Path $localFilePath -Algorithm MD5).Hash
       $hashBytes = [System.Convert]::FromHexString($localHash)
       $localHashBase64 = [System.Convert]::ToBase64String($hashBytes)
       Write-Progress -Activity "Calculating MD5 hash" -Status "Done." -PercentComplete 100

       # Send a HEAD request to the URL
       Write-Progress -Activity "Checking remote file" -Status "Processing request $url..." -PercentComplete 0
       $response = Invoke-WebRequest -Uri $url -Method Head
       Write-Progress -Activity "Checking remote file" -Status "Done." -PercentComplete 100

       # Access the headers in the response
       $responseHeaders = $response.Headers

       # Compare the local hash with the CONTENT-MD5 header
       if ($localHashBase64 -ne $responseHeaders['CONTENT-MD5']) {
         Write-Host "File $localFilePath has changed since last update... updating..."

         # if the file is cs-dns-proxy.exe, stop the service
         if ($localFilePath -eq "$dirPath\cs-dns-proxy.exe" && (Get-Service -Name $serviceName -ErrorAction SilentlyContinue)) {
            Write-Host "Stopping service..."
            Stop-Service -Name $serviceName
         }
           # If they are different, download the file from the URL
           Write-Progress -Activity "Downloading file" -Status "Processing download $url..." -PercentComplete 0
           Invoke-WebRequest -Uri $url -OutFile $localFilePath
           Write-Progress -Activity "Downloading file" -Status "Done." -PercentComplete 100

         # if the file is cs-dns-proxy.exe, start the service
         if ($localFilePath -eq "$dirPath\cs-dns-proxy.exe" && (Get-Service -Name $serviceName -ErrorAction SilentlyContinue)) {
            Write-Host "Starting service..."
            Start-Service -Name $serviceName
         }
       }  else {
            Write-Host "File $localFilePath hasn't changed since last update... skipping"
       }
       Write-Host "File $localFilePath done!"
   } else {
       # If the local file does not exist, download it from the URL
       Write-Progress -Activity "Downloading file" -Status "Processing download $url..." -PercentComplete 0
       Invoke-WebRequest -Uri $url -OutFile $localFilePath
       Write-Progress -Activity "Downloading file" -Status "Done." -PercentComplete 100
       Write-Host "File $localFilePath done!"
   }
}

Write-Host "Installing trusted CA cert..."
$params = @{
   FilePath = "$dirPath\cybersift.crt"
   CertStoreLocation = 'Cert:\LocalMachine\Root'
}
Import-Certificate @params

Write-Host "Checking service state..."
# Check if the service exists
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
if ($service) {
   Write-Host "Service $serviceName installed."   
} else {
   Write-Host "Service $serviceName not installed."
   # Run install_service.exe
   Write-Host "Installing service..."
   Start-Process -FilePath "$dirPath\install_service.exe"
   Write-Host "Service $serviceName installed."
}

# Check if the service is running
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
if ($service -and $service.Status -eq "Running") {
   Write-Host "Service $serviceName is running."
} else {
   Write-Host "Service $serviceName is not running."
   Write-Host "Starting service..."
   Start-Service -Name $serviceName
}

Write-Host ""
Write-Host "Complete!"
Write-Host ""


tags:

  • Enterprise setup

  • Powershell script