Bekijk een demo×
×

Zie NinjaOne in actie!

Door dit formulier in te dienen geef ik aan akkoord te gaan met het privacybeleid van NinjaOne.

Hoe Controleren op een Gewijzigd Hosts-bestand met PowerShell

In de uitgebreide wereld van IT staat het hosts-bestand als een stille schildwacht die zorgt voor een soepele netwerkcommunicatie. Voor degenen die dit niet weten: het hosts-bestand is een tekstbestand dat hostnamen koppelt aan IP-adressen. Het wordt door het besturingssysteem gebruikt om hostnamen om te zetten naar IP-adressen wanneer een computer verbinding probeert te maken met een netwerkbron. Maar wat gebeurt er als er met het hosts-bestand wordt geknoeid of het wordt gewijzigd? Hoe kunnen IT-professionals en Managed Service Providers (MSP’s) de integriteit van dit cruciale bestand waarborgen? Voer het script in waar we ons nu in gaan verdiepen.

Het Hosts-bestand Begrijpen

Het hosts-bestand is in essentie het adresboek van het computersysteem. Het speelt een cruciale rol bij het sturen van netwerkverkeer en zorgt ervoor dat gebruikers op de juiste websites en services terechtkomen. Het belang ervan maakt het echter ook een doelwit bij uitstek voor kwaadwillende actoren.

De Potentiële Risico’s van Ongeoorloofde Wijzigingen

Een aanvaller met snode bedoelingen kan het hosts-bestand wijzigen om verkeer om te leiden naar een kwaadaardige website of server. Dergelijke wijzigingen kunnen ernstige gevolgen hebben:

  • Persoonlijke Informatie Stelen: Door gebruikers om te leiden naar vervalste sites, kunnen aanvallers naar persoonlijke gegevens vragen, wat leidt tot identiteitsdiefstal.
  • Installatie van Malware: Gebruikers kunnen worden misleid om kwaadaardige software te downloaden, terwijl ze denken dat ze op een legitieme site zijn.
  • Verstoring van Netwerkcommunicatie: Essentiële diensten kunnen worden geblokkeerd, met operationele onderbrekingen tot gevolg.

Gezien deze risico’s is het voor IT-professionals en MSP’s van het grootste belang om tools te hebben waarmee ongeautoriseerde wijzigingen snel kunnen worden gedetecteerd. Dit is waar ons script om de hoek komt kijken.

Het Script

#Requires -Version 5.1

<#
.SYNOPSIS
    Checks if the hosts file was modified from last run.
.DESCRIPTION
    Checks if the hosts 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 of the hosts file
    [string]
    $HostsPath = "C:WindowsSystem32driversetchosts",
    # Path and file where the cache file will be saved for comparison
    [string]
    $CachePath = "C:ProgramDataNinjaRMMAgentscriptingTest-HostsFile.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
    }

    # Check if hosts file exists
    if ($(Test-Path -Path $HostsPath)) {
        # Get content and create hash of hosts file
        $HostsContent = Get-Content -Path $HostsPath
        $HostsHash = Get-FileHash -Path $HostsPath -Algorithm SHA256

        $Current = [PSCustomObject]@{
            Content = $HostsContent
            Hash    = $HostsHash
        }

        # 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 "Hosts 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
        }
    }
    else {
        Write-Error "Hosts file is missing!"
        exit 1
    }
    exit 0
}
end {}

 

Toegang tot 300+ scripts in de NinjaOne Dojo

Toegang Krijgen

Een Deep Dive in het Script

Het meegeleverde PowerShell-script is ontworpen om te controleren of het hosts-bestand is gewijzigd sinds de laatste keer dat het is uitgevoerd. Hier volgt een overzicht van de functionaliteit:

  1. Hoogtecontrole: Het script controleert eerst of het wordt uitgevoerd met beheerdersrechten. Dit is cruciaal omdat elke poging om systeembestanden te lezen of te wijzigen verhoogde rechten vereist.
  2. Hosts Bestandsverificatie: Vervolgens wordt het bestaan van het hosts-bestand gecontroleerd. Als het bestand ontbreekt, geeft het script een foutmelding.
  3. Hashvergelijking: Het script berekent een hash (SHA256) van het huidige hosts-bestand en vergelijkt deze met een versie uit de cache van de laatste run. Als er een mismatch is, geeft dit aan dat het bestand is gewijzigd.
  4. Inhoud Vergelijken: Het script controleert niet alleen de hash, maar vergelijkt ook de inhoud regel voor regel en markeert eventuele toevoegingen of verwijderingen.

Voordelen voor IT-professionals en MSP’s

  • Proactieve Monitoring: Dit script biedt een proactieve benadering om het hosts-bestand te controleren, de integriteit ervan te waarborgen en beheerders te waarschuwen bij ongeautoriseerde wijzigingen.
  • Gedetailleerd Inzicht: Door inhoudsverschillen te vergelijken, kunnen IT-teams snel vaststellen wat er is toegevoegd of verwijderd, wat helpt bij een snelle oplossing.
  • Klaar voor Automatisering: Gezien het ontwerp kan het script worden geïntegreerd in geautomatiseerde workflows, waardoor regelmatige controles zonder handmatige tussenkomst mogelijk zijn.

De kracht van NinjaOne

NinjaOne is meer dan alleen een IT-beheeroplossing. Het is een uitgebreid platform waarmee IT-professionals en MSP’s potentiële bedreigingen voor kunnen blijven. Door ons hosts file check script te integreren in NinjaOne:

  • Gecentraliseerde Waarschuwingen: Ontvang direct waarschuwingen op het NinjaOne-dashboard wanneer het hosts-bestand wordt gewijzigd. Deze onmiddellijke melding zorgt ervoor dat u snel kunt handelen en uw systemen kunt beschermen tegen mogelijke aanvallen.
  • Geplande Controles: Automatiseer het script om het op gespecificeerde intervallen uit te voeren, zodat het continu wordt bewaakt.
  • Gedetailleerde Rapportage: Combineer de inzichten uit het script met de rapportagemogelijkheden van NinjaOne voor een holistisch beeld van uw IT-omgeving.

Tot slot is het hosts bestand, hoewel het vaak over het hoofd wordt gezien, een hoeksteen van netwerkcommunicatie. Het waarborgen van de integriteit is van het grootste belang. Ons script, vooral in combinatie met de kracht van NinjaOne, biedt IT-teams de tools die ze nodig hebben om ongeautoriseerde wijzigingen te bewaken, op te sporen en tegen te gaan, zodat een veilige en probleemloze IT-omgeving gegarandeerd is.

Volgende stappen

Het opbouwen van een efficiënt en effectief IT-team vereist een gecentraliseerde oplossing die fungeert als uw kerndienstleveringstool. NinjaOne stelt IT-teams in staat om al hun apparaten te monitoren, beheren, beveiligen en ondersteunen, waar ze ook zijn, zonder de noodzaak van complexe on-premises infrastructuur.

Leer meer over NinjaOne Endpoint Management, bekijk een live rondleiding, of start uw gratis trial van het NinjaOne-platform

Categorieën:

Dit vindt u misschien ook leuk

NinjaOne Terms & Conditions

By clicking the “I Accept” button below, you indicate your acceptance of the following legal terms as well as our 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 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).