0x05 SLAE - Msfvenom samples
Last updated
Was this helpful?
Last updated
Was this helpful?
The goal of the assignment 5 is to analyze three shellcode samples in the linux/x86 msfvenom payload list. The first shellcode that I choose is the linux/x86/chmod. The chmod system command on Linux allow to change the permission on a file. From the man page, we have the following description:
chmod changes the file mode bits of each given file according to mode, which can be either a symbolic representation of changes to make, or an octal number representing the bit pattern for the new mode bits.
Let's see what is the options from msfvenom:
From the screenshot above, there is two parameters for the payload chmod. The option FILE
and MODE
are the basic options that are required to apply chmod on a file.
First thing first, we have to generate the payload with msfvenom. The following command will generate the shellcode in a c array.
msfvenom -p linux/x86/chmod -f c -a x86
Then, we can take the shellcode and insert it in the C program that compile and execute shellcode.
Compiling the linux-chmod.c :
gcc -m32 -fno-stack-protector -z execstack linux-chmod.c -o linux-chmod
We can run the file
command on the binary to look the details of the binary :
It's indeed an elf compiled for 32 bit architecture and it is not stripped because we kept the debugging information when we compiled it.
The first step is to use GDB and disassemble the shellcode part. In the C code above, the shellcode is stored in the char array code
. To be able to access to this part in the debugging process, we can set a breakpoint to *&code
. Before running the binary, we can also disassemble the shellcode part by using the command : disassemble &code
.
In the disassembly output above, we will analyse what this code is doing. The goal will not be to tell what each lines is doing but more an overview of what is the important part of this shellcode. The first push 0xf
equal to 15 in integer which is the syscall number for sys_chmod
syscall.
Then, the code below is pretty straight forward. See comment in the code below.
Jumping to the address 0x00002036
lead us to the next:
If we step line by line with GDB, we can see that the top of the stack there is the address 0xffffce84
which point 0x5666702a
which contains : 0x6374652f
, 0x6168732f
, 0x00776f64
. If we transform these value in ASCII from little endian to big endian:
Which is /etc/shadow
and if you remember in the introduction, the FILE
options was set to /etc/shadow
and this is where is come from.
The screenshot above show what I did to obtain the values obtained.
Let's see what else we have.
The first line, 0x1b6
, push to the top of the stack the value 0666
which is the MODE
value that was set by default when generating the msfvenom payload. It is important to note that the hexadecimal value 0x1b6
has to be converted to octal per the documentation or the description.
The mode 0666
mean that any users can read and write the file but can't execute it. Then, it put the 0x1b6
in ecx since it pop it from the stack : 0x0000203c <+28>: pop ecx
The line +29, call the chmod syscall. Then, it push the value 1 to the top of the stack. And finally, gravefully exit the program since the syscall 1 is the syscall exit
.
If we execute the binary, it will change the right in read and write to the file /etc/shadow
. Here is the right on the file before.
Running the binary give the following result.
The goal of this assignment was to analyse one of the available payload in the list linux/x86/ of msfvenom. I choose to analyse the linux/x86/chmod shellcode payload and we have seen that the shellcode is relatively simple. This is the part 1 of the assignment 5 since we need to analyse 3 different payload from linux/x86 library.
The goal of the assignment 5 is to analyze three shellcode samples in the linux/x86 msfvenom payload list. The second shellcode that I choose is the linux/x86/adduser. The adduser system command on Linux allow to add a user in the system. Let's see how the msfvenom payload is adding a user into the system.
Here is a preview of the msfvenom linux/x86/adduser parameters :
From the screenshot above, there is three parameters: PASS, SHELL and USER. The parameters name are obvious here.
Compiling the shellcode was pretty similar as the first shellcode that we analyzed (linux/x86/chmod).
msfvenom -p linux/x86/adduser -f c -a x86
Now we copy the shellcode above and put it in the shellcode C program below to compile it.
gcc -m32 -fno-stack-protector -z execstack linux-adduser.c -o linux-adduser
The first step is to disassemble the assembly in the *&code
function.
As we can see in the code above, we have the result of the shellcode in assembly generated by msfvenom. I will describe and explain each important part of that assembly code.
The first two lines are necessary to initialize the registers.
The next lines are calling the syscall setreuid16 to sets real and effective user IDs of the calling process.
Then, there is an interesting part here where the program open the file /etc/passwd for writing or creating it if it doesn't exist.
After the syscall of sys_open, the program need to keep the file descriptor.
The next assembly code are not necessary to show it since it's the construction of the line to add in /etc/passwd. Here we can see the full line by outputting the result in gdb.
As we can see in the image below, the user metasploit as it was configured in the options of the payload is there. So, from the address 0x5655704b
to 0x56557072
is the construction of the string metasploit:Az/dIsj4p4IRc:0:0::/:/bin/sh\nY\213Q\374j\004X̀j\001X̀
.
After that part, the shellcode will put the constructed string above into the register ecx.
Finally, it will write this line in /etc/passwd
and gracefully exit the program.
Running the binary, give us the following result:
The goal of this assignment was to analyse one of the available payload in the list linux/x86/ of msfvenom. I choose to analyse the linux/x86/useradd shellcode payload and we have seen that the shellcode is pretty straight forward. This is the part 2 of the assignment 5 since we need to analyse 3 different payload from linux/x86 library.
The goal of the assignment 5 is to analyze three shellcode samples in the linux/x86 msfvenom payload list. The third shellcode that I choose is the linux/x86/exec. The exec system call allow to run a command on the system. Let's see how the msfvenom payload is running the command into the system.
Here is a preview of the msfvenom linux/x86/exec parameters :
As we can see on the screenshot above, there is a command to specify and this is the command that the syscall exec will execute.
First, we need to generate the shellcode with the following command:
msfvenom --payload linux/x86/exec cmd=whoami -f c -a x86
gcc -m32 -fno-stack-protector -z execstack linux-exec.c -o linux-exec
Let's disassemble the code function with disassemble &code
The assembly code is not that big. Let's divide it by section as we did for the past msfvenom payload analysis.
The first section before the call instruction does some manipulation:
Then, the call instruction will push "/bin/sh" in the stack.
The assembly code between the call instruction and the push edi (from 0x0000203d <+29> to 0x00002042 <+34>
) is pushing the value "whoami" which is the command that we put in the options CMD in msfvenom. We can see the following assembly code :
At the final instruction, syscall 0x80, the stack look like this :
The addess 0xffffce26 point to another address:
So, the last line of assembly code will prepare the stack to do the syscall sys_execve.
Finally, the command call that the linux/x86/exec will do is : execve("/bin/sh", ["/bin/sh", ["/bin/sh", "-c", "whoami"], 0)
Running the binary give the following result:
Which is exactly what we were expecting.
The goal of this assignment was to analyse one of the available payload in the list linux/x86/ of msfvenom. I choose to analyse the linux/x86/exec shellcode payload and we have seen that the shellcode is doing some manipulation. This is the part 3 of the assignment 5 since we need to analyse 3 different payload from linux/x86 library.
This blog post has been created for completing the requirements of the SecurityTube Linux Assembly Expert Certification
http://securitytube-training.com/online-courses/securitytube-linux-assembly-expert/
Student ID: SLAE-1374