Skip to content

Instantly share code, notes, and snippets.

@deoxykev
Created February 24, 2021 21:53
Show Gist options
  • Save deoxykev/7a48b566ea0eb72d2ecb302fb02fd044 to your computer and use it in GitHub Desktop.
Save deoxykev/7a48b566ea0eb72d2ecb302fb02fd044 to your computer and use it in GitHub Desktop.
#Requires -RunAsAdministrator
# Creates split VPN tunnel using routing rules
# To find this, run: Get-NetAdapter | select InterfaceDescription
# And paste the one that corresponds to your VPN client
$VPNInterfaceDescription = "PANGP Virtual Ethernet Adapter"
# This is the subnet you want to route to the VPN; all other traffic will be through local connection
$SplitVPNSubnet = "10.2.0.0/16"
$VPNInterfaceIndex = (
Get-NetAdapter -ErrorAction Stop | ? {
$_.InterfaceDescription -eq $VPNInterfaceDescription
}).ifIndex
try {
$VPNGatewayIPv4 = [String](
Get-NetIPConfiguration -ErrorAction Stop -InterfaceIndex $VPNInterfaceIndex
).IPv4Address
} catch {
Write-Host "[-] Could not find VPN default gateway address, is VPN on?"
exit 0
}
try {
$SNIArgs = @{
InterfaceIndex = $VPNInterfaceIndex
InterfaceMetric = 51
}
Set-NetIPInterface @SNIArgs
$SNArgs = @{
DestinationPrefix = "0.0.0.0/0"
RouteMetric = 100
InterfaceIndex = $VPNInterfaceIndex
ErrorAction = "Ignore"
}
if (! $(Get-NetRoute @SNArgs)){
Set-NetRoute @SNArgs
} else {
Write-Host "skipping set netroute"
}
$NNArgs = @{
DestinationPrefix = $SplitVPNSubnet
RouteMetric = 50
InterfaceIndex = $VPNInterfaceIndex
NextHop = $VPNGatewayIPv4
ErrorAction = "Ignore"
}
if (! $(Get-NetRoute @NNArgs)){
New-NetRoute @NNArgs
} else {
Write-Host "skipping new netroute"
}
} catch {
Write-host "[-] Could not create split VPN tunnel, are you administrator?"
exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment