> For the complete documentation index, see [llms.txt](https://0xyogesh.gitbook.io/exploitnotes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://0xyogesh.gitbook.io/exploitnotes/ctf/broncoctf/custom-cipher-writeup.md). Agents can also search and read this documentation through its [MCP server](https://0xyogesh.gitbook.io/exploitnotes/~gitbook/mcp).

# Custom Cipher Writeup

`pscheme.py` implements a toy "encryption" scheme that encodes each 4-byte chunk of the flag as extra roots multiplied directly into a public polynomial. Because polynomial multiplication is invertible and the *original* public key is handed to you in the ciphertext, the scheme collapses to straightforward polynomial division: divide each ciphertext polynomial by the known public key, factor the resulting quartic to recover the plaintext bytes, then use the appended 8-bit `order` field to undo the scheme's internal shuffle.

No knowledge of the private root set is needed.

## The Source

```python
import cmath
import itertools
import random


class Poly:
    def __init__(self, coeff: list):
        self.coeff = coeff
        self.coeff.reverse()
        self.coeff = list(itertools.dropwhile(lambda x: x == 0, self.coeff))
        self.coeff.reverse()

        if len(self.coeff) == 0:
            self.coeff = [0]

    def get_degree(self):
        return len(self.coeff) - 1

    def __mul__(self, other):
        if other.get_degree() == 1 and other.coeff[0] == 0:
            return Poly([0])

        newdeg = self.get_degree() + other.get_degree()
        newcoeff = [0 for _ in range(newdeg + 1)]
        for i1, c1 in enumerate(self.coeff):
            for i2, c2 in enumerate(other.coeff):
                newcoeff[i1 + i2] += c1 * c2

        return Poly(newcoeff)

    def to_distrib_form(self, delim: str = " "):
        return delim.join(map(str, self.coeff[:-1]))


def keygen(root_bits: int, key_degree: int):
    private = sorted([random.randint(1, 2**root_bits - 1) for _ in range(key_degree)])
    public = Poly([1])
    for root in private:
        root_poly = Poly([-root, 1])
        public = public * root_poly
    return (private, public)


def encrypt(public: Poly, message: list[int]):
    for root in message:
        root_poly = Poly([-root, 1])
        public = public * root_poly

    order = sorted(message)
    for i, e in enumerate(order):
        ind = message.index(e)
        order[i] = ind
        message[ind] = -1
    order = sum([x << (2 * i) for i, x in enumerate(order)])

    return (public, order)


def encrypt_string(enc: str, pub: Poly):
    send_list = []
    for substr in itertools.batched(enc, 4):
        message = list(map(ord, substr))
        message += [0 for _ in range(4 - len(message))]
        to_send, order = encrypt(pub, message)
        send_list.append(to_send.to_distrib_form() + " " + str(order))
    return "/".join(send_list)


if __name__ == "__main__":
    N = 64
    B = 8
    FLAG = "bronco{REDACTED}"

    priv, pub = keygen(8, 64)
    print("====PUBLIC KEY====")
    print(pub.to_distrib_form())
    print("====MESSAGE====")
    print(encrypt_string(FLAG, pub))
```

Key observations:

* `keygen` picks 64 private roots in `[1, 255]` and builds a monic degree-64 polynomial:

  ```python
  pub = ∏(x - root_i)
  ```

  `pub` is printed under `====PUBLIC KEY====`, excluding the leading coefficient. The leading coefficient is always `1` because the polynomial is monic; `to_distrib_form` drops `coeff[-1]`.
* `encrypt` takes the *same* `pub` object reference for every 4-byte chunk. Python passes the reference, but:

  ```python
  public = public * root_poly
  ```

  only rebinds the local name. It never mutates the caller's `pub`.

  Therefore, every chunk is encrypted against the identical known degree-64 public key, not a cumulative polynomial.
* Each chunk's ciphertext is:

  ```python
  pub * (x-m0)(x-m1)(x-m2)(x-m3)
  ```

  This is a degree-68 monic polynomial, again printed with the leading `1` omitted.
* The `order` value packs, in 2 bits each, the original index of the i-th smallest message byte. It tells you how to unscramble the sorted roots back into message order.

The private root set is completely irrelevant to breaking this scheme.

## Attempt 1 — Run the Script

The first instinct was to run `pscheme.py` locally to inspect the output format against a known flag. However, the script hardcodes the real flag and errors out without it:

```
$ python3 pscheme.py
Traceback (most recent call last):
  File "/home/kali/ctf/easy/pscheme.py", line 95, in <module>
    FLAG = [REDACTED]
            ^^^^^^^^
NameError: name 'REDACTED' is not defined
```

There is no local execution path. The only usable artifact is `enc.txt`, so the attack has to be purely algebraic against the supplied ciphertext.

## Attempt 2 — Polynomial Division and Root Recovery

{% stepper %}
{% step %}

### Parse `enc.txt`

`enc.txt` has two sections:

```
====PUBLIC KEY====
<64 space-separated coefficients, ascending degree, leading 1 omitted>
====MESSAGE====
<ciphertext chunks, "/"-joined, each "<68 coefficients> <order>">
```

{% endstep %}

{% step %}

### Recover the known public key polynomial

Reconstruct the monic degree-64 public key by appending the implicit leading `1`:

```python
pub_coeffs_asc = [int(x) for x in re.findall(r'-?\d+', pub_joined)] + [1]
pub_desc = list(reversed(pub_coeffs_asc))
```

{% endstep %}

{% step %}

### Divide each ciphertext chunk by the public key

Since both polynomials are monic, exact integer polynomial long division needs no fractional arithmetic. Division by the leading coefficient is always division by `1`.

```python
def poly_divmod_desc(dividend_desc, divisor_desc):
    dividend = dividend_desc[:]
    n = len(divisor_desc)
    quotient = []
    for i in range(len(dividend) - n + 1):
        coef = dividend[i]
        quotient.append(coef)
        if coef != 0:
            for j in range(n):
                dividend[i + j] -= coef * divisor_desc[j]
    remainder = dividend[len(dividend) - n + 1:]
    return quotient, remainder
```

Dividing a chunk polynomial by `pub` leaves a degree-4 monic quotient whose roots are exactly the chunk's four plaintext byte values.
{% endstep %}

{% step %}

### Factor the quartic

The roots are ASCII bytes: small integers in `[0, 255]`. Brute-force trial roots with synthetic deflation is fast and exact, so no general quartic formula is needed.

```python
def find_roots(poly_desc, lo=0, hi=256):
    coeffs = poly_desc[:]
    roots = []
    for _ in range(len(coeffs) - 1):
        found = None
        for r in range(lo, hi):
            val = 0
            for c in coeffs:
                val = val * r + c
            if val == 0:
                found = r
                break
        if found is None:
            return None
        roots.append(found)
        new_coeffs = [coeffs[0]]
        for c in coeffs[1:]:
            new_coeffs.append(new_coeffs[-1] * found + c)
        coeffs = new_coeffs[:-1]
    return roots  # ascending
```

{% endstep %}

{% step %}

### Undo the `order` shuffle

The `order` field's 2-bit values give the original position of each sorted root. Decode it with a direct scatter:

```python
def decode_chunk(order, sorted_roots):
    message = [None] * len(sorted_roots)
    for i, root in enumerate(sorted_roots):
        idx = (order >> (2 * i)) & 3
        message[idx] = root
    return ''.join(chr(m) for m in message if m is not None)
```

{% endstep %}
{% endstepper %}

### Putting It Together

```
$ cat > flag.py << 'EOF'
heredoc> [... solver assembled from steps above ...]
heredoc> EOF

$ python3 flag.py enc.txt
bronco{REDACTED}
```

No brute force of the private key and no factoring of RSA-scale numbers are required. Each chunk needs only one exact polynomial division and a quartic root search.

## Attack Chain

```
enc.txt (public key + per-chunk ciphertext)
        │
        ▼
reconstruct monic public key polynomial (add back implicit leading 1)
        │
        ▼
for each ciphertext chunk:
        │
        ├─► exact polynomial long division by public key  →  degree-4 quotient
        │
        ├─► brute-force integer root search (0-255) + synthetic deflation
        │        →  4 plaintext ASCII bytes (sorted)
        │
        └─► undo `order` bitfield shuffle  →  4 bytes in original order
        │
        ▼
concatenate chunks, strip null padding
        │
        ▼
        FLAG
```

## Key Vulnerabilities

| # | Issue                                                                                             | Impact                                                                                                                  |
| - | ------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
| 1 | Public key is reused unmodified for every chunk instead of accumulating, and is disclosed in full | Lets an attacker isolate each chunk's added roots via simple polynomial division — private key material is never needed |
| 2 | "Encryption" is just multiplying plaintext values in as polynomial roots                          | Roots of a public polynomial with known factors are trivially recoverable; this is not a hard problem                   |
| 3 | Root search space is tiny (`[0, 255]`, ASCII bytes)                                               | Even without factoring cleverness, brute-force plus deflation solves each quartic in milliseconds                       |
| 4 | `order` field is a simple deterministic bit-packed permutation with no keying                     | Trivially invertible once you have the sorted roots                                                                     |
