CyberPolice

An epic windows securing and hardening script
Log | Files | Refs | README

ProcessDMA.ps1 (4617B)


      1 Write-Host "The CYBER POLICE are managing services..." -ForegroundColor Gray
      2 $thispath = Split-Path -parent $MyInvocation.MyCommand.Definition
      3 $path = Split-Path -parent $thispath
      4 $serviceConfigpath = "$path/ConfigFiles/Services"
      5 $path2 = Get-content $path/CyberPoliceOutput/path.txt
      6 $servicesD = @()
      7 $servicesM = @()
      8 $servicesA = @()
      9 
     10 if (!(Test-path "$path2\Services\ChangedServices.txt")) { New-Item -path $path2\Services\ChangedServices -name ChangedServices.txt -type "file" -Force | Out-Null}
     11 
     12 Write-host "Would you like the CYBER POLICE to manage services (Default is No)" -ForegroundColor Yellow 
     13 $Readhost = Read-Host "[Y/N]" 
     14 Switch ($ReadHost) { 
     15     Y {
     16         Write-Host "The CYBER POLICE will go ahead and enforce the law!" -ForegroundColor Green
     17 
     18         foreach ($line in Get-Content $serviceConfigpath/Disabled.txt) {
     19             $servicesD += $line
     20         }
     21         foreach ($line in Get-Content $serviceConfigpath/Manual.txt) {
     22             $servicesM += $line
     23         }
     24         foreach ($line in Get-Content $serviceConfigpath/Auto.txt) {
     25             $servicesA += $line
     26         }
     27 
     28         Write-Host "CYBER POLICE are disabling bad services..." -ForegroundColor Gray
     29         foreach ($ser in $servicesD) {
     30             $serviceCheck = Get-Service -Name $ser -ErrorAction SilentlyContinue
     31             if ($serviceCheck.Length -gt 0) {
     32                 Write-Host "Disabling service: $ser" -ForegroundColor Yellow
     33                 $serST = Get-WmiObject -Class Win32_Service -Filter "Name='$ser'"
     34                 $starttype = $serST | Select-Object StartMode
     35                 if (!($starttype -like '*Disabled*')) {
     36                     Write-Host "$ser status changed!" -ForegroundColor Cyan
     37                     "$ser set to Disabled">>$path2\Services\ChangedServices.txt 
     38                 }
     39                 Set-Service $ser -StartupType Disabled
     40                 Stop-Service $ser -Force
     41                 Write-Host "$ser has been disabled" -ForegroundColor Green
     42             }
     43             else {
     44                 Write-Host "$ser does not exist, nothing happened!" -ForegroundColor Red
     45             }
     46         }
     47         Write-Host "CYBER POLICE disabled bad services" -ForegroundColor Green
     48         Write-Host "CYBER POLICE are setting services to manual..." -ForegroundColor Gray 
     49         foreach ($ser in $servicesM) {
     50             $serviceCheck = Get-Service -Name $ser -ErrorAction SilentlyContinue
     51             if ($serviceCheck.Length -gt 0) {
     52                 Write-Host "Making service manual: $ser" -ForegroundColor Yellow
     53                 $serST = Get-WmiObject -Class Win32_Service -Filter "Name='$ser'"
     54                 $starttype = $serST | Select-Object StartMode
     55                 if (!($starttype -like '*Manual*')) {
     56                     Write-Host "$ser status changed!" -ForegroundColor Cyan
     57                     "$ser set to Manual">>$path2\Services\ChangedServices.txt 
     58                 }
     59                 Set-Service $ser -StartupType Manual
     60                 Write-Host "$ser has been set to manual" -ForegroundColor Green
     61             }
     62             else {
     63                 Write-Host "$ser does not exist, nothing happened!" -ForegroundColor Red
     64             }
     65         }
     66         Write-Host "CYBER POLICE made services manual" -ForegroundColor Green
     67         Write-Host "CYBER POLICE are setting services to automatic..." -ForegroundColor Gray
     68         foreach ($ser in $servicesA) {
     69             $serviceCheck = Get-Service -Name $ser -ErrorAction SilentlyContinue
     70             if ($serviceCheck.Length -gt 0) {
     71                 Write-Host "Making service automatic: $ser" -ForegroundColor Yellow
     72                 $serST = Get-WmiObject -Class Win32_Service -Filter "Name='$ser'"
     73                 $starttype = $serST | Select-Object StartMode
     74                 if (!($starttype -like '*Auto*')) {
     75                     Write-Host "$ser status changed!" -ForegroundColor Cyan
     76                     "$ser set to Automatic">>$path2\Services\ChangedServices.txt 
     77                 }
     78                 Set-Service $ser -StartupType Automatic
     79                 Write-Host "$ser has been set to automatic" -ForegroundColor Green
     80             }
     81             else {
     82                 Write-Host "$ser does not exist, nothing happened!" -ForegroundColor Red
     83             }
     84         }
     85         Write-Host "CYBER POLICE made services automatic" -ForegroundColor Green
     86         Write-Host "CYBER POLICE are done managing services!" -ForegroundColor Green
     87     } 
     88     N { Write-Host "CYBER POLICE will not enforce the law." -ForegroundColor Red } 
     89     Default { Write-Host "YBER POLICE will not enforce the law." -ForegroundColor Red } 
     90 }