erlang中通过debug_info还原源代码(转)


转自:http://www.phpx.com/index.php
具体网址忘记了。。。

源码:
-module(a).
 
-export([test/0]).
 
test() ->
  io:format("source code.~n", []).

带 debug_info 编译,并运行之。

$ erlc +debug_info a.erl
$ erl -s a test -s c q -noshell
source code.
$

我们可以这样还原它的源码:

$ erl
1>  {ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(code:which(a), abstract_code]).
{ok,{a,[{abstract_code,
            {raw_abstract_v1,
                [{attribute,1,file,{"./a.erl",1}},
                 {attribute,1,module,a},
                 {attribute,3,export,[{test,0}]},
                 {function,5,test,0,
                     [{clause,5,[],[],[{call,6,{remote,...},[...]}]}]},
                 {eof,7}]}}]}}
2> io:fwrite("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).
-file("./a.erl", 1).

-module(a).

-export([test/0]).

test() -> io:format("source code.~n", []).


ok
3>

既保留 debug_info 又阻止其他人通过 debug_info 来得到源码的办法:加密 debug_info
首先建立一个 ~/.erlang.crypt 文件,内容如下:

$ cat ~/.erlang.crypt
[{debug_info, des3_cbc, [], "my_source_code_secret_key"}].
这里的 “my_source_code_secret_key” 就被用来生成对 debug_info 加密的密钥。用 encrypt_debug_info 参数编译,并运行之。

$ erlc +encrypt_debug_info a.erl
$ erl -s a test -s c q -noshell
source code.

拿掉.erlang.crypt文件后仍然可以运行;并且使用之前的反编译方法失败:
$ erl
1>  beam_lib:chunks(code:which(a), [abstract_code]).
{error,beam_lib,
       {key_missing_or_invalid,"./a.beam",abstract_code}}

如果需要现场调试,就再加上~/.erlang.crypt 文件。此时也可以再次还原代码。

$ mv ~/.erlang.old.crypt ~/.erlang.crypt
$ erl
1>  {ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(code:which(a), abstract_code]).
{ok,{a,[{abstract_code,
            {raw_abstract_v1,
                [{attribute,1,file,{"./a.erl",1}},
                 {attribute,1,module,a},
                 {attribute,3,export,[{test,0}]},
                 {function,5,test,0,
                     [{clause,5,[],[],[{call,6,{remote,...},[...]}]}]},
                 {eof,7}]}}]}}
2> io:fwrite("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).
-file("./a.erl", 1).

-module(a).

-export([test/0]).

test() -> io:format("source code.~n", []).


ok
3>

 

你可能感兴趣的:(lang/erlang)