erlang beam文件反编译

来源:互联网 发布:党章党规网络测试题 编辑:程序博客网 时间:2024/06/05 18:27

hello~ ,

如果erlang源码被意外删除只留下编译后的beam文件的时候怎么办?如果源码能还原也就大事化了, 如果不能就懵了?都有手误的是时候,今天就来聊聊erlang的反编译,到时也不至于乱了手脚。

在仔细翻阅文档之后终于找到了erlang反编译相关的说明,地址:http://erlang.org/doc/man/beam_lib.html 。beam_lib 模块就是对deam文件格式的接口。

其中最主要的是这两行代码:

{ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(Beam,[abstract_code]).io:fwrite("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).

so ~ 废话少说,直接上代码。

源码:

-module(test).-export([hello/0,say/0]).hello() ->"hello ~".%%@doc say hello~say() ->lists:foreach(fun hello/0 , lists:seq(1, 10)).

先看下面


在chunks之后并没有成功,提示是no_abstract_code,这是为什么呢?查看文档,在文档中找到如下说明:

Option debug_info can be specified to the Compiler (see compile(3)) to have debug information in the form of abstract code (see section The Abstract Format in the ERTS User's Guide) stored in the abstract_code chunk. Tools such as Debugger and Xref require the debug information to be included.

 大致的意思是:要想反编译就必须在编译的时候加上+debug_info标记,现在我们再来试试:



这次成功了~

不过欢喜之余我们马上会发现,自己能反编译成功那别人也肯定能, 这样就存在安全问题了,那么有没有办法解决这个问题呢?幸运的是文档里给出了解决方法。有两种方法:

The key can be provided in the following two ways:

  • Use Compiler option {debug_info,Key}, see compile(3) and function crypto_key_fun/1 to register a fun that returns the key whenever beam_lib must decrypt the debug information.

    If no such fun is registered, beam_lib instead searches for an .erlang.crypt file, see the next section.

  • Store the key in a text file named .erlang.crypt.

    In this case, Compiler option encrypt_debug_info can be used, see compile(3).

我们这里选择第二种最简单的方法,在用户root目录下创建 .erlang.crypt文件,格式内容是[{debug_info, Mode, Module, Key}].

我这里是[{debug_info, des3_cbc, [], "my_key"}].

再来看


也成功了, 注意这里要加encrypt_debug_info而不是debug_info,

现在我们来修改.erlang.crypt文件里的内容为[{debug_info, des3_cbc, [], "no_key"}]. 或者删除.erlang.crypt文件再来尝试:


失败了,意思是说加密验证不通过。

现在没什么好担心的啦~放心去干吧!!

祝生活愉快!







0 0