Public/Network/Get-WebPortalHTMLCode.ps1
|
FUNCTION Get-WebPortalHTMLCode { [CmdletBinding()] PARAM ( [Parameter(Mandatory)] $IP, [ValidateSet('HTTP', 'HTTPS')] $Protocol = "HTTPS", $Port = 443, $TimeoutSeconds = 5 ) $Matchfound = $False $NewURL = $null $NewPort = $null $NewPage = $null $SinglePage = $null $RegexChecks = @('(?i)content\s*=\s*["'']?\d+;\s*url\s*=\s*''([^"''>\s]+)', '(?i)top\.document\.location.href\s*=\s*["'']([^"''>\s]+)', '(?i)\<script\>window.onload=function\(\){ url\s*=\s*["'']([^"''>\s]+)', '(?i)http-equiv=Refresh content="0; URL=([^"]+)', '(?i)onload="location.replace\({*["'']([^"''>\s]+)', '(?i)onload="init\({*["'']([^"''>\s]+)', '(?i)window.location ={*["'']([^"''>\s]+)' '(?i)path.indexOf\({*["'']([^"''>\s]+)', '(?i)window.location.href = {*["'']([^"''>\s]+)') IF ($PSVersionTable.PSVersion.Major -lt 7) { Write-Warning "This PowerShell Command requires PowerShell 7 to run properly."; RETURN } $PageTotal = $null TRY { $SinglePage = (Invoke-WebRequest -Uri "$($Protocol)://$($IP):$Port" -UseBasicParsing -TimeoutSec $TimeoutSeconds -ErrorAction SilentlyContinue -SkipCertificateCheck).RawContent } CATCH { } IF ($SinglePage -eq $null) { Return $null } $PageTotal += $SinglePage $Count = 0 WHILE ($true) { $Count++ $Matchfound = $False FOREACH ($Check in $RegexChecks) { Write-Verbose "Checking for Match $Check" IF ($SinglePage -match $Check) { $NewURL = $matches[1] ### Remove Leading '/' IF ($NewURL.Substring(0,1) -eq "/") { $NewURL = $NewURL.Substring(1,$NewURL.Length-1) } ### Get New Port If there is one. # IF ($NewURL -match '(?i)https?:\/\/[^:\/]+:(\d+)') { $NewPort = $Matches[1] } ELSE { $NewPort = $Port } $NewPage = $null TRY { $NewPage += (Invoke-WebRequest -Uri "$($Protocol)://$($IP):$Port/$NewURL" -UseBasicParsing -TimeoutSec $TimeoutSeconds -ErrorAction SilentlyContinue -SkipCertificateCheck).RawContent } CATCH { } IF ($NewPage -ne $null) { $PageTotal += $NewPage $SinglePage = $NewPage $Matchfound = $True break } } } IF ($Matchfound -eq $False) { break } IF ($Count -ge 4) { break } } RETURN $PageTotal } |