Tips & Tricks
Problems
Environment Variables
When debugging, GDB adds two environment variables:
LINESCOLUMNS
Because environment variables are placed on the stack when the program is executed, they can cause address changes when attempting to exploit a vulnerability, due to the additional data on the stack.
To avoid this, remove them to prevent headaches:
(gdb) unset env LINES
(gdb) unset env COLUMNSArguments
You can call a program in multiple ways, for example:
./my_programm // Relative
/absolute/path/my_programm // Absolute
However, these are NOT the same thing.
When calling a program, the way you invoke it (its name) is stored in argv[0], which is on the stack.
To quote StackOverflow
One further subtle but important detail: there’s a difference between calling
./stackand/path/to/stack: sinceargv[0]holds the program exactly how you invoked it, you need to ensure equal invocation strings. That’s why I used/path/to/stackin the above examples and not just./stackandgdb stack.
Executable Wrapper
When learning to exploit with memory safety vulnerabilities, I recommend using the wrapper program below, which does the heavy lifting and ensures equal stack offsets :
| |
invoke stack # just call the executable
invoke -d stack # run the executable in GDBYou can also ensure the environment variables are the same by calling GDB and your program the same way :
env -i PWD="/root/Documents/MSec" SHELL="/bin/bash" SHLVL=0 /root/Documents/MSec/shelltest
env -i PWD="/root/Documents/MSec" SHELL="/bin/bash" SHLVL=0 gdb /root/Documents/MSec/shelltestSUID Wrapper
Modern versions of bash and tcsh automatically check if the process’s UID matches its EUID when they start. If they don’t match, the shell resets the EUID to the UID as a security measure.
However, this limitation can be bypassed by using a wrapper program that calls setuid(0) to regain root privileges before launching the shell.
C Code
The following is the C code for such a wrapper :
| |
Shell
As well as the following bash script :
#!/bin/bash -p
/bin/bash -pTest Shellcode
| |
Helpers
Stabilize reverse shell
SHELL=/bin/bash script -q /dev/null
# Ctrl-Z
stty raw -echo
fg
reset
xtermor
python3 -c "import pty;pty.spawn('/bin/bash')"
export TERM=xterm; export SHELL=/bin/bash
# CTRL+Z
stty raw -echo;fg