Re: Dynamic Disassembler (determine main() location at runtime)
- From: "Jeffrey Walton" <noloader@xxxxxxxxx>
- Date: 9 Feb 2007 10:56:17 -0800
1. I would enumerate the sections in the image (PE header) and look for theIt appears this is the method which will work somewhat reliably
one with the lowest address that contains the IMAGE_SCN_MEM_EXECUTE
characteristic.
(however, one cannot use MEM_IMAGE).
I hate the thought of "brute forcing" a search staing at
SYSTEM_INFORMATION minimun and Maximum address...
2. This information is available from processing the section headerMore bridges I have not crossed...
corresponding to `.text'. Please note that there is no requirement that the
code section be named `.text', and there is furthermore no requirement that
there must only be one section with code. Also, there could be `dead space'
after the section for alignment reasons.
Please note that there is no requirement that theIn this case, how does the loader/linker know the "executable"
code section be named `.text',
segment?
Also, there could be `dead space'I expect this. The current dead space seems to be 0x00 and 0xCC on
after the section for alignment reasons.
disk. Windows 2000 allocates Virtual Memory on 64KB boundaries for the
application (even though my current Intel x86 uses 4 KB pages). So, I
may see more dead space with a new BYTE fill character.
3. For the entrypoint address invoked by the loader, look atInterestingly, this seems to be placed at the 'End of the Executable'
`AddressOfEntryPoint' RVA field in the IMAGE_OPTIONAL_HEADER.
in Memory. Startup Code, nhen, a CALL to an earlier are of executable
(main, wmain, etc).
BTW, I don't see why you would care if there is a jump stub for main or not;A flag in the sand - I helps with the discovery/learning of what is
it is executable and will produce the same result regardless. The OS loader
certainly doesn't care.
really going on. If I determine that in memory main() is located at
0x00401E20, and I'm working around 0x004182E6, I can probably say I'm
off the mark.
Jeff
On Feb 8, 12:09 pm, "Skywing [MVP]"
<skywing_NO_SP...@xxxxxxxxxxxxxxxxxxx> wrote:
1. I would enumerate the sections in the image (PE header) and look for the
one with the lowest address that contains the IMAGE_SCN_MEM_EXECUTE
characteristic.
2. This information is available from processing the section header
corresponding to `.text'. Please note that there is no requirement that the
code section be named `.text', and there is furthermore no requirement that
there must only be one section with code. Also, there could be `dead space'
after the section for alignment reasons.
3. For the entrypoint address invoked by the loader, look at
`AddressOfEntryPoint' RVA field in the IMAGE_OPTIONAL_HEADER. For other
functions, without them being exported, you would have to rely on either
debug symbols or code flow analysis.
BTW, I don't see why you would care if there is a jump stub for main or not;
it is executable and will produce the same result regardless. The OS loader
certainly doesn't care.
--
Ken Johnson (Skywing)
Windows SDK MVPhttp://www.nynaeve.net"Jeffrey Walton" <noloa...@xxxxxxxxx> wrote in message
news:1170891286.106224.274650@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi Ken,
... but you didn't really describe what problem you were havingBasically, I desire three pieces of information. The 'in memory' is
other than listing some observations about how there might be a jump stub
for main (perhaps due to incremental linking)...
the caveat:
* First code page in memory
* Size of .text section in memory
* Location of function main()
One - First code page in memory:
Open Question
Two - Size of .text section in memory
I _think_ this is the same as on disk (considering the padding I am
observing based on page size boundaries).
Three - Location of function
I _think_ I can find an arbitrary function (my example uses main())
whether in Debug or Release.
Any comments would be greatly appreciated.
Jeff
int _tmain(int argc, _TCHAR* argv[])
{
const UINT PATH_SIZE = 2 * MAX_PATH;
TCHAR szFilename[ PATH_SIZE + 1 ] = { 0 };
__try {
/////////////////////////////////////////////////
/////////////////////////////////////////////////
if( 0 == GetModuleFileName( NULL, szFilename, PATH_SIZE ) )
{
std::cout << _T("Error Retrieving Process Filename") <<
std::endl;
__leave;
}
std::cout << _T("File: ") << szFilename << std::endl <<
std::endl;
/////////////////////////////////////////////////
/////////////////////////////////////////////////
PVOID pfnMain = (PVOID) &_tmain;
std::cout << _T("Original main(): ") << pfnMain << std::endl;
/////////////////////////////////////////////////
/////////////////////////////////////////////////
{
PBYTE pPossibleJump = static_cast<PBYTE>(pfnMain);
BYTE opcode = *pPossibleJump;
if( 0xE9 /* Jump */ != opcode )
{
std::cout << _T("main() is not a jump opcode... No
fixup applied");
std::cout << std::endl << std::endl;
}
else
{
DWORD dwJump =
*( reinterpret_cast<PDWORD>(pPossibleJump+1) );
pfnMain = pPossibleJump + dwJump + sizeof(opcode) +
sizeof(dwJump);
std::cout << _T("main() is a jump opcode... fixup
applied") << std::endl;
std::cout << _T("Calculated main() at ") << pfnMain;
std::cout << std::endl << std::endl;
}
}
...
}
__except( EXCEPTION_EXECUTE_HANDLER ) {
std::tcout << _T("Caught Exception") << std::endl;
}
}
On Feb 7, 4:38 pm, "Skywing [MVP]"
<skywing_NO_SP...@xxxxxxxxxxxxxxxxxxx> wrote:
What is the exact problem that you are trying to solve here? Your post
makes sense, but you didn't really describe what problem you were having
other than listing some observations about how there might be a jump stub
for main (perhaps due to incremental linking)...
--
Ken Johnson (Skywing)
Windows SDK MVP
http://www.nynaeve.net
"Jeffrey Walton" <noloa...@xxxxxxxxx> wrote in message:
news:1170882501.458467.173940@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi All,
I apologive for the cross post (I hope three is not considered too
bad). I wanted to enlist help from the kernel folks, and the power
debuggers...
SNIP
.
- Follow-Ups:
- Re: Dynamic Disassembler (determine main() location at runtime)
- From: Skywing [MVP]
- Re: Dynamic Disassembler (determine main() location at runtime)
- References:
- Re: Dynamic Disassembler (determine main() location at runtime)
- From: Jeffrey Walton
- Re: Dynamic Disassembler (determine main() location at runtime)
- From: Skywing [MVP]
- Re: Dynamic Disassembler (determine main() location at runtime)
- Prev by Date: Re: Dynamic Disassembler (determine main() location at runtime)
- Next by Date: Re: Dynamic Disassembler (determine main() location at runtime)
- Previous by thread: Re: Dynamic Disassembler (determine main() location at runtime)
- Next by thread: Re: Dynamic Disassembler (determine main() location at runtime)
- Index(es):
Relevant Pages
|