This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSHeaderChecker.ps1
186 lines (152 loc) · 6.89 KB
/
PSHeaderChecker.ps1
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]
$Uri
)
$headerSts = "Strict-Transport-Security"
$headerFrameOpt = "X-Frame-Options"
$headerXssProt = "X-XSS-Protection"
$headerContentType = "X-Content-Type-Options"
$headerCsp = "Content-Security-Policy"
$headerRefPol = "Referrer-Policy"
$headerFeaturePol = "Feature-Policy"
try {
$req = Invoke-WebRequest -Uri $Uri
$requestHeaders = $req.Headers
<# $headerAnalysis = [PSCustomObject]@{
"$headerSts" = $null
"$headerFrameOpt" = $null
"$headerXssProt" = $null
"$headerContentType" = $null
"$headerCsp" = $null
"$headerRefPol" = $null
} #>
$headerAnalysis = @()
#region Strict-Transport-Security
$headerStsObj = New-Object -TypeName psobject
Add-Member -InputObject $headerStsObj -MemberType NoteProperty -Name "HTTP Header" -Value $headerSts
Add-Member -InputObject $headerStsObj -MemberType NoteProperty -Name "Value" -Value ""
Add-Member -InputObject $headerStsObj -MemberType NoteProperty -Name "Result" -Value ""
Add-Member -InputObject $headerStsObj -MemberType NoteProperty -Name "Recommendation" -Value ""
$headerStsValue = $requestHeaders[$headerSts]
if($null -ne $headerStsValue) {
$headerStsObj.Value = $headerStsValue
if($headerStsValue -ne "max-age=31536000; includeSubDomains") {
$headerStsObj.Result = "ISSUE"
$headerStsObj.Recommendation = "Should be set to `"max-age=31536000; includeSubDomains`""
}
else {
$headerStsObj.Result = "OK"
}
}
else {
$headerStsObj.Result = "MISSING"
$headerStsObj.Recommendation = "Should be added and set to `"max-age=31536000; includeSubDomains`""
}
$headerAnalysis += $headerStsObj
#endregion
#region X-Frame-Options
$headerFrameOptObj = New-Object -TypeName psobject
Add-Member -InputObject $headerFrameOptObj -MemberType NoteProperty -Name "HTTP Header" -Value $headerFrameOpt
Add-Member -InputObject $headerFrameOptObj -MemberType NoteProperty -Name "Value" -Value ""
Add-Member -InputObject $headerFrameOptObj -MemberType NoteProperty -Name "Result" -Value ""
Add-Member -InputObject $headerFrameOptObj -MemberType NoteProperty -Name "Recommendation" -Value ""
$headerFrameOptValue = $requestHeaders[$headerFrameOpt]
if($null -ne $headerFrameOptValue) {
$headerFrameOptObj.Value = $headerFrameOptValue
if(($headerFrameOptValue -ne "SAMEORIGIN") -and ($headerFrameOptValue -ne "DENY")) {
$headerFrameOptObj.Result = "ISSUE"
$headerFrameOptObj.Recommendation = "Should be set to `"DENY`" or `"SAMEORIGIN`""
}
else {
$headerFrameOptObj.Result = "OK"
}
}
else {
$headerFrameOptObj.Result = "MISSING"
$headerFrameOptObj.Recommendation = "Should be added and set to `"DENY`" or `"SAMEORIGIN`""
}
$headerAnalysis += $headerFrameOptObj
#endregion
#region X-XSS-Protection
$headerXssProtObj = New-Object -TypeName psobject
Add-Member -InputObject $headerXssProtObj -MemberType NoteProperty -Name "HTTP Header" -Value $headerXssProt
Add-Member -InputObject $headerXssProtObj -MemberType NoteProperty -Name "Value" -Value ""
Add-Member -InputObject $headerXssProtObj -MemberType NoteProperty -Name "Result" -Value ""
Add-Member -InputObject $headerXssProtObj -MemberType NoteProperty -Name "Recommendation" -Value ""
$headerXssProtValue = $requestHeaders[$headerXssProt]
if($null -ne $headerXssProtValue) {
$headerXssProtObj.Value = $headerXssProtValue
if($headerXssProtValue -ne "1; mode=block") {
$headerXssProtObj.Result = "ISSUE"
$headerXssProtObj.Recommendation = "Should be set to `"1; mode=block`""
}
else {
$headerXssProtObj.Result = "OK"
}
}
else {
$headerXssProtObj.Result = "MISSING"
$headerXssProtObj.Recommendation = "Should be added and set to `"1; mode=block`""
}
$headerAnalysis += $headerXssProtObj
#endregion
#region X-Content-Type-Options
$headerContentTypeObj = New-Object -TypeName psobject
Add-Member -InputObject $headerContentTypeObj -MemberType NoteProperty -Name "HTTP Header" -Value $headerContentType
Add-Member -InputObject $headerContentTypeObj -MemberType NoteProperty -Name "Value" -Value ""
Add-Member -InputObject $headerContentTypeObj -MemberType NoteProperty -Name "Result" -Value ""
Add-Member -InputObject $headerContentTypeObj -MemberType NoteProperty -Name "Recommendation" -Value ""
$headerContentTypeValue = $requestHeaders[$headerContentType]
if($null -ne $headerContentTypeValue) {
$headerContentTypeObj.Value = $headerContentTypeValue
if($headerContentTypeValue -ne "nosniff") {
$headerContentTypeObj.Result = "ISSUE"
$headerContentTypeObj.Recommendation = "Should be set to `"nosniff`""
}
else {
$headerContentTypeObj.Result = "OK"
}
}
else {
$headerContentTypeObj.Result = "MISSING"
$headerContentTypeObj.Recommendation = "Should be added and set to `"nosniff`""
}
$headerAnalysis += $headerContentTypeObj
#endregion
#region Referrer-Policy
$headerRefPolObj = New-Object -TypeName psobject
Add-Member -InputObject $headerRefPolObj -MemberType NoteProperty -Name "HTTP Header" -Value $headerRefPol
Add-Member -InputObject $headerRefPolObj -MemberType NoteProperty -Name "Value" -Value ""
Add-Member -InputObject $headerRefPolObj -MemberType NoteProperty -Name "Result" -Value ""
Add-Member -InputObject $headerRefPolObj -MemberType NoteProperty -Name "Recommendation" -Value ""
$headerRefPolValue = $requestHeaders[$headerRefPol]
if($null -ne $headerRefPolValue) {
$headerRefPolObj.Value = $headerRefPolValue
if($headerRefPolValue -match "(^|,\s{0,1})(origin-when-cross-origin|unsafe-url|origin)($|,\s{0,1})") {
$headerRefPolObj.Result = "ISSUE"
$headerRefPolObj.Recommendation = "`"origin-when-cross-origin`", `"origin`" and `"unsafe-url`" can leak sensitive information via HTTP connections"
}
else {
$headerRefPolObj.Result = "OK"
}
}
else {
$headerRefPolObj.Result = "MISSING"
$headerRefPolObj.Recommendation = "Should be added (note: `"origin-when-cross-origin`", `"origin`" and `"unsafe-url`" can leak sensitive information via HTTP connections)"
}
$headerAnalysis += $headerRefPolObj
#endregion
#region Content-Security-Policy
# TODO
#endregion
#region Feature-Policy
# TODO
#endregion
$headerAnalysis
} catch [System.Net.WebException] {
$statusCode = [int]$_.Exception.Response.StatusCode
$statusDesc = $_.Exception.Response.StatusDescription
Write-Error "Web Request failed: $($statusCode): $statusDesc"
}