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 print
ingenerate-shellcode/
to generate the shellcode. It is part of theshellcode
array inexec.c
inrun-shellcode/
.Test what it does by running
strace ./exec
ingenerate-shellcode
.Inspect the shellcode implementation in
shellcode.asm
. Update the shellcode to also open the two files from../../jail/a.txt
and../../b.txt
using theopen
system 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 theexec
executable, and then test it again by runningstrace ./vuln
.Let’s use AppArmor to allow the
exec
executable from the previous task to open thea.txt
file, but not theb.txt
file (i.e. the firstopen
system call in the shellcode will succeed, but the secondopen
system 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/grub
for configuring GRUB. Do not use/etc/default/grub.d/apparmor.cfg
as shown in the page.Create a profile for
exec
in/etc/apparmor.d/
to alllow theexec
executable to accessa.txt
but prevent it from accessingb.txt
. Start from thebin.ping
profile. You need to use full paths.At any point you can use
ps -efZ
to see what processes are confined.Now we want to use
chroot
to allow the program to access../jail/a.txt
but prevent it from accessing../b.txt
.Access the
chroot/
subfolder. Go into theexec
executable and update theA_PATH
andB_PATH
macros for full paths toa.txt
andb.txt
. Compile theexec
executable and run it underchroot
in the../jail/
folder.In order to run an executable inside a folder as a chroot jail run:
sudo chroot ../jail/ ./exec
The executable
exec
has to be part of the jail. All required files (including library files) need to be inside the jail.a.txt
is already part of the jail. Useldd exec
to 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
exec
program will open thea.txt
file but not theb.txt
file.Now we want to use sandboxing (
seccomp
) to allow the program to open the../jail/a.txt
file, but not the../b.txt
file.Access the
seccomp/
subfolder. Inspect theexec.c
file and see what it does and what calls should succeed or not. Compile it usingmake
. Runstrace ./exec
and see what calls are allowed and what calls are not allowed.Update the
exec.c
file to enforce and check that read and write is allowed to thea.txt
file, but only read is allowed to theb.txt
file.You need the
libseccomp-dev:i386
installed on the system. To install it run:sudo apt install gcc-multilib libseccomp-dev:i386
Bonus: Update the
exec.c
program to invoke theread
andwrite
system calls from inside a shellcode, similar to way its done in theshellcode/run-shellcode/exec.c
.