I'm trying to essentially patch in a dll a far call to my own address instead of the one compiled already.
Here is the bytes I am trying to patch :
{ 0xFF, 0x15, 0x30, 0x20, 0x00, 0x10 }
Which should translate to : call DWORD PTR ds:0x10002030
Which I believe means the same thing as : call [0x10002030]
Yes, I am trying to patch an actual call to an IAT function to something else. Because the iat is essentially storing a integer that gets later assigned to the actual address of the api function, to call the function we have to dereference the address given to us from the IAT.
Now because I want to patch this to go to my own function, I am not going to be needing to dereference anything. Now the issue is the original dereference call is 6 bytes long, so I need to stay at 6 bytes or lower.
I'm trying to essentially find a way to do a call to an address, which is not relative (important!) and is not inside the current module I am in, and finally do it in less than or equal to 6 bytes.
I want something like so :
call 0xDEADBEEF
but I don't want 0xDEADBEEF to be a relative address. I want to directly call 0xDEADBEEF
To reiterate, I need to specifically patch this call, there is no other way around it. A normal IAT hook will not do in this situation.
Edit :
I've been looking into it some more and it seems that
push 0xdeadbeef
ret
might work, but I have found an issue. Because I have to handle the return myself, I can not do that in 6 bytes. But I think I know a way I can.
Original call goes from : call [0x10002030]
to :
push jFunction
ret
And jFunction would be essentially a stub of sorts :
mov eax, 0xDEADBEEF
call eax
But now the issue is how do I get back to my original execution?
Using a simple
pop ebx
ret
causes it to jmp back to the original main function, not to the instruction after the ret.
As I stated before because of the packer I am using, I can not use relative offsets, so again in this situation I cannot determine the relative offset between the start of the main function and the instruction I would like to go to. And I cannot push the wanted address to the "jFunction" because I only have 6 bytes to deal with.
I'm starting to think this is impossible without relative addresses.
Here is my current test code : x86 (debug) MSVC 2015 I am using inline assembly and __declspec(naked) to easily debug the solution instead of patching what I need and testing.