Module 13: Command Injection

Discovery of Command Injection

Accessing the Command Injection Sandbox

Start the VPN, the VM, and add IP to hosts.

Familiarizing Ourselves with the Sandbox

Python Ping Output

First command injection payload

First successful command injection

Where is Command Injection Most Common?

Vulnerable Code Snippet

About the Chaining of Commands & System Calls

Executing a single command

Chaining our first commands

In addition to the semicolon, which allows us to chain multiple commands together in one statement, another unique separator for Linux is the newline (\n), which exists in every HTTP request. Its hexadecimal value is 0x0A.

Chaining with Logical AND (Success)

Chaining with Logical AND (Failure)

Chaining with Logical OR (Failure)

Chaining with Logical OR (Success)

Inline Execution Characters

Chaining with inline execution

Dealing with Common Protections

Typical Input Normalization - Sending Clean Payloads

Starting a Netcat Listener on port 9090

Our Wrapped Payload with No URL Encoding

Encoding our Payload in Burp Suite's Repeater

Our Encoded Payload result

Our Wrapped Payload with URL Encoding (ready to be sent)

The above could have just been sent in Burp Suite's Repeater after URL-encoding the request...

Receiving a root privileged shell

Typical Input Sanitization - Blocklisted Strings Bypass

Blocklisted string

A Null Statement Injection Bypass can be inserted between any characters of our choosing.

This technique also works for more complex payloads like "nc -nlvp 9090", which becomes: n$()c -n$()lvp 9090

Null Statement Injection into our previous command injection

Bypassing blocklisted strings with a null statement

A short wordlist to work with wfuzz

Fuzzing with our Custom Wordlist

Fuzzing with our Custom Wordlist and a Suppressed Response-Size of 1156 bytes

Results in our web browser

Encoding our payload with Base64

Our Full and Complete Payload

Bypassed Blocklisted Strings with Base64 Payload

Blind OS Command Injection Bypass

Attempting to execute the `id`command with blind command injection

No output from our id command

Capturing the initial time

Bypassing blind sanitization wtih sleep for execution verification

Extra Mile

Do the lab.

Enumeration and Exploitation

Enumerating Command Injection Capabilities

Common Linux Capability Checks

Command

Used For

wget

File Transfer

curl

File Transfer

fetch

File Transfer

gcc

Compilation

cc

Compilation

nc

Shells, File Transfer, Port Forwarding

socat

Shells, File Transfer, Port Forwarding

ping

Networking, Code Execution Verification

netstat

Networking

ss

Networking

ifconfig

Networking

ip

Networking

hostname

Networking

php

Shells, Code Execution

python

Shells, Code Execution

python3

Shells, Code Execution

perl

Shells, Code Execution

java

Shells, Code Execution

Common Windows Capability Checks

Capability

Used For

Powershell

Code Execution, Enumeration, Movement, Payload Delivery

Visual Basic

Code Execution, Enumeration, Movement, Payload Delivery

tftp

File Transfer

ftp

File Transfer

certutil

File Transfer

Python

Code Execution, Enumeration

.NET

Code Execution, Privilege Escalation, Payload Delivery

ipconfig

Networking

netstat

Networking

hostname

Networking

systeminfo

System Information, Patches, Versioning, Arch, etc.

Custom Linux Capability Wordlist

Fuzzing and checking for capabilities with our Custom Wordlist

Enumerated capabilities

We highly recommend taking note of the various options available when attempting to gain a reverse shell. PayloadAllTheThings is an excellent resource to learn about these.

Obtaining a Shell - Netcat

Starting a Netcat listener

Evaluating our execution privileges

Endpoin for our command injection

A root shell with netcat

Some versions of Netcat don't have the "-e" option; however, the Netcat binary on Kali Linux has no such restriction.

Obtaining a Shell - Python

Python Reverse Shell

Unfolded Python Payload

Starting a Netcat Listener on port 9090

Full endpoint with payload in the command injection sandbox

Receiving the reverse shell

Obtaining a Shell - Node.js

Chained Node.js Reverse Shell

The Full Command Injection Endpoint

The full URL-encoded command injection endpoint

Starting a Netcat listener on port 9090

Receiving our reverse shell

As additional practice, take note of additional Node payload options available from PayloadAllTheThings.

Obtaining a Shell - PHP

PHP Reverse Shell Examples

The first half of our PHP Payloads

Various PHP Execution Options

Are there any disabled functions?

Finding a live system with phpinfo() is rare but not unheard of. It contains a wealth of information and is sometimes left behind by system administrators or web developers.

phpinfo(); function and disabled functions

Starting a netcat listener on port 9090

Not encoded endpoing (including our payload)

Complete endpoint (including our payload)

Receiving the reverse shell

Obtaining a Shell - Perl

Perl Reverse Shell

Perl Reverse Shell Unfolded

Full URL Encoded Endpoint

Starting a netcat listener on port 9090

Receiving the reverse shell

File Transfer

File transfer enumeration in the web browser

Placing the nc binary in our Apache2 web root

Starting the Apache2 Service

The full payload (unencoded)

The full payload (encoded)

Starting a netcat listener on port 9090

Receiving the reverse shell

Extra Mile I

Transfer a different payload, permit the payload, and change the file permissions to execute it for a reverse shell.

Writing a Web Shell

Finding our present working directory

Document Root

Using echo to write out our own webshell

Writing our own webshell

Webshell confirmation
Change Directory and Listing Contents
Contents of the web root

This limitation of web shells is why we always try to leverage them to acquire a full reverse shell.

Extra Mile II

What are the three world writable directories (permissions of 777 or rwx) with which we can write to, execute from, and read from in Linux Operating Systems? It's useful to know these if we ever need to write out to the file system.
  • /tmp/

  • /var/tmp/

  • /dev/shm/

Case Study - OpenNetAdmin (ONA)

Accessing OpenNetAdmin

Start the VPN, VM, and add the IP to your hosts file.

Discovery and Assessment

OpenNetAdmin Landing Page
Logged in as Administrator

More often than not, if an exploit requires authentication, a low-privileged user (or even a guest user) will suffice for successful exploitation.

ONA Search Functionality
Examining the Nmap Functionality
ONA Reports Section
Discovery of Ping Functionality
Ping Results Window

Exploitation

Activating the Intercept Feature of Burp Suite
Initial Request of Ping in Burp Suite
The Second Request of "Ping to Verify"
Analysis of Request in the Repeater Tab

Full POST Data Payload attempting command injection

Introduction of our Payload into Repeater
Validation of command injection

Last updated