2009/07/21

jjencode素晴らしいですね。記号だけでjavascriptが実行出来てしまいます。
何かに使えないかなーと考えて、apacheのmod_ext_filterを使ってjavascriptの難読サーブをやってみようかと思います。
javascriptエンジンとして使うのは、v8。本当は全部Cで書けば速いのでしょうがメンドクサイのでやりません。
以下フィルタのソース
crypt-js.cc
#include <v8.h>
#include <string>
#include <iostream>

static std::string&
replace_string(std::string& str, const std::string from, const std::string dest) {
    std::string::size_type n, nb = 0;
    while((n = str.find(from, nb)) != std::string::npos) {
        str.replace(n, from.size(), dest);
        nb = n + dest.size();
    }
    return str;
}

v8::Handle<v8::Value> Print(const v8::Arguments& args) {
  bool first = true;
  for (int i = 0; i < args.Length(); i++) {
    v8::HandleScope handle_scope;
    if (first) {
      first = false;
    } else {
      printf(" ");
    }
    v8::String::Utf8Value str(args[i]);
    const char* cstr = *str ? *str : "";
    printf("%s", cstr);
  }
  printf("\n");
  return v8::Undefined();
}

int main(int argc, char* argv[]) {
  v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
  v8::HandleScope handle_scope;
  v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
  global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print));
  v8::Handle<v8::Context> context = v8::Context::New(NULL, global);
  v8::Context::Scope context_scope(context);

  std::string line, content;
  while(!std::cin.eof()) {
    std::getline(std::cin, line);
    content += line;
  }
  replace_string(content, "\\", "\\\\");
  replace_string(content, "'", "\\'");
  line = "var text = '";
  line += content;
  line += "';";
  line +=
    "var r=\"\";"
    "var n;"
    "var t;"
    "var b=[ \"___\", \"__$\", \"_$_\", \"_$$\", \"$__\", \"$_$\", \"$$_\", \"$$$\", \"$___\", \"$__$\", \"$_$_\", \"$_$$\", \"$$__\", \"$$_$\", \"$$$_\", \"$$$$\", ];"
    "for( var i = 0; i < text.length; i++ ){"
    "    n = text.charCodeAt( i );"
    "    if( n < 128 ){"
    "        r += \"\\\"\\\\\\\\\\\"+\" + n.toString( 8 ).replace( /[0-7]/g, function(c){ return \"$.\"+b[ c ]+\"+\" } );"
    "    }else{"
    "        r += \"\\\"\\\\\\\\\\\"+$._+\" + n.toString(16).replace( /[0-9a-f]/gi, function(c){ return \"$.\"+b[parseInt(c,16)]+\"+\"} );"
    "    }"
    "}"
    "print("
    "\"$=~[];$={___:++$,$$$$:(![]+\\\"\\\")[$],__$:++$,$_$_:(![]+\\\"\\\")[$],_$_:++$,$_$$:({}+\\\"\\\")[$],$$_$:($[$]+\\\"\\\")[$],_$$:++$,$$$_:(!\\\"\\\"+\\\"\\\")[$],$__:++$,$_$:++$,$$__:({}+\\\"\\\")[$],$$_:++$,$$$:++$,$___:++$,$__$:++$};\"+"
    "\"$.$_=\"+"
    "\"($.$_=$+\\\"\\\")[$.$_$]+\"+"
    "\"($._$=$.$_[$.__$])+\"+"
    "\"($.$$=($.$+\\\"\\\")[$.__$])+\"+"
    "\"((!$)+\\\"\\\")[$._$$]+\"+"
    "\"($.__=$.$_[$.$$_])+\"+"
    "\"($.$=(!\\\"\\\"+\\\"\\\")[$.__$])+\"+"
    "\"($._=(!\\\"\\\"+\\\"\\\")[$._$_])+\"+"
    "\"$.$_[$.$_$]+\"+"
    "\"$.__+\"+"
    "\"$._$+\"+"
    "\"$.$;\"+"
    "\"$.$$=\"+"
    "\"$.$+\"+"
    "\"(!\\\"\\\"+\\\"\\\")[$._$$]+\"+"
    "\"$.__+\"+"
    "\"$._+\"+"
    "\"$.$+\"+"
    "\"$.$$;\"+"
    "\"$.$=($.___)[$.$_][$.$_];\"+"
    "\"$.$($.$($.$$+\\\"\\\\\\\"\\\"+\" + r + \"\\\"\\\\\\\"\\\")())();\");";
  v8::Handle<v8::String> source = v8::String::New(line.c_str(), line.size());
  v8::TryCatch try_catch;
  v8::Handle<v8::Script> script = v8::Script::Compile(source, v8::String::New(""));
  v8::Handle<v8::Value> result = script->Run();
  return 0;
}

