Skip to content

Instantly share code, notes, and snippets.

@richard24se
Created August 14, 2020 03:35
Show Gist options
  • Save richard24se/6c3483a6a16e904a37f7efc47c8b9101 to your computer and use it in GitHub Desktop.
Save richard24se/6c3483a6a16e904a37f7efc47c8b9101 to your computer and use it in GitHub Desktop.
#Elevated Shell
param([switch]$Elevated)
function Check-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Check-Admin) -eq $false) {
if ($elevated) {
# could not elevate, quit
} else {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
$remoteport = wsl.exe /bin/ash -c "ifconfig eth0 | grep 'inet '"
Write-Host $remoteport
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
if( $found ){
$remoteport = $matches[0];
} else{
echo "The Script Exited, the ip address of WSL 2 cannot be found";
exit;
}
#[Ports]
#All the ports you want to forward separated by coma
$ports=@(9000);
#[Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr='0.0.0.0';
$ports_a = $ports -join ",";
#remove blank output
$iex_supress
#Remove Firewall Exception Rules
$iex_supress = iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
#adding Exception Rules for inbound and outbound Rules
$iex_supress = iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
$iex_supress = iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";
for( $i = 0; $i -lt $ports.length; $i++ ){
$port = $ports[$i];
Write-Host $port
$iex_supress = iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
$iex_supress = iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment