Santa has launched a new product, the Emoji finder! This is the first version, can you find your favorite emoji?
Visit <https://16.adventofctf.com> to start the challenge.
## Recon
Upon opening the challenge website we're greeted with some text and an input field. The text says the following: "Santa likes emojis! Enter one to find out what it means. Try 'santa' for instance.". If we then enter 'santa' in the input field and press the search button, we get a santa emoji: 🎅.
When opening the source of the page we also find the following comment: "Here is a cheatsheet of the emojis you can use: <https://www.webfx.com/tools/emoji-cheat-sheet/>" and some javascript:
```js
function send() {
let emoji = $("#emoji")[0].value;
if (emoji.length > 0) {
$.post("/", { emoji: emoji }, function (data) {
$("#msg")[0].innerHTML = "<b>" + data + "</b>";
});
}
}
```
_Note: when I was writing this write-up, I noticed that the subtitle hints at the `os` module as the letters "os" are marked yellow in "d**os**e"._
## Finding the vulnerability
After trying several things, [Server-Side Template Injection](https://portswigger.net/research/server-side-template-injection) (SSTI) came to mind.
SSTI occurs when an attacker is able to use native template syntax to inject a malicious payload into a template, which is then executed server-side.
An easy way to check for SSTI is by using the following graph from PortSwigger:
So I followed this graph and got the following results:
`emoji=${7*7}` -> `You entered an unknown emoji: ${7*7}`
`emoji={{7*7}}` -> `You entered an unknown emoji: 49`
`emoji={{7*'7'}}` -> `You entered an unknown emoji: 7777777`
This means the server is most likely using either Jinja2 of Twig.
## Exploit
Now that we found the vulnerability, we can start exploiting it. Let's start by getting the config. We can try to get it by entering `{{config}}` or `{{config.items()}}` as the emoji.
Decrypting the flag was easier said then done. I tried several algorithms but none resulted in any usefull output and it doesn't look like the output of any common encryption algorithm.
This is when I started to try to get the source of the server so we can see how it gets and/or creates the flag. To get the source we first need arbitrary code execution.
### Arbitrary Code Execution (ACE)
The easiest way to get ACE is using the `os` module in python. To use it, the server has to have it imported though.
We can find the `os` module in the `globals` dictionary, which, in turn, can be found in the `__init__` function of the `Config` class. To get to it we can use the following path:
```python
config.__class__.__init__.__globals__["os"]
```
If we enter this as the input but within brackets, we get the following output:
return ''.join(chr(x ^ ord(flag[x]) ^ ord(key[::-1][x]) ^ ord(key[x])) for x in range(len(flag)))
```
The `magic` function takes a flag and key as the input and returns an encrypted string for the output. When anaylizing this function, we see it does some bitwise XOR operations and because we have the key we've got two known values and can, thus, calculate the third.
We can do this by just passing our encrypted flag along with to key to the `magic` function.
```text
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def magic(flag, key):
... return ''.join(chr(x ^ ord(flag[x]) ^ ord(key[::-1][x]) ^ ord(key[x])) for x in range(len(flag)))