Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- BF1942 console unlock
- Removes the "Unauthorised method!" check so commands like
- ai.killBotName <name>, ai.killBotId <id> and ai.killAllBots work in the console.
- Instead of fixed offsets it locates the check by structure, so it should work on
- other builds of BF1942.exe: it finds the "Unauthorised method!" string, every
- instruction that pushes it, and the conditional jumps that decide whether that
- code runs. If anything doesn't look as expected it stops without changing the file.
- Usage (game closed):
- powershell -ExecutionPolicy Bypass -File bf1942-console-unlock.ps1 # patch
- powershell -ExecutionPolicy Bypass -File bf1942-console-unlock.ps1 -WhatIf # show changes only
- powershell -ExecutionPolicy Bypass -File bf1942-console-unlock.ps1 -Restore # undo
- Optional: -Path "D:\Games\BF1942\BF1942.exe"
- Offline / bot / LAN play only - a modified exe may get you kicked or banned online (PunkBuster).
- #>
- param(
- [string]$Path = (Join-Path $PSScriptRoot 'BF1942.exe'),
- [switch]$Restore,
- [switch]$WhatIf
- )
- $ErrorActionPreference = 'Stop'
- $backup = "$Path.orig"
- if ($Restore) {
- if (-not (Test-Path $backup)) { throw "No backup found at $backup" }
- Copy-Item $backup $Path -Force
- Write-Host "Restored original from $backup"
- return
- }
- if (-not (Test-Path $Path)) { throw "BF1942.exe not found at $Path (use -Path)" }
- $b = [IO.File]::ReadAllBytes($Path)
- $latin1 = [Text.Encoding]::GetEncoding(28591) # 1 byte = 1 char, for fast byte searches
- $s = $latin1.GetString($b)
- function Fail($msg) { throw "$msg Nothing changed." }
- function U32($o) { [BitConverter]::ToUInt32($b, $o) }
- function I32($o) { [BitConverter]::ToInt32($b, $o) }
- function I8($o) { [int][sbyte]$b[$o] } # PS 5.1-safe signed byte
- # --- PE sections ---
- $pe = I32 0x3C
- if ((U32 $pe) -ne 0x4550) { Fail 'Not a Windows executable.' }
- $imageBase = U32 ($pe + 52)
- $secStart = $pe + 24 + [BitConverter]::ToUInt16($b, $pe + 20)
- $sections = for ($i = 0; $i -lt [BitConverter]::ToUInt16($b, $pe + 6); $i++) {
- $o = $secStart + 40 * $i
- [pscustomobject]@{ VA = U32 ($o + 12); Raw = U32 ($o + 20); Size = U32 ($o + 16); Exec = ((U32 ($o + 36)) -band 0x20000000) -ne 0 }
- }
- # --- 1. the error string and its address in memory ---
- $strOff = $s.IndexOf("Unauthorised method!`0")
- if ($strOff -lt 0) { Fail "Couldn't find the 'Unauthorised method!' text (packed/encrypted exe?)." }
- $sec = $sections | Where-Object { $strOff -ge $_.Raw -and $strOff -lt $_.Raw + $_.Size } | Select-Object -First 1
- $strVA = $imageBase + $sec.VA + ($strOff - $sec.Raw)
- # --- 2. every 'push <string>' in executable code ---
- $needle = $latin1.GetString([byte[]](@(0x68) + [BitConverter]::GetBytes([uint32]$strVA)))
- $pushes = @()
- foreach ($c in $sections | Where-Object Exec) {
- $i = $s.IndexOf($needle, [int]$c.Raw, [int]$c.Size, [StringComparison]::Ordinal)
- while ($i -ge 0) { $pushes += $i; $i = $s.IndexOf($needle, $i + 1, [int]($c.Raw + $c.Size - $i - 1), [StringComparison]::Ordinal) }
- }
- if ($pushes.Count -eq 0) { Fail "Found the text but no code that uses it." }
- # --- 3. decide how each one is reached ---
- $changes = @(); $allowedTargets = @(); $alreadyPatched = 0
- foreach ($p in $pushes) {
- if ($b[$p - 2] -ge 0x70 -and $b[$p - 2] -le 0x7F -and (I8 ($p - 1)) -gt 0) {
- # short jcc that skips the error when the check passes -> always jump
- $changes += , @(($p - 2), [byte[]]@(0xEB))
- $allowedTargets += $p + (I8 ($p - 1))
- }
- elseif ($b[$p - 6] -eq 0x0F -and $b[$p - 5] -ge 0x80 -and $b[$p - 5] -le 0x8F -and (I32 ($p - 4)) -gt 0) {
- # near jcc variant -> nop + jmp with the same displacement
- $changes += , @(($p - 6), [byte[]]@(0x90, 0xE9))
- $allowedTargets += $p + (I32 ($p - 4))
- }
- elseif ($b[$p - 2] -eq 0xEB -and (I8 ($p - 1)) -gt 0) {
- $alreadyPatched++; $allowedTargets += $p + (I8 ($p - 1))
- }
- elseif ($b[$p - 5] -eq 0xE9 -or $b[$p - 2] -eq 0xEB -or $b[$p - 1] -eq 0xC3) {
- # only reachable by jumping to it: find the 'test ... / jcc <here>' that does
- $hits = @()
- for ($j = $p - 0x80; $j -lt $p - 2; $j++) {
- $isTest = ($b[$j - 2] -in 0x84, 0x85 -and $b[$j - 1] -ge 0xC0) -or $b[$j - 2] -eq 0xA8
- if ($isTest -and $b[$j] -ge 0x70 -and $b[$j] -le 0x7F -and $j + 2 + (I8 ($j + 1)) -eq $p) { $hits += $j }
- }
- if ($hits.Count -eq 1) { $changes += , @($hits[0], [byte[]]@(0x90, 0x90)) }
- elseif ($hits.Count -eq 0) { $alreadyPatched++ } # jump already removed
- else { Fail ("Ambiguous jumps to the check at file offset 0x{0:X}." -f $p) }
- }
- else { Fail ("Unrecognised code around file offset 0x{0:X}." -f $p) }
- }
- # --- 4. sanity: every 'check passed' jump must go to the same place ---
- if (@($allowedTargets | Select-Object -Unique).Count -ne 1) { Fail "The checks don't all lead to the same place - unexpected layout." }
- if ($changes.Count -eq 0) { Write-Host 'Already patched.'; return }
- if ($alreadyPatched -gt 0) { Fail 'File looks partly patched. Use -Restore first.' }
- foreach ($c in $changes) {
- $old = ($b[$c[0]..($c[0] + $c[1].Length - 1)] | ForEach-Object { $_.ToString('X2') }) -join ' '
- $new = ($c[1] | ForEach-Object { $_.ToString('X2') }) -join ' '
- Write-Host (" 0x{0:X}: {1} -> {2}" -f $c[0], $old, $new)
- }
- if ($WhatIf) { Write-Host 'WhatIf: no changes written.'; return }
- if (-not (Test-Path $backup)) { Copy-Item $Path $backup; Write-Host "Backup saved to $backup" }
- foreach ($c in $changes) { for ($i = 0; $i -lt $c[1].Length; $i++) { $b[$c[0] + $i] = $c[1][$i] } }
- [IO.File]::WriteAllBytes($Path, $b)
- Write-Host 'Patched. In game, open the console (~) and try: ai.killBotName <BotName>'
Advertisement