-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx-vuln-check.ps1
440 lines (343 loc) · 17.7 KB
/
nginx-vuln-check.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
function CheckSecurityHeaders {
param (
[string]$url
)
$response = Invoke-WebRequest -Uri $url -ErrorAction Stop
if ($response -eq $null) {
throw "Failed to retrieve headers from $url"
}
$headers = $response.Headers
if ($headers -eq $null) {
throw "Failed to retrieve headers from the response"
}
$requiredHeaders = @{
"Strict-Transport-Security" = "CVE-2019-13498 :`nNot using HTTP Strict Transport Security (HSTS) may lead to man-in-the-middle (MITM) attacks."
"X-Content-Type-Options" = "CVE-2018-17031 : `nan attacker can use a crafted .eml file to trigger MIME type sniffing, which leads to XSS, as demonstrated by Internet Explorer, `nbecause an X-Content-Type-Options: nosniff header is not sent."
"X-Frame-Options" = "CVE-2022-3260 : `nThe response header has not enabled X-FRAME-OPTIONS, Which helps prevents against Clickjacking attack."
"X-XSS-Protection" = "CVE-2018-7504 : `nThe X-XSS-Protection response header is not set to block, allowing attempts at reflected cross-site scripting"
"Content-Security-Policy" = "CVE-2018-5164 : `nContent Security Policy (CSP) is not applied correctly to all parts of multipart content sent with the multipart/x-mixed-replace MIME type"
}
$missingHeaders = @()
foreach ($header in $requiredHeaders.GetEnumerator()) {
if (!$headers.ContainsKey($header.Key)) {
$missingHeaders += [PSCustomObject]@{
Header = $header.Key
CVE = $header.Value
}
}
}
return $missingHeaders
}
$output = @()
$successCount = 10
# Check if an Apache Version Header was found
function CheckLinuxNginxVersions {
param (
[string]$url,
[ref]$output
)
$headers = Invoke-WebRequest -Uri $url | Select-Object -ExpandProperty Headers
$XPoweredBy = $headers['X-Powered-By']
# Check if X-Powered-By header exists
if ($XPoweredBy) {
Write-Host "Warning: An X-Powered-By header was found. Value: $XPoweredBy" -ForegroundColor Yellow
$output.Value += "`nWarning: An X-Powered-By header was found. Value: $XPoweredBy. `nRelated vulnerability: `nCWE-200. `nShort Description:The web/application server is leaking information via one or more X-Powered-By HTTP response headers.
`nAccess to such information may facilitate attackers identifying other frameworks/components your web application is reliant
`nupon and the vulnerabilities such components may be subject to."
$output.Value+= "`nPossible Solution: `nLocate the nginx config file (usually located at /etc/nginx/nginx.conf) and add `nthe following inside the http { } section:
`nserver_tokens and set it to:
`nserver_tokens off;
`nRestart nginx service to apply changes. "
$successCount = $successCount - 1
} else {
Write-Host "Didn't find any X-Powered-By headers."
$output.Value += "`nDidn't find any X-Powered-By headers."
}
}
function CheckInsecureCookies {
param(
[string]$url
)
$response = Invoke-WebRequest -Uri $url
$cookies = $response.Headers["Set-Cookie"]
$insecureCookies = @()
$cookiesWithoutHttpOnly = @()
$cookiesWithoutSameSite = @()
foreach ($cookie in $cookies) {
if ($cookie -notlike "*Secure*") {
$cookieName = ($cookie -split ";")[0]
$insecureCookies += $cookieName
}
if ($cookie -notlike "*HttpOnly*") {
$cookieName = ($cookie -split ";")[0]
$cookiesWithoutHttpOnly += $cookieName
}
if ($cookie -notlike "*SameSite*") {
$cookieName = ($cookie -split ";")[0]
$cookiesWithoutSameSite += $cookieName
}
}
$result = @{
"InsecureCookies" = $insecureCookies
"CookiesWithoutHttpOnly" = $cookiesWithoutHttpOnly
"CookiesWithoutSameSite" = $cookiesWithoutSameSite
}
return $result
}
function CheckNginxServerHeader {
param (
[string]$url
)
$response = Invoke-WebRequest -Uri $url -ErrorAction Stop
if ($response -eq $null) {
throw "Failed to retrieve headers from $url"
}
$headers = $response.Headers
if ($headers -eq $null) {
throw "Failed to retrieve headers from the response"
}
$serverHeader = $headers["Server"]
if ($serverHeader -match "nginx/(\d+(\.\d+)*)") {
$nginxVersion = $Matches[1]
return $nginxVersion
} else {
return $null
}
}
function CheckTLSVersions {
param(
[string]$url
)
$request = [System.Net.HttpWebRequest]::Create($url)
$request.Method = "HEAD"
try {
$response = $request.GetResponse()
$tlsVersion = $response.ProtocolVersion
if ($tlsVersion -eq "Tls" -or $tlsVersion -eq "Tls11") {
return $true
} else {
return $false
}
} catch {
# In case of an error, assume that TLS 1.0 or TLS 1.1 is not supported.
return $false
}
}
function CheckHttpsRedirect {
param (
[string]$url
)
$response = Invoke-WebRequest -Uri $url -MaximumRedirection 0 -ErrorAction SilentlyContinue
if ($response -eq $null) {
return $false
}
$statusCode = $response.StatusCode
if ($statusCode -eq 301 -or $statusCode -eq 302) {
return $true
} else {
return $false
}
}
$output += "`n++++++++++++++++++++++++++++++++++++++++++++++++++"
# Replace 'http://example.com' with the URL of your app
# Prompt the user for the domain to check
$targetUrl = Read-Host "Domain to check (e.g., example.com)"
$output += "`n`n`nVULNERABILITY REPORT FOR DOMAIN: $targetUrl`n`n`n"
# Add "https://" to the beginning if not provided by the user
if (-not ($targetUrl -like "https://*")) {
$targetUrl = "https://$targetUrl"
}
# Check if an Apache Version Header was found
$output += "`n++++++++++++++++++++++++++++++++++++++++++++++++++"
$output += "`n`nHTTP to HTTPS Redirect Result:"
$isHttpsRedirect = CheckHttpsRedirect -url $targetUrl
if ($isHttpsRedirect) {
Write-Host "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ " -ForegroundColor Green
Write-Host "Domain is redirected to HTTPS scheme." -ForegroundColor Green
Write-Host "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ " -ForegroundColor Green
$output += "`nDomain is redirected to HTTPS scheme."
} else {
Write-Host "------------------------------------------------------------------ " -ForegroundColor Red
Write-Host "No HTTPS redirect found! Enforcing HTTP to HTTPS redirection is an essential security practice to ensure secure connections and protect user data from potential eavesdropping or
`nman-in-the-middle attacks. Web servers and applications should be configured to automatically redirect HTTP requests to their corresponding HTTPS versions." -ForegroundColor Red
Write-Host "------------------------------------------------------------------ " -ForegroundColor Red
$output += "`nNo HTTPS redirect found! `nRealted Vulnerability: Enforcing HTTP to HTTPS redirection is an essential security practice to ensure secure connections and protect user data
`nfrom potential eavesdropping or man-in-the-middle attacks. Web servers and applications should be configured to automatically redirect HTTP requests to their corresponding HTTPS versions."
$output +="`nRisk: HIGH."
$output +="`nPossible Solution: `nGo to your nginx website conf file.
`nedit it and add the following:
`nif (`$scheme -eq `"http`") {
return 301 https://`$server_name`$request_uri;
}"
$successCount-=1.5
}
$output += "`n----------------------------------------------------------------"
$output += "`nFramework Header Discovery:"
CheckLinuxNginxVersions -url $targetUrl -output ([ref]$output)
$missingHeaders = CheckSecurityHeaders -url $targetUrl
$insecureCookiesResult = CheckInsecureCookies -url $targetUrl
$insecureCookies = $insecureCookiesResult["InsecureCookies"]
$cookiesWithoutHttpOnly = $insecureCookiesResult["CookiesWithoutHttpOnly"]
$output += "`n----------------------------------------------------------------"
$output += "`nMissing Security Headers Result:"
$missingHeaders = CheckSecurityHeaders -url $targetUrl
if ($missingHeaders.Count -gt 0) {
Write-Host "Missing security headers:"
Write-Host "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ " -ForegroundColor DarkYellow
Write-Host "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ " -ForegroundColor DarkYellow
$missingHeaders | Format-Table -AutoSize -Property Header, CVE
$successCount = $successCount-3
Write-Host "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ " -ForegroundColor DarkYellow
Write-Host "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ " -ForegroundColor DarkYellow
$headers = $missingHeaders | ForEach-Object { $_.Header }
$cves = $missingHeaders | ForEach-Object { $_.CVE }
$output += "`nMissing security headers: `n$($headers -join ', ')`nRelated CVE's: `n$($cves -join ', ')`n"
$output +="`nRisk: HIGH."
$output += "`nPossible Solution: `nGo to your nginx website conf file, and add the desired header name and its value `n
inside the server { } section. for example:
`nserver{
`nadd_header X-Frame-Options `"SAMEORIGIN`"
`n}
`nThen reload or restart nginx service."
} else {
Write-Host "All required security headers are present." -ForegroundColor Green
$output += "`nAll required security headers are present."
}
# Check if an Apache Version Header was found
$output += "`n----------------------------------------------------------------"
$output += "`nInsecure Cookies Check:"
if ($insecureCookies.Count -gt 0) {
Write-Host "------------------------------------------------------------------ "
Write-Host "Insecure cookies found:"
$insecureCookies | ForEach-Object { Write-Host " $_" -ForegroundColor Red }
$successCount--
Write-Host "------------------------------------------------------------------ "
$output += "`nInsecure cookies found: $insecureCookies"
$output +="`nInsecure cookies can lead to various security risks, including session hijacking, cross-site scripting (XSS) attacks, and man-in-the-middle attacks."
$output +="`nRisk: Medium."
} else {
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
Write-Host "No Insecured cookies found." -ForegroundColor Green
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
$output += "`nNo Insecured cookies found."
}
if ($cookiesWithoutHttpOnly.Count -gt 0) {
Write-Host "------------------------------------------------------------------ "
Write-Host "Cookies without HttpOnly attribute found:"
$successCount = $successCount-0.5
$cookiesWithoutHttpOnly | ForEach-Object { Write-Host " $_" -ForegroundColor Red }
Write-Host "------------------------------------------------------------------ "
$output += "`nCookies without HttpOnly attribute found: $cookiesWithoutHttpOnly "
$output += "`nVulnerability Description:Not using the HttpOnly attribute may lead to client-side scripts to access cookies, increasing the risk of XSS attacks"
$output += "`nRisk: MEDIUM"
} else {
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
Write-Host "Did not find and cookies without HttpOnly attribute." -ForegroundColor Green
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
$output += "`nDid not find and cookies without HttpOnly attribute."
}
if ($cookiesWithoutSameSite.Count -gt 0) {
Write-Host "------------------------------------------------------------------ "
Write-Host "Cookies without SameSite attribute found:"
$cookiesWithoutHttpOnly | ForEach-Object { Write-Host " $_" -ForegroundColor Red }
Write-Host "------------------------------------------------------------------ "
$successCount = $successCount-0.5
$output += "`nCookies without HttpOnly attribute found: $cookiesWithoutSameSite "
$output += "`nThe SameSite attribute is a relatively new feature in web browsers that helps control how cookies are sent in cross-origin requests.
`nNot using the SameSite attribute for cookies can introduce security risks, particularly related to Cross-Site Request Forgery (CSRF) attacks and user privacy. "
$output += "`nRisk: MEDIUM"
} else {
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
Write-Host "Did not find and cookies without SameSite attribute." -ForegroundColor Green
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
$output += "`nDid not find and cookies without SameSite attribute."
}
# Check if an Apache Version Header was found
$output += "`n----------------------------------------------------------------"
$output += "`nWeb Server Version Check:"
$nginxVersion = CheckNginxServerHeader -url $targetUrl
if ($nginxVersion) {
Write-Host "------------------------------------------------------------------ "
Write-Host "`nnginx Version Header Found: $nginxVersion." -ForegroundColor Red
Write-Host "Risk: MEDIUM"
Write-Host "`nPossible Solution: `nLocate the nginx config file (usually located at /etc/nginx/nginx.conf) and add `nthe following inside the http { } section:
`nserver_tokens and set it to:
`nserver_tokens off;
`nRestart nginx service to apply changes. "
$successCount--
Write-Host "------------------------------------------------------------------ "
$output += "`nnginx Version Header Found: $nginxVersion."
$output += "`nRISK: MEDIUM"
$output += "`nPossible Solution: `nLocate the nginx config file (usually located at /etc/nginx/nginx.conf) and add `nthe following inside the http { } section:
`nserver_tokens and set it to:
`nserver_tokens off;
`nRestart nginx service to apply changes. "
}
else {
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
Write-Host "No Nginx Version Header Was Found." -ForegroundColor Green
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
$output += "`nNo Nginx Version Header Was Found."
}
$output += "`n----------------------------------------------------------------"
$output += "`n`nTLS Version Check:"
$insecureTLS = CheckTLSVersions -url $targetUrl
if ($insecureTLS) {
Write-Host "------------------------------------------------------------------ "
Write-Host "`nWarning: Insecure TLS version (TLS 1.0 or TLS 1.1) is supported by the domain SSL certificate. Consider using a higher TLS supported certificate." -ForegroundColor -ForegroundColor DarkYellow
Write-Host "------------------------------------------------------------------ "
$output += "`nWarning: Insecure TLS version (TLS 1.0 or TLS 1.1) is supported by the domain SSL certificate. Consider using a higher TLS supported certificate."
$successCount = $successCount-1
$output +="`nRisk: HIGH."
}
else{
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
Write-Host "`nNo 1.1 or 1.0 TLS versions found on target domain: " $targetUrl -ForegroundColor Green
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
$output += "`nNo 1.1 or 1.0 TLS versions found on target domain: $targetUrl "
}
$output += "`n----------------------------------------------------------------"
$output += "`n`nAfter Everything Checked, this is the following calculted score:"
# Check if all checks were successful (all results are green)
if ($successCount -eq 10) {
Write-Host "Total result score: 10/10" -ForegroundColor Green
$output += "`n`n`nTotal result score: $successCount/10"
} elseif ($successCount -gt 6) {
Write-Host "Total result score: $successCount/10" -ForegroundColor DarkYellow
$output += "`n`n`nTotal result score: $successCount/10"
} else {
Write-Host "Total result score: $successCount/10" -ForegroundColor Red
$output += "`n`n`nTotal result score: $successCount/10"
}
$output += "`n----------------------------------------------------------------"
function ConvertTo-PDF {
param (
[string]$Source,
[string]$Destination
)
try {
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$doc = $word.Documents.Open($Source)
$doc.SaveAs([ref]$Destination, [ref]17)
$doc.Close()
$word.Quit()
Write-Host "PDF created: $Destination" -ForegroundColor Green
}
catch {
Write-Host "Failed to create PDF: $_" -ForegroundColor Red
}
}
# Prompt the user to export a PDF results file
$exportToPdf = Read-Host "Would you like to export a PDF results file (y/n)?"
if ($exportToPdf -eq "y" -or $exportToPdf -eq "Y") {
# Save the output to a text file
$outputFilePath = "$env:TEMP\script_output.txt"
$output | Out-File -FilePath $outputFilePath -Encoding UTF8
# Convert the text file to PDF
$pdfFilePath = Read-Host "Enter the PDF file path to save the results (e.g., C:\Path\To\Results.pdf)"
ConvertTo-PDF -Source $outputFilePath -Destination $pdfFilePath
}
else {
Write-Host "Done checking."
}