General ShellCode deep analysis

Source: Internet
Author: User
There have been a lot of articles on ShellCode writing skills on the internet. why should I write this article? This article is my companion article on how the buffer overflow technique of Windows2000 works.

Preface:
There have been a lot of articles on ShellCode writing skills on the internet. why should I write this article? This is my previous article on overflow techniques. On the Internet, we often see some articles about ShelCode writing skills, which do not seem to be prepared for beginners, here, I will compare the general ShellCode with the specific analysis from the perspective of beginners. with the overflow theory in the previous article and the general ShellCode theory in this article, on the basis of this, we can try to write some overflow attack testing programs based on some pop-up Window overflow vulnerabilities or the overflow vulnerabilities we have analyzed in the disassembly of some software systems.
The article first briefly analyzes the PE file structure and PE picking table, and provides a routine to demonstrate how to find the picking function and its address based on the PE coherence technique, then we analyze a method to obtain the Kernel32 base address that is more common. Finally, we use the fairness theory briefly and provide a general ShellCode.
This article also describes the methods that are easier to understand when I am studying. However, due to the complexity of ShellCode, the important application of C and Asm is explained in this article, the author assumes that you already have a certain C/Asm hybrid programming Foundation and the overflow theoretical basis of the previous article. I hope this article will help my new friends learn overflow skills better.

[Directory]

1. Introduction to the PE file structure and analysis of the PE extraction table.
1.1 PE file overview
1.2 export table analysis
1.3 compile a common utility function that obtains the derived function address based on the base address of the DLL in the application.
GetFunctionByName

2. obtain the common Kernel32.DLL address.
2.1 Introduction to structured exception handling and TEB
2.2 compile a common utility function for obtaining the base address of the Kernel32.DLL function in the application.
GetKernel32

3. comprehensive application (a simple general ShellCode)
3.1 compile a simple ShellCode for adding an account and enabling Telnet based on the skills described above:
Apply the GetFunctionByName implemented by ourselves according to the techniques described in section 2nd to obtain LoadLibraryA and
GetProcAddress function address, and then apply the two functions to introduce all the functions we need for implementation.

4. reference materials.

5. key words.
----------------------------------------------------------------------

1. basic PE file structure and lead table
1. Introduction to PE file structure

PE (Portable Executable) is the scale pattern of Executable files in Microsoft Win32 environment (the so-called Executable file is not only. EXE file, including. DLL /. VXD /. SYS /. VDM)

PE file structure (simplified ):

-----------------
│ 1, dos mz header │
-----------------
│ 2, DOS stub │
-----------------
│ 3, PE header │
-----------------
│ 4, Section table │
-----------------
│ 5, Section 1 │
-----------------
│ 6, Section 2 │
-----------------
│ Section... │
-----------------
│ N, Section n │
-----------------

I remember that when I had not completed Win32 Programming, I ran a Win32 executable file in Dos, and the program output only one line "This program cannot be run in DOS mode. "How does one identify that he is not on the Win32 Platform? Actually, it is not identified. it may be as simple as entering only this line of text and exit. the source code may be as simple as the C program below:

# Include
Void main (void)
{
Printf ("This program cannot be run in DOS mode. \ n ");
}

