こんとろーるしーこんとろーるぶい

週末にカチャカチャッターン!したことを貼り付けていくブログ

Harekaze CTF 2019 Writeup - [a-z().]

Question

if (eval(your_code) === 1337) console.log(flag);

http://problem.harekaze.com:40001

Solution

1337の値を返す、a-z().の文字種かつ200文字未満のJavaScriptコードを作成できたらフラグがゲットできる問題。
環境はNode.jsのvmモジュール。

  if (code && code.length < 200 && !/[^a-z().]/.test(code)) {
    try {
      const result = vm.runInNewContext(code, {}, { timeout: 500 });
      if (result === 1337) {
        output = process.env.FLAG;
      } else {
        output = 'nope';
      }
    } catch (e) {
      output = 'nope';
    }
  } else {
    output = 'nope';
  }

参照可能なオブジェクトのnameプロパティを起点に、Stringの各種メソッドを使用して文字数を増幅し、1337に到達させる作戦。

developer.mozilla.org

> vm.runInNewContext("escape().length", {}, { timeout: 500 });
9
> vm.runInNewContext("this.constructor.name.length", {}, { timeout: 500 });
6
> vm.runInNewContext("escape().big().length", {}, { timeout: 500 });
20
> vm.runInNewContext("escape().strike().length", {}, { timeout: 500 });
26
> vm.runInNewContext("escape().link().length", {}, { timeout: 500 });
33
> vm.runInNewContext("escape().repeat(escape().length).length", {}, { timeout: 500 });
81

以下のように組み合わせると1337になった。

> vm.runInNewContext("escape().big().repeat(this.constructor.name.length).repeat(escape().length).link().link().link().link().link().link().link().link().link().link().strike().length", {}, { timeout: 500 });
1337

フラグゲット
HarekazeCTF{sorry_about_last_year's_js_challenge...}