Home [FCSC 2023] - Tri sélectif
Post
Cancel

[FCSC 2023] - Tri sélectif


Vous devez trier un tableau dont vous ne voyez pas les valeurs !

nc challenges.france-cybersecurity-challenge.fr 2051



Given files

We are given the following file, showing us how to interact with the remote shell:

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
import os

def usage():
    print('Actions possibles:')
    print('  - "comparer X Y": compare les valeurs du tableau aux cases X et Y, et retourne 1 si la valeur en X est inférieure ou égale à celle en Y, 0 sinon.')
    print('  - "echanger X Y": échange les valeurs du tableau aux cases X et Y, et affiche le taleau modifié.')
    print('  - "longueur:      retourne la longueur du tableau.')
    print('  - "verifier:      retourne le flag si le tableau est trié.')

def printArray(A):
    print(" ".join("*" for a in A))

def verifier(A):
    return all([ A[i] <= A[i + 1] for i in range(len(A) - 1) ])

if __name__ == "__main__":

    A = list(os.urandom(32))
    print("Votre but est de trier un tableau dont vous ne voyez pas les valeurs (chacune est remplacée par *) :")
    printArray(A)
    usage()
    B = A[:]
    
    try:
        while True:
            x = input(">>> ")

            if x.startswith("comparer"):
                x, y = list(map(int, x.split(" ")[1:]))
                print(int(A[x] <= A[y]))

            elif x.startswith("echanger"):
                x, y = list(map(int, x.split(" ")[1:]))
                A[x], A[y] = A[y], A[x]

            elif x.startswith("longueur"):
				print(len(A))

            elif x.startswith("verifier"):
                c = verifier(A)
                if c:
                    flag = open("flag.txt").read().strip()
                    print(f"Le flag est : {flag}")
                else:
                    print("Erreur : le tableau n'est pas trié")
                    print(f"Le tableau de départ était : {B}")
                    print(f"Le tableau final est :       {A}")
                    print("Bye bye!")
                    break
            else:
                usage()
    except:
        print("Erreur : vérifier les commandes envoyées.")

Exploit

Here is my simple script to solve this challenge:

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
from pwn import remote
import re 
import time

HOST, PORT = "challenges.france-cybersecurity-challenge.fr", 2051

r = remote(HOST, PORT)

length = 32

# skip énoncé
for i in range(7):
    print(r.recvline().decode())

for i in range(length):
    for j in range(i + 1, length):
        r.sendline(f"comparer {i} {j}".encode())
        print(f">>> comparer {i} {j}")
        res = r.recvline().decode()
        match = re.search(r'\d+', res)
        if match:
            val = int(match.group())
            print(">>> "+str(val))
            if val == 0:
                r.sendline(f"echanger {i} {j}".encode())
                print(f">>> echanger {i} {j}")

time.sleep(1)
r.sendline(b'verifier')
print(">>> verifier")
print(r.recvline().decode())
print(r.recvline().decode()) 

An example of a runtime:

0

🚩FCSC{e687c4749f175489512777c26c06f40801f66b8cf9da3d97bfaff4261f121459}

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