Command Injection
Command Injection
Abstract
7 References 24
2
1. What is Command Injection ?
3
2. Possible consequences of command injection
• Network traffic from the target system can be redirected to another system
under the attacker’s control, allowing them to intercept and see private
information.
• Using command injection, an attacker can change direction and target dif-
ferent systems connected to the same network as the vulnerable machine.
• Attackers can alter data on the target system, which might result in lost
or inaccurate data.
4
3. How to exploit example vulnerabilities ?
5
3.1. Detecting vulnerability in first form
When we enter an IP address using the first button for its intended purpose,
we encounter an expected output, as in Figure 2.
Then we write ’; whoami’ to the search query to check if we can run extra
OS Commands, suspecting that user-supplied input is not sanitized. In the
red framed output section in Figure 3, we see a new field consisting of a ’dev.’
This is because we bypassed the IP field with ’;’ and executed the ’whoami’
instruction in the host machine.
Figure 3: IP
6
3.2. Detecting vulnerability in alternative implementation of first form
bypass this control by changing the inputs accepted by the system. So, first,
we try the valid input form; after changing the inputs step by step, we see that
the commands added to the end of the proper IP addresses are not stuck in this
control. We successfully printed out working directory with ’pwd’ command.
In Figure 5.
7
3.3. Detecting vulnerability in second form
First, we can understand the save form and what it does in the background.
With two inputs, it gives the output as ”Your alias saved successfully.” Nothing
changes if we try to give OS commands to the Username field (Figure 7).
But when we use the Alias field for the commands, we also get ’test’ in addition
to the success message. We tried to read the passwords in the etc/host/ direc-
tory, and although we did not have a decent output, the change in the output
gave us a clue that something was wrong here. (Figure 8)
Based on the previous tip, a string manipulation might exist. When we try
to execute our command between semi-colons, we see that we can inject the
command in Figure 9. We can also get working directory and search for other
user inputs. Figure 10
8
Figure 9: Successfully injecting ls command
We will use the Ping form while showing the Burp Suite usage step by step.
• Open web page and ensure under the Proxy tab, Intercept is on
• Burp Suite will intercept our request, right click on the Intercepted Raw
request and Send To Intruder.
9
Figure 12: Intercepted request
10
• Clear unrelated payload positions with buttons on the right such that we
will only have our IP field as payload position.
11
• Go to Payloads, under Payload Options, load your payload and start the
attack.
12
Figure 15: Preview of the payload file.
13
• As seen in the Figure 16, it can be determined which command is working,
and you can examine the responses.
14
3.6. One step further, Reverse TCP Shell
15
4. Vulnerable code examples
In this part, we will discuss vulnerable code examples and codes we ex-
ploited in the previous section. Like other injection attacks, unsanitized user
input makes command injection possible. And this is irrespective of the pro-
gramming language used. Code examples will be written in Python.
The figures below contain the vulnerable codes we exploited in the previous
section.
16
Figure 20: DNS Lookup
• Validate and sanitize user input: This includes evaluating all user input to
ensure it is in the desired format and free of potentially harmful characters
or commands. This can assist in avoiding dangerous code injection into
your applications by attackers.
17
attacks. To stop these attacks, ensure you are utilizing an up-to-date and
secure framework.
• Use WAF: By filtering incoming traffic and preventing requests that con-
tain harmful code or commands, a web application firewall (WAF) can
help to safeguard your applications. This may add another level of de-
fense against command injection attacks.
• Lastly, informing your users about the dangers of injection attacks and
the necessity of only entering reliable information into your applications
is crucial. This can lessen the chance of attackers taking advantage of
unsuspecting users.
To avoid command injection, Use an internal API whenever possible (if one
exists) rather than an OS command. Avoid passing user-controlled input wher-
ever possible, and if you have to, use an array with a sequence of program
arguments rather than a single string. To do this in Python, we must set the
subprocess’s shell flag to false. (shell=False’). Because shell=True propagates
existing shell settings and variables, using it is risky. Executing OS commands
with shell=True makes it considerably more straightforward for a malicious ac-
tor to execute commands since variables, glob patterns, and other unique shell
features in the command string are processed before the command is launched.
On the other hand, when using particular shell features, such as word splitting
or argument expansion, shell=True can be useful. If such a feature is necessary,
use the various modules available to you instead, such as os.path.expandvars()
for parameter expansion or shlex for word splitting. More work is required, but
other issues are avoided.
In our code, for the ping function, first, we disabled the shell flag for the
subprocess.run command. After we added a function to use the ipaddress li-
brary, Our IP address is not actually being verified by the ipaddress module. It
attempts to generate a Python IP address object using the supplied string. We
must develop our IP validation function to apply this reasoning to an IP.
18
After implementing the validate ip function, we add a tested, secure regex to
ensure only if the input is a valid IP address.
19
Also in nick function, we used write() and open() instead of OS commands.
20
6. Attempt to exploit more secure code
Since we have provided more secure implementation, we can test using the
Burp suite as we did in the 3rd section by combining open-source payload ex-
amples and payloads that we used previously. Figure 25
21
First, we turn intercept on and send our request from the web page. After
loading payloads, Figure 26, we start an attack on different forms in our web
application.
22
Finally, we can inspect the responses of different payloads and ensure that
we did manage to prevent injecting commands to the host OS.
23
7. References
• https://semgrep.dev/docs/cheat-sheets/python-command-injection/
• https://portswigger.net/support/using-burp-to-test-for-os-command-injection-vulnerabil
• https://knowledge-base.secureflag.com/vulnerabilities/code_injection/
os_command_injection_python.html
• https://thesecmaster.com/what-is-command-injection-vulnerability-and-how-to-prevent-it
• https://portswigger.net/web-security/os-command-injection
• https://github.com/commixproject/commix
• https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess
• https://www.stackhawk.com/blog/command-injection-python/
• https://www.shiftleft.io/blog/find-command-injection-in-source-code/
• https://www.abstractapi.com/guides/validate-ip-address-python
• https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_
Defense_Cheat_Sheet.html
• https://github.com/payloadbox/command-injection-payload-list
24