Implementation of proper data validation and sanitization techniques
import os
from django.http import HttpResponse
def run_command(request):
command = request.GET.get('command', '')
os.system(command)
return HttpResponse("Command executed successfully")
In the above code snippet, the function
run_command(request)
is designed to receive a GET request, extract a command from the request parameters, and execute it on the server using the
os.system()
function.
This is a serious security vulnerability, as it lacks proper data validation and sanitization. The application is using user-provided data (the 'command' variable) directly in a system command without first sanitizing it. This means that an attacker could potentially manipulate the 'command' variable to execute arbitrary system commands on the server.
For instance, an attacker could send a GET request with a 'command' parameter like
; rm -rf /
, which would delete all files on the server if it has sufficient permissions. This is a classic example of a Command Injection vulnerability.
import os
import shlex
from django.http import HttpResponse
ALLOWED_COMMANDS = ['ls', 'pwd', 'date']
def sanitize_command(command):
return ''.join(ch for ch in command if ch.isalnum())
def run_command(request):
command = request.GET.get('command', '')
command = sanitize_command(command)
if command in ALLOWED_COMMANDS:
os.system(shlex.quote(command))
return HttpResponse("Command executed successfully")
else:
return HttpResponse("Invalid command")
The updated code includes a function
sanitize_command
that sanitizes the user input by removing any character that is not alphanumeric. This effectively neutralizes special elements that could modify the system command.
The
run_command
function now uses the sanitized command instead of the raw user input. It also checks if the command is in the
ALLOWED_COMMANDS
list before executing it. This is a whitelist of allowed commands. If the command is not in the list, the function returns an "Invalid command" response.
The
os.system
call now uses
shlex.quote
to further ensure that the command is properly escaped before it's executed. This provides an additional layer of protection against command injection attacks.
This approach significantly reduces the risk of command injection attacks by ensuring that only safe, whitelisted commands can be executed and that any user input used in these commands is properly sanitized and escaped.