Scripting
Bash scripting
Link to original
Debugging Bash scripts
Using the
-x
flag will force Bash to output each line of the shell script you’re running before that line is executed. This can be useful for debugging.The
-x
flag can also be incorporated into the interpreter line.Finally, this mode can be toggled on and off with the
set
command within the script itself.Frequently
Link to originalset -x
is used at the start of a script without a closingset +x
, which will just cause all lines of the script to be echoed back before execution.
Exploitation
Port scanning
Port scanning with Bash
Link to original
A simple reverse shell
Bash reverse shell
(Based on the PayloadsAllTheThings Bash TCP reverse shell.)
Catch it with netcat or socat.
(That said, the fact that all of my file descriptors wind up pointing at /dev/tcp is a little mysterious to me. I think what’s happening here is that /dev/tcp is bidirectional “out of the box” — incoming data comes out, just as outgoing data goes in — so binding all three “core” file descriptors to it does the right thing. That, and realize that the
Link to originalX>&Y
construct means “bind file descriptor X to file descriptor Y”, and&>
is just short for2>&1 >
, and>
is just short for1 >
. So really what’s happening here is that we bind STDERR to STDOUT with and implicit2>&1
, then bind STDOUT to /dev/tcp with an implicit1 >
, then bind STDIN to /dev/tcp as well with0>&1
.)
Using wildcard expansion to pass command line options
Abusing wildcard expansion in Bash
The wildcard expansion (
Link to original*
) in Bash scripts doesn’t get pushed to the command, but is instead expanded in place. This means that files named like command-line switches will be interpreted as command line switches. This can be used, for example, to exploit sloppy tar-based backup scripts.
Exploiting functions
How to use Bash functions to “backdoor” executables
This only works on versions of Bash before v4.2-048!
In versions of Bash < 4.2-048, it’s possible to export functions with the same form as absolute paths to files. These functions will then be executed instead of the fully-specified path if the calling application is relying on the current shell for helper execution.
For example:
Link to original
Exploiting $PS4
How to exploit the Bash PS4 (debugging) prompt
This only works on versions of Bash before v4.4!
When Bash is in debugging mode (
SHELLOPTS=xtrace
), the$PS4
prompt is used to display debugging information.It would appear that this prompt somehow inherits the permissions of the executable being run. This includes SUID/SGID permissions (at least for Bash < 4.4)!
If you have access to a SUID/SGID executable, this can be abused to create root shells:
Again, this only works if the calling application is relying on the current shell for helper execution.
Link to original
Avoid dropping privileges with SUID Bash
How to avoid dropping privileges with SUID Bash
Bash will drop privileges by default if SUID. To avoid this, simply supply the
Link to original-p
flag.