🚩babyshell
Notes
Online Shellcode Assembler/Disassembler
https://defuse.ca/online-x86-assembler.htm
The catflag
C Wrapper
catflag
C WrapperIn this series of challenges, our objective is to read /flag
. We need to make the following two syscalls consecutively:
Call
open("/flag", 0)
.Use the result from step 1 to call
sendfile(1, open("/flag", 0), 0, 1000)
.
To simplify our shellcode, we can combine these two steps into a C wrapper:
Compile it:
Why name this file a special character ;
? The ASCII number for ;
is 59
in decimal, which is just the syscall number of execve
. In our shellcode, there will always be mov al, 59
. By naming catflag as ;
, we could utilize rax for two purposes at the same time: syscall number and filename.
Writing Shellcode
Method 1: Pwntools (Connor's Method)
For example:
Method 2: Manually (Yan's Method)
Compile the shellcode source shellcode.s
into raw bytes:
We can create an alias to make life easier. Add the following line to ~/.bashrc
:
Reload ~/.bashrc
:
Now if you want to compile shellcode, simply type compile
inside the babyshell
directory.
Run the shellcode:
For debugging purposes, to get the disassembly of the shellcode:
Similarly, add the following line to ~/.bashrc
:
Reload ~/.bashrc
:
Now if you want to check the disassembly of the shellcode, simply type debug
inside the babyshell
directory.
Examine system calls:
Breakpoint
In some levels, we need to examine the registers at the moment of shellcode execution. This can be done with the int 3
instruction, which sets a breakpoint in GDB:
To run it:
Utilizing Register States
By inserting the int 3
shellcode as breakpoint, we could examine the registers at the moment of shellcode execution. For example, in level1_teaching1
, the registers are in the following state:
Utilizing those values that already reside in the registers is crucial for some of the levels in this assignment.
Level 1
Challenge
Solution
Write a program named catflag.c
which is a wrapper for calling sendfile()
:
This wrapper is needed because it simplifies the shellcoding process a lot. Compile it and name it as ;
:
This weird naming would further simplify our shellcode: the ascii value of ;
equals the syscall number of execve
, which is 59.
Our objective is calling execve(catflag_pathname, 0, 0)
. To achieve this objective, our shellcode should do the following things:
Set rax to 59. This is used for the syscall number and the C wrapper filename at the same time.
Push rax onto the stack, so that rsp now contains the address of 59. Give this address to rdi.
Zero out rsi and rdx.
Invoke syscall.
Exploit
Level 2
Challenge
Solution
This level can be solved with the same shellcode as in Level 1.
Exploit
Last updated