Skip to main content

Automatic information enumeration

Need

Prevention of automatic information enumeration

Context

  • Usage of Python 3 for writing and executing Python code
  • Usage of Django for building web applications in Python

Description

Non compliant code

from django.http import HttpResponse
from django.views import View
import os

class SystemInfoView(View):
def get(self, request, *args, **kwargs):
ports = os.popen('netstat -tuln').read()
services = os.popen('systemctl list-units --type=service').read()
user_data = os.popen('cat /etc/passwd').read()
return HttpResponse(f'Open Ports: {ports} \\n\\n Services: {services} \\n\\n User Data: {user_data}')

The above Python code is a Django View that exposes system information such as open ports, available services and users personal data.

When a GET request is made to this view, the get method is executed. Inside this method, we are using os.popen function to execute system commands and get the output:

  1. netstat -tuln - This command lists all the network ports that are currently open.
  2. systemctl list-units --type=service - This command lists all the services that are currently running on the system.
  3. cat /etc/passwd - This command lists all the users' data on the system.

The output of these commands is then returned in the HTTP response. This is a major security vulnerability as it exposes sensitive system information to potential attackers.

Steps

  • Remove the code that exposes sensitive system information. The application should not provide any functionality that allows users to retrieve system information such as open ports, available services, and user data.
  • Avoid using os.popen to execute system commands from within the application. This can lead to command injection vulnerabilities.
  • If it's necessary to provide some system information, ensure that it's properly sanitized and does not expose sensitive details. For example, instead of listing all services, you could provide a status check for specific services that the user needs to know about.
  • Implement proper access controls to ensure that only authorized users can access certain views or endpoints. This can help prevent unauthorized information disclosure.
  • Regularly audit your code for security vulnerabilities and use security tools to help identify potential issues.

Compliant code

from django.http import HttpResponse
from django.views import View

class SystemInfoView(View):
def get(self, request, *args, **kwargs):
return HttpResponse('System Info View is currently under maintenance.')

The previous version of the code had a serious security vulnerability. It was using os.popen to execute system commands and retrieve sensitive system information such as open ports, available services, and user data. This information was then returned in the HTTP response.

This is a major security risk as it allows for automatic information enumeration. An attacker could use this to gain valuable information about the system, which could be used in further attacks.

The new version of the code removes this functionality entirely. Now, when the SystemInfoView is accessed, it simply returns a message stating that the view is under maintenance. This is a temporary measure and in a real-world scenario, you would want to replace this with functionality that is safe and meets the needs of your application.

If it's necessary to provide some system information, ensure that it's properly sanitized and does not expose sensitive details. For example, instead of listing all services, you could provide a status check for specific services that the user needs to know about.

Additionally, it's important to implement proper access controls to ensure that only authorized users can access certain views or endpoints. This can help prevent unauthorized information disclosure.

Regularly audit your code for security vulnerabilities and use security tools to help identify potential issues. This will help you maintain a high level of security in your applications.

References