Find the kernel32.dll base address from the stack

Source: Internet
Author: User

The principle is as follows:

The winmain code of each user-state process is called by the START code (crtstartup), and the start code is called by a function in kernel32.dll, of course, there may also be no functions such as winmain and winmaincrtstart (such as the Code Compiled by Delphi). However, after the process is started, an unknown code location in kernel32.dll must be stored in the stack, therefore, the base address of kernel32.dll can be found from the top to the bottom of the test stack, and a short piece of code is written. It passes the test under Win XP SP2 and VC ++ 6.0 SP5, the base address of kernel32.dll on my machine is 0x7c800000.
Code:


# Include <stdio. h>
# Include <conio. h>
# Include <windows. h>

DWORD getkernel32 (void)
{
DWORD tryaddr = 0;
Pimage_dos_header PDOS = NULL;
Pimage_nt_headers PNT = NULL;
Pimage_export_directory pied = NULL;
DWORD * pkernel = NULL;
Int I = 0;

Pdword PESP = (pdword) & PESP;
Bool bfound = false;
Do
{
_ Try
{
Tryaddr = * PESP;

For (I = 0; I <4; I ++)
{
Tryaddr & = 0xffff0000 <(I <2 );

PDOS = (pimage_dos_header) tryaddr;
PNT = (pimage_nt_headers) (tryaddr + PDOS-> e_lfanew );

If (PDOS-> e_magic = 'zm' & PNT-> Signature = 'EP ')
{
Pied = (pimage_export_directory) (tryaddr + PNT-> optionalheader. datadirectory [0]. virtualaddress );
Pkernel = (DWORD *) (tryaddr + pied-> name );

// Kernel32 or Kernel32
If (* pkernel = 'nrek' & * (pkernel + 1) = '23le') |
(* Pkernel = 'nrek' & * (pkernel + 1) = '23le '))
{
Bfound = true;
Break;
}; // End if pkernel

} // End if PDOS

} // End for I
}
_ Partition T (1)
{
NULL;
}
PESP ++;
} While (! Bfound );

Return tryaddr;
}

Int main (void)
{
Printf ("kernel32.dll at: 0x %. 8x/N", getkernel32 ());
Return getchar ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.