Solution de timruff pour Intro to pwntools

intro misc

9 mars 2026

Contexte

Ici nous avons une introduction à l’outil pwntools qui permet de nous faciliter certaines tâches pour les exploitations et le CTF. Le but ici c’est :

Solution

Installation de pwntools.

pip install pwntools --break-system-packages

Explication du fichier template.py

#!/usr/bin/env python

# Install using pip:
#    pip install pwntools --break-system-packages
from pwn import *  # importation des différents librairies de pwn

'''
To use this template, execute the script with python3:
$ python3 template.py

If you want to run it with a different server and port, use
$ python3 template.py HOST=localhost PORT=4000

If you want to debug and see the data going exchanged, add "DEBUG":
$ python3 template.py DEBUG
'''

# Define the remote connexion details: server address and port
# Prend en argument une adresse et un port sinon si rien est mis on en met par défaut
HOST = args.HOST or "chall.fcsc.fr"
PORT = args.PORT or 2053

# Connect to the remote server
# The variable "io" will be used to send and receive text to the remove server
# Connextion au serveur distant avec l'adresse et le port
io = remote(HOST, PORT)

# Receive 'Welcome to this introduction to pwntools!'
# We don't need to save it to a variable as there is no need to process this line
# Reçoit la ligne 'Welcome to this introduction to pwntools!'
io.recvline()

# Receive "First, send me 'GO' after the '>>>' of the next line"
# Reçoit la ligne "First, send me 'GO' after the '>>>' of the next line"
io.recvline()

# Then, we write "GO" after reading ">>> "
# Après avoir lu ">>>" nous écrivons "Go!"
io.sendlineafter(b">>> ", b"Go!")

# Then, there are 2 lines containing some instructions.
# We don't need to save them.
# Nous récupérons 2 autre lignes, nous n'avons pas besoin de sauvegarder les valeurs
io.recvlines(2)

# Now we need to extract a value from this line.
# It is of the form:
# 	"then I will write a number: add XXXX to it, then write the result back to me!",
# where XXXX is the value we need to extract.
# Nous allons extraire les valeurs de cette lignes
# Récupère la ligne la nettoie (enlève espace blanc) et la convertie en utf-8
val = io.recvline().strip().decode()
# La valeur à récupérer se trouve à l'indice 31 inclut à -41 exclut cette
# valeur est ensuite convertie en entier
val = int(val[32:-41])

# Now, we need to read lines until b"===" (we read bytes, not strings)
# Maintenant nous lisons les lignes jusqu'à b"===" nous lisons des octects, pas une 
# chaîne
io.recvuntil(b"===")

# Next, we receive a number, and we store it in x.
# Nous lisons jusqu'a b"Here is a number: "
io.recvuntil(b"Here is a number: ")
# Nous recevons un nombre et nous le stockons dans x 
x = io.recvline()

# It is currently stored in a variable of type "bytes".
# So we convert it first to a string.
# On transforme la variable en utf8 et nous la nettoyons
x = x.decode().strip()

# And then to an integer.
# On la convertie en entier
x = int(x)

# Log what we have read (there is also `log.info()`, `log.error()`, etc.).
# Le Log est ce que nous avons lu (il peut être aussi `log.info()`, `log.error()`, etc.).
log.success(f"Read number: {x = }")

# We add val
# Ajoutous val à la valeur x
x = x + val

# Convert the result to a byte string
# On convertit x en chaîne et en utf-8
x = str(x).encode()

# And finally send it back after reading ">>> "
# Et finalement on envoi x après avoir lu b">>> "
io.sendlineafter(b">>> ", x)

# Receive the flag!
# On reçoit le flag!
io.recvline()
flag = io.recvline().strip().decode()

# If needed, we can keep the connexion opened using io.interactive()
# Otherwise, we close the connexion
# Nous fermons la connexion
io.close()

# Print the flag
# Affichage du flag
print(flag)

Exécution de template.py

$ python3 template.py HOST=127.0.0.1 PORT=4000
[+] Opening connection to 127.0.0.1 on port 4000: Done
[+] Read number: x = 1384
[*] Closed connection to 127.0.0.1 port 4000
FCSC{XXX}