Skip to content

Commit

Permalink
feat: exec using user input rule
Browse files Browse the repository at this point in the history
  • Loading branch information
didroe committed Sep 22, 2023
1 parent e6179a3 commit 3903f1a
Show file tree
Hide file tree
Showing 6 changed files with 453 additions and 0 deletions.
75 changes: 75 additions & 0 deletions rules/php/lang/exec_using_user_input.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
languages:
- php
imports:
- php_shared_lang_user_input
patterns:
- pattern: $<FUNCTION>($<USER_INPUT>$<...>)
filters:
- variable: FUNCTION
values:
- exec
- passthru
- system
- shell_exec
- popen
- proc_open
- variable: USER_INPUT
detection: php_shared_lang_user_input
scope: result
- pattern: proc_open($<_>, $<_>, $<_>, $<WORKING_DIR>)
filters:
- variable: WORKING_DIR
detection: php_shared_lang_user_input
scope: result
- pattern: proc_open($<_>, $<_>, $<_>, $<_>, $<ENVIRONMENT>)
filters:
- variable: ENVIRONMENT
detection: php_shared_lang_user_input
scope: result
- pattern: pcntl_exec($<COMMAND>, $<ARGUMENTS>, $<ENVIRONMENT>)
filters:
- either:
- variable: COMMAND
detection: php_shared_lang_user_input
scope: result
- variable: ARGUMENTS
detection: php_shared_lang_user_input
scope: result
- variable: ENVIRONMENT
detection: php_shared_lang_user_input
scope: result
severity: high
metadata:
description: "Execution of OS command formed with user input detected."
remediation_message: |
## Description
Applications should not execute OS commands that are formed from user input.
This rule checks for external commands containing user-supplied data.
## Remediations
❌ Avoid using user input when executing commands:
```php
exec($_GET["command"]);
```
✅ Use user input indirectly when executing commands:
```php
if ($_GET["action"] == "option1") {
$command = "command1";
} else {
$command = "command2";
}
exec($command);
```
## Resources
- [OWASP OS command injection cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html)
cwe_id:
- 78
id: php_lang_exec_using_user_input
documentation_url: https://docs.bearer.com/reference/rules/php_lang_exec_using_user_input
316 changes: 316 additions & 0 deletions tests/php/lang/exec_using_user_input/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,316 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`php_lang_exec_using_user_input bad 1`] = `
"{
"high": [
{
"cwe_ids": [
"78"
],
"id": "php_lang_exec_using_user_input",
"title": "Execution of OS command formed with user input detected.",
"description": "## Description\\n\\nApplications should not execute OS commands that are formed from user input.\\nThis rule checks for external commands containing user-supplied data.\\n\\n## Remediations\\n\\n❌ Avoid using user input when executing commands:\\n\\n\`\`\`php\\nexec($_GET[\\"command\\"]);\\n\`\`\`\\n\\n✅ Use user input indirectly when executing commands:\\n\\n\`\`\`php\\nif ($_GET[\\"action\\"] == \\"option1\\") {\\n $command = \\"command1\\";\\n} else {\\n $command = \\"command2\\";\\n}\\n\\nexec($command);\\n\`\`\`\\n\\n## Resources\\n- [OWASP OS command injection cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html)\\n",
"documentation_url": "https://docs.bearer.com/reference/rules/php_lang_exec_using_user_input",
"line_number": 3,
"full_filename": "/tmp/bearer-scan/bad.php",
"filename": ".",
"source": {
"start": 3,
"end": 3,
"column": {
"start": 1,
"end": 35
}
},
"sink": {
"start": 3,
"end": 3,
"column": {
"start": 1,
"end": 35
},
"content": "shell_exec(\\"cat \\" . $_GET[\\"oops\\"])"
},
"parent_line_number": 3,
"snippet": "shell_exec(\\"cat \\" . $_GET[\\"oops\\"])",
"fingerprint": "5effa33687807c3d7e79f772b9059ae2_0",
"old_fingerprint": "5a8d4d52352f8573763b065947165e1c_0",
"code_extract": "shell_exec(\\"cat \\" . $_GET[\\"oops\\"]);"
},
{
"cwe_ids": [
"78"
],
"id": "php_lang_exec_using_user_input",
"title": "Execution of OS command formed with user input detected.",
"description": "## Description\\n\\nApplications should not execute OS commands that are formed from user input.\\nThis rule checks for external commands containing user-supplied data.\\n\\n## Remediations\\n\\n❌ Avoid using user input when executing commands:\\n\\n\`\`\`php\\nexec($_GET[\\"command\\"]);\\n\`\`\`\\n\\n✅ Use user input indirectly when executing commands:\\n\\n\`\`\`php\\nif ($_GET[\\"action\\"] == \\"option1\\") {\\n $command = \\"command1\\";\\n} else {\\n $command = \\"command2\\";\\n}\\n\\nexec($command);\\n\`\`\`\\n\\n## Resources\\n- [OWASP OS command injection cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html)\\n",
"documentation_url": "https://docs.bearer.com/reference/rules/php_lang_exec_using_user_input",
"line_number": 6,
"full_filename": "/tmp/bearer-scan/bad.php",
"filename": ".",
"source": {
"start": 6,
"end": 6,
"column": {
"start": 1,
"end": 38
}
},
"sink": {
"start": 6,
"end": 6,
"column": {
"start": 1,
"end": 38
},
"content": "exec(\\"cat \\" . $_GET[\\"oops\\"], $output)"
},
"parent_line_number": 6,
"snippet": "exec(\\"cat \\" . $_GET[\\"oops\\"], $output)",
"fingerprint": "5effa33687807c3d7e79f772b9059ae2_1",
"old_fingerprint": "5a8d4d52352f8573763b065947165e1c_1",
"code_extract": "exec(\\"cat \\" . $_GET[\\"oops\\"], $output);"
},
{
"cwe_ids": [
"78"
],
"id": "php_lang_exec_using_user_input",
"title": "Execution of OS command formed with user input detected.",
"description": "## Description\\n\\nApplications should not execute OS commands that are formed from user input.\\nThis rule checks for external commands containing user-supplied data.\\n\\n## Remediations\\n\\n❌ Avoid using user input when executing commands:\\n\\n\`\`\`php\\nexec($_GET[\\"command\\"]);\\n\`\`\`\\n\\n✅ Use user input indirectly when executing commands:\\n\\n\`\`\`php\\nif ($_GET[\\"action\\"] == \\"option1\\") {\\n $command = \\"command1\\";\\n} else {\\n $command = \\"command2\\";\\n}\\n\\nexec($command);\\n\`\`\`\\n\\n## Resources\\n- [OWASP OS command injection cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html)\\n",
"documentation_url": "https://docs.bearer.com/reference/rules/php_lang_exec_using_user_input",
"line_number": 9,
"full_filename": "/tmp/bearer-scan/bad.php",
"filename": ".",
"source": {
"start": 9,
"end": 9,
"column": {
"start": 1,
"end": 47
}
},
"sink": {
"start": 9,
"end": 9,
"column": {
"start": 1,
"end": 47
},
"content": "passthru(\\"cat \\" . $_GET[\\"oops\\"], $result_code)"
},
"parent_line_number": 9,
"snippet": "passthru(\\"cat \\" . $_GET[\\"oops\\"], $result_code)",
"fingerprint": "5effa33687807c3d7e79f772b9059ae2_2",
"old_fingerprint": "5a8d4d52352f8573763b065947165e1c_2",
"code_extract": "passthru(\\"cat \\" . $_GET[\\"oops\\"], $result_code);"
},
{
"cwe_ids": [
"78"
],
"id": "php_lang_exec_using_user_input",
"title": "Execution of OS command formed with user input detected.",
"description": "## Description\\n\\nApplications should not execute OS commands that are formed from user input.\\nThis rule checks for external commands containing user-supplied data.\\n\\n## Remediations\\n\\n❌ Avoid using user input when executing commands:\\n\\n\`\`\`php\\nexec($_GET[\\"command\\"]);\\n\`\`\`\\n\\n✅ Use user input indirectly when executing commands:\\n\\n\`\`\`php\\nif ($_GET[\\"action\\"] == \\"option1\\") {\\n $command = \\"command1\\";\\n} else {\\n $command = \\"command2\\";\\n}\\n\\nexec($command);\\n\`\`\`\\n\\n## Resources\\n- [OWASP OS command injection cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html)\\n",
"documentation_url": "https://docs.bearer.com/reference/rules/php_lang_exec_using_user_input",
"line_number": 10,
"full_filename": "/tmp/bearer-scan/bad.php",
"filename": ".",
"source": {
"start": 10,
"end": 10,
"column": {
"start": 1,
"end": 45
}
},
"sink": {
"start": 10,
"end": 10,
"column": {
"start": 1,
"end": 45
},
"content": "system(\\"cat \\" . $_GET[\\"oops\\"], $result_code)"
},
"parent_line_number": 10,
"snippet": "system(\\"cat \\" . $_GET[\\"oops\\"], $result_code)",
"fingerprint": "5effa33687807c3d7e79f772b9059ae2_3",
"old_fingerprint": "5a8d4d52352f8573763b065947165e1c_3",
"code_extract": "system(\\"cat \\" . $_GET[\\"oops\\"], $result_code);"
},
{
"cwe_ids": [
"78"
],
"id": "php_lang_exec_using_user_input",
"title": "Execution of OS command formed with user input detected.",
"description": "## Description\\n\\nApplications should not execute OS commands that are formed from user input.\\nThis rule checks for external commands containing user-supplied data.\\n\\n## Remediations\\n\\n❌ Avoid using user input when executing commands:\\n\\n\`\`\`php\\nexec($_GET[\\"command\\"]);\\n\`\`\`\\n\\n✅ Use user input indirectly when executing commands:\\n\\n\`\`\`php\\nif ($_GET[\\"action\\"] == \\"option1\\") {\\n $command = \\"command1\\";\\n} else {\\n $command = \\"command2\\";\\n}\\n\\nexec($command);\\n\`\`\`\\n\\n## Resources\\n- [OWASP OS command injection cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html)\\n",
"documentation_url": "https://docs.bearer.com/reference/rules/php_lang_exec_using_user_input",
"line_number": 12,
"full_filename": "/tmp/bearer-scan/bad.php",
"filename": ".",
"source": {
"start": 12,
"end": 12,
"column": {
"start": 6,
"end": 40
}
},
"sink": {
"start": 12,
"end": 12,
"column": {
"start": 6,
"end": 40
},
"content": "popen(\\"cat \\" . $_GET[\\"oops\\"], \\"r\\")"
},
"parent_line_number": 12,
"snippet": "popen(\\"cat \\" . $_GET[\\"oops\\"], \\"r\\")",
"fingerprint": "5effa33687807c3d7e79f772b9059ae2_4",
"old_fingerprint": "5a8d4d52352f8573763b065947165e1c_4",
"code_extract": "$f = popen(\\"cat \\" . $_GET[\\"oops\\"], \\"r\\");"
},
{
"cwe_ids": [
"78"
],
"id": "php_lang_exec_using_user_input",
"title": "Execution of OS command formed with user input detected.",
"description": "## Description\\n\\nApplications should not execute OS commands that are formed from user input.\\nThis rule checks for external commands containing user-supplied data.\\n\\n## Remediations\\n\\n❌ Avoid using user input when executing commands:\\n\\n\`\`\`php\\nexec($_GET[\\"command\\"]);\\n\`\`\`\\n\\n✅ Use user input indirectly when executing commands:\\n\\n\`\`\`php\\nif ($_GET[\\"action\\"] == \\"option1\\") {\\n $command = \\"command1\\";\\n} else {\\n $command = \\"command2\\";\\n}\\n\\nexec($command);\\n\`\`\`\\n\\n## Resources\\n- [OWASP OS command injection cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html)\\n",
"documentation_url": "https://docs.bearer.com/reference/rules/php_lang_exec_using_user_input",
"line_number": 14,
"full_filename": "/tmp/bearer-scan/bad.php",
"filename": ".",
"source": {
"start": 14,
"end": 14,
"column": {
"start": 1,
"end": 40
}
},
"sink": {
"start": 14,
"end": 14,
"column": {
"start": 1,
"end": 40
},
"content": "proc_open([\\"cat\\", $_GET[\\"oops\\"]], null)"
},
"parent_line_number": 14,
"snippet": "proc_open([\\"cat\\", $_GET[\\"oops\\"]], null)",
"fingerprint": "5effa33687807c3d7e79f772b9059ae2_5",
"old_fingerprint": "5a8d4d52352f8573763b065947165e1c_5",
"code_extract": "proc_open([\\"cat\\", $_GET[\\"oops\\"]], null);"
},
{
"cwe_ids": [
"78"
],
"id": "php_lang_exec_using_user_input",
"title": "Execution of OS command formed with user input detected.",
"description": "## Description\\n\\nApplications should not execute OS commands that are formed from user input.\\nThis rule checks for external commands containing user-supplied data.\\n\\n## Remediations\\n\\n❌ Avoid using user input when executing commands:\\n\\n\`\`\`php\\nexec($_GET[\\"command\\"]);\\n\`\`\`\\n\\n✅ Use user input indirectly when executing commands:\\n\\n\`\`\`php\\nif ($_GET[\\"action\\"] == \\"option1\\") {\\n $command = \\"command1\\";\\n} else {\\n $command = \\"command2\\";\\n}\\n\\nexec($command);\\n\`\`\`\\n\\n## Resources\\n- [OWASP OS command injection cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html)\\n",
"documentation_url": "https://docs.bearer.com/reference/rules/php_lang_exec_using_user_input",
"line_number": 16,
"full_filename": "/tmp/bearer-scan/bad.php",
"filename": ".",
"source": {
"start": 16,
"end": 16,
"column": {
"start": 1,
"end": 46
}
},
"sink": {
"start": 16,
"end": 16,
"column": {
"start": 1,
"end": 46
},
"content": "pcntl_exec(\\"/bin/\\" . $_GET[\\"oops\\"], [], null)"
},
"parent_line_number": 16,
"snippet": "pcntl_exec(\\"/bin/\\" . $_GET[\\"oops\\"], [], null)",
"fingerprint": "5effa33687807c3d7e79f772b9059ae2_6",
"old_fingerprint": "5a8d4d52352f8573763b065947165e1c_6",
"code_extract": "pcntl_exec(\\"/bin/\\" . $_GET[\\"oops\\"], [], null);"
},
{
"cwe_ids": [
"78"
],
"id": "php_lang_exec_using_user_input",
"title": "Execution of OS command formed with user input detected.",
"description": "## Description\\n\\nApplications should not execute OS commands that are formed from user input.\\nThis rule checks for external commands containing user-supplied data.\\n\\n## Remediations\\n\\n❌ Avoid using user input when executing commands:\\n\\n\`\`\`php\\nexec($_GET[\\"command\\"]);\\n\`\`\`\\n\\n✅ Use user input indirectly when executing commands:\\n\\n\`\`\`php\\nif ($_GET[\\"action\\"] == \\"option1\\") {\\n $command = \\"command1\\";\\n} else {\\n $command = \\"command2\\";\\n}\\n\\nexec($command);\\n\`\`\`\\n\\n## Resources\\n- [OWASP OS command injection cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html)\\n",
"documentation_url": "https://docs.bearer.com/reference/rules/php_lang_exec_using_user_input",
"line_number": 17,
"full_filename": "/tmp/bearer-scan/bad.php",
"filename": ".",
"source": {
"start": 17,
"end": 17,
"column": {
"start": 1,
"end": 41
}
},
"sink": {
"start": 17,
"end": 17,
"column": {
"start": 1,
"end": 41
},
"content": "pcntl_exec(\\"cat\\", [$_GET[\\"oops\\"]], null)"
},
"parent_line_number": 17,
"snippet": "pcntl_exec(\\"cat\\", [$_GET[\\"oops\\"]], null)",
"fingerprint": "5effa33687807c3d7e79f772b9059ae2_7",
"old_fingerprint": "5a8d4d52352f8573763b065947165e1c_7",
"code_extract": "pcntl_exec(\\"cat\\", [$_GET[\\"oops\\"]], null);"
},
{
"cwe_ids": [
"78"
],
"id": "php_lang_exec_using_user_input",
"title": "Execution of OS command formed with user input detected.",
"description": "## Description\\n\\nApplications should not execute OS commands that are formed from user input.\\nThis rule checks for external commands containing user-supplied data.\\n\\n## Remediations\\n\\n❌ Avoid using user input when executing commands:\\n\\n\`\`\`php\\nexec($_GET[\\"command\\"]);\\n\`\`\`\\n\\n✅ Use user input indirectly when executing commands:\\n\\n\`\`\`php\\nif ($_GET[\\"action\\"] == \\"option1\\") {\\n $command = \\"command1\\";\\n} else {\\n $command = \\"command2\\";\\n}\\n\\nexec($command);\\n\`\`\`\\n\\n## Resources\\n- [OWASP OS command injection cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html)\\n",
"documentation_url": "https://docs.bearer.com/reference/rules/php_lang_exec_using_user_input",
"line_number": 18,
"full_filename": "/tmp/bearer-scan/bad.php",
"filename": ".",
"source": {
"start": 18,
"end": 18,
"column": {
"start": 1,
"end": 40
}
},
"sink": {
"start": 18,
"end": 18,
"column": {
"start": 1,
"end": 40
},
"content": "pcntl_exec(\\"cat\\", [$ok], $_GET[\\"oops\\"])"
},
"parent_line_number": 18,
"snippet": "pcntl_exec(\\"cat\\", [$ok], $_GET[\\"oops\\"])",
"fingerprint": "5effa33687807c3d7e79f772b9059ae2_8",
"old_fingerprint": "5a8d4d52352f8573763b065947165e1c_8",
"code_extract": "pcntl_exec(\\"cat\\", [$ok], $_GET[\\"oops\\"]);"
}
]
}"
`;

exports[`php_lang_exec_using_user_input ok 1`] = `"{}"`;
Loading

0 comments on commit 3903f1a

Please sign in to comment.