Log in

View Full Version : How to get the baseaddress of the running process?


linhan
October 14th, 2005, 12:06
I have Enumulate all the running process,
How can I get the baseaddress of the running
process?

blabberer
October 14th, 2005, 12:15
base adrress ?? imagebase of the process ?? GetModuleHandle() returns the imagebase
hope thats what you are looking for ??

linhan
October 14th, 2005, 12:35
I use GetModuleHaneleA(FProcessEntry32.szExeFile);
return value of the first Process and the lasr process
is right,the others is null.Why?
Have any other way to get the baseaddress

Quote:
[Originally Posted by blabberer]base adrress ?? imagebase of the process ?? GetModuleHandle() returns the imagebase
hope thats what you are looking for ??

ancev
October 14th, 2005, 14:57
hi,

if you use Process32First() and Process32Next() to enumerate the process, the structure filled by these APIs have a member that hold the base address

if you use GetModuleHandleA(), only the base address of modules mapped in your current process show

ancev

LLXX
October 14th, 2005, 17:12
Of all the 32-bit PEs I have seen, the base address is 400000. Safe assumption to make. Of course, you can always open the file on the disk and read the PE header...

o_o
October 14th, 2005, 17:57
Quote:
[Originally Posted by LLXX]Of all the 32-bit PEs I have seen, the base address is 400000. Safe assumption to make. Of course, you can always open the file on the disk and read the PE header...


True for almost every PE, except "dll one" and some .exe in system32 directory '-'

Kayaker
October 14th, 2005, 18:57
Quote:
Of all the 32-bit PEs I have seen, the base address is 400000


Not necessarily, take a look at the attached nspacked file, base address of 10000000. What's interesting, besides the packer itself, is that PE tools such as LordPE etc., when viewing running processes, as well as OllyDbg *when attaching to the process*, report the WRONG base address of 400000.

