Easy bytes for asm

08/04/2020 04:31 [Beatrice]#1
Hey. Although I'm aware that AutoIt is not the best way for code injection and such, I use it quite often (especially for calling WinApi in asm). The number one problem is calculation of the relative addresses on injection site. So I wrote a small UDF that calculates them for me instead of me bothering every single time. Hope this helps someone out there.

Code:
#include <String.au3>

Func CalculateBytes($dwAddress,$sByte)
	If Not IsBinary($dwAddress) Then $dwAddress = "0x" & Hex($dwAddress,8)
	If Not StringInStr($sByte,"0x") Then $sByte = "0x" & $sByte
	$aStat = _StringBetween($sByte,"-","-",1)
	If IsArray($aStat) Then
		For $i = 0 To UBound($aStat)-1
			Local $sAddress = StringReplace($aStat[$i],"0x","")
			Local $sReversedAddress = ""
				For $b = 7 To 1 Step -2
					$sReversedAddress = $sReversedAddress & StringMid($sAddress, $b, 2)
				Next
			$sByte = StringReplace($sByte,"-" & $aStat[$i] & "-",$sReversedAddress)
		Next
	EndIf
	Do
		Local $iOccurance = StringInStr($sByte,"|")
		If Not $iOccurance Then ExitLoop
		Local $iOccurance2 = StringInStr($sByte,"|",0,1,$iOccurance+1)
		Local $sCalcAddress = StringMid($sByte,$iOccurance+1,$iOccurance2-$iOccurance-1)
		If Not StringInStr($sCalcAddress,"0x") Then $sCalcAddress = "0x" & $sCalcAddress
		Local $sCalcDist = Hex(Execute(($sCalcAddress - ($dwAddress+($iOccurance-1)/2)  - 4) +1),8)
		Local $sNewAddress = ""
			For $i = StringLen($sCalcDist) - 1 To 1 Step -2
				$sNewAddress = $sNewAddress & StringMid($sCalcDist, $i, 2)
			Next
		$sByte = StringLeft($sByte,$iOccurance - 1) & $sNewAddress & StringRight($sByte,(StringLen($sByte)-$iOccurance2))
	Until StringInStr($sByte,"|") = 0
Return $sByte
EndFunc
While wrapping an address in "-" results in reversed bytes (such as for a push), wrapping in "|" results in the relative distance of page+previous bytes to desired address. (calls jmps etc.) Make sure all your addresses are in 0x hex format.

Code:
CalculateBytes($dwPage,"0x6A0068-" & $lpCaption & "-68-" & $lpText & "-6A00E8|" & $dwMessageBoxA & "|C3")
Example for calling MessageBoxA in asm is attached.