You may ask, "I did not write such a statement when I was writing a Win32 program? ", This is a 16-bit DOS program built for you by the connector (linker, when a Win32 program is run in a 16-bit system (DOS/Windows 3.x), it is executed to output a string of characters prompting the user that "this program cannot be run in DOS Mode ".

Let's take a look at what the dos mz header is. below is its structure description in Winnt. h:

Typedef struct _ IMAGE_DOS_HEADER {// DOS. EXE header
WORD e_magic; // 0x00 Magic number
WORD e_cblp; // 0x02 Bytes on last page of file
WORD e_cp; // 0x04 Pages in file
WORD e_crlc; // 0x06 Relocations
WORD e_cparhdr; // 0x08 Size of header in paragraphs
WORD e_minalloc; // 0x0a Minimum extra paragraphs needed
WORD e_maxalloc; // 0x0c Maximum extra paragraphs needed
WORD e_ss; // 0x0e Initial (relative) SS value
WORD e_sp; // 0x10 Initial SP value
WORD e_csum; // 0x12 Checksum
WORD e_ip; // 0x14 Initial IP value
WORD e_cs; // 0x16 Initial (relative) CS value
WORD e_lfarlc; // 0x18 File address of relocation table
WORD e_ovno; // 0x1a Overlay number
WORD e_res [4]; // 0x1c Reserved words
WORD e_oemid; // 0x24 OEM identifier (for e_oeminfo)
WORD e_oeminfo; // 0x26 OEM information; e_oemid specific
WORD e_res2 [10]; // 0x28 Reserved words
LONG e_lfanew; // 0x3c File address of new exe header
} IMAGE_DOS_HEADER, * PIMAGE_DOS_HEADER;

Dos mz header contains the initial value of some 16-bit DOS programs. for example, IP address (command pointer), cs (code segment storage device), memory size to be allocated, checksum (checksum) etc. when the DOS is prepared for the creation of executable files, the value will be read to complete the initial chemical operation.

Have you noticed the last structure member? Microsoft described the File address of new exe header as "new exe File header address", which is a relative offset value, I think you must know the file offset!
E_lfanew is a file offset value that points to the PE header, which is very important to us. exactly as the dos mz header is DOS stub, it is the code entity part of the 16-bit DOS program established by linker for us, that is, it outputs "This program cannot be run in DOS mode. ". the next step is PE header. someone once asked me if the offset of the peat header relative to the .exe file is fixed? It is not easy to say that different compilers may have different inherent stub lengths (for example, it may store such a string to prompt The user "The Currnet OS is not Win32, I want to run in Win32 Mode. ", then the length of the stub will be longer than the previous one), so it is unscientific to use a fixed value to locate the PE header. at this time, we use e_lfanew, it points to the real PE header. is it always accurate? That's of course! Linker always assigns an accurate value to it. therefore, we need it to precisely locate the PE header. The same Win32 PELoader also locates the real PE header based on e_lfanew, and applies different member values in the PE header for initial activation, PE also contains many sections for storing data, for storing executable code, and for storing resources (such as program icons and bitmaps, sound, dialog box template, etc)
Next, I will briefly analyze the parts related to PE structure and ShellCode compilation. if you are more interested in other parts, you can refer to what Mr. Hou Junjie translated from taogang. And the classic PE tutorial of Iczelion.

2. extract table analysis

The PE header structure (you can find it in Winnt. h) contains an array of members of the DataDirectory structure. you can find its position in this way:
PE header offset = executable file memory Image Base address 0x3c (e_lfanew)
PE base address = executable file memory Image Base address PE header offset
Export Table directory pointer (IMAGE_EXPORT_DIRECTORY *) = PE base address 0x78 <= --- DataDirectory
Name of the extracted function table first pointer (char **) = base address of the extracted table directory 0x20
The first pointer to the function address table (DWORD **) = the table directory pointer 0x1c. its structure definition is as follows:

Typedef struct _ Image_Data_Directory {
DWORDVirtualAddress;
DWORDisize;
} IMAGE_DATA_DIRECTORY, * PIMAGE_DATA_DIRECTORY;

The structure array contains 16 members. the VirtualAddress of the first member stores a relative offset, which points to an IMAGE_EXPORT_DIRECTORY structure. its definition is as follows:

Typedef struct _ IMAGE_EXPORT_DIRECTORY {
DWORD Characteristics; // 0x00
DWORD TimeDateStamp; // 0x04
WORDMajorVersion; // 0x08
WORDMinorVersion; // 0x0a
DWORD Name; // 0x0c
DWORD Base; // 0x10
DWORD NumberOfFunctions; // 0x14
DWORD NumberOfNames; // 0x18
DWORD AddressOfFunctions; // 0x1c RVA from base of image
DWORD AddressOfNames; // 0x20 RVA from base of image
DWORD AddressOfNameOrdinals; // 0x24 RVA from base of image
} IMAGE_EXPORT_DIRECTORY, * PIMAGE_EXPORT_DIRECTORY;

AddressOfFunctions stores a second-level pointer, which points to a DWORD pointer array. this array member refers to the function address value, however, the value is a relative offset between the function and the address of the executable file in the memory image. the real function address is the base address of the executable file in the memory image, we can Call the calculated real address to Call the number. addressOfNames is a second-level character pointer. the array member refers to an offset value of the function name string relative to the base address of the executable file in the memory image, you can also reference the function name string by the base address of the file in the memory image through the relative offset value. name is also a character pointer, and it only stores the relative offset value. if it is the IMAGE_EXPORT_DIRECTORY of kernel32, the string it points to is "KERNEL32.dll ".

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.