HGLOBAL question
-
Hey all, I had a post on here about a week ago about executing code from memory instead of a PE file. So far this is what I've done. have this as the test code compiled ############################################ #include int main(int argc, char* argv[]) { printf("Hello World!\n"); getc(stdin); return 0; } compiled that and stored it as RCDATA resource 130 in my new project ############################################ this is the code I'm trying to execute it with, without writing it back to a file in my MFC dialog app HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int (*ptr)(int argc, char *str[]); ptr = (int (__cdecl *)(int, char *[]))hbytes; int j = 1; char *strs; (*ptr)(j, &strs); CODE DONE ###################################### MODIFIED: I also tried reallocating it with a FIXED flag, and it also didn't work ############ TRIED THIS TOO HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int size = (UINT)SizeofResource(AfxGetResourceHandle(), hrInfo); HGLOBAL pexec = GlobalReAlloc(hbytes, size, GMEM_FIXED); int (*ptr)(int argc, char* argv[]); ptr = (int (__cdecl *)(int, char *[]))pexec; int j = 1; char *strs; (*ptr)(j, &strs); ################ OK, the function I cast it to matches that of the main of the test code. BUT, it gives me an access violation. WHY? HGLOBAL DATA is supposed to be executable from memory without any special function right?, Will this work with the HGLOBAL as is, or do I need to explicitly GlobalAlloc it? I realize that this may be an advanced topic, and I urge some people to refrain from replying as that my last post got a couple of very ignorant responses. Thanks for your understanding.
-
Hey all, I had a post on here about a week ago about executing code from memory instead of a PE file. So far this is what I've done. have this as the test code compiled ############################################ #include int main(int argc, char* argv[]) { printf("Hello World!\n"); getc(stdin); return 0; } compiled that and stored it as RCDATA resource 130 in my new project ############################################ this is the code I'm trying to execute it with, without writing it back to a file in my MFC dialog app HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int (*ptr)(int argc, char *str[]); ptr = (int (__cdecl *)(int, char *[]))hbytes; int j = 1; char *strs; (*ptr)(j, &strs); CODE DONE ###################################### MODIFIED: I also tried reallocating it with a FIXED flag, and it also didn't work ############ TRIED THIS TOO HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int size = (UINT)SizeofResource(AfxGetResourceHandle(), hrInfo); HGLOBAL pexec = GlobalReAlloc(hbytes, size, GMEM_FIXED); int (*ptr)(int argc, char* argv[]); ptr = (int (__cdecl *)(int, char *[]))pexec; int j = 1; char *strs; (*ptr)(j, &strs); ################ OK, the function I cast it to matches that of the main of the test code. BUT, it gives me an access violation. WHY? HGLOBAL DATA is supposed to be executable from memory without any special function right?, Will this work with the HGLOBAL as is, or do I need to explicitly GlobalAlloc it? I realize that this may be an advanced topic, and I urge some people to refrain from replying as that my last post got a couple of very ignorant responses. Thanks for your understanding.
The topic is advanced, and using that kind of trickery requires skills and most importantly reason. I am not even going to discuss what possible reason you have to write code like this. Lets concentrate on what you actually trying to do: Beer26 wrote: HGLOBAL pexec = GlobalReAlloc(hbytes, size, GMEM_FIXED); I am not completely sure what you expect from the statement above. I assume you want to create a copy of the resource in memory (I do not think GMEM_FIXED is going to be honored in Win32) You might be better off allocating new block of memory, insteadof realloc-ing something that you did not alloc-ed. Beer26 wrote: ptr = (int (__cdecl *)(int, char *[]))pexec; I read the line above as - you assume that recourse you loaded is actually executable code of the function with signature "int (*ptr)(int argc, char* argv[]);" (please confirm). Are you sure that it is a valid executable code? Beer26 wrote: int j = 1; char *strs; (*ptr)(j, &strs); the line above actually try to execute the loaded code. (please confirm). Please note also that strs is not initialized. How did you compile it? How did you put into resource? How are calls "getc" or "printf" going to be resolved ?
-
Hey all, I had a post on here about a week ago about executing code from memory instead of a PE file. So far this is what I've done. have this as the test code compiled ############################################ #include int main(int argc, char* argv[]) { printf("Hello World!\n"); getc(stdin); return 0; } compiled that and stored it as RCDATA resource 130 in my new project ############################################ this is the code I'm trying to execute it with, without writing it back to a file in my MFC dialog app HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int (*ptr)(int argc, char *str[]); ptr = (int (__cdecl *)(int, char *[]))hbytes; int j = 1; char *strs; (*ptr)(j, &strs); CODE DONE ###################################### MODIFIED: I also tried reallocating it with a FIXED flag, and it also didn't work ############ TRIED THIS TOO HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int size = (UINT)SizeofResource(AfxGetResourceHandle(), hrInfo); HGLOBAL pexec = GlobalReAlloc(hbytes, size, GMEM_FIXED); int (*ptr)(int argc, char* argv[]); ptr = (int (__cdecl *)(int, char *[]))pexec; int j = 1; char *strs; (*ptr)(j, &strs); ################ OK, the function I cast it to matches that of the main of the test code. BUT, it gives me an access violation. WHY? HGLOBAL DATA is supposed to be executable from memory without any special function right?, Will this work with the HGLOBAL as is, or do I need to explicitly GlobalAlloc it? I realize that this may be an advanced topic, and I urge some people to refrain from replying as that my last post got a couple of very ignorant responses. Thanks for your understanding.
a) there is no guarantee that you have EXECUTE right on a HGLOBAL, but this doesn#t matter on Wintel Boxes b) You *are+ aware that EXE files come a) with a PE header, and b) with a relocation table? They are not just "sequences of code bytes"...
"Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
sighist | Agile Programming | doxygen -
a) there is no guarantee that you have EXECUTE right on a HGLOBAL, but this doesn#t matter on Wintel Boxes b) You *are+ aware that EXE files come a) with a PE header, and b) with a relocation table? They are not just "sequences of code bytes"...
"Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
sighist | Agile Programming | doxygen"a) there is no guarantee that you have EXECUTE right on a HGLOBAL, but this doesn#t matter on Wintel Boxes" From MSDN "Remarks If the heap does not contain sufficient free space to satisfy the request, GlobalAlloc returns NULL. Because NULL is used to indicate an error, virtual address zero is never allocated. It is, therefore, easy to detect the use of a NULL pointer. Memory allocated with this function is guaranteed to be aligned on an 8-byte boundary. All memory is created with execute access; no special function is required to execute dynamically generated code. " "b) You *are+ aware that EXE files come a) with a PE header, and b) with a relocation table? They are not just "sequences of code bytes"..." Yes, I am. What if i write a small bit of code in ASM and only generate the OPCODE? Then try it with that? Would that work? I'm assuming it's not going to work with all the PE junk linked and compiled with it?
-
Hey all, I had a post on here about a week ago about executing code from memory instead of a PE file. So far this is what I've done. have this as the test code compiled ############################################ #include int main(int argc, char* argv[]) { printf("Hello World!\n"); getc(stdin); return 0; } compiled that and stored it as RCDATA resource 130 in my new project ############################################ this is the code I'm trying to execute it with, without writing it back to a file in my MFC dialog app HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int (*ptr)(int argc, char *str[]); ptr = (int (__cdecl *)(int, char *[]))hbytes; int j = 1; char *strs; (*ptr)(j, &strs); CODE DONE ###################################### MODIFIED: I also tried reallocating it with a FIXED flag, and it also didn't work ############ TRIED THIS TOO HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int size = (UINT)SizeofResource(AfxGetResourceHandle(), hrInfo); HGLOBAL pexec = GlobalReAlloc(hbytes, size, GMEM_FIXED); int (*ptr)(int argc, char* argv[]); ptr = (int (__cdecl *)(int, char *[]))pexec; int j = 1; char *strs; (*ptr)(j, &strs); ################ OK, the function I cast it to matches that of the main of the test code. BUT, it gives me an access violation. WHY? HGLOBAL DATA is supposed to be executable from memory without any special function right?, Will this work with the HGLOBAL as is, or do I need to explicitly GlobalAlloc it? I realize that this may be an advanced topic, and I urge some people to refrain from replying as that my last post got a couple of very ignorant responses. Thanks for your understanding.
__asm { jmp (start of your code); return; }
-
__asm { jmp (start of your code); return; }
-
"a) there is no guarantee that you have EXECUTE right on a HGLOBAL, but this doesn#t matter on Wintel Boxes" From MSDN "Remarks If the heap does not contain sufficient free space to satisfy the request, GlobalAlloc returns NULL. Because NULL is used to indicate an error, virtual address zero is never allocated. It is, therefore, easy to detect the use of a NULL pointer. Memory allocated with this function is guaranteed to be aligned on an 8-byte boundary. All memory is created with execute access; no special function is required to execute dynamically generated code. " "b) You *are+ aware that EXE files come a) with a PE header, and b) with a relocation table? They are not just "sequences of code bytes"..." Yes, I am. What if i write a small bit of code in ASM and only generate the OPCODE? Then try it with that? Would that work? I'm assuming it's not going to work with all the PE junk linked and compiled with it?
Beer26 wrote: All memory is created with execute access; Oops, sorry, I missed that (it's late over here ;-O ) b) yes that should work (as long as you refrain from absolute jumps/calls (or adress them correctly, with regards to the allocated base address) If it doesn't, try to follow the thing in the debugger (disassembly window)
"Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
sighist | Agile Programming | doxygen