Destructuring Challenge Writeup
Challenge: destructuring
Category: Misc
Difficulty: Easy
Solves: 57
Author: ark
Challenge Description
“Introduction to modern JavaScript!”
The challenge provides a netcat connection:
nc 34.170.146.252 25646
Initial Analysis
We’re given a Node.js script that implements a 5-stage challenge. Each stage uses JavaScript destructuring to extract values from JSON input. Let’s examine the code structure:
const stages = [
// Stage 0
(json) => {
const { a: _0, b: _1 } = json;
return [_0, _1];
},
// ... more stages
];
The script:
- Loops through each stage (0-4)
- Prompts for JSON input with
json> - Parses the JSON and applies the destructuring pattern
- Checks if the result equals
[0, 1, 2, 3, ...](each value equals its index) - If all stages pass, reveals the flag from
process.env.FLAG
Understanding Destructuring
JavaScript destructuring allows extracting values from objects and arrays into variables. For example:
// Object destructuring
const { a: x, b: y } = { a: 10, b: 20 };
// x = 10, y = 20
// Array destructuring
const [, , x, y] = [1, 2, 3, 4];
// x = 3, y = 4 (skips first two elements)
// Nested destructuring
const { a: [, x, { b: y }] } = { a: [1, 2, { b: 3 }] };
// x = 2, y = 3
Solution Approach
For each stage, we need to construct JSON that places values 0, 1, 2, 3, ... at the exact positions where the destructuring pattern expects them.
Stage 0
Pattern:
const { a: _0, b: _1 } = json;
Analysis:
- Extracts
json.ainto_0(should be 0) - Extracts
json.binto_1(should be 1)
Solution:
{"a":0,"b":1}
Stage 1
Pattern:
const [, , _0, _1, _2, , , _3, _4] = json;
Analysis:
- Array destructuring with holes (commas skip elements)
_0 = json[2](should be 0)_1 = json[3](should be 1)_2 = json[4](should be 2)_3 = json[7](should be 3)_4 = json[8](should be 4)
Solution:
[null,null,0,1,2,null,null,3,4]
Stage 2
Pattern:
const {a: [, , { b: _0, c: [{ d: _1 }] }] } = json;
Analysis:
- Nested object and array destructuring
_0 = json.a[2].b(should be 0)_1 = json.a[2].c[0].d(should be 1)
Solution:
{"a":[null,null,{"b":0,"c":[{"d":1}]}]}
Stage 3
Pattern:
const { a: { a: { a: { a: _0, b: _1 }, b: { a: _2, b: _3 }, c: [, _4, , _5, _6] }, b: [{ a: _7 }, [_8, _9, _10, , _11]] } } = json;
Analysis: This extracts 12 values from a deeply nested structure:
_0 = json.a.a.a.a→ 0_1 = json.a.a.a.b→ 1_2 = json.a.a.b.a→ 2_3 = json.a.a.b.b→ 3_4 = json.a.a.c[1]→ 4_5 = json.a.a.c[3]→ 5_6 = json.a.a.c[4]→ 6_7 = json.a.b[0].a→ 7_8 = json.a.b[1][0]→ 8_9 = json.a.b[1][1]→ 9_10 = json.a.b[1][2]→ 10_11 = json.a.b[1][4]→ 11
Solution:
{"a":{"a":{"a":{"a":0,"b":1},"b":{"a":2,"b":3},"c":[null,4,null,5,6]},"b":[{"a":7},[8,9,10,null,11]]}}
Stage 4
Pattern: This is the most complex stage with 374 variables (_0 through _373) extracted from an extremely deeply nested structure. The destructuring pattern spans multiple lines and includes:
- Nested objects 6-8 levels deep
- Arrays within objects within arrays
- Mixed array and object destructuring
- Holes in array destructuring (skipped elements)
Strategy: Rather than manually parsing the entire pattern, I wrote a script to systematically build the JSON structure by:
- Analyzing each variable assignment path
- Creating the nested structure level by level
- Placing each number (0-373) at its corresponding path
The resulting JSON is approximately 4KB in size due to the extreme nesting.
Solution:
See stage4.json file (too large to include in writeup inline)
Building the Solution Script
I created a Node.js script to generate all the JSON inputs:
// Stage 0
const stage0 = { a: 0, b: 1 };
// Stage 1
const stage1 = [null, null, 0, 1, 2, null, null, 3, 4];
// Stage 2
const stage2 = {
a: [null, null, { b: 0, c: [{ d: 1 }] }]
};
// Stage 3
const stage3 = {
a: {
a: {
a: { a: 0, b: 1 },
b: { a: 2, b: 3 },
c: [null, 4, null, 5, 6]
},
b: [
{ a: 7 },
[8, 9, 10, null, 11]
]
}
};
// Stage 4 - see full implementation
const stage4 = { /* very large nested structure */ };
Exploitation
Connect to the challenge server:
nc 34.170.146.252 25646
For each json> prompt, paste the corresponding stage’s JSON input:
Stage: 0
const { a: _0, b: _1 } = json;
json> {"a":0,"b":1}
Nice!
Stage: 1
const [, , _0, _1, _2, , , _3, _4] = json;
json> [null,null,0,1,2,null,null,3,4]
Nice!
Stage: 2
const {a: [, , { b: _0, c: [{ d: _1 }] }] } = json;
json> {"a":[null,null,{"b":0,"c":[{"d":1}]}]}
Nice!
Stage: 3
const { a: { a: { a: { a: _0, b: _1 }, ... } } } = json;
json> {"a":{"a":{"a":{"a":0,"b":1},"b":{"a":2,"b":3},"c":[null,4,null,5,6]},"b":[{"a":7},[8,9,10,null,11]]}}
Nice!
Stage: 4
const { a: [{ a: { a: { a: [{ a: _0, ... } } } }] } = json;
json> [paste stage4.json contents]
Nice!
Here's your flag: [FLAG]
Key Takeaways
JavaScript Destructuring: This challenge provides excellent practice with JavaScript’s destructuring syntax, including:
- Object destructuring with renaming
- Array destructuring with holes
- Nested destructuring patterns
- Mixed object/array destructuring
Pattern Matching: The challenge requires careful analysis of nested patterns to determine the exact path to each variable.
Automation: For complex stages (especially Stage 4), automation is essential. Manually constructing 374-variable destructuring would be error-prone and time-consuming.
JSON Structure: Understanding how JavaScript destructuring maps to JSON structure is crucial - objects use
{}and properties, arrays use[]and indices.
Files
stage0.json- Stage 0 inputstage1.json- Stage 1 inputstage2.json- Stage 2 inputstage3.json- Stage 3 inputstage4.json- Stage 4 input (4KB file)solve.js- Script to generate all inputssolution.md- Quick reference guide
Flag
After successfully completing all 5 stages, the server reveals the flag stored in the environment variable.