# Neonify

## Enumeration

Checking the website, we are greeted with a service that allows our input to be "neonified".

<figure><img src="/files/Ee5urK5UKK1UoYFr3Anp" alt=""><figcaption></figcaption></figure>

Checking the Dockerfile, we see that we're using Ruby so this is most likely a Ruby on Rails web service.

<figure><img src="/files/IWglJILwqdL76J54dU7F" alt=""><figcaption></figcaption></figure>

## Finding the vulnerable sink

Checking the source code, we find that when we do a `POST` request, it gets the `neon` parameter from that request, and throws it into a regex which checks if its only alphanumeric. After that, it calls in ERB which is a templating language built for Ruby.

<figure><img src="/files/UdPLeQ0oGgebTSL9hxGt" alt=""><figcaption></figcaption></figure>

Looking for vulnerabilities on the source code, there's a[ StackOverflow post](https://stackoverflow.com/questions/577653/difference-between-a-z-and-in-ruby-regular-expressions) regarding the use of `^` and `$` in regex. Basically, these two characters only match up to a newline character so when the application receives an input such as `hello\n{malicious_input}`, the malicious input will not be inspected by the regex.&#x20;

What is instead recommended is to either use:

* &#x20;`\A` - which matches the beginning of the string.
* &#x20;`\z` which matches the end of a string.
* &#x20;`\Z` in some cases where you want to match the end of a string unless there's a newline character in which it'll try to match just before that.

We can see that in the code, it uses both `^` and `$`. Since this is being passed to an ERB function, we can then perform server-side template injection. [HackTricks](https://book.hacktricks.xyz/pentesting-web/ssti-server-side-template-injection#erb-ruby) has a good article on how we can exploit SSTI for ERB on Ruby.

## SSTI via neon parameter

As a proof-of-concept, we'll send this payload which tries to read `/etc/passwd`.

```
test\n<%= File.open('/etc/passwd').read %>
test%0A%3C%25%3D+File%2Eopen%28%27%2Fetc%2Fpasswd%27%29%2Eread+%25%3E
```

Testing it out, we see it works and we have code execution. It is now trivial to retrieve the flag.

<figure><img src="/files/9jHAUVjqalReG0PuEFDf" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mknukn.gitbook.io/infosec-blog/ctfs/hackthebox/web-challenges/neonify.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
