Application Confinement
We discuss solutions for confining (isolating) applications for damage control: in case an application is compromised the damage done must be limited.
- Slides for this session:
Tasks
Download the session task archive.
Access the
shellcode/subfolder. We assume a shellcode is injected maliciously inside an executable and we want to use different mechanisms to protect it. To simplify things we add the shellcode in the source code and compile it to execute the shellcode.There are two subfolders:
generate-shellcode/for generating a shellcode andrun-shellcode/for running a shellcode inside an executable. Usemake printingenerate-shellcode/to generate the shellcode. It is part of theshellcodearray inexec.cinrun-shellcode/.Test what it does by running
strace ./execingenerate-shellcode.Inspect the shellcode implementation in
shellcode.asm. Update the shellcode to also open the two files from../../jail/a.txtand../../b.txtusing theopensystem call: the first argument (inebx) is a pointer to the string denoting the filename, the second argument (inecx) isO_RDONLY(see its numeric value in/usr/include/bits/fcntl-linux.h).Generate the new shellcode (using
make print), replace it inexec.c, recompile theexecexecutable, and then test it again by runningstrace ./vuln.Let’s use AppArmor to allow the
execexecutable from the previous task to open thea.txtfile, but not theb.txtfile (i.e. the firstopensystem call in the shellcode will succeed, but the secondopensystem call will fail).AppArmor needs to be installed on your system. If you you have trouble installing it, use this Debian virtual machine. Use the cs.curs.pub.ro account to download it.
Follow the Debian AppArmor HowToUse instructions. Use the file
/etc/default/grubfor configuring GRUB. Do not use/etc/default/grub.d/apparmor.cfgas shown in the page.Create a profile for
execin/etc/apparmor.d/to alllow theexecexecutable to accessa.txtbut prevent it from accessingb.txt. Start from thebin.pingprofile. You need to use full paths.At any point you can use
ps -efZto see what processes are confined.Now we want to use
chrootto allow the program to access../jail/a.txtbut prevent it from accessing../b.txt.Access the
chroot/subfolder. Go into theexecexecutable and update theA_PATHandB_PATHmacros for full paths toa.txtandb.txt. Compile theexecexecutable and run it underchrootin the../jail/folder.In order to run an executable inside a folder as a chroot jail run:
sudo chroot ../jail/ ./exec
The executable
exechas to be part of the jail. All required files (including library files) need to be inside the jail.a.txtis already part of the jail. Useldd execto determine the library files required by the executable. You need to copy them full paths inside the jail.After a successful run in the chroot jail, the
execprogram will open thea.txtfile but not theb.txtfile.Now we want to use sandboxing (
seccomp) to allow the program to open the../jail/a.txtfile, but not the../b.txtfile.Access the
seccomp/subfolder. Inspect theexec.cfile and see what it does and what calls should succeed or not. Compile it usingmake. Runstrace ./execand see what calls are allowed and what calls are not allowed.Update the
exec.cfile to enforce and check that read and write is allowed to thea.txtfile, but only read is allowed to theb.txtfile.You need the
libseccomp-dev:i386installed on the system. To install it run:sudo apt install gcc-multilib libseccomp-dev:i386
Bonus: Update the
exec.cprogram to invoke thereadandwritesystem calls from inside a shellcode, similar to way its done in theshellcode/run-shellcode/exec.c.