Skip to content

Instantly share code, notes, and snippets.

@axiieflex
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axiieflex/a5cc88ce25aff2519030 to your computer and use it in GitHub Desktop.
Save axiieflex/a5cc88ce25aff2519030 to your computer and use it in GitHub Desktop.
#RequireAdmin
#Region
#AutoIt3Wrapper_Icon=C:\Users\Speedi\Desktop\Icons\crack_256X256.ico
#AutoIt3Wrapper_OutFile=Patch_BF4_Activation.exe
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#EndRegion
Global Const $prov_rsa_full = 1
Global Const $prov_rsa_aes = 24
Global Const $crypt_verifycontext = -268435456
Global Const $hp_hashsize = 4
Global Const $hp_hashval = 2
Global Const $crypt_exportable = 1
Global Const $crypt_userdata = 1
Global Const $calg_md2 = 32769
Global Const $calg_md4 = 32770
Global Const $calg_md5 = 32771
Global Const $calg_sha1 = 32772
Global Const $calg_3des = 26115
Global Const $calg_aes_128 = 26126
Global Const $calg_aes_192 = 26127
Global Const $calg_aes_256 = 26128
Global Const $calg_des = 26113
Global Const $calg_rc2 = 26114
Global Const $calg_rc4 = 26625
Global Const $calg_userkey = 0
Global $__g_acryptinternaldata[3]
Func _crypt_startup()
If __crypt_refcount() = 0 Then
Local $hadvapi32 = DllOpen("Advapi32.dll")
If @error Then Return SetError(1, 0, False)
__crypt_dllhandleset($hadvapi32)
Local $aret
Local $iproviderid = $prov_rsa_aes
If @OSVersion = "WIN_2000" Then $iproviderid = $prov_rsa_full
$aret = DllCall(__crypt_dllhandle(), "bool", "CryptAcquireContext", "handle*", 0, "ptr", 0, "ptr", 0, "dword", $iproviderid, "dword", $crypt_verifycontext)
If @error OR NOT $aret[0] Then
DllClose(__crypt_dllhandle())
Return SetError(2, 0, False)
Else
__crypt_contextset($aret[1])
EndIf
EndIf
__crypt_refcountinc()
Return True
EndFunc
Func _crypt_shutdown()
__crypt_refcountdec()
If __crypt_refcount() = 0 Then
DllCall(__crypt_dllhandle(), "bool", "CryptReleaseContext", "handle", __crypt_context(), "dword", 0)
DllClose(__crypt_dllhandle())
EndIf
EndFunc
Func _crypt_derivekey($vpassword, $ialg_id, $ihash_alg_id = $calg_md5)
Local $aret
Local $hcrypthash
Local $hbuff
Local $ierror
Local $vreturn
_crypt_startup()
Do
$aret = DllCall(__crypt_dllhandle(), "bool", "CryptCreateHash", "handle", __crypt_context(), "uint", $ihash_alg_id, "ptr", 0, "dword", 0, "handle*", 0)
If @error OR NOT $aret[0] Then
$ierror = 1
$vreturn = -1
ExitLoop
EndIf
$hcrypthash = $aret[5]
$hbuff = DllStructCreate("byte[" & BinaryLen($vpassword) & "]")
DllStructSetData($hbuff, 1, $vpassword)
$aret = DllCall(__crypt_dllhandle(), "bool", "CryptHashData", "handle", $hcrypthash, "struct*", $hbuff, "dword", DllStructGetSize($hbuff), "dword", $crypt_userdata)
If @error OR NOT $aret[0] Then
$ierror = 2
$vreturn = -1
ExitLoop
EndIf
$aret = DllCall(__crypt_dllhandle(), "bool", "CryptDeriveKey", "handle", __crypt_context(), "uint", $ialg_id, "handle", $hcrypthash, "dword", $crypt_exportable, "handle*", 0)
If @error OR NOT $aret[0] Then
$ierror = 3
$vreturn = -1
ExitLoop
EndIf
$ierror = 0
$vreturn = $aret[5]
Until True
If $hcrypthash <> 0 Then DllCall(__crypt_dllhandle(), "bool", "CryptDestroyHash", "handle", $hcrypthash)
Return SetError($ierror, 0, $vreturn)
EndFunc
Func _crypt_destroykey($hcryptkey)
Local $aret = DllCall(__crypt_dllhandle(), "bool", "CryptDestroyKey", "handle", $hcryptkey)
Local $nerror = @error
_crypt_shutdown()
If $nerror OR NOT $aret[0] Then
Return SetError(1, 0, False)
Else
Return SetError(0, 0, True)
EndIf
EndFunc
Func _crypt_encryptdata($vdata, $vcryptkey, $ialg_id, $ffinal = True)
Local $hbuff
Local $ierror
Local $vreturn
Local $reqbuffsize
Local $aret
_crypt_startup()
Do
If $ialg_id <> $calg_userkey Then
$vcryptkey = _crypt_derivekey($vcryptkey, $ialg_id)
If @error Then
$ierror = 1
$vreturn = -1
ExitLoop
EndIf
EndIf
$aret = DllCall(__crypt_dllhandle(), "bool", "CryptEncrypt", "handle", $vcryptkey, "handle", 0, "bool", $ffinal, "dword", 0, "ptr", 0, "dword*", BinaryLen($vdata), "dword", 0)
If @error OR NOT $aret[0] Then
$ierror = 2
$vreturn = -1
ExitLoop
EndIf
$reqbuffsize = $aret[6]
$hbuff = DllStructCreate("byte[" & $reqbuffsize & "]")
DllStructSetData($hbuff, 1, $vdata)
$aret = DllCall(__crypt_dllhandle(), "bool", "CryptEncrypt", "handle", $vcryptkey, "handle", 0, "bool", $ffinal, "dword", 0, "struct*", $hbuff, "dword*", BinaryLen($vdata), "dword", DllStructGetSize($hbuff))
If @error OR NOT $aret[0] Then
$ierror = 3
$vreturn = -1
ExitLoop
EndIf
$ierror = 0
$vreturn = DllStructGetData($hbuff, 1)
Until True
If $ialg_id <> $calg_userkey Then _crypt_destroykey($vcryptkey)
_crypt_shutdown()
Return SetError($ierror, 0, $vreturn)
EndFunc
Func _crypt_decryptdata($vdata, $vcryptkey, $ialg_id, $ffinal = True)
Local $hbuff
Local $ierror
Local $vreturn
Local $htempstruct
Local $iplaintextsize
Local $aret
_crypt_startup()
Do
If $ialg_id <> $calg_userkey Then
$vcryptkey = _crypt_derivekey($vcryptkey, $ialg_id)
If @error Then
$ierror = 1
$vreturn = -1
ExitLoop
EndIf
EndIf
$hbuff = DllStructCreate("byte[" & BinaryLen($vdata) + 1000 & "]")
DllStructSetData($hbuff, 1, $vdata)
$aret = DllCall(__crypt_dllhandle(), "bool", "CryptDecrypt", "handle", $vcryptkey, "handle", 0, "bool", $ffinal, "dword", 0, "struct*", $hbuff, "dword*", BinaryLen($vdata))
If @error OR NOT $aret[0] Then
$ierror = 2
$vreturn = -1
ExitLoop
EndIf
$iplaintextsize = $aret[6]
$htempstruct = DllStructCreate("byte[" & $iplaintextsize & "]", DllStructGetPtr($hbuff))
$ierror = 0
$vreturn = DllStructGetData($htempstruct, 1)
Until True
If $ialg_id <> $calg_userkey Then _crypt_destroykey($vcryptkey)
_crypt_shutdown()
Return SetError($ierror, 0, $vreturn)
EndFunc
Func _crypt_hashdata($vdata, $ialg_id, $ffinal = True, $hcrypthash = 0)
Local $ierror
Local $vreturn = 0
Local $ihashsize
Local $aret
Local $hbuff = 0
_crypt_startup()
Do
If $hcrypthash = 0 Then
$aret = DllCall(__crypt_dllhandle(), "bool", "CryptCreateHash", "handle", __crypt_context(), "uint", $ialg_id, "ptr", 0, "dword", 0, "handle*", 0)
If @error OR NOT $aret[0] Then
$ierror = 1
$vreturn = -1
ExitLoop
EndIf
$hcrypthash = $aret[5]
EndIf
$hbuff = DllStructCreate("byte[" & BinaryLen($vdata) & "]")
DllStructSetData($hbuff, 1, $vdata)
$aret = DllCall(__crypt_dllhandle(), "bool", "CryptHashData", "handle", $hcrypthash, "struct*", $hbuff, "dword", DllStructGetSize($hbuff), "dword", $crypt_userdata)
If @error OR NOT $aret[0] Then
$ierror = 2
$vreturn = -1
ExitLoop
EndIf
If $ffinal Then
$aret = DllCall(__crypt_dllhandle(), "bool", "CryptGetHashParam", "handle", $hcrypthash, "dword", $hp_hashsize, "dword*", 0, "dword*", 4, "dword", 0)
If @error OR NOT $aret[0] Then
$ierror = 3
$vreturn = -1
ExitLoop
EndIf
$ihashsize = $aret[3]
$hbuff = DllStructCreate("byte[" & $ihashsize & "]")
$aret = DllCall(__crypt_dllhandle(), "bool", "CryptGetHashParam", "handle", $hcrypthash, "dword", $hp_hashval, "struct*", $hbuff, "dword*", DllStructGetSize($hbuff), "dword", 0)
If @error OR NOT $aret[0] Then
$ierror = 4
$vreturn = -1
ExitLoop
EndIf
$ierror = 0
$vreturn = DllStructGetData($hbuff, 1)
Else
$vreturn = $hcrypthash
EndIf
Until True
If $hcrypthash <> 0 AND $ffinal Then DllCall(__crypt_dllhandle(), "bool", "CryptDestroyHash", "handle", $hcrypthash)
_crypt_shutdown()
Return SetError($ierror, 0, $vreturn)
EndFunc
Func _crypt_hashfile($sfile, $ialg_id)
Local $hfile
Local $ierror, $vreturn
Local $hhashobject = 0
Local $btempdata
_crypt_startup()
Do
$hfile = FileOpen($sfile, 16)
If $hfile = -1 Then
$ierror = 1
$vreturn = -1
ExitLoop
EndIf
Do
$btempdata = FileRead($hfile, 512 * 1024)
If @error Then
$vreturn = _crypt_hashdata($btempdata, $ialg_id, True, $hhashobject)
If @error Then
$vreturn = -1
$ierror = 2
ExitLoop 2
EndIf
ExitLoop 2
Else
$hhashobject = _crypt_hashdata($btempdata, $ialg_id, False, $hhashobject)
If @error Then
$vreturn = -1
$ierror = 3
ExitLoop 2
EndIf
EndIf
Until False
Until True
_crypt_shutdown()
If $hfile <> -1 Then FileClose($hfile)
Return SetError($ierror, 0, $vreturn)
EndFunc
Func _crypt_encryptfile($ssourcefile, $sdestinationfile, $vcryptkey, $ialg_id)
Local $hinfile, $houtfile
Local $ierror = 0, $vreturn = True
Local $btempdata
Local $ifilesize = FileGetSize($ssourcefile)
Local $iread = 0
_crypt_startup()
Do
If $ialg_id <> $calg_userkey Then
$vcryptkey = _crypt_derivekey($vcryptkey, $ialg_id)
If @error Then
$ierror = 1
$vreturn = -1
ExitLoop
EndIf
EndIf
$hinfile = FileOpen($ssourcefile, 16)
If @error Then
$ierror = 2
$vreturn = -1
ExitLoop
EndIf
$houtfile = FileOpen($sdestinationfile, 26)
If @error Then
$ierror = 3
$vreturn = -1
ExitLoop
EndIf
Do
$btempdata = FileRead($hinfile, 1024 * 1024)
$iread += BinaryLen($btempdata)
If $iread = $ifilesize Then
$btempdata = _crypt_encryptdata($btempdata, $vcryptkey, $calg_userkey, True)
If @error Then
$ierror = 4
$vreturn = -1
EndIf
FileWrite($houtfile, $btempdata)
ExitLoop 2
Else
$btempdata = _crypt_encryptdata($btempdata, $vcryptkey, $calg_userkey, False)
If @error Then
$ierror = 5
$vreturn = -1
ExitLoop 2
EndIf
FileWrite($houtfile, $btempdata)
EndIf
Until False
Until True
If $ialg_id <> $calg_userkey Then _crypt_destroykey($vcryptkey)
_crypt_shutdown()
If $hinfile <> -1 Then FileClose($hinfile)
If $houtfile <> -1 Then FileClose($houtfile)
Return SetError($ierror, 0, $vreturn)
EndFunc
Func _crypt_decryptfile($ssourcefile, $sdestinationfile, $vcryptkey, $ialg_id)
Local $hinfile, $houtfile
Local $ierror = 0, $vreturn = True
Local $btempdata
Local $ifilesize = FileGetSize($ssourcefile)
Local $iread = 0
_crypt_startup()
Do
If $ialg_id <> $calg_userkey Then
$vcryptkey = _crypt_derivekey($vcryptkey, $ialg_id)
If @error Then
$ierror = 1
$vreturn = -1
ExitLoop
EndIf
EndIf
$hinfile = FileOpen($ssourcefile, 16)
If @error Then
$ierror = 2
$vreturn = -1
ExitLoop
EndIf
$houtfile = FileOpen($sdestinationfile, 26)
If @error Then
$ierror = 3
$vreturn = -1
ExitLoop
EndIf
Do
$btempdata = FileRead($hinfile, 1024 * 1024)
$iread += BinaryLen($btempdata)
If $iread = $ifilesize Then
$btempdata = _crypt_decryptdata($btempdata, $vcryptkey, $calg_userkey, True)
If @error Then
$ierror = 4
$vreturn = -1
EndIf
FileWrite($houtfile, $btempdata)
ExitLoop 2
Else
$btempdata = _crypt_decryptdata($btempdata, $vcryptkey, $calg_userkey, False)
If @error Then
$ierror = 5
$vreturn = -1
ExitLoop 2
EndIf
FileWrite($houtfile, $btempdata)
EndIf
Until False
Until True
If $ialg_id <> $calg_userkey Then _crypt_destroykey($vcryptkey)
_crypt_shutdown()
If $hinfile <> -1 Then FileClose($hinfile)
If $houtfile <> -1 Then FileClose($houtfile)
Return SetError($ierror, 0, $vreturn)
EndFunc
Func __crypt_refcount()
Return $__g_acryptinternaldata[0]
EndFunc
Func __crypt_refcountinc()
$__g_acryptinternaldata[0] += 1
EndFunc
Func __crypt_refcountdec()
If $__g_acryptinternaldata[0] > 0 Then $__g_acryptinternaldata[0] -= 1
EndFunc
Func __crypt_dllhandle()
Return $__g_acryptinternaldata[1]
EndFunc
Func __crypt_dllhandleset($hadvapi32)
$__g_acryptinternaldata[1] = $hadvapi32
EndFunc
Func __crypt_context()
Return $__g_acryptinternaldata[2]
EndFunc
Func __crypt_contextset($hcryptcontext)
$__g_acryptinternaldata[2] = $hcryptcontext
EndFunc
Dim $changelog = ""
Dim $getbf4installdir = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\EA Games\Battlefield 4", "Install Dir")
If @error Then
$getbf4installdir = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\EA Games\Battlefield 4", "Install Dir")
If @error Then
MsgBox(16, "Error", "Couldn't find bf4 directory!")
Exit
EndIf
EndIf
$getbf4installdir = StringReplace($getbf4installdir, "\\", "\")
Dim $activationexe = $getbf4installdir & "Core\ActivationUI.exe"
Dim $activation64dll = $getbf4installdir & "Core\Activation64.dll"
Dim $activation32dll = $getbf4installdir & "Core\Activation.dll"
Dim $activationexemd5 = _crypt_hashfile($activationexe, $calg_md5)
Dim $patchmd5 = String("0x9AC4E77ABCD8B7FD90F74D81F2A958BC")
If $activationexemd5 = $patchmd5 Then
$changelog &= "[!]ERROR: ActivationUI.exe already patched!" & @CRLF
Else
FileMove($activationexe, $getbf4installdir & "Core\ActivationUI_backup.exe")
Sleep(500)
FileInstall("ActivationUI.exe", $activationexe, 1)
$changelog &= "[+] ActivationUI.exe Patched!" & @CRLF
EndIf
Dim $activation64dllmd5 = _crypt_hashfile($activation64dll, $calg_md5)
Dim $dllpatchmd5 = String("0xC2E9C14182CE4B051B836B7E682DC53E")
If $activation64dllmd5 = $dllpatchmd5 Then
$changelog &= "[!]ERROR: Activation64.dll already patched!" & @CRLF
Else
FileMove($activation64dll, $getbf4installdir & "Core\Activation64_backup.dll")
Sleep(500)
FileInstall("Activation64.dll", $activation64dll, 1)
$changelog &= "[+] Activation64.dll Patched!" & @CRLF
EndIf
Dim $activation32dllmd5 = _crypt_hashfile($activation32dll, $calg_md5)
Dim $dll32patchmd5 = String("0x1767DEBE484A9482B443808B74DF62A8")
If $activation32dllmd5 = $dll32patchmd5 Then
$changelog &= "[!]ERROR: Activation.dll already patched!" & @CRLF
Else
FileMove($activation32dll, $getbf4installdir & "Core\Activation_backup.dll")
Sleep(500)
FileInstall("Activation.dll", $activation32dll, 1)
$changelog &= "[+] Activation.dll Patched!" & @CRLF
EndIf
MsgBox(48, "Patched", $changelog)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment