-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
b756568
commit 6d1b466
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | | ||
`...` | ||