> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/web-exploitation/some-assembly-required2.md).

# some-assembly-required2

### 🔍 Challenge Overview

We are given a web challenge that validates input using **WebAssembly (WASM)** instead of plain JavaScript.

Unlike the previous challenge, the flag is **not directly visible via `strings`**, so we must reverse the logic.

### 🧩 Step 1: Extract WASM

From the website:

* Inspect JS → find `.wasm` file
* Download it:

```bash
curl -O http://wily-courier.picoctf.net:61986/<file>
```

Rename for convenience:

```bash
mv <long_name> OH
```

### 🧩 Step 2: Convert WASM → WAT

Install tool:

```bash
sudo dnf install wabt
```

Convert:

```bash
wasm2wat OH
```

### 🧠 Step 3: Identify Important Functions

From output:

* `copy_char` → processes input
* `strcmp` → compares buffers
* `check_flag` → main validation

***

### 🔥 Step 4: Understand Validation Logic

#### In `check_flag`:

```wat
i32.const 1072  ;; user input
i32.const 1024  ;; expected data
call strcmp
```

👉 So:

* Input stored at **1072**
* Expected flag stored at **1024**

***

### 🔥 Step 5: Extract Encoded Flag

At bottom:

```wat
(data (i32.const 1024) "xakgK\5cNsn08m?80jj:i;j:m0?klnm<i1:8>iljinu\00\00")
```

This is the **encoded flag**.

***

### 🔥 Step 6: Reverse Encoding

From `copy_char`:

```wat
i32.const 8
i32.xor
```

👉 Each character is transformed as:

```
encoded = input ^ 8
```

So reverse:

```
input = encoded ^ 8
```

***

### ⚠️ Common Mistake (YOU HIT THIS)

Your output:

```
picoCT=kF{...}
```

❌ Wrong because:

```
"\5c" ≠ literal "\" in Python string
```

👉 `\5c` = hex escape → **92 → ''**

So your encoded string must be written correctly.

***

### ✅ Correct Python Script

```python
encoded = b"xakgK\\5cNsn08m?80jj:i;j:m0?klnm<i1:8>iljinu"

flag = ""
i = 0

while i < len(encoded):
    if encoded[i] == 92:  # '\'
        # handle \5c manually
        hex_val = encoded[i+1:i+3]
        c = int(hex_val.decode(), 16)
        flag += chr(c ^ 8)
        i += 3
    else:
        flag += chr(encoded[i] ^ 8)
        i += 1

print(flag)
```

***

### 🏁 Final Flag

```
picoCTF{f80e708bb2a3b2e87cdfe4a9206adbaf}
```

***

### 💡 Key Takeaways

* WASM stores **raw data with escape sequences**
* Always watch for:
  * `\x??`
  * `\5c` → hex values
* XOR is reversible:

  ```
  A ^ B ^ B = A
  ```
* Client-side validation is always breakable


---

# 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://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/web-exploitation/some-assembly-required2.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.
