Single branching in ROP
Single branching in ver A
Notes: All addresses mentioned below is for verA
Traditional Branching Method (1:3B8EH Method)
Initial Setup: Single branching method requires a boolean-like address and stores the result into register
R0to0x00or0x01accordingly.Below are some basic boolean addresses:
0:9AE6: If
ER0 = ER2, thenR0 = 1, elseR0 = 0. Additionally, ifR0is between0x4Eand0x5F,R0is also set to 1, and the function returns viaRT.
(Since the keycode valueKIis not expected to fall within0x4Eto0x5F, this address is suitable for keycode checks.)1:9618: If
ER2 = ER4, thenR0 = 1; otherwise,R0 = 0.
Note: This function resets all registers, includingLR.0:C790: If
ER0 > ER2, thenR0 = 0; otherwise,R0 = 1.
The function returns viaRT.0:C7A8: If
ER2 > ER0, thenR0 = 0; otherwise,R0 = 1.
The function returns viaRT.
- Branching: After assigning a flag to
R0, call the1:3B8Efunction.- This function multiplies
R0withR2, and storing the result inER0 - It then sets
ER2to the value ofER0and add an offset ofER4toER0. - The function ends with
RTso make sure tosetlrbefore calling it.
- This function multiplies
Address Setup: Place the smaller jump address in
ER4and the difference between the jump addresses inR2. When calling1:3B8E: IfR0 = 0: MultiplyingR0byR2yields0, and adding it toER4still returnsER4, giving the smaller address inER0IfR0 = 1: MultiplyingR0byR2givesR2, which is the address difference, and adding it toER4returns the larger address inER0Jumping: After obtaining the jump address in
ER0, call1:7B22to load the address intoER14and jump withMOV SP, ER14. The brach is completed. However it ends withPOP XR0so all addresses have to pad with-4 bytes
Improved Method (1:3B7E Method):
- Address Setup: After assigning the flag to
R0(aka completing the boolean step):- Call
1:4BD6to setR2toR0(MOV ER2, ER0) - Call
1:893Aand assign the smaller address inER0and the address difference inR4
- Call
- Jumping:
- Call
1:3B7E, the logic behind this is relatively similar to the former method. It stores the jump address inER4(The instructionMOV R8, R8serves no actual purpose here.)
- Finally, call
1:939Eto setSPtoER4and jump, completing the branch.
- Call
- Adjustments:
- Note that after setting
SPtoER4, the actual jump occurs toER4 + 0x3E(ADD SP, +32Hand pop 16 bytes)
- Note that after setting
Comparison Between Methods:
- Advantages of the new method:
1:3B7Ecan use1:893Ato assign values to two registers in one call.- Does not require passing
ER0toER14 - Takes up less memory than the old method
- Advantages of the old method:
1:3B8Estores the jump address inER14, allowing more options to choose jump functions to control the subsequentPOPinstructions.- The new method
1:3B7Ecan only use1:939Efor jumping, which resetsXR4andQR8during thePOPinstructions.




