Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <# https://pastebin.com/kGhTXXA8
- This script will add a network firewall rule which will block inbound and outbound traffic for either one or multiple applications.
- You can call this script with the parameter $InputObject or just drag and drop the application(s) and/or folder(s) onto the script.
- Note:
- - You need administration rights
- - Get-ChildItem is recursive
- - For drag and drop you need to change/add these registry values (https://stackoverflow.com/a/14051969/9248774):
- [HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\ShellEx\DropHandler]
- @="{60254CA5-953B-11CF-8C96-00AA00B8708C}"
- [HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command]
- @="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoLogo -ExecutionPolicy Unrestricted -File \"%1\" %*"
- ConnectionBlocker.ps1 [-InputObject] <String[]>
- ConnectionBlocker.ps1 -InputObject 'C:\folder\application.exe'
- ConnectionBlocker.ps1 -InputObject ('.\application1.exe', '.\application2.exe', '.\folder')
- #>
- Param (
- [Parameter(ValueFromRemainingArguments=$true)]
- [string[]] $InputObject
- )
- $Host.UI.RawUI.WindowTitle = 'Connection Blocker'
- if (!(New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
- $InputObject | ForEach-Object {
- [string]$ArgString += "'$_', "
- }
- Start-Process PowerShell -ArgumentList ('-NoExit & {0} -InputObject ' -f $PSCommandPath + $ArgString.TrimEnd(', ')) -Verb RunAs
- Stop-Process -Id $PID
- }
- if ($InputObject.Count -gt 1) {
- $InputObject | ForEach-Object {
- if ((Get-Item $_) -is [System.IO.DirectoryInfo]) {
- Get-ChildItem $_ -File -Recurse -Include *.exe | ForEach-Object {
- New-NetFirewallRule -DisplayName ($_.Name + ' (Blocked by script)') -Direction Inbound -Program $_ -Action Block >$null
- New-NetFirewallRule -DisplayName ($_.Name + ' (Blocked by script)') -Direction Outbound -Program $_ -Action Block >$null
- Write-Host "Blocked inbound and outbound traffic for '$_'!"
- }
- } elseif ((Get-Item $_) -is [System.IO.FileInfo]) {
- New-NetFirewallRule -DisplayName ($_.Name + ' (Blocked by script)') -Direction Inbound -Program $_ -Action Block >$null
- New-NetFirewallRule -DisplayName ($_.Name + ' (Blocked by script)') -Direction Outbound -Program $_ -Action Block >$null
- Write-Host "Blocked inbound and outbound traffic for '$_'!"
- }
- }
- } elseif ((Get-Item ($InputObject -as [string])) -is [System.IO.DirectoryInfo]) {
- Get-ChildItem ($InputObject -as [string]) -File -Recurse -Include *.exe | ForEach-Object {
- New-NetFirewallRule -DisplayName ($_.Name + ' (Blocked by script)') -Direction Inbound -Program $_ -Action Block >$null
- New-NetFirewallRule -DisplayName ($_.Name + ' (Blocked by script)') -Direction Outbound -Program $_ -Action Block >$null
- Write-Host "Blocked inbound and outbound traffic for '$_'!"
- }
- } elseif ((Get-Item ($InputObject -as [string])) -is [System.IO.FileInfo]) {
- New-NetFirewallRule -DisplayName ((Get-Item ($InputObject -as [string])).Name + ' (Blocked by script)') -Direction Inbound -Program ($InputObject -as [string]) -Action Block >$null
- New-NetFirewallRule -DisplayName ((Get-Item ($InputObject -as [string])).Name + ' (Blocked by script)') -Direction Outbound -Program ($InputObject -as [string]) -Action Block >$null
- Write-Host "Blocked inbound and outbound traffic for '$InputObject'!"
- }
- Write-Host -ForegroundColor Green "`nDone!"
- Start-Sleep 3
- Stop-Process -Id $PID
Add Comment
Please, Sign In to add comment