Skip to content

Commit

Permalink
PHP tainted exec (#3468)
Browse files Browse the repository at this point in the history
* PHP tainted exec

When user input is passed to a function that executes a shell command, without
escaping.

* Correct message string YAML operator

Co-authored-by: Pieter De Cremer (Semgrep) <pieter@r2c.dev>

---------

Co-authored-by: Pieter De Cremer (Semgrep) <pieter@r2c.dev>
Co-authored-by: Lewis <LewisArdern@live.co.uk>
  • Loading branch information
3 people authored Sep 19, 2024
1 parent b756568 commit 6d1b466
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
21 changes: 21 additions & 0 deletions php/lang/security/injection/tainted-exec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

$userinput = $_GET['userinput'];

// ruleid: tainted-exec
system("ls $userinput");

$escaped = escapeshellarg($userinput);
// ok: tainted-exec
system("ls $escaped");

$descriptors = [["pipe", "r"], ["pipe", "w"], ["pipe", "w"]];
$pipes = [];

// ruleid: tainted-exec
$p = proc_open("ls $userinput", $descriptors, $pipes);
echo stream_get_contents($pipes[1]);

// ok: tainted-exec
$p = proc_open(["ls", $userinput], $descriptors, $pipes);
echo stream_get_contents($pipes[1]);
51 changes: 51 additions & 0 deletions php/lang/security/injection/tainted-exec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
rules:
- id: tainted-exec
languages:
- php
severity: WARNING
message: >-
User input is passed to a function that executes a shell command. This can lead to remote code execution.
metadata:
cwe:
- "CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')"
category: security
technology:
- php
owasp:
- A03:2021 - Injection
references:
- https://owasp.org/Top10/A03_2021-Injection
subcategory:
- vuln
impact: HIGH
likelihood: MEDIUM
confidence: MEDIUM
mode: taint
pattern-sources:
- patterns:
- pattern-either:
- pattern: $_GET
- pattern: $_POST
- pattern: $_COOKIE
- pattern: $_REQUEST
- pattern: file_get_contents('php://input')
pattern-sanitizers:
- patterns:
- pattern-either:
- pattern: escapeshellcmd(...)
- pattern: escapeshellarg(...)
pattern-sinks:
- patterns:
- pattern-either:
- pattern: exec(...)
- pattern: system(...)
- pattern: passthru(...)
- patterns:
- pattern: proc_open(...)
- pattern-not: proc_open([...], ...)
- pattern: popen(...)
- pattern: expect_popen(...)
- pattern: shell_exec(...)
- pattern: |
`...`

0 comments on commit 6d1b466

Please sign in to comment.