Så här övervakar du ändringar i BootConfig-filen i Windows [PowerShell Script]

I den komplicerade IT-världen är det av yttersta vikt att upprätthålla systemintegriteten. En viktig komponent i operativsystemet Windows är filen BootConfig. Att övervaka ändringar i denna fil är avgörande för systemadministratörer och IT-proffs för att säkerställa systemets säkerhet och stabilitet. Den här guiden beskriver ett PowerShell-skript som söker efter ändringar i BootConfig-filen, vilket ger en proaktiv metod för systemhantering.

Bakgrund

BootConfig-filen i Windows är ett grundläggande element som innehåller startkonfigurationsdata. Det styr hur systemet startas, vilka OS-versioner som ska laddas och andra startparametrar. Med tanke på dess roll i startprocessen kan alla obehöriga eller oväntade ändringar leda till systemfel eller sårbarheter. Detta gör den till ett utmärkt mål för skadliga aktörer eller skadlig kod. För IT-specialister och tjänsteleverantörer (MSP) är det ovärderligt att ha ett verktyg som kan upptäcka dessa förändringar. Det hjälper till med proaktiv systemhantering och kriminalteknisk analys vid säkerhetsöverträdelser.

Manus

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#Requires -Version 5.1
<#
.SYNOPSIS
Checks if the BootConfig file was modified from last run.
.DESCRIPTION
Checks if the BootConfig file was modified from last run.
On first run this will not produce an error, but will create a cache file for later comparison.
.EXAMPLE
No parameters needed.
.OUTPUTS
None
.NOTES
Minimum OS Architecture Supported: Windows 10, Windows Server 2016
Release Notes:
Initial Release
By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider.
Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
#>
[CmdletBinding()]
param (
# Path and file where the cache file will be saved for comparison
[string]
$CachePath = "C:ProgramDataNinjaRMMAgentscriptingTest-BootConfig.clixml"
)
begin {
function Test-IsElevated {
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
$p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
}
}
process {
if (-not (Test-IsElevated)) {
Write-Error -Message "Access Denied. Please run with Administrator privileges."
exit 1
}
# Get content and create hash of BootConfig file
$BootConfigContent = bcdedit.exe /enum
$Stream = [IO.MemoryStream]::new([byte[]][char[]]"$BootConfigContent")
$BootConfigHash = Get-FileHash -InputStream $Stream -Algorithm SHA256
$Current = [PSCustomObject]@{
Content = $BootConfigContent
Hash = $BootConfigHash
}
# Check if this is first run or not
if ($(Test-Path -Path $CachePath)) {
# Compare last content and hash
$Cache = Import-Clixml -Path $CachePath
$ContentDifference = Compare-Object -ReferenceObject $Cache.Content -DifferenceObject $Current.Content -CaseSensitive
$HashDifference = $Cache.Hash -like $Current.Hash
$Current | Export-Clixml -Path $CachePath -Force -Confirm:$false
if (-not $HashDifference) {
Write-Host "BootConfig file has changed since last run!"
Write-Host ""
$ContentDifference | ForEach-Object {
if ($_.SideIndicator -like '=>') {
Write-Host "Added: $($_.InputObject)"
}
elseif ($_.SideIndicator -like '<=') {
Write-Host "Removed: $($_.InputObject)"
}
}
exit 1
}
}
else {
Write-Host "First run, saving comparison cache file."
$Current | Export-Clixml -Path $CachePath -Force -Confirm:$false
}
exit 0
}
end {}
#Requires -Version 5.1 <# .SYNOPSIS Checks if the BootConfig file was modified from last run. .DESCRIPTION Checks if the BootConfig file was modified from last run. On first run this will not produce an error, but will create a cache file for later comparison. .EXAMPLE No parameters needed. .OUTPUTS None .NOTES Minimum OS Architecture Supported: Windows 10, Windows Server 2016 Release Notes: Initial Release By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use. Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA). #> [CmdletBinding()] param ( # Path and file where the cache file will be saved for comparison [string] $CachePath = "C:ProgramDataNinjaRMMAgentscriptingTest-BootConfig.clixml" ) begin { function Test-IsElevated { $id = [System.Security.Principal.WindowsIdentity]::GetCurrent() $p = New-Object System.Security.Principal.WindowsPrincipal($id) $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) } } process { if (-not (Test-IsElevated)) { Write-Error -Message "Access Denied. Please run with Administrator privileges." exit 1 } # Get content and create hash of BootConfig file $BootConfigContent = bcdedit.exe /enum $Stream = [IO.MemoryStream]::new([byte[]][char[]]"$BootConfigContent") $BootConfigHash = Get-FileHash -InputStream $Stream -Algorithm SHA256 $Current = [PSCustomObject]@{ Content = $BootConfigContent Hash = $BootConfigHash } # Check if this is first run or not if ($(Test-Path -Path $CachePath)) { # Compare last content and hash $Cache = Import-Clixml -Path $CachePath $ContentDifference = Compare-Object -ReferenceObject $Cache.Content -DifferenceObject $Current.Content -CaseSensitive $HashDifference = $Cache.Hash -like $Current.Hash $Current | Export-Clixml -Path $CachePath -Force -Confirm:$false if (-not $HashDifference) { Write-Host "BootConfig file has changed since last run!" Write-Host "" $ContentDifference | ForEach-Object { if ($_.SideIndicator -like '=>') { Write-Host "Added: $($_.InputObject)" } elseif ($_.SideIndicator -like '<=') { Write-Host "Removed: $($_.InputObject)" } } exit 1 } } else { Write-Host "First run, saving comparison cache file." $Current | Export-Clixml -Path $CachePath -Force -Confirm:$false } exit 0 } end {}
#Requires -Version 5.1

