erlang中通过debug_info还原源代码 (反编译;防止反编译)(转)

具体网址忘记了。。。

源码:
-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(命令行或者Emakefile的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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值