> For the complete documentation index, see [llms.txt](https://digitalgarden.batamladen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://digitalgarden.batamladen.com/writeups/ctfs/0xfun-ctf-2026/crypto-leonine-misbegotten.md).

# Crypto - Leonine Misbegotten

### Challenge Summary

The challenge gives you a huge file. Looking at `chall.py`, we see that the flag is **encoded 16 times** in layers using a random choice of one of four encoding schemes:

* Base16 (`b16encode`)
* Base32 (`b32encode`)
* Base64 (`b64encode`)
* Base85 (`b85encode`)

After each encoding, the program **appends the SHA-1 hash of the data before encoding**.

The `output.txt` file is therefore:

```
ENCODED_DATA + SHA1
ENCODED_DATA + SHA1
...
```

And the goal is to recover the original flag.

***

### Solve.py

```python
from base64 import b16decode, b32decode, b64decode, b85decode
from hashlib import sha1

SCHEMES = [b16decode, b32decode, b64decode, b85decode]

with open("output", "rb") as f:
    data = f.read()

for round in range(16):
    new_data = None
    checksum = data[-20:]  # SHA-1 is 20 bytes
    candidate = data[:-20]

    for dec in SCHEMES:
        try:
            decoded = dec(candidate)
            if sha1(decoded).digest() == checksum:
                new_data = decoded
                break
        except Exception:
            pass

    if new_data is None:
        raise Exception("Failed at round", round)

    data = new_data

print(data.decode())

```

Flag:\
`0xfun{p33l1ng_l4y3rs_l1k3_an_0n10n}`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://digitalgarden.batamladen.com/writeups/ctfs/0xfun-ctf-2026/crypto-leonine-misbegotten.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
