# Ruby: regex authentication bypass

## Introdução

Vamos começar analisando o ambiente que iremos atacar:

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

Esse código irá verificar se a entrada do usuário não possui caracteres especiais, os caracteres permitidos são: **caracteres numéricos** e **letras**

Se digitarmos outro valor por exemplo "**<**" a aplicação irá retornar "Malicious Input Detected".

Após entender o comportamento da aplicação precisamos bypassar esse filtro pra conseguirmos obter SSTI (pressumi que seria isso por estar usando um sistema de templates)

## Bypass Time

Em regex o ^ e $ indicam respectivamente início e fim de linha, ou seja, esa expressão regular está verificando o início e fim de cada linha, então se uma linha for suficiente conseguimos bypassar o filtro, podemos utilizar o **\n** para isso, para explorar podemos utilizar o **Burp** ou o **curl**

Com o **Burp** eu tive que acrescenter um **\n** para conseguir bypassar, depois vi outras soluções na internet e vi que com o curl conseguimos explorar apenas com um **\n**, vamos ver os dois exemplos

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

Agora vamos ver o exemplo com o curl:

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

Podemos copiar o **\n** do Burp e colar na linha de comando do curl

## **Considerações**

Esse ataque é exclusivo (até onde eu testei) do ruby, para conseguirmos o mesmo resultado em outras linguagens precisaria de outras opções no filtro, por exemplo em python se pareceria com isso:

```
if re.match(r'^[0-9a-z]+$', my_input, re.MULTILINE):
    print('Matches...')
```

## Links de apoio

{% embed url="<https://davidhamann.de/2022/05/14/bypassing-regular-expression-checks/>" %}

{% embed url="<https://youtu.be/ClF8sit8a-U>" %}


---

# 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://vida03.gitbook.io/redteam/web/ruby-regex-authentication-bypass.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.