エスケープして変数textに入れて、残りはjjencodeそのままです。
これをhttpd.confで以下の様に設定します。 LoadModule ext_filter_module modules/mod_ext_filter.so
ExtFilterDefine crypt_js mode=output intype=application/x-javascript cmd="/path/to/crypt-js"
出来ました。簡単ですね。
これで以下のjavascriptをダウンロードしてみます。
window.alert('hasegawa!');
コマンドラインから...
curl http://localhost:8080/filter/foo.js
$=~[];$={___:++$,$$$$:(![]+"")[$],__$:++$,$_$_:(![]+"")[$],_$_:++$,$_$$:({}+"")[$],$$_$:($[$]+"")[$],_$$:++$,$$$_:(!""+"")[$],$__:++$,$_$:++$,$$__:({}+"")[$],$$_:++$,$$$:++$,$___:++$,$__$:++$};$.$_=($.$_=$+"")[$.$_$]+($._$=$.$_[$.__$])+($.$$=($.$+"")[$.__$])+((!$)+"")[$._$$]+($.__=$.$_[$.$$_])+($.$=(!""+"")[$.__$])+($._=(!""+"")[$._$_])+$.$_[$.$_$]+$.__+$._$+$.$;$.$$=$.$+(!""+"")[$._$$]+$.__+$._+$.$+$.$$;$.$=($.___)[$.$_][$.$_];$.$($.$($.$$+"\""+"\\"+$.__$+$.$$_+$.$$$+"\\"+$.__$+$.$_$+$.__$+"\\"+$.__$+$.$_$+$.$$_+"\\"+$.__$+$.$__+$.$__+"\\"+$.__$+$.$_$+$.$$$+"\\"+$.__$+$.$$_+$.$$$+"\\"+$.$_$+$.$$_+"\\"+$.__$+$.$__+$.__$+"\\"+$.__$+$.$_$+$.$__+"\\"+$.__$+$.$__+$.$_$+"\\"+$.__$+$.$$_+$._$_+"\\"+$.__$+$.$$_+$.$__+"\\"+$.$_$+$.___+"\\"+$.$__+$.$$$+"\\"+$.__$+$.$_$+$.___+"\\"+$.__$+$.$__+$.__$+"\\"+$.__$+$.$$_+$._$$+"\\"+$.__$+$.$__+$.$_$+"\\"+$.__$+$.$__+$.$$$+"\\"+$.__$+$.$__+$.__$+"\\"+$.__$+$.$$_+$.$$$+"\\"+$.__$+$.$__+$.__$+"\\"+$.$__+$.__$+"\\"+$.$__+$.$$$+"\\"+$.$_$+$.__$+"\\"+$.$$$+$._$$+"\"")())();
おぉ!
HTMLから実行してみました。
hasegawa-crypt-js
おぉ!

これいいね!とか思ってjQueryでやってみたら、30秒以上は戻って来ませんでした... orz
Posted at 17:30 | WriteBacks () | Edit
Edit this entry...

wikieditish message: Ready to edit this entry.






















A quick preview will be rendered here when you click "Preview" button.