Guest User

Untitled

a guest
Oct 13th, 2026
45
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.   BF1942 console unlock
  3.   Removes the "Unauthorised method!" check so commands like
  4.   ai.killBotName <name>, ai.killBotId <id> and ai.killAllBots work in the console.
  5.  
  6.   Instead of fixed offsets it locates the check by structure, so it should work on
  7.   other builds of BF1942.exe: it finds the "Unauthorised method!" string, every
  8.   instruction that pushes it, and the conditional jumps that decide whether that
  9.   code runs. If anything doesn't look as expected it stops without changing the file.
  10.  
  11.   Usage (game closed):
  12.     powershell -ExecutionPolicy Bypass -File bf1942-console-unlock.ps1            # patch
  13.     powershell -ExecutionPolicy Bypass -File bf1942-console-unlock.ps1 -WhatIf    # show changes only
  14.     powershell -ExecutionPolicy Bypass -File bf1942-console-unlock.ps1 -Restore   # undo
  15.   Optional: -Path "D:\Games\BF1942\BF1942.exe"
  16.  
  17.   Offline / bot / LAN play only - a modified exe may get you kicked or banned online (PunkBuster).
  18. #>
  19. param(
  20.     [string]$Path = (Join-Path $PSScriptRoot 'BF1942.exe'),
  21.     [switch]$Restore,
  22.     [switch]$WhatIf
  23. )
  24. $ErrorActionPreference = 'Stop'
  25. $backup = "$Path.orig"
  26.  
  27. if ($Restore) {
  28.     if (-not (Test-Path $backup)) { throw "No backup found at $backup" }
  29.     Copy-Item $backup $Path -Force
  30.     Write-Host "Restored original from $backup"
  31.     return
  32. }
  33. if (-not (Test-Path $Path)) { throw "BF1942.exe not found at $Path (use -Path)" }
  34.  
  35. $b = [IO.File]::ReadAllBytes($Path)
  36. $latin1 = [Text.Encoding]::GetEncoding(28591)   # 1 byte = 1 char, for fast byte searches
  37. $s = $latin1.GetString($b)
  38. function Fail($msg) { throw "$msg Nothing changed." }
  39. function U32($o) { [BitConverter]::ToUInt32($b, $o) }
  40. function I32($o) { [BitConverter]::ToInt32($b, $o) }
  41. function I8($o) { [int][sbyte]$b[$o] }   # PS 5.1-safe signed byte
  42.  
  43. # --- PE sections ---
  44. $pe = I32 0x3C
  45. if ((U32 $pe) -ne 0x4550) { Fail 'Not a Windows executable.' }
  46. $imageBase = U32 ($pe + 52)
  47. $secStart = $pe + 24 + [BitConverter]::ToUInt16($b, $pe + 20)
  48. $sections = for ($i = 0; $i -lt [BitConverter]::ToUInt16($b, $pe + 6); $i++) {
  49.     $o = $secStart + 40 * $i
  50.     [pscustomobject]@{ VA = U32 ($o + 12); Raw = U32 ($o + 20); Size = U32 ($o + 16); Exec = ((U32 ($o + 36)) -band 0x20000000) -ne 0 }
  51. }
  52.  
  53. # --- 1. the error string and its address in memory ---
  54. $strOff = $s.IndexOf("Unauthorised method!`0")
  55. if ($strOff -lt 0) { Fail "Couldn't find the 'Unauthorised method!' text (packed/encrypted exe?)." }
  56. $sec = $sections | Where-Object { $strOff -ge $_.Raw -and $strOff -lt $_.Raw + $_.Size } | Select-Object -First 1
  57. $strVA = $imageBase + $sec.VA + ($strOff - $sec.Raw)
  58.  
  59. # --- 2. every 'push <string>' in executable code ---
  60. $needle = $latin1.GetString([byte[]](@(0x68) + [BitConverter]::GetBytes([uint32]$strVA)))
  61. $pushes = @()
  62. foreach ($c in $sections | Where-Object Exec) {
  63.     $i = $s.IndexOf($needle, [int]$c.Raw, [int]$c.Size, [StringComparison]::Ordinal)
  64.     while ($i -ge 0) { $pushes += $i; $i = $s.IndexOf($needle, $i + 1, [int]($c.Raw + $c.Size - $i - 1), [StringComparison]::Ordinal) }
  65. }
  66. if ($pushes.Count -eq 0) { Fail "Found the text but no code that uses it." }
  67.  
  68. # --- 3. decide how each one is reached ---
  69. $changes = @(); $allowedTargets = @(); $alreadyPatched = 0
  70. foreach ($p in $pushes) {
  71.     if ($b[$p - 2] -ge 0x70 -and $b[$p - 2] -le 0x7F -and (I8 ($p - 1)) -gt 0) {
  72.         # short jcc that skips the error when the check passes -> always jump
  73.         $changes += , @(($p - 2), [byte[]]@(0xEB))
  74.         $allowedTargets += $p + (I8 ($p - 1))
  75.     }
  76.     elseif ($b[$p - 6] -eq 0x0F -and $b[$p - 5] -ge 0x80 -and $b[$p - 5] -le 0x8F -and (I32 ($p - 4)) -gt 0) {
  77.         # near jcc variant -> nop + jmp with the same displacement
  78.         $changes += , @(($p - 6), [byte[]]@(0x90, 0xE9))
  79.         $allowedTargets += $p + (I32 ($p - 4))
  80.     }
  81.     elseif ($b[$p - 2] -eq 0xEB -and (I8 ($p - 1)) -gt 0) {
  82.         $alreadyPatched++; $allowedTargets += $p + (I8 ($p - 1))
  83.     }
  84.     elseif ($b[$p - 5] -eq 0xE9 -or $b[$p - 2] -eq 0xEB -or $b[$p - 1] -eq 0xC3) {
  85.         # only reachable by jumping to it: find the 'test ... / jcc <here>' that does
  86.         $hits = @()
  87.         for ($j = $p - 0x80; $j -lt $p - 2; $j++) {
  88.             $isTest = ($b[$j - 2] -in 0x84, 0x85 -and $b[$j - 1] -ge 0xC0) -or $b[$j - 2] -eq 0xA8
  89.             if ($isTest -and $b[$j] -ge 0x70 -and $b[$j] -le 0x7F -and $j + 2 + (I8 ($j + 1)) -eq $p) { $hits += $j }
  90.         }
  91.         if ($hits.Count -eq 1) { $changes += , @($hits[0], [byte[]]@(0x90, 0x90)) }
  92.         elseif ($hits.Count -eq 0) { $alreadyPatched++ }   # jump already removed
  93.         else { Fail ("Ambiguous jumps to the check at file offset 0x{0:X}." -f $p) }
  94.     }
  95.     else { Fail ("Unrecognised code around file offset 0x{0:X}." -f $p) }
  96. }
  97.  
  98. # --- 4. sanity: every 'check passed' jump must go to the same place ---
  99. if (@($allowedTargets | Select-Object -Unique).Count -ne 1) { Fail "The checks don't all lead to the same place - unexpected layout." }
  100.  
  101. if ($changes.Count -eq 0) { Write-Host 'Already patched.'; return }
  102. if ($alreadyPatched -gt 0) { Fail 'File looks partly patched. Use -Restore first.' }
  103.  
  104. foreach ($c in $changes) {
  105.     $old = ($b[$c[0]..($c[0] + $c[1].Length - 1)] | ForEach-Object { $_.ToString('X2') }) -join ' '
  106.     $new = ($c[1] | ForEach-Object { $_.ToString('X2') }) -join ' '
  107.     Write-Host ("  0x{0:X}: {1} -> {2}" -f $c[0], $old, $new)
  108. }
  109. if ($WhatIf) { Write-Host 'WhatIf: no changes written.'; return }
  110.  
  111. if (-not (Test-Path $backup)) { Copy-Item $Path $backup; Write-Host "Backup saved to $backup" }
  112. foreach ($c in $changes) { for ($i = 0; $i -lt $c[1].Length; $i++) { $b[$c[0] + $i] = $c[1][$i] } }
  113. [IO.File]::WriteAllBytes($Path, $b)
  114. Write-Host 'Patched. In game, open the console (~) and try: ai.killBotName <BotName>'
  115.  
Tags: bf1942
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment