Skip to content

Commit

Permalink
Work around the latest Microsoft server restrictions
Browse files Browse the repository at this point in the history
* It looks like Microsoft are actively filtering our script by detecting our first request with the
  spoofed user-agent, so work around that.
* Also switch to using 'curl' instead of 'Invoke-WebRequest'. May break PowerShell 7.0, but I'm only
  concerned about Rufus usage for now.
* Also add timeouts on web requests.
* Closes #88.
  • Loading branch information
pbatard committed Feb 7, 2024
1 parent d43d7ae commit 4e6f25f
Showing 1 changed file with 42 additions and 31 deletions.
73 changes: 42 additions & 31 deletions Fido.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Fido v1.54 - Feature ISO Downloader, for retail Windows images and UEFI Shell
# Fido v1.55 - Feature ISO Downloader, for retail Windows images and UEFI Shell
# Copyright © 2019-2023 Pete Batard <[email protected]>
# Command line support: Copyright © 2021 flx5
# ConvertTo-ImageSource: Copyright © 2016 Chris Carter
Expand Down Expand Up @@ -437,6 +437,7 @@ function Get-RandomDate()

#region Globals
$ErrorActionPreference = "Stop"
$DefaultTimeout = 30
$dh = 58
$Stage = 0
$SelectedIndex = 0
Expand Down Expand Up @@ -508,8 +509,18 @@ function Check-Locale
if ($Verbosity -ge 2) {
Write-Host Querying $url
}
Invoke-WebRequest -UseBasicParsing -MaximumRedirection 0 -UserAgent $UserAgent $url | Out-Null
# Looks Microsoft are filtering our script according to the first query it performs with the spoofed user agent.
# So, to continue this pointless cat and mouse game, we simply add an extra first query with the default user agent.
# Also: "Hi Microsoft. You sure have A LOT OF RESOURCES TO WASTE to have assigned folks of yours to cripple scripts
# that merely exist because you have chosen to make the user experience from your download website utterly subpar.
# And while I am glad senpai noticed me (UwU), I feel compelled to ask: Don't you guys have better things to do?"
curl -UseBasicParsing -TimeoutSec $DefaultTimeout -MaximumRedirection 0 $url | Out-Null
curl -UseBasicParsing -TimeoutSec $DefaultTimeout -MaximumRedirection 0 -UserAgent $UserAgent $url | Out-Null
} catch {
# Of course PowerShell 7 had to BREAK $_.Exception.Status on timeouts...
if ($_.Exception.Status -eq "Timeout" -or $_.Exception.GetType().Name -eq "TaskCanceledException") {
Write-Host Operation Timed out
}
$script:QueryLocale = "en-US"
}
}
Expand Down Expand Up @@ -564,7 +575,7 @@ function Get-Windows-Languages([int]$SelectedVersion, [int]$SelectedEdition)
Write-Host Querying $url
}
try {
Invoke-WebRequest -UseBasicParsing -MaximumRedirection 0 -UserAgent $UserAgent $url | Out-Null
curl -UseBasicParsing -TimeoutSec $DefaultTimeout -MaximumRedirection 0 -UserAgent $UserAgent $url | Out-Null
} catch {
Error($_.Exception.Message)
return @()
Expand All @@ -583,7 +594,7 @@ function Get-Windows-Languages([int]$SelectedVersion, [int]$SelectedEdition)

$script:SelectedIndex = 0
try {
$r = Invoke-WebRequest -Method Post -UseBasicParsing -UserAgent $UserAgent -SessionVariable "Session" $url
$r = curl -Method Post -UseBasicParsing -TimeoutSec $DefaultTimeout -UserAgent $UserAgent -SessionVariable "Session" $url
if ($r -match "errorModalMessage") {
Throw-Error -Req $r -Alt "Could not retrieve languages from server"
}
Expand Down Expand Up @@ -673,7 +684,7 @@ function Get-Windows-Download-Links([int]$SelectedVersion, [int]$SelectedRelease
$Is64 = [Environment]::Is64BitOperatingSystem
# Must add a referer for this request, else Microsoft's servers will deny it
$ref = "https://www.microsoft.com/software-download/windows11"
$r = Invoke-WebRequest -Method Post -Headers @{ "Referer" = $ref } -UseBasicParsing -UserAgent $UserAgent -WebSession $Session $url
$r = curl -Method Post -Headers @{ "Referer" = $ref } -UseBasicParsing -TimeoutSec $DefaultTimeout -UserAgent $UserAgent -WebSession $Session $url
if ($r -match "errorModalMessage") {
$Alt = [regex]::Match($r.Content, '<p id="errorModalMessage">(.+?)<\/p>').Groups[1].Value -replace "<[^>]+>" -replace "\s+", " " -replace "\?\?\?", "-"
$Alt = [System.Text.Encoding]::UTF8.GetString([byte[]][char[]]$Alt)
Expand Down Expand Up @@ -726,7 +737,7 @@ function Process-Download-Link([string]$Url)
$pattern = '.*\/(.*\.iso).*'
$File = [regex]::Match($Url, $pattern).Groups[1].Value
# PowerShell implicit conversions are iffy, so we need to force them...
$str_size = (Invoke-WebRequest -UseBasicParsing -Uri $Url -Method Head).Headers.'Content-Length'
$str_size = (curl -UseBasicParsing -TimeoutSec $DefaultTimeout -Uri $Url -Method Head).Headers.'Content-Length'
$tmp_size = [uint64]::Parse($str_size)
$Size = Size-To-Human-Readable $tmp_size
Write-Host "Downloading '$File' ($Size)..."
Expand Down Expand Up @@ -1059,8 +1070,8 @@ exit $ExitCode
# SIG # Begin signature block
# MIIkWAYJKoZIhvcNAQcCoIIkSTCCJEUCAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG
# KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCB3CIxxVBpyap6s
# WzEDj8dyJ/82xI3cuRQ7wUuMgmNSH6CCElkwggVvMIIEV6ADAgECAhBI/JO0YFWU
# KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCBHf44+d5nlaax1
# zDK20N+s1BjbUkc/aTGHLUQuXBU9BqCCElkwggVvMIIEV6ADAgECAhBI/JO0YFWU
# jTanyYqJ1pQWMA0GCSqGSIb3DQEBDAUAMHsxCzAJBgNVBAYTAkdCMRswGQYDVQQI
# DBJHcmVhdGVyIE1hbmNoZXN0ZXIxEDAOBgNVBAcMB1NhbGZvcmQxGjAYBgNVBAoM
# EUNvbW9kbyBDQSBMaW1pdGVkMSEwHwYDVQQDDBhBQUEgQ2VydGlmaWNhdGUgU2Vy
Expand Down Expand Up @@ -1163,22 +1174,22 @@ exit $ExitCode
# aWMgQ29kZSBTaWduaW5nIENBIEVWIFIzNgIRAL+xUAG79ZLUlip3l+pzb6MwDQYJ
# YIZIAWUDBAIBBQCgfDAQBgorBgEEAYI3AgEMMQIwADAZBgkqhkiG9w0BCQMxDAYK
# KwYBBAGCNwIBBDAc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wYLKoZIhvcNAQkQAi8xKDAmMCQwIgQgxHTOdgB9AjlODaXk3nwUxoD54oIB
# PP72U+9dtx/fYfgwCwYJKoZIhvcNAQEBBIIBAInhOfqTOXDdnHraAsocyn30sofK
# m/jHs++YFntM7KV18KMzYmuDtWJlZf98q2LqryJ/4Lq9rwbR+F83GprDYWD7LnpW
# XI/GdSxdUDIO4BDQp6ckAbQ3uzh6DxtlsW9CCMIHdJpPpY5pjeI0Gv4YyGFPDgY+
# QC4d/95+yC3pUOqxMdeK5O0IHuHOF0iER+52doaqbnAPaA6s0UD1jkJKVk7SXej5
# YXa+0hI0Oseu6j1yaqkoFSV24uecmEh+xnfRvVM7dlltGsVJ5L1xLx396/7LW9Wr
# J8HxJBxc775T9RcpIMTzxPkrRfngY0e7+D13hVLyfmYNkC0OxuekJ7f16O8=
# SIG # End signature block

0 comments on commit 4e6f25f

Please sign in to comment.