V8 JavaScript Engine 入門指南 3 — 一個簡單的V8應用

來源:互聯網
上載者:User

編譯通過了之前的hello world程式之後,忍不住想實際運用一下V8,V8 JavaScript Engine 做為一款強大的JavaScript解析引擎,到底能為我們提供什麼樣的功能呢?
再來仔細看一下hello world程式:

    ......    // Create a string containing the JavaScript source code.    Handle<String> source = String::New("'Hello' + ', World!'");    // Compile the source code.    Handle<Script> script = Script::Compile(source);    // Run the script to get the result.    Handle<Value> result = script->Run();    
    // Convert the result to an ASCII string and print it.    String::AsciiValue ascii(result);    printf("%s/n", *ascii);    return 0;

注釋解釋的非常清楚:使用String::New產生原始碼,使用Script::Compile編譯原始碼,script->Run()執行指令碼得到結果

而且可以將指令碼執行結果轉化成ASCII字串。並且String::New接受一個ASCII字串作為參數。

所以,儘管我們現在對於V8的一些基本概念都不瞭解,但只要稍微改造一下這個hello world程式就可以實現一個簡單的JavaScript解密程式!

一個簡單的JavaScript解密程式功能:將加密的JavaScript代碼拷貝到input.txt檔案中,解密結果產生到output.txt檔案中

關鍵代碼:

/************************************************************************/

/*函數名:V8_Decryt

/*功  能:得到一段JavaScript代碼的執行結果,可以用來解密JavaScript

/*傳回值:結果字串的字元數

/*注  意:返回字串pResult的記憶體由調用者清理

/************************************************************************/

unsigned int V8_Decryt(const char* pExpression, //包含JavaScript代碼的字串

                                           char* &pResult          //輸出參數,返回包含JavaScript代碼執行結果的字串

                                      )

{

    // Create a stack-allocated handle scope.

    v8::HandleScope handle_scope;

    // Create a new context.

    v8::Persistent<v8::Context> context = v8::Context::New();

    // Enter the created context for compiling and

    // running the hello world script.

    v8::Context::Scope context_scope(context);

    // Create a string containing the JavaScript source code.

    v8::Handle<v8::String> source = v8::String::New(pExpression);

    // Compile the source code.

    v8::Handle<v8::Script> script = v8::Script::Compile(source);

    // Run the script to get the result.

    v8::Handle<v8::Value> result = script->Run();

    // Dispose the persistent context.

    context.Dispose();

    // Convert the result to a two-byte string .

    v8::String::AsciiValue ascii(result);

    // TODO: Add extra codes here

    unsigned int size = ascii.length();

    if(size != 0)

    {

        pResult = (char*)malloc(size+1);

        memset(pResult,0,size+1);

        memcpy_s(pResult,size,*ascii,size);

    }

    return size;

}

測試例子:

看一段eval加密

eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};if(!''.replace(/^/,String)){while(c--){d[c.toString(a)]=k[c]||c.toString(a)}k=[function(e){return d[e]}];e=function(){return'//w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('//b'+e(c)+'//b','g'),k[c])}}return p}('9 a(){8();7 0=4.5(/'6/');0.b(/'#c#i/');4.f(0);d{0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1)}h(e){}1.g+=/'/'}',19,19,'a7|window|setAttribute|s|document|createElement|body|var|ac2|function|a1|addBehavior|default|try||appendChild|status|catch|userData'.split('|'),0,{}))

將eval去掉,拷貝到input.txt中去(記住保留左右括弧啊),運行,查看output.txt

function a1(){ac2();var a7=document.createElement('body');a7.addBehavior('#default#userData');document.appendChild(a7);try{a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window)}catch(e){}window.status+=''}

解密成功!

附上完整代碼(vs2010的,就只有一個.cpp你可以方便轉化到自己的vs版本)

http://download.csdn.net/source/2718386

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.