I am fairly new to reversing so apologies in advance if any terminology is incorrect.
I am currently using ghidra on windows to look at the instructions/decompilation of a binary and I am looking to add some instructions to an existing function to change its behaviour. In this case, it is fairly trivial as I only want to add a fixed value to an existing function parameter, but I would also like some information on more advanced cases where the inserted code is slightly more complex.
I made a small test program to test this out and I managed to add the instruction by editing the binary in a hex editor and simply shifting the function bytes and inserting my own. However I realized that this is only possible because there was a bunch of empty memory following the function, so I could just shift them all down but this isn't always the case.
**************************************************************
* FUNCTION *
**************************************************************
ulonglong __fastcall FUN_140011810(int param_1, int para
ulonglong RAX:8 <RETURN>
int ECX:4 param_1
int EDX:4 param_2
undefined4 Stack[0x10]:4 local_res10 XREF[3]: 140011810(W),
140011835(R),
140011848(R)
undefined4 Stack[0x8]:4 local_res8 XREF[2]: 140011814(W),
14001184e(R)
undefined1 Stack[-0x10]:1 local_10 XREF[1]: 140011867(*)
undefined4 Stack[-0xf4]:4 local_f4 XREF[4]: 140011858(W),
14001185b(R),
140011861(W),
140011864(R)
undefined1 Stack[-0xf8]:1 local_f8 XREF[1]: 140011821(*)
FUN_140011810 XREF[1]: thunk_FUN_140011810:14001104b(T),
thunk_FUN_140011810:14001104b(j)
140011810 89 54 24 10 MOV dword ptr [RSP + local_res10],param_2
140011814 89 4c 24 08 MOV dword ptr [RSP + local_res8],param_1
140011818 55 PUSH RBP
140011819 57 PUSH RDI
14001181a 48 81 ec SUB RSP,0x108
08 01 00 00
140011821 48 8d 6c LEA RBP=>local_f8,[RSP + 0x20]
24 20
140011826 48 8b fc MOV RDI,RSP
140011829 b9 42 00 MOV param_1,0x42
00 00
14001182e b8 cc cc MOV EAX,0xcccccccc
cc cc
140011833 f3 ab STOSD.REP RDI
140011835 8b 8c 24 MOV param_1,dword ptr [RSP + local_res10]
28 01 00 00
14001183c 48 8d 0d LEA param_1,[DAT_140021008] = 01h
c5 f7 00 00
140011843 e8 44 f8 CALL thunk_FUN_140011e80 undefined thunk_FUN_140011e80(ch
ff ff
140011848 8b 85 08 MOV EAX,dword ptr [RBP + local_res10]
01 00 00
14001184e 8b 8d 00 MOV param_1,dword ptr [RBP + local_res8]
01 00 00
140011854 03 c8 ADD param_1,EAX
140011856 8b c1 MOV EAX,param_1
140011858 89 45 04 MOV dword ptr [RBP + local_f4],EAX
14001185b 8b 45 04 MOV EAX,dword ptr [RBP + local_f4]
14001185e 83 c0 0a ADD EAX,0xa
140011861 89 45 04 MOV dword ptr [RBP + local_f4],EAX
140011864 8b 45 04 MOV EAX,dword ptr [RBP + local_f4]
140011867 48 8d a5 LEA RSP=>local_10,[RBP + 0xe8]
e8 00 00 00
14001186e 5f POP RDI
14001186f 5d POP RBP
140011870 c3 RET
140011871 cc ?? CCh
140011872 cc ?? CCh
140011873 cc ?? CCh
140011874 cc ?? CCh
140011875 cc ?? CCh
140011876 cc ?? CCh
140011877 cc ?? CCh
140011878 cc ?? CCh
140011879 cc ?? CCh
14001187a cc ?? CCh
14001187b cc ?? CCh
14001187c cc ?? CCh
14001187d cc ?? CCh
14001187e cc ?? CCh
14001187f cc ?? CCh
140011880 cc ?? CCh
140011881 cc ?? CCh
140011882 cc ?? CCh
Specifically, 14001185e 83 c0 0a ADD EAX,0xa
I could duplicate this instruction and change 0xa
to alter the output value.
In the more complex binary I have a larger function with similar parameters, except there is no additional memory at the end of the function so this approach to shift the remaining bytes wouldn't work as there is another function directly below. I also can't remove any of the current instructions to make space as that might break existing functionality. There is plenty of empty memory elsewhere in the binary so I thought of adding a jmp instruction to perform some instructions, and then jumping back but some of the instructions use local variables so I'm unsure if this will work.
So given the above example, and none of the extra memory at the end of the function, how can I insert some custom instructions?