Bekijk een demo×
×

Zie NinjaOne in actie!

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

PowerShell-script om Windows-firewallprofielen In of Uit te Schakelen

De beveiliging van netwerken en systemen is een fundament van IT. Een van de belangrijkste verdedigingsmechanismen in een Windows-omgeving is de Windows Firewall. Door de stroom van inkomend en uitgaand verkeer te controleren, fungeert het als een poortwachter. Dit artikel gaat in op een PowerShell-script dat een gestroomlijnde methode biedt om Windows Firewall-profielen in of uit te schakelen, een cruciale taak voor IT-professionals.

Achtergrond

Het gepresenteerde script is ontworpen om alle Windows Firewall profielen in of uit te schakelen, namelijk Domein, Openbaar en Privé. Deze profielen bepalen de instellingen en regels die worden toegepast op basis van het netwerktype waarmee een computer verbonden is. Voor Managed Service Providers (MSP’s) en IT-professionals is een tool waarmee snel tussen deze profielen kan worden geschakeld van onschatbare waarde. Of het nu gaat om probleemoplossing, beveiliging hardening of netwerkconfiguratie, dit script biedt een snelle oplossing.

Het Script

#Requires -Version 5.1

<#
.SYNOPSIS
    Enable or disable all Windows Firewall profiles(Domain, Public, Private).
.DESCRIPTION
    Enable or disable all Windows Firewall profiles(Domain, Public, Private).
.EXAMPLE
     -Disable
    Disables all Windows Firewall profiles(Domain, Public, Private).
.EXAMPLE
     -Enable
    Enables all Windows Firewall profiles(Domain, Public, Private).
.EXAMPLE
     -Enable -BlockAllInbound
    Enables all Windows Firewall profiles(Domain, Public, Private).
    Blocks all inbound traffic on the Domain, Public, Private profiles
.OUTPUTS
    String[]
.OUTPUTS
    PSCustomObject[]
.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).
.COMPONENT
    ProtocolSecurity
#>

[CmdletBinding(DefaultParameterSetName = "Enable")]
param (
    [Parameter(
        Mandatory = $true,
        ParameterSetName = "Enable"
    )]
    [Switch]
    $Enable,
    [Parameter(
        Mandatory = $true,
        ParameterSetName = "Disable"
    )]
    [Switch]
    $Disable,
    [Parameter(
        ParameterSetName = "Enable"
    )]
    [Switch]
    $BlockAllInbound
)

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
    }
    if ($(Get-Command "Get-NetFirewallProfile" -ErrorAction SilentlyContinue).Name -like "Get-NetFirewallProfile") {
        # Use Get-NetFirewallProfile if available
        try {
            $NetFirewallSplat = @{
                Profile     = @("Domain", "Public", "Private")
                Enabled     = $(if ($Enable) { "True" }elseif ($Disable) { "False" })
                ErrorAction = "Stop"
            }
            if ($Enable -and $BlockAllInbound) {
                $NetFirewallSplat.Add('DefaultInboundAction', 'Block')
                $NetFirewallSplat.Add('DefaultOutboundAction', 'Allow')
            }
            Set-NetFirewallProfile @NetFirewallSplat
            
        }
        catch {
            Write-Error $_
            Write-Host "Failed to turn $(if ($Enable) { "on" }elseif ($Disable) { "off" }) the firewall."
            exit 1
        }
        # Proof of work
        Get-NetFirewallProfile -ErrorAction Stop | Format-Table Name, Enabled        
    }
    else {
        # Fall back onto netsh
        netsh.exe AdvFirewall set AllProfiles state $(if ($Enable) { "on" }elseif ($Disable) { "off" })
        if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }
        netsh.exe AdvFirewall set DomainProfile state $(if ($Enable) { "on" }elseif ($Disable) { "off" })
        if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }
        netsh.exe AdvFirewall set PrivateProfile state $(if ($Enable) { "on" }elseif ($Disable) { "off" })
        if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }
        netsh.exe AdvFirewall set PublicProfile state $(if ($Enable) { "on" }elseif ($Disable) { "off" })
        if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }
        
        if ($Enable -and $BlockAllInbound) {
            try {
                netsh.exe AdvFirewall set DomainProfile FirewallPolicy "BlockInbound,AllowOutbound"
                if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }
                netsh.exe AdvFirewall set PrivateProfile FirewallPolicy "BlockInbound,AllowOutbound"
                if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }
                netsh.exe AdvFirewall set PublicProfile FirewallPolicy "BlockInbound,AllowOutbound"
                if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }
            }
            catch {
                Write-Error $_
                Write-Host "Could not set Block All Inbound Traffic to 1"
            }
        }
        # Proof of work
        netsh.exe AdvFirewall show AllProfiles state
        if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }
    }
}
end {}

 

