Tags: pwn

Rating:

[write-up by mi0s]

**CTF:** PoliCTF 2017

**Team:** spritzers (from [SPRITZ Research Group](http://spritz.math.unipd.it/))

**Points:** 120

# Challenge

> This Box memorizes a statuses sequence composed by a current status and all the previous ones.
> It already contains a small sequence of statuses, but you can show only the current one.
> You can set a new status, modify the current one or delete it: in this way the box goes back to the previous one in the sequence.
> The box can keep track of maximum 200 statuses. It seems just to work fine, even though we didn't test it a lot...;

The hostname and the port of the box were given:

nc statusbox.chall.polictf.it 31337

# Analysis

Since we know nothing about the box, the first thing we do is connecting to it using netcat.

The following output is printed on the CLI:

> StatusBox started! This Box memorizes a statuses
> sequence composed by a current status and all the previous ones.
> It already contains a small sequence of statuses, but you can show
> only the current one.
> You can set a new status, modify the current one or delete it: in this way
> the box goes back to the previous one in the sequence.
> The box can keep track of maximum 200 statuses.
> It seems just to work fine, even though we didn't test it a lot...
> CURRENT STATUS:
> This is the status set as default current status, change it!
>
>
>
> 0 - Print the current status;
>
> 1 - Set a new current status;
>
> 2 - Delete the current status and go back to the previous one;
>
> 3 - Modify the current status.
>
> 4 - Exit (statuses will be lost.)

All the info we need is given in the header part:

* It already contains a small sequence of statuses, but you can only show the current one;

* You can set a new status, modify the current one or delete it: in this way the box goes back to the previous one in the sequence.

So, we can suppose that there is something before the first status. The problem is:

__how can we reach it?__

Also, we are restricted to only four meaningful actions: *print*, *set*, *delete* and *modify*.

In this scenario the restriction is good for us, we know exactly what we can do. We only have to find the correct sequence of actions.

# Fail to win

The straightforward approach is to delete all the statuses to reach the ones before the first. But when we try to delete the first one, the process loops in the same status without proceeding any further.

At this stage only two useful actions were left:

* set;

* modify.

Note that print does not modify the status.

> It seems just to work fine, even though we didn’t test it a lot…

Ok, probably one of them is buggy. We start checking the corner cases for an input string.

First, we try the modify action by replacing the current status with an empty string.

The box replies with:
>
> Insert the new status, it will modify the current one:
>
> You set the current state to empty, so it was deleted.
> Going back to the previous state.
>
>
> 0 - Print the current status;
>
> 1 - Set a new current status;
>
> 2 - Delete the current status and go back to the previous one;
>
> 3 - Modify the current status.
>
> 4 - Exit (statuses will be lost.)
>
> 0
>
>
> CURRENT STATUS:
>
> That's strange...
>
>
> 0 - Print the current status;
>
> 1 - Set a new current status;
>
> 2 - Delete the current status and go back to the previous one;
>
> 3 - Modify the current status.
>
> 4 - Exit (statuses will be lost.)
>
> 3
>
>
> Insert the new status, it will modify the current one:
>
> You set the current state to empty, so it was deleted.
>
> Going back to the previous state.
>
>
> 0 - Print the current status;
>
> 1 - Set a new current status;
>
> 2 - Delete the current status and go back to the previous one;
>
> 3 - Modify the current status.
>
> 4 - Exit (statuses will be lost.)
>
> 0
>
>
> CURRENT STATUS:
>
> Are you sure of what you're doing?
>
> ...

It works!

When we modify the current status with an empty string the process deletes the current status and give us access to the previous status of the sequence.
Unfortunately, the flag isn't at the -1 status. However, the process give us some hints that make us think that the flag could be hidden deeper in the sequence.
Ok, hoping to find the flag by manually iterating the sequence is a PITA... not an option!

So, we automate the process with a shell oneliner which iterates the sequence, generating a log of the actions and inspecting it for the flag:

bash
cat <(for i in \$(seq 0 99); do echo "0"; sleep 2s; echo "3"; sleep 2s; echo ""; done;) | nc statusbox.chall.polictf.it 31337 | tee status_box.log && cat status_box.log | grep -E "flag{|FLAG{"


Since we do not know how far we have to search, we cat a subshell which iterates over 100 statuses for the first try. The sleep command is a dirty trick used to wait between two actions so that the process will correctly recognize them. Then we tee stdout because we may want to check from time to time if the search is going well while generating the log file. Since the flag format was given in the polictf instruction section, we can easily grep it from the generated log file. The search will take about 100*4s = 400s.

The resulting flag is: flag{g00d_0ld_m1ss1ng_ch3cks!}.

Original writeup (https://github.com/SPRITZ-Research-Group/ctf-writeups/tree/master/polictf-2017/pwnables/status-box-120).
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.2676580548286438, "perplexity": 1995.0364980556785}, "config": {"markdown_headings": true, "markdown_code": false, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2020-34/segments/1596439739048.46/warc/CC-MAIN-20200813161908-20200813191908-00079.warc.gz"}