This may indicate that psapi itself makes the *assumption* that all exe modules will be loaded at 400000 (I'm assuming that most of these PE editors use psapi). If this is the case, why in hell doesn't psapi just read the friggin' PE header?

I'm not sure which of the other methods would be more reliable, maybe you can test with this file. In fact, the file itself could make a nice mini-project (moderator?)

Kayaker

Admiral
October 15th, 2005, 07:22
So are there any circumstances under which the operating system will load an exe at a base address different to that specified in the PE header?

blabberer
October 15th, 2005, 08:56
well almost all exes dont have reloc section or have those reloc sections stripped so it would invariably fail to load
or would crash in runtime if it loaded

naides
October 15th, 2005, 11:15
Question: What would be the USE of an exe that load with a base address different from 40000000?

What happens when you load two exe files (not simultanously, one loads the other) in the same memory context? do they share the base address or one of them gets relocated.

blabberer
October 15th, 2005, 12:41
well best kind of example would be to create a standalone exe insted of plugin to ollydbg (no dllinit blah blah)
and then when you try to debug it you will see ollydbg would vie for the same address viz 0x4000000 and since it was occupied already the loader would try to relocate it and during relocation you will see a debug message
saying since blah blah is not dll failed to relocate the image and then would crash miserably and actually while debugging such an application
the crash is in very very early stages (not even peb is filled and exactly no memory sections are initialised (uses some LoaderLock Acquire blah blah code in ntdll if you set a break here it would never break in the normal course of things )

naides
October 15th, 2005, 12:46
Now I know why they call you blabberer

blabberer
October 15th, 2005, 12:53
they ? who ?

o_o
October 15th, 2005, 16:58
Quote:
[Originally Posted by naides]Question: What would be the USE of an exe that load with a base address different from 40000000?

What happens when you load two exe files (not simultanously, one loads the other) in the same memory context? do they share the base address or one of them gets relocated.


I think that "0x400000" is just one choice of image base, (any???%64k o_o''') other are possible. If the loader can load at the image base (and for ".exe" file it always happen i think) no relocation is needed.

If a pe "load" somehow (usually a dll via LoadLibrary) another pe file and base addresses clash the second pe must be relocated using information in the relocation directory of the pe file.

'-'

Small edit to fix the any?? above ^^

Under Windows Xp every base address between 0x10000<=base<=0x7ffd0000 (in 64k multiple of course) is "valid" (the small application used as a test linked and ran successfully)

But for value between 0x10000 and 0x400000 the linker complained

C:\masm32\src>..\bin\LINK.EXE /base:0x10000 b.obj /subsystem:windows
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

LINK : warning LNK4096: /BASE value "10000" is invalid for Windows 95; image may not run

o_o''

dELTA
October 15th, 2005, 18:17
Yes, and just like blabberer possibly tries to convey above , if the second exe file lacks relocation info (as is often the case for normal exe files) the loading will fail if the recommended address range of the second module intersects with the address range of the first one.

naides
October 15th, 2005, 19:50
Quote:
[Originally Posted by blabberer]they ? who ?


The little red men that live inside my computer screen.
They tell me what to write and what to think

disavowed
October 16th, 2005, 04:18
Quote:
[Originally Posted by naides]What happens when you load two exe files (not simultanously, one loads the other) in the same memory context?

What do you mean by "load"? If you mean one .exe runs the other .exe (via CreateProcess(...), etc.), then they use two different process-space memory contexts, so there's not even a chance for a collision within a single process. If you mean "load" as in LoadLibrary(...), then yes, they are relocated just like DLLs.

POC code:
Code:

HMODULE x = NULL;
HMODULE y = NULL;

x = LoadLibrary("notepad.exe";
printf("Only notepad.exe in memory:\n";
printf("Base address of notepad.exe: 0x%08X\n\n", x);
FreeLibrary(x);

y = LoadLibrary("explorer.exe";
printf("Only explorer.exe in memory:\n";
printf("Base address of explorer.exe: 0x%08X\n\n", y);
FreeLibrary(y);

x = LoadLibrary("notepad.exe";
y = LoadLibrary("explorer.exe";
printf("notepad.exe AND explorer.exe in memory:\n";
printf("Base address of notepad.exe: 0x%08X\n", x);
printf("Base address of explorer.exe: 0x%08X\n", y);
FreeLibrary(y);
FreeLibrary(x);



Output:
Code:

Only notepad.exe in memory:
Base address of notepad.exe: 0x01000000

Only explorer.exe in memory:
Base address of explorer.exe: 0x01000000

notepad.exe AND explorer.exe in memory:
Base address of notepad.exe: 0x01000000
Base address of explorer.exe: 0x00430000

blabberer
October 16th, 2005, 09:25
Quote:

possibly tries to convey above

haha the blah blah does have its effect powerfully it seems

instead of possibly trying to convey
i would try to state as facts

loading as in using CreateProcess,WinExec,ShellExecute,and other
variation does not count for image relocation problems because a new process is spawned and the clashes do not occur

an exe compiled normally does not have relocation information

an exe can have export section and it can export functions (plugins architecture relies on these export function from an exe)
a dll also has export sections and it also exports functions

so a .lib can be produced for both of them

and in simple masm terms
includelib "//yourpath//yourthuscreatedlib"

start
push params
call the exported function from exe
end start

would
assemble without problems

now running this without the exe in path would say the application is
linked to some such dll that cannot be found so cannot continue
because of missing application now you would drag and drop the exe
to the path and double clicking it would eliminate the missing dll problem
but would exit silently (not even the standard windows need to terminate
the application box that would appear normally on misbehaving application would be shown)

and if you try to debug it to find the cause of mysterious disaapperance
you can easily find the imagebase address clash as the exe is loaded in 0x4000000 already and during import resolution the windows loader tries to load the application find a clash of address tries to relocate would map it successfully to a different address but since there is no relocation information it would emit a debug message which basically would mean

yeah i have relocated this clashing crap but it doesnt have full info to help me rehabilitate it so i just reloacted it to a mental hospital runnning under social security may bite if you go near so take care while dealing with it have your prods ready and easily accesible

whereas the dll having reloc info would be successfully relocated
and would work flawlessly

i actually created some plugin to ollydbg as standalone exe because
it was easier for me to debug them at some stages
a standlone didnt need to load ollydbg on ollydbg and then set break on action and have the comments erased becuse a new compiled exe has a different crc and time stamp so the udd is discarded