Toegang tot 300+ scripts in de NinjaOne Dojo

Toegang Krijgen

Gedetailleerd overzicht

In de kern controleert het script op beheerdersrechten, wat essentieel is omdat het wijzigen van firewallinstellingen verhoogde rechten vereist. Vervolgens wordt de aanwezigheid van het cmdlet Get-NetFirewallProfile gecontroleerd, een modern PowerShell-commando voor het beheren van firewallprofielen.

Als dit cmdlet beschikbaar is, gebruikt het script het om de opgegeven profielen in of uit te schakelen. Een optie om al het inkomende verkeer te blokkeren, terwijl het uitgaande verkeer wel wordt toegestaan, voegt een extra beveiligingslaag toe.

Als het cmdlet Get-NetFirewallProfile ontbreekt, keert het script terug naar het oudere opdrachtregelprogramma netsh.exe.

Potentiële Gebruikssituaties

Neem een IT-professional, Jane, bij een groot bedrijf. Ze introduceren een nieuwe applicatie, maar tijdens het testen merken ze dat de applicatie niet kan communiceren met de server. Jane vermoedt een probleem met de firewall en gebruikt dit script om de firewallprofielen tijdelijk uit te schakelen, de applicatie te testen en ze vervolgens direct weer in te schakelen. Deze snelle actie helpt bij het diagnosticeren van het probleem zonder handmatige navigatie.

Vergelijkingen

Het script biedt een programmatische aanpak voor het beheren van firewallprofielen. Alternatieven zijn onder andere het handmatig aanpassen via de Windows Firewall GUI of het gebruik van Groepsbeleidobjecten (GPO’s) voor machines met domeinsamenstelling. Beide missen echter de directheid van dit script.

FAQs

  • Kan ik dit script op elke Windows-machine uitvoeren?
    Het is ontworpen voor Windows 10 en Windows Server 2016 en hoger.
  • Heb ik speciale rechten nodig om dit script uit te voeren?
    Ja, beheerdersrechten zijn nodig.

Implicaties voor de veiligheid

De mogelijkheid om snel te wisselen tussen firewallprofielen is een tweesnijdend zwaard. Het uitschakelen ervan, zelfs tijdelijk, kan systemen blootstellen aan bedreigingen. Het is van vitaal belang om de gevolgen voor de beveiliging te begrijpen en ervoor te zorgen dat systemen beschermd blijven.

Aanbevelingen

  • Test het script eerst in een gecontroleerde omgeving.
  • Als je de firewall uitschakelt voor diagnostiek, schakel deze dan direct daarna weer in.
  • Controleer regelmatig de firewallregels om ervoor te zorgen dat deze overeenkomen met het beveiligingsbeleid.

Slotbeschouwingen:

Het beheren van Windows Firewall-profielen is essentieel voor netwerk- en systeembeveiliging. Hoewel tools zoals NinjaOne allesomvattende IT-beheeroplossingen bieden, zijn scripts zoals het besproken script van onschatbare waarde voor specifieke taken. Zoals altijd zorgt een goed begrip van de werking en implicaties voor een effectief en veilig gebruik.

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).