Command Injection

Languages

Go

// os/exec Write
cmd := exec.Command("bash")
cmdWriter, _ := cmd.StdinPipe()
cmd.Start()
cmdWriter.Write([]byte("os command here\n"))
cmd.Wait()

// os/exec CommandContext
exec.CommandContext(ctx, "os command here", "arguments here").Run()

// os/exec Cmd
cmd := &exec.Cmd {
    Path: "os command here",
    Args: []string{ "arguments here" },
}
cmd.Start();

// os/exec Command
cmd := exec.Command(
    "os command here", 
    "arguments here",
)
cmd.Run()

// syscall Exec
execErr := syscall.Exec(
    "os command here", 
    "arguments here",
    os.Environ()
)

Java

Node.js

Python

Ruby

Tips

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:

You can use brace expansion to create payloads:

References:

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 command in a subshell environment and replacing the command substitution with the standard output of the command.

References:

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.

  2. echo command:

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

  3. xxd command:

References:

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.

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 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 .

Moreover, you can use pipelines for the same purposes:

References:

Producing slash with tr

Redirections

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

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

References:

Shell parameter expansion

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

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

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

References:

Special shell parameters

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

References:

Shell variables

Bash automatically assigns default values to a number of 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):

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

References:

Tricks

References

Last updated