-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-FindInWebContent.ps1
More file actions
102 lines (102 loc) · 6.46 KB
/
Invoke-FindInWebContent.ps1
File metadata and controls
102 lines (102 loc) · 6.46 KB
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
. "$PSScriptRoot\Get-FirstRegexGroupValue.ps1"
function Global:Invoke-FindInWebContent {
param(
[System.Xml.XmlElement]$AppXmlInfo,
$Uri,
$Patterns,
[switch]$ReturnUri
)
begin {
if ([Net.ServicePointManager]::SecurityProtocol -ne [Net.SecurityProtocolType]::Tls12) {
Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 1 -Value "Activating TLS 1.2"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
}
}
process {
if ($AppXmlInfo) {
$CurrentUri = $AppXmlInfo.Uri
$_Patterns = $AppXmlInfo.Patterns.Pattern
}
else {
$CurrentUri = $Uri
$_Patterns = $Patterns
}
$_Patterns | ForEach-Object -begin { $i = 1 } {
#Write-Debug $_
#Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 1 -Value "Find '$([System.Net.WebUtility]::UrlDecode($_))' in data fetched from '$CurrentUri'"
#Invoke-FindInWebContent -Uri $CurrentUri -Pattern ([System.Net.WebUtility]::UrlDecode($_))
#Write-Information $CurrentUri
try {
Write-Debug $CurrentUri
$Request = Invoke-WebRequest -SessionVariable VCSessionVariable -ErrorAction Stop -Uri $CurrentUri -UseBasicParsing
} catch {
try {
Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 1 -Value "Switching to TLS 1.1 and trying again"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls11
$Request = Invoke-WebRequest -SessionVariable VCSessionVariable -ErrorAction Stop -Uri $CurrentUri -UseBasicParsing
} catch {
Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 3 "Error: $_"
}
if (-not $Request) {
Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 3 -Value "Failed request: $_"
}
}
#Write-Information $Request.Content
if ($Request.StatusCode -eq 200) {
#Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 1 -Value "Request for '$($CurrentUri)' was successful"
#Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 1 -Value $Request.Content
$MatchedContent = ([string]::new($Request.Content)) | Get-FirstRegexGroupValue -Pattern ([System.Net.WebUtility]::UrlDecode($_))
Write-Debug "$MatchedContent"
if (-not [string]::IsNullOrEmpty($MatchedContent)) {
#Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 1 -Value "Debug:'$MatchedContent'"
if ($ReturnUri.IsPresent -eq $true -and $MatchedContent -notmatch '^http') {
$BaseUri = "$($Request.BaseResponse.RequestMessage.RequestUri.Scheme)://$($Request.BaseResponse.RequestMessage.RequestUri.Host)"
#if ($Request.BaseResponse.RequestMessage.RequestUri.PathAndQuery -notcontains $MatchedContent) {
# $BaseUri += $Request.BaseResponse.RequestMessage.RequestUri.AbsolutePath
#}
if (-not (Split-Path -Path $MatchedContent -Parent)) {
Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 1 -Value "ABSOLUTEPATH $($BaseUri + $Request.BaseResponse.RequestMessage.RequestUri.AbsolutePath)"
$BaseUri += $Request.BaseResponse.RequestMessage.RequestUri.AbsolutePath
}
if ($MatchedContent.SubString(0,1) -ne '/' -and ($BaseUri.LastIndexOf('/')+1) -ne $BaseUri.Length) {
Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 1 -Value "No SLASH $($BaseUri + $MatchedContent)"
$CurrentUri = "$($BaseUri)/$MatchedContent"
}
else {
if ($MatchedContent.SubString(0,2) -eq '//') {
Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 1 -Value "CurrentURI: $CurrentURI"
$CurrentUri = $MatchedContent -replace '//',"$($Request.BaseResponse.RequestMessage.RequestUri.Scheme)://"
Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 1 -Value "CurrentURI: $CurrentURI"
}
else {
Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 1 -Value "CurrentUri = $BaseUri $MatchedContent"
$CurrentUri = "$($BaseUri)$MatchedContent"
}
}
#Write-LogEntry -EntryType CMLogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 3 -Value "$i / $($_Patterns.Count)"
if ($i -eq ($_Patterns.Count)) {
$return = ($CurrentUri -replace '[^https:]\/\/','/')
Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 1 -Value "No more patterns, will return $return"
return $return
}
}
else {
if ($i -eq ($_Patterns.Count)) {
return $MatchedContent
}
else {
$CurrentUri = $MatchedContent
}
}
}
else {
Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 1 -Value "Debug: $($Request.Content)"
}
}
else {
Write-LogEntry -Component $MyInvocation.MyCommand -FileName $Global:LogFileName -Severity 3 -Value "Request for '$($CurrentUri)' failed with status $($Request.StatusCode)"
}
$i++
}
}
}