why06 (03-28-2011)
Anyone know of any compile-time macros that could be used? I've been searching for awhile and got some crap solutions that were very unpredictable and unreliable.
Anyway, I was writing an injector and I wrote my own solution - I'm not sure how applicable it is though but it should be reliable, does anyone want to give it a shot?
NOTE: The epilog(the part that determines the size of the function at runtime) is not supposed to be factored into the function's size.
Also: If you have variables local to the function you need to make sure the compiler isn't using eax for those local variables.
*edit* Made it more neat, I just quickly hacked this up.
Code:#include "stdafx.h" #include <stdio.h> #include <windows.h> const unsigned long SIGOFSIZE = 'SGSZ'; const void* LOADLIBA = (void*)LoadLibraryA; //Should be constant for every application... void __declspec(naked) inject() { start: __asm { push ebp; mov ebp, esp; mov eax, dword ptr [ebp + 0x8]; cmp eax, SIGOFSIZE; jz getSize; push eax; call LOADLIBA; pop ebp ret 0x4; }; getSize: __asm { mov eax, getSize; sub eax, start; pop ebp ret 0x4; } }; int _tmain(int argc, _TCHAR* argv[]) { unsigned long cb; printf("Alterations Injector\n"); __asm { push SIGOFSIZE; call inject; mov cb, eax; } printf("Injectee is of %u bytes in length..\n", cb); return 0; }
Last edited by radnomguywfq3; 03-28-2011 at 04:11 PM.
There are two types of tragedies in life. One is not getting what you want, the other is getting it.
If you wake up at a different time in a different place, could you wake up as a different person?
why06 (03-28-2011)
Add another empty naked right behind it e.g.
Hasn't fucked up yet for me :3Code:void __declspec(naked) myFunction(void) { //code } void __declspec(naked) myFunctionEnd(void) { }
Ah we-a blaze the fyah, make it bun dem!
Last edited by radnomguywfq3; 03-28-2011 at 08:50 AM.
There are two types of tragedies in life. One is not getting what you want, the other is getting it.
If you wake up at a different time in a different place, could you wake up as a different person?
INT3 and an exception handler then? :P
Ah we-a blaze the fyah, make it bun dem!
There are two types of tragedies in life. One is not getting what you want, the other is getting it.
If you wake up at a different time in a different place, could you wake up as a different person?
What is this supposed to do? const unsigned long SIGOFSIZE = 'SGSZ';
I guess it is pretty complicated to get the size of a naked function at runtime. I've never seen a simple solution, but I never looked much either.
Here's maybe one other solution:
Code:GetSize(int beg,int end) { int size = end - beg; return size; } int Function(void) { _asm push EIP; //function code here... _asm { push EIP; push [ESP + C]; ret; } int size = GetSize(esp, esp+4); //rebalance stack... I think, my math may be wrong stack confuses me... _asm { pop ebx; pop ebx; } return size; }
Last edited by why06; 03-28-2011 at 10:04 AM.
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
radnomguywfq3 (03-28-2011)
Look at the code snippet again. I pasted the wrong version in there.
Your solution is confusing :/ GetSize should in theory be handling the data incorrectly(though most compilers see int as 4 bytes it should still be of type DWORD(unsigned long)).
Also your code misses the prolog-eiplog evident in(I assume) the cdecl calling convention which would normally be the default. This wouldn't be an issue if your calling convention was void of any prolog or epilog but assuming the cdecl calling convention your going to get a clip of the routine if you use the size returned.
mmz then you push the instruction pointer twice(at the start and end of the function body) which make sense but then you push the third parameter which is then being called(indirectly via ret instruction). You should be finding the difference of these two by making a call to getsize.
I see where you were going with this though and it's not a bad idea.
I'm going to inject this block of executable code into a remotely allocated memory region via VirtualAllocEx and the initiate a remote thread at it's base. At which point it will load a library via the LoadLibraryA api inside of the targets address space - Dll-Injection.
*edit*
Here, this is my final solution:
Code:#include "stdafx.h" #include <stdio.h> #include <windows.h> const unsigned long SIGOFSIZE = 'SGSZ'; const void* LOADLIBA = (void*)LoadLibraryA; //Should be constant for every application... void __declspec(naked) inject() { start: __asm { push ebp; mov ebp, esp; mov eax, dword ptr [ebp + 0x8]; cmp eax, SIGOFSIZE; jz getSize; push eax; call LOADLIBA; pop ebp ret 0x4; }; getSize: __asm { mov eax, getSize; sub eax, start; pop ebp ret 0x4; } }; int _tmain(int argc, _TCHAR* argv[]) { unsigned long cb; printf("Alterations Injector\n"); __asm { push SIGOFSIZE; call inject; mov cb, eax; } printf("Injectee is of %u bytes in length..\n", cb); return 0; }
Last edited by radnomguywfq3; 03-28-2011 at 04:13 PM.
There are two types of tragedies in life. One is not getting what you want, the other is getting it.
If you wake up at a different time in a different place, could you wake up as a different person?
Hell_Demon (03-29-2011),why06 (03-28-2011)