Skip to content

Commit

Permalink
Test global cache initialization performance
Browse files Browse the repository at this point in the history
  • Loading branch information
itm4n committed Jan 6, 2025
1 parent 795b4cd commit 1490abd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
1 change: 0 additions & 1 deletion src/check/Globals.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ $script:GlobalVariable = @{
$script:GlobalCache = @{
ServiceList = $null
DriverList = $null
HotFixList = $null
ScheduledTaskList = $null
RegisteredComList = $null
CurrentUserSids = $null
Expand Down
14 changes: 2 additions & 12 deletions src/check/Main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ function Invoke-PrivescCheck {
)

begin {
# Check wether the current process has admin privileges.
# The following check was taken from Pow*rUp.ps1
# Check whether the current process has admin privileges.
# The following check was taken from PowerUp.ps1
$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if ($IsAdmin) {
if (-not $Force) {
Expand All @@ -81,16 +81,6 @@ function Invoke-PrivescCheck {
$script:GlobalCache.$CacheEntryName = $null
}

# TODO: Initialize global cache.

# Ensure global cache was initialized.
# TODO: Uncomment after implementing cache initialization
# foreach ($CacheEntryName in $($script:GlobalCache.Keys)) {
# if ($null -eq $script:GlobalCache.$CacheEntryName) {
# Write-Warning "Cache entry '$($CacheEntryName)' was not initialized."
# }
# }

# Once the cache is fully initialized, we can build an InitialSessionState
# object that we can use in different runspaces.
$script:GlobalVariable.InitialSessionState = New-InitialSessionState
Expand Down
2 changes: 2 additions & 0 deletions src/helper/Environment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function Get-CurrentUserSid {
param()

if ($null -eq $script:GlobalCache.CurrentUserSids) {
Write-Verbose "Initializing cache: CurrentUserSids"
$UserIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$script:GlobalCache.CurrentUserSids = $UserIdentity.Groups | Select-Object -ExpandProperty Value
$script:GlobalCache.CurrentUserSids += $UserIdentity.User.Value
Expand All @@ -18,6 +19,7 @@ function Get-CurrentUserDenySid {
param()

if ($null -eq $script:GlobalCache.CurrentUserDenySids) {
Write-Verbose "Initializing cache: CurrentUserDenySids"
$script:GlobalCache.CurrentUserDenySids = [string[]](Get-TokenInformationGroup -InformationClass Groups | Where-Object { $_.Attributes.Equals("UseForDenyOnly") } | Select-Object -ExpandProperty SID)
if ($null -eq $script:GlobalCache.CurrentUserDenySids) {
$script:GlobalCache.CurrentUserDenySids = @()
Expand Down
11 changes: 8 additions & 3 deletions src/helper/SystemInformation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ function Get-ComClassFromRegistry {
process {
if ($null -eq $script:GlobalCache.RegisteredComList) {

Write-Verbose "Initializing cache: RegisteredComList"

$script:GlobalCache.RegisteredComList = @()

Get-ChildItem -Path "Registry::$($RootKey)" -ErrorAction SilentlyContinue |
Expand Down Expand Up @@ -686,9 +688,8 @@ function Get-ServiceFromRegistry {

[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateSet(0,1,2,3)]
[Int] $FilterLevel
[UInt32] $FilterLevel = 1
)

begin {
Expand Down Expand Up @@ -725,6 +726,8 @@ function Get-ServiceFromRegistry {
# If the cached service list hasn't been initialized yet, enumerate all services and populate the
# cache.

Write-Verbose "Initializing cache: ServiceList"

$ServicesRegPath = "HKLM\SYSTEM\CurrentControlSet\Services"
$RegAllServices = Get-ChildItem -Path "Registry::$($ServicesRegPath)" -ErrorAction SilentlyContinue

Expand Down Expand Up @@ -787,7 +790,7 @@ function Get-KernelDriver {
# If the cached driver list hasn't been initialized yet, enumerate all drivers,
# resolve their paths and populate the cache.

Write-Verbose "Populating driver list cache..."
Write-Verbose "Initializing cache: DriverList"

$Services = Get-ServiceFromRegistry -FilterLevel 1 | Where-Object { @('KernelDriver','FileSystemDriver','RecognizerDriver') -contains $_.Type }

Expand Down Expand Up @@ -1191,6 +1194,8 @@ function Get-ScheduledTaskList {

# If the cache is empty, enumerate scheduled tasks and populate the cache.

Write-Verbose "Initializing cache: ScheduledTaskList"

$script:GlobalCache.ScheduledTaskList = @()

$ScheduleService = New-Object -ComObject("Schedule.Service")
Expand Down

0 comments on commit 1490abd

Please sign in to comment.