winapi - EnumProcessModulesEx and CreateToolhelp32Snapshot fails - whatever 32bit or 64bit -
edit:
the answer of question here:
https://stackoverflow.com/a/27317947/996540
when create project in msvc, option /dynamicbase default enabled now. because of aslr(address space layout randomization, since windows vista), everytime run exe, it's load address random.
i doing dll injection job recently, did research on google, , have read projects. load address (base address) of exe important.
it seems there're 2 simple apis this: enumprocessmodulesex , createtoolhelp32snapshot. never succeeded.
so code sample:
void testenumprocessmodulesex(const char* app) { std::cout << "begin testenumprocessmodulesex(" << mybit() << ")" << std::endl; startupinfoa startupinfo = {0}; startupinfo.cb = sizeof(startupinfo); process_information processinformation = {0}; if (createprocessa(app, null, null, null, false, create_suspended, null, null, &startupinfo, &processinformation)) { std::vector<hmodule> buf(128); dword needed = 0; (;;) { if (enumprocessmodulesex(processinformation.hprocess, &buf[0], dword(buf.size()*sizeof(hmodule)), &needed, list_modules_all) == false) { dword ec = getlasterror(); std::cout << "getlasterror() = " << ec << std::endl; break; } else if (needed <= buf.size() * sizeof(hmodule)) { break; } else { const size_t oldsize = buf.size(); buf.resize(oldsize * 2); } } resumethread(processinformation.hthread); waitforsingleobject(processinformation.hprocess, infinite); } std::cout << "end testenumprocessmodulesex(" << mybit() << ")" << std::endl; }
to reduce length of question, complete code - including createtoolhelp32snapshot's test code - not listed here, can from:
https://dl.dropboxusercontent.com/u/235920/enum_proc_mods_sample.7z or https://www.mediafire.com/?cry3pnra8392099
"if function called 32-bit application running on wow64, can enumerate modules of 32-bit process. if process 64-bit process, function fails , last error code error_partial_copy (299)." - msdn.
and blog post question: http://winprogger.com/getmodulefilenameex-enumprocessmodulesex-failures-in-wow64/
unfortunately, not make sence, because whatever specified process 32bit or 64bit, fails 299; whatever caller process 32-bit or 64bit, fails 299.
this output of sample:
begin testenumprocessmodulesex(32bit) getlasterror() = 299 hello world 32bit end testenumprocessmodulesex(32bit) begin testenumprocessmodulesex(32bit) getlasterror() = 299 hello world 64bit end testenumprocessmodulesex(32bit) begin testenumprocessmodulesex(64bit) getlasterror() = 299 hello world 32bit end testenumprocessmodulesex(64bit) begin testenumprocessmodulesex(64bit) getlasterror() = 299 hello world 64bit end testenumprocessmodulesex(64bit)
as see, combination failed.
my os windows 7 64bit pro , compiler vs2013.
so, can ?
i have no idea unsuccess of enumprocessmodulesex , createtoolhelp32snapshot, let's leave question expert.
my goal load address (base address) of child process, find entry point , patch - reason patch entry point here: https://opcode0x90.wordpress.com/2011/01/15/injecting-dll-into-process-on-load/
since dll injection main purpose of mine, have reconsider question. use "createremotethread & loadlibrary technique" http://www.codeproject.com/articles/4610/three-ways-to-inject-your-code-into-another-proces#section_2 dll injection (in fact aslr not barrier of technique way), although there many limits in dllmain http://msdn.microsoft.com/en-us/library/windows/desktop/dn633971%28v=vs.85%29.aspx , little works ok: find base address of exe using getmodulehandlea(null), save hmodule returned shared memory, next, caller process read shared memory , hmodule. synchronization mechanism necessary of course.
so, answer ipc. (not every ipc mechanism safe in dllmain way)
Comments
Post a Comment