<#
.SYNOPSIS
    Checks if the BootConfig file was modified from last run.
.DESCRIPTION
    Checks if the BootConfig file was modified from last run.
    On first run this will not produce an error, but will create a cache file for later comparison.
.EXAMPLE
    No parameters needed.
.OUTPUTS
    None
.NOTES
    Minimum OS Architecture Supported: Windows 10, Windows Server 2016
    Release Notes:
    Initial Release
By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. 
    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. 
    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. 
    Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. 
    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. 
    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. 
    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
#>

[CmdletBinding()]
param (
    # Path and file where the cache file will be saved for comparison
    [string]
    $CachePath = "C:ProgramDataNinjaRMMAgentscriptingTest-BootConfig.clixml"
)

begin {
    function Test-IsElevated {
        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $p = New-Object System.Security.Principal.WindowsPrincipal($id)
        $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
    }
}
process {
    if (-not (Test-IsElevated)) {
        Write-Error -Message "Access Denied. Please run with Administrator privileges."
        exit 1
    }

    # Get content and create hash of BootConfig file
    $BootConfigContent = bcdedit.exe /enum
    $Stream = [IO.MemoryStream]::new([byte[]][char[]]"$BootConfigContent")
    $BootConfigHash = Get-FileHash -InputStream $Stream -Algorithm SHA256

    $Current = [PSCustomObject]@{
        Content = $BootConfigContent
        Hash    = $BootConfigHash
    }

    # Check if this is first run or not
    if ($(Test-Path -Path $CachePath)) {
        # Compare last content and hash
        $Cache = Import-Clixml -Path $CachePath
        $ContentDifference = Compare-Object -ReferenceObject $Cache.Content -DifferenceObject $Current.Content -CaseSensitive
        $HashDifference = $Cache.Hash -like $Current.Hash
        $Current | Export-Clixml -Path $CachePath -Force -Confirm:$false
        if (-not $HashDifference) {
            Write-Host "BootConfig file has changed since last run!"
            Write-Host ""
            $ContentDifference | ForEach-Object {
                if ($_.SideIndicator -like '=>') {
                    Write-Host "Added: $($_.InputObject)"
                }
                elseif ($_.SideIndicator -like '<=') {
                    Write-Host "Removed: $($_.InputObject)"
                }
            }
            exit 1
        }
    }
    else {
        Write-Host "First run, saving comparison cache file."
        $Current | Export-Clixml -Path $CachePath -Force -Confirm:$false
    }
    exit 0
}
end {}

 

Få tillgång till detta skript och hundratals fler i NinjaOne Dojo

Få tillgång till

Detaljerad uppdelning

