Skip to content

Instantly share code, notes, and snippets.

@binarytemple
Last active April 29, 2020 10:34
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save binarytemple/d0dd5644a473e0ba1d73790168681351 to your computer and use it in GitHub Desktop.
Save binarytemple/d0dd5644a473e0ba1d73790168681351 to your computer and use it in GitHub Desktop.
Erlang decompiler I found on the web, works for Elixir beam files too

DON'T BOTHER USING - BROKE CODE

%% Author: PCHAPIER
%% Created: 25 mai 2010
-module(utility).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([decompile/1, decompdir/1]).

-export([shuffle/1]).


%%
%% API Functions
%%

decompdir(Dir) ->
    Cmd = "cd " ++ Dir,
    os:cmd(Cmd),
    L = os:cmd("dir /B *.beam"),
    L1 = re:split(L,"[\t\r\n+]",[{return,list}]),
    io:format("decompdir: ~p~n",[L1]),
    decompile(L1).


decompile(Beam = [H|_]) when is_integer(H) ->
    io:format("decompile: ~p~n",[Beam]),
    {ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(Beam ++ ".beam",[abstract_code]),
    {ok,File} = file:open(Beam ++ ".erl",[write]),
    io:fwrite(File,"~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]),
    file:close(File);

decompile([H|T]) ->
    io:format("decompile: ~p~n",[[H|T]]),
    decompile(removebeam(H)),
    decompile(T);

decompile([]) ->
    ok.

shuffle(P) ->
    Max = length(P)*10000,
    {_,R}= lists:unzip(lists:keysort(1,[{random:uniform(Max),X} || X <- P])),
    R.



%%
%% Local Functions
%%
removebeam(L) ->
    removebeam1(lists:reverse(L)).

removebeam1([$m,$a,$e,$b,$.|T]) ->
    lists:reverse(T);
removebeam1(L) ->
    lists:reverse(L).
@binarytemple
Copy link
Author

Not working at all

{"init terminating in do_boot",{{badmatch,{error,beam_lib,{not_a_beam_file,'/test.beam'}}},[{utility,decompile,1,[{file,"utility.erl"},{line,32}]},{utility,decompile,1,[{file,"utility.erl"},{line,39}]},{init,start_em,1,[]},{init,do_boot,3,[]}]}}
init terminating in do_boot ({{badmatch,{error,beam_lib,{not_a_beam_file,/test.beam}}},[{utility,decompile,1,[{_},{_}]},{utility,decompile,1,[{_},{_}]},{init,start_em
Crash dump is being written to: erl_crash.dump...done

Sorry, this is some ancient code - I haven't had to dissasemble Erlang files in quite some time - seems quite broken although in your case you tried to decompile the erl file rather than the beam (it would have broken in any case) - suggest you try :

https://github.com/scout119/beamdasm (Visual Studio beam decompiler (written in Typescript))

or the following escript

https://gist.github.com/kevsmith/7e6f6426634e9bb2d87e

or these instructions -

curl https://gist.githubusercontent.com/kevsmith/7e6f6426634e9bb2d87e/raw/43f72d616b5108761ccbf9def9ceb1f79ebb5016/b2f.erl -O
chmod 755 b2f.erl
./b2f.erl utility.beam
decompdir(Dir) ->
    Cmd = "cd " ++ Dir,
    os:cmd(Cmd),
    L = os:cmd("dir /B *.beam"),
    L1 = re:split(L, "[\t\r\n+]", [{return, list}]),
    io:format("decompdir: ~p~n", [L1]),
    decompile(L1)
decompile(Beam = [H | _]) when is_integer(H) ->
    io:format("decompile: ~p~n", [Beam]),
    {ok, {_, [{abstract_code, {_, AC}}]}} =
        beam_lib:chunks(Beam ++ ".beam", [abstract_code]),
    {ok, File} = file:open(Beam ++ ".erl", [write]),
    io:fwrite(File, "~s~n",
              [erl_prettypr:format(erl_syntax:form_list(AC))]),
    file:close(File)

([H | T]) ->
    io:format("decompile: ~p~n", [[H | T]]),
    decompile(removebeam(H)),
    decompile(T)

([]) -> ok
removebeam(L) -> removebeam1(lists:reverse(L))
removebeam1([$m, $a, $e, $b, $. | T]) -> lists:reverse(T)

(L) -> lists:reverse(L)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment