🚩babyjail
Notes
Shellcoding
Majority of levels in this module require shellcode writing. I recommend using pwn.shellcraft() from now on since this chapter is about sandboxing instead of shellcoding itself. Read more:
https://docs.pwntools.com/en/stable/shellcraft/amd64.html#pwnlib.shellcraft.amd64.linux.syscall
strace
For each level, go to practice mode first and use strace. This step is required. If you skip this step, there is no way to figure out the file descriptor corresponding to each syscall.
Level 1
Challenge
Code Review
int main(int argc, char **argv, char **envp)
{
assert(argc > 0);
printf("###\n");
printf("### Welcome to %s!\n", argv[0]);
printf("###\n");
printf("\n");
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 1);
puts("This challenge will chroot into a jail in /tmp/jail-XXXXXX. You will be able to easily read a fake flag file inside this");
puts("jail, not the real flag file outside of it. If you want the real flag, you must escape.\n");
puts("The only thing you can do in this challenge is read out one single file, as specified by the first argument to the");
puts("program (argv[1]).\n");
assert(argc > 1);
char jail_path[] = "/tmp/jail-XXXXXX";
assert(mkdtemp(jail_path) != NULL);
printf("Creating a jail at `%s`.\n", jail_path);
assert(chroot(jail_path) == 0);
int fffd = open("/flag", O_WRONLY | O_CREAT);
write(fffd, "FLAG{FAKE}", 10);
close(fffd);
printf("Sending the file at `%s` to stdout.\n", argv[1]);
sendfile(1, open(argv[1], 0), 0, 128);
}This program calls assert(chroot(jail_path) == 0); to put us in a jail. Recall that calling chroot() without calling chdir("/") is useless. This is a fake jail and we are actually able to read the real flag directly.
Solution
Note that the choice of argv[1] depends on your current working directory where you execute the script. The idea is:
If we use
/flagasargv[1], this pathname will be interpreted as/tmp/jail-XXXXXX/flag. This is the fake flag.If we use
flagasargv[1], the result depends on the current working directory where we execute the script. We can provide an additional argumentcwd="/"when starting the process to escape this sandbox.Another option is utilizing path traversal. If we use
../../../../../flagasargv[1], the real flag will be sent to us as well.
Exploit
Method 1
Method 2
Level 2
Challenge
Code Review
This program is essentially the same as Level 1 but it takes a piece of shellcode and executes it. Again, this is a fake jail and we are able to esacpe it easily.
Solution
The idea of this level is same as Level 1, but we are asked to write shellcode that does the same thing. Here we can use the pwntools built-in shellcode shellcraft.readfile("flag", 1) to send the flag to STDOUT.
Be careful, it is "flag", not "/flag".
Note that we are not doing readfile("/flag", 1) because of the existence of the fake flag. Instead, we use the same trick as in Level 1: start the process with an additional argument cwd="/" and call readfile() using relative path.
Exploit
Last updated