Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (CodeQL) Fixed finding: "Uncontrolled command line" #26

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions introduction/mitre.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,18 @@
return render(request, 'mitre/mitre_lab_17.html')

def command_out(command):
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process = subprocess.Popen(command, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Check failure

Code scanning / CodeQL

Uncontrolled command line Critical

This command line depends on a
user-provided value
.

Copilot Autofix AI about 1 month ago

To fix the problem, we need to ensure that the user-provided input (ip) is properly validated and sanitized before being used in the subprocess.Popen call. The best way to do this is to use a regular expression to validate that the input is a valid IP address. This will prevent any malicious input from being executed as part of the command.

We will add a function to validate the IP address and modify the mitre_lab_17_api function to use this validation before constructing the command variable.

Suggested changeset 1
introduction/mitre.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/introduction/mitre.py b/introduction/mitre.py
--- a/introduction/mitre.py
+++ b/introduction/mitre.py
@@ -231,2 +231,6 @@
 
+def is_valid_ip(ip):
+    pattern = re.compile(r"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$")
+    return pattern.match(ip) is not None
+
 def command_out(command):
@@ -240,2 +244,4 @@
         ip = request.POST.get('ip')
+        if not is_valid_ip(ip):
+            return JsonResponse({'error': 'Invalid IP address'}, status=400)
         command = ["nmap", ip]
EOF
@@ -231,2 +231,6 @@

def is_valid_ip(ip):
pattern = re.compile(r"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$")
return pattern.match(ip) is not None

def command_out(command):
@@ -240,2 +244,4 @@
ip = request.POST.get('ip')
if not is_valid_ip(ip):
return JsonResponse({'error': 'Invalid IP address'}, status=400)
command = ["nmap", ip]
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
return process.communicate()


@csrf_exempt
def mitre_lab_17_api(request):
if request.method == "POST":
ip = request.POST.get('ip')
command = "nmap " + ip
command = ["nmap", ip]
res, err = command_out(command)
res = res.decode()
err = err.decode()
pattern = "STATE SERVICE.*\\n\\n"
ports = re.findall(pattern, res,re.DOTALL)[0][14:-2].split('\n')
return JsonResponse({'raw_res': str(res), 'raw_err': str(err), 'ports': ports})
return JsonResponse({'raw_res': str(res), 'raw_err': str(err), 'ports': ports})
Loading