Saturday, April 12, 2014

Shellcode Analysis

What is Shellcode?

Shellcode is nothing but self contained executable code which gets executed on the injected program. Shellcode is usually a binary chunk of data that cannot run in the same way as a normal executable. Shellcodes have the following features:
  • They are position independent code. 
  •  They identify the execution location.
Historically, shellcode has been used to spawn a shell on the exploited system. However, shellcode is not limited to launching a shell--- it is capable of executing any code on the system that is vulnerable to an attack. 
Shellcode is often the payload of an exploit, such as buffer overflow, that attempts to execute code on the vulnerable system.It is just a bunch of hexadecimal values that represent assembly instructions (opcodes). 

Problems in analyzing shellcode

Loading shellcode into IDA Pro or in a gdb for static analysis is problematic because there is no executable format that describes the contents of shellcode. Hence, in case of IDA Pro the user must provide input during the load process.IDA Pro loads the binary but performs no automatic analysis. But, I will discuss methods in the following sections to analyze shellcode that forces IDA pro or gdb debugger to perform automatic analysis.

Analyzing shellcode

Before analyzing shellcode, we have to check whether the shellcode is in hexadecimal format or in binary format.If the shellcode is in binary format, we have convert it into hexadecimal. Use the python script BinaryToHexConverter  to convert the binary file into hexadecimal file. Or you can open the binary format in hex editor and copy and format the hexadecimal values to \x00 format. For example, following is the real world shellcode,which I will be using during analysis, in hexadecimal format:
Now once you have the above format, you can make the C skeleton as follows:
 
Use shellcode as a variable and compile the program. You can use any c compiler. I have used Visual Studio 2012 to compile the C program. Once you have compiled the c program open the executable program in either IDA Pro or gdb debugger.
Fig.1 Template of shellcode

Analyzing shellcode using IDA Pro

After the C program has been compiled, open it using IDA Pro as shown below:
Fig. 2

After you have opened the compiled program in IDA Pro search for the text shellcode because shellcode variable contains the actual shellcode . 
Fig.3
Fig. 4
Fig. 5
Fig. 6
Place the cursor on the shellcode and press c key in order to convert data into code. After pressing c you get the following shellcode .

Fig. 7
Fig. 8
Hence IDA pro has done  the automatic analysis of shellcode for you. Now you can look into the code and find out its functionality.

Analyzing shellcode using gdb debugger

First compile the c program using gcc compiler.Then load the compiled program in gdb. For example:
 
Use disas shellcode  command in order to analyze the shellcode variable. By default gdb debugger shows AT&T syntax. For intel syntax use the following approach:Add the following line in ~/.gdbinit file.
Fig 9
Fig. 10
Finally you can analyze the shellcode in gdb or in IDA Pro using the above approaches. For any queries please comment.

No comments:

Post a Comment