C.O.P

Description

The C.O.P (Cult of Pickles) have started up a new web store to sell their merch. We believe that the funds are being used to carry out illicit pickle-based propaganda operations! Investigate the site and try and find a way into their operation!

URL: https://app.hackthebox.com/challenges/C.O.Parrow-up-right

Source

app.py

from flask import Flask, g
from application.blueprints.routes import web
import pickle, base64

app = Flask(__name__)
app.config.from_object('application.config.Config')

app.register_blueprint(web, url_prefix='/')

@app.template_filter('pickle')
def pickle_loads(s):
	return pickle.loads(base64.b64decode(s))

@app.teardown_appcontext
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None: db.close()

database.py

models.py

blueprints/routes.py

schema.sql

Solution

The webapp has some merch we can view. Merch is indexed by IDs in /view/{id} endpoint.

C.O.P.png

The first thing that caught my eye was pickle module.

https://flask.palletsprojects.com/en/2.0.x/templating/#registering-filtersarrow-up-right: If you want to register your own filters in Jinja2 you have two ways to do that. You can either put them by hand into the jinja_envarrow-up-right of the application or use the template_filter()arrow-up-right decorator.

https://flask.palletsprojects.com/en/2.0.x/api/#flask.Flask.template_filterarrow-up-right: A decorator that is used to register custom template filter. You can specify a name for the filter, otherwise the function name will be used.

The database doesn't hold records in standard way, it stores them as base64 pickle data.

The view product endpoint is vulnerable to SQLi

There's no sanitization, so we can dump the tables. There's one huge problem problem tho, the database is SQLite and it doesn't contain the flag and nor can we achieve RCE via SQLite.

Python-Pickle-RCE-Exploitarrow-up-right is a famous Python exploit. We can take advantage of base64/pickle usage and send a malicious request.

First let's test locally:

C.O.P-1.png

Same payload doesn't work remotely, probably because curl doesn't exist.

I wasn't able to make application talk to outside using different methods via http. We could hijack the module

The trickiest part was making __reduce__ method work for the Item class, the SimpleNamespace trick worked as a workaround.

circle-check

Last updated