1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
param ( [Parameter(Mandatory=$true)][string]$ServiceName, [Parameter(Mandatory=$true)][string]$IP = "192.168.0.5", [Parameter(Mandatory=$true)][string]$PortRange = "9999-9999" #[string]$password = $( Read-Host "Input password, please" ) ) #Make sure your port range is not large like 4000-7000 #[string]$ServiceName="MyServiceName" #[string]$IP = "192.168.0.5" #[string]$PortRange = "9999-9999" $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession $Bytes = [System.Text.Encoding]::ASCII.GetBytes("USERNAME:PASSWORD") $Creds = [Convert]::ToBase64String($Bytes) $session.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36" try { $Response = Invoke-WebRequest -UseBasicParsing -Uri "http://192.168.0.1/setup.cgi?todo=nbtscan&next_file=FW_forward_service.htm" ` -WebSession $session ` -Headers @{ "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" "Accept-Encoding"="gzip, deflate" "Accept-Language"="en-US,en;q=0.9" "Authorization"="Basic $($Creds)" "Referer"="http://192.168.0.1/FW_forward.htm&todo=cfg_init" "Upgrade-Insecure-Requests"="1" } } catch { } $Response = Invoke-WebRequest -UseBasicParsing -Uri "http://192.168.0.1/setup.cgi?todo=nbtscan&next_file=FW_forward_service.htm" ` -WebSession $session ` -Headers @{ "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" "Accept-Encoding"="gzip, deflate" "Accept-Language"="en-US,en;q=0.9" "Authorization"="Basic $($Creds)" "Referer"="http://192.168.0.1/FW_forward.htm&todo=cfg_init" "Upgrade-Insecure-Requests"="1" } $URLID = $Response.Content | Select-String -Pattern "[0-9a-f]{16}" -AllMatches | % { $_.Matches.Value } #Request to add port $Body = "portname=$($ServiceName)&srvtype=BOTH&ex_port_range=$($PortRange)&same_range=use_same_port&in_port_range=$($PortRange)&server_ip1=192&server_ip2=168&server_ip3=0&server_ip4=$($IP.Split('.')[-1])&todo=pfw_add&h_pfw_name=$($ServiceName)&c4_pfw_ip=$($IP)&h_pfw_proto=0&h_pfw_ex_range=$($PortRange)&h_pfw_in_range=$($PortRange)&h_ruleSelect=0&ruleSelect=&edit=0&lanIP=192.168.0.1&h_srvtype=&c4_server_ip=&this_file=FW_forward_service.htm&next_file=FW_forward.htm&SID=" $URL = "http://192.168.0.1/setup.cgi?id=$($URLID)" $Response = Invoke-WebRequest -UseBasicParsing -Uri $URL ` -Method "POST" ` -WebSession $session ` -Headers @{ "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" "Accept-Encoding"="gzip, deflate" "Accept-Language"="en-US,en;q=0.9" "Authorization"="Basic $($Creds)" "Cache-Control"="max-age=0" "Origin"="http://192.168.0.1" "Referer"="http://192.168.0.1/setup.cgi?todo=nbtscan&next_file=FW_forward_service.htm" "Upgrade-Insecure-Requests"="1" } ` -ContentType "application/x-www-form-urlencoded" ` -Body $Body Start-Sleep -s 30 Write-Host "Removing" #Start to remove try { #Authenticate $Response = Invoke-WebRequest -UseBasicParsing -Uri "http://192.168.0.1/FW_forward.htm&todo=cfg_init" ` -WebSession $session ` -Headers @{ "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" "Accept-Encoding"="gzip, deflate" "Accept-Language"="en-US,en;q=0.9" "Authorization"="Basic $($Creds)" "Cache-Control"="max-age=0" "Referer"="http://192.168.0.1/adv_index.htm" "Upgrade-Insecure-Requests"="1" } } catch { } #Get Table Data $Response = Invoke-WebRequest -UseBasicParsing -Uri "http://192.168.0.1/FW_forward.htm&todo=cfg_init" ` -WebSession $session ` -Headers @{ "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" "Accept-Encoding"="gzip, deflate" "Accept-Language"="en-US,en;q=0.9" "Authorization"="Basic $($Creds)" "Referer"="http://192.168.0.1/adv_index.htm" "Upgrade-Insecure-Requests"="1" } $HTML = New-Object -Com "HTMLFile" [string]$htmlBody = $Response.Content $HTML.write([ref]$htmlBody) $HowManyRows = ($HTML.getElementById("PortForwarding").children[0].outerText.toString() | Measure-Object -Line).Lines - 1 write-host "Port forward Entries found: " $HowManyRows $FoundServiceID = 0 for ($i=1; $i -ile $HowManyRows; $i++) { #$HTML.getElementById("PortForwarding").children[0].children[$i].children[1].children[0].innerHTML() #$HTML.getElementById("PortForwarding").children[0].children[$i].children[2].children[0].innerHTML() #$HTML.getElementById("PortForwarding").children[0].children[$i].children[5].children[0].innerHTML() if ($HTML.getElementById("PortForwarding").children[0].children[$i].children[2].children[0].innerHTML() -eq $ServiceName) { $FoundServiceID = $i write-host "Found service at: " $i } } try { #Auth to remove $Response = Invoke-WebRequest -UseBasicParsing -Uri "http://192.168.0.1/setup.cgi?todo=nbtscan&next_file=FW_forward_service.htm" ` -WebSession $session ` -Headers @{ "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" "Accept-Encoding"="gzip, deflate" "Accept-Language"="en-US,en;q=0.9" "Authorization"="Basic $($Creds)" "Referer"="http://192.168.0.1/FW_forward.htm&todo=cfg_init" "Upgrade-Insecure-Requests"="1" } } catch { } #GetToken $URLID = $Response.Content | Select-String -Pattern "[0-9a-f]{16}" -AllMatches | % { $_.Matches.Value } $URL = "http://192.168.0.1/setup.cgi?id=$($URLID)" $DeleteBodyRequest = "serv_type=pf&svs_gm=FTP&SV_IP1=192&SV_IP2=168&SV_IP3=0&SV_IP4=&RouteSelect=$($FoundServiceID)&lanIP=192.168.0.1&h_pfw_name=&c4_pfw_ip=&h_pfw_proto=&h_pfw_ex_range=&h_pfw_in_range=&h_ruleSelect=$($FoundServiceID)&ruleSelect=$($FoundServiceID)&edit=&todo=delete&this_file=FW_forward.htm&next_file=FW_forward.htm&SID=" #Remove $Response = Invoke-WebRequest -UseBasicParsing -Uri $URL ` -Method "POST" ` -WebSession $session ` -Headers @{ "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" "Accept-Encoding"="gzip, deflate" "Accept-Language"="en-US,en;q=0.9" "Authorization"="Basic $($Creds)" "Cache-Control"="max-age=0" "Origin"="http://192.168.0.1" "Referer"="http://192.168.0.1/setup.cgi?id=$($URLID)" "Upgrade-Insecure-Requests"="1" } ` -ContentType "application/x-www-form-urlencoded" ` -Body $DeleteBodyRequest |