{% extends "introduction/base.html" %} {% load static %} {% block content %} {% block title %} OS Command Injection {% endblock %}

CWE-78: OS Command Injection

The software builds all or a portion of an OS command using input that has been modified externally by an upstream component, but it fails to remove or removes them wrongly specific aspects that could change the intended OS command when it is sent to a downstream component.
As a result, attackers might be able to run risky, unexpected commands on the operating system. In contexts where the attacker does not have direct access to the operating system, like web applications, this flaw can result in a vulnerability. Alternatively, if the flaw exists in a privileged programme, it might allow the attacker to specify commands that aren't typically available or to call alternative commands with rights they don't have. If the compromised process does not adhere to the concept of least privilege, the issue is made worse since attacker-controlled commands may run with elevated system privileges, increasing the potential for damage.
There are at least two subtypes of OS command injection: These versions exhibit distinct programmer faults, which is a weakness. In the first version, it is obvious that the author intended for the command to be run to include input from unreliable sources as one of its arguments. While the programmer in the second variation does not intend for the command to be accessible to any untrusted parties, it is likely that the programmer did not consider other potential channels via which hostile attackers could supply input.

This lab helps us to understand how command injection is exploitable in scenarios where inputs are sent to exec,eval,sys etc.
The user on accessing the lab is provided with a feature to perform a name server lookup on the given domain. A domain name has to be provided after which the server would perform a ns lookup and return back to the client. If the user is running the lab, based on the OS they can select Windows or Linux.

Exploiting the Bug

  1. Method 1
    • The user can cause the server to execute commands ,because of the lack of input validation.
    • The user can give a domain say domain && [any cmd]
    • In This case lets give google.com && dir and choose windows.
    • This should give you the output for both ns lookup as well as for the dir
  2. Method 2
    • The user can give a domain say domain; [any cmd]
    • In This case lets give google.com; dir and choose windows.
    • This should give you the output for both ns lookup as well as for the dir

Understanding the cause

Lets first see how the name server lookup is performed
command="nslookup {}".format(domain)
Here the domain is the user input domain. This command variable is then sent to exec function and the output is displayed. If the user inputs google.com the command variable will hold nslookup google.com.

How CMD injection works Method 1 Now when the user enters google.com && dir The command variable will hold nslookup google.com && dir. The && means and.
The system will execute nslookup google.com first and then dir
Method 2 When the user enters google.com ; dir The command variable will hold nslookup google.com ; dir. The ; implies the completion of the command before it, in this case the nslookup command.
The system will execute nslookup google.com first and then dir


{% endblock %}