Det medföljande skriptet är ett PowerShell-skript som är noggrant utformat för att övervaka ändringar i BootConfig-filen sedan den senaste körningen. Här är en mer detaljerad genomgång steg för steg:

  1. Förkunskapskrav: Skriptet kräver PowerShell version 5.1.
  2. Kontroll av höjd: Innan skriptet dyker ner i sin huvudfunktion kontrollerar det om det körs med administratörsbehörighet, vilket är nödvändigt för att komma åt vissa systemfiler och kommandon.
  3. Hashning av BootConfig: Med hjälp av kommandot bcdedit.exe /enum hämtar skriptet innehållet i BootConfig. Därefter skapas en SHA256-hash av innehållet, som fungerar som en unik identifierare för det specifika innehållet.
  4. Jämförelse av cacheminne: Skriptet letar sedan efter en tidigare sparad cachefil. Om den finns jämför skriptet det aktuella BootConfig-innehållet och hashen med den cachade versionen.
  5. Utgång: Om skillnader upptäcks ger skriptet en detaljerad utskrift av ändringarna och anger vad som har lagts till eller tagits bort. Om det är första gången skriptet körs sparar det aktuella BootConfig-data för framtida jämförelser.

Potentiella användningsområden

  • Rutinmässiga kontroller av systemet: IT-proffs som Alex kan distribuera detta skript över företagsservrar för rutinkontroller, vilket säkerställer att inga obehöriga ändringar har skett.
  • Installation efter programvarans slut: Efter installation av ny programvara eller uppdateringar kan skriptet köras för att verifiera att BootConfig-filen förblir oförändrad, vilket säkerställer att programvaran inte har manipulerat viktiga startdata.
  • Ändringar i systemkonfigurationen: Före och efter betydande ändringar av systemkonfigurationen kan skriptet användas för att säkerställa att BootConfig-filens integritet förblir intakt.

Jämförelser

Även om det finns verktyg och programvara från tredje part som erbjuder systemövervakningsfunktioner, ligger fördelen med detta skript i dess enkelhet och specificitet. Den är lätt, enkel att distribuera och fokuserar enbart på BootConfig-filen, vilket garanterar en målinriktad och effektiv kontroll.

Vanliga frågor

  • Q: Kan detta skript köras på äldre versioner av Windows?
    Svar: Skriptet stöder Windows 10 och Windows Server 2016 och nyare.
  • Q: Vad händer om BootConfig-filen är oförändrad?
    Svar: Skriptet avslutas helt enkelt utan några varningar, vilket indikerar att inga förändringar har skett sedan den senaste körningen.

Konsekvenser för säkerheten

Att upptäcka ändringar i BootConfig-filen handlar inte bara om systemstabilitet utan även om säkerhet. Obehöriga ändringar kan tyda på ett potentiellt intrång eller skadlig aktivitet. Genom att övervaka den här filen kan IT-personal agera proaktivt mot potentiella hot.

Rekommendationer

  • Kör alltid skriptet med administratörsbehörighet för att säkerställa korrekta resultat.
  • Schemalägg skriptet så att det körs med jämna mellanrum för kontinuerlig övervakning.
  • Behåll säkerhetskopior av BootConfig-filen för att snabbt kunna återställa den om obehöriga ändringar görs.

Avslutande tankar

När det gäller IT-säkerhet och systemhantering erbjuder verktyg som NinjaOne heltäckande lösningar för olika utmaningar. Övervakning av kritiska systemfiler, t.ex. BootConfig-filen i Windows, är ett bevis på vikten av proaktiv systemhantering. Med skript som det som diskuteras kan IT-personal säkerställa systemintegritet, stärka säkerheten och upprätthålla driftseffektiviteten.

Här är några ytterligare resurser som du kan ha nytta av:

Next Steps

Building an efficient and effective IT team requires a centralized solution that acts as your core service delivery tool. NinjaOne enables IT teams to monitor, manage, secure, and support all their devices, wherever they are, without the need for complex on-premises infrastructure.

Learn more about NinjaOne Remote Script Deployment, book a demo, or start your free trial of the NinjaOne platform.

Kategorier:

Du kanske även gillar

×

Se NinjaOne i aktion!

Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält är dolt när formuläret visas
Detta fält används för valideringsändamål och ska lämnas oförändrat.

Genom att skicka detta formulär accepterar jag NinjaOne:s integritetspolicy.