Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

add r2ghidra #34

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
angr \
ghidra \
r2dec \
r2ghidra \
reko \
retdec \
snowman
Expand All @@ -28,6 +29,7 @@ jobs:
angr \
ghidra \
r2dec \
r2ghidra \
reko \
retdec \
snowman
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Supported Decompilers
* [Hex-Rays](https://hex-rays.com/decompiler/)
* [JEB CE](https://www.pnfsoftware.com/jeb/community-edition)
* [r2dec](https://github.com/wargio/r2dec-js)
* [r2ghidra](https://github.com/radareorg/r2ghidra)
* [Reko](https://github.com/uxmal/reko)
* [RetDec](https://github.com/avast/retdec)
* [Snowman](https://github.com/yegord/snowman)
Expand Down
32 changes: 29 additions & 3 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ CMD ["python3", "-m", "mdecjeb"]
#
FROM base as r2dec

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
&& apt-get install -qy build-essential cmake git
RUN apt-get install -qy build-essential cmake git
RUN ln -sf /usr/bin/python3 /usr/bin/python

USER mdec
Expand All @@ -154,6 +152,34 @@ USER mdec
ENV PATH=$PATH:/home/mdec/bin
CMD ["python3", "-m", "mdecr2dec"]

#
# r2ghidra
#
FROM base as r2ghidra

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get install -qy build-essential cmake git wget pkg-config meson unzip
RUN ln -sf /usr/bin/python3 /usr/bin/python

USER mdec
RUN cd /home/mdec \
&& git clone --depth=1 https://github.com/radareorg/radare2 \
&& cd radare2 \
&& ./sys/user.sh \
&& export PATH=$PATH:/home/mdec/bin \
&& r2pm init \
&& r2pm -ci r2ghidra

USER root
COPY common/mdec-base /mdec-base
COPY r2ghidra/mdec-r2ghidra /mdec-r2ghidra
RUN pip install /mdec-base /mdec-r2ghidra \
&& rm -rf /mdec-base /mdec-r2ghidra

USER mdec
ENV PATH=$PATH:/home/mdec/bin
CMD ["python3", "-m", "mdecr2ghidra"]

#
# Reko
#
Expand Down
1 change: 1 addition & 0 deletions backend/r2ghidra/mdec-r2ghidra/mdecr2ghidra/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .service import R2ghidraService
6 changes: 6 additions & 0 deletions backend/r2ghidra/mdec-r2ghidra/mdecr2ghidra/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from mdecr2ghidra import R2ghidraService
from mdecbase import mdec_main


if __name__ == '__main__':
mdec_main(R2ghidraService)
34 changes: 34 additions & 0 deletions backend/r2ghidra/mdec-r2ghidra/mdecr2ghidra/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import traceback
import r2pipe
import subprocess

from mdecbase import Service

class R2ghidraService(Service):
"""
r2ghidr aas a service
"""

def decompile(self, path: str) -> str:
"""
Decompile all the function in the binary located at `path`.
"""
r2 = r2pipe.open(path, flags=['-e bin.cache=true'])
r2.cmd('a'*6)
funcs = [func['name'] for func in r2.cmdj('aflj')]

out = []

for func in funcs:
try:
dec = r2.cmd(f'pdg @{func}')
out.append(dec)
except:
out.append(f'/* Decompilation of {func} failed:\n{traceback.format_exc()}\n*/')


return '\n'.join(out)

def version(self) -> str:
return subprocess.run(['/usr/local/bin/r2', '-v'], stdout=subprocess.PIPE).stdout.splitlines()[0].split()[1].decode('utf-8', 'ignore')

13 changes: 13 additions & 0 deletions backend/r2ghidra/mdec-r2ghidra/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python3
from setuptools import setup

__version__ = '0.0.1'


setup(name='mdec-r2ghidra',
version=__version__,
description='mdec-r2ghidra',
packages=['mdecr2ghidra'],
install_requires=['mdec-base', 'r2pipe'],
python_requires='>=3.8'
)
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ services:
context: backend
target: r2dec

r2ghidra:
build:
context: backend
target: r2ghidra

reko:
build:
context: backend
Expand Down
8 changes: 8 additions & 0 deletions frontend/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ http {
proxy_set_header X-Forwarded-For $remote_addr;
}

location ~ ^/r2ghidra/(.*)$ {
set $service_host http://r2ghidra:8000;
limit_except GET HEAD POST { deny all; }
proxy_pass $service_host/$1;
proxy_redirect off;
proxy_set_header X-Forwarded-For $remote_addr;
}

location ~ ^/reko/(.*)$ {
set $service_host http://reko:8000;
limit_except GET HEAD POST { deny all; }
Expand Down
9 changes: 8 additions & 1 deletion frontend/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ <h1><a href="https://github.com/mborgerson/mdec">mdec</a></h1>
r2dec
</a>
</li>
<li>
<a href="#" class="dropdown-item service-checkbox-link" for="r2ghidra-checkbox">
<input class="form-check-input service-checkbox" type="checkbox" data-value="r2ghidra" value="" id="r2ghidra-checkbox">
r2ghidra
</a>
</li>
<li>
<a href="#" class="dropdown-item service-checkbox-link" for="reko-checkbox">
<input class="form-check-input service-checkbox" type="checkbox" data-value="reko" value="" id="reko-checkbox">
Expand Down Expand Up @@ -164,6 +170,7 @@ <h1><a href="https://github.com/mborgerson/mdec">mdec</a></h1>
<div class="editor-col" id="hexrays-col"><h3>Hex-Rays</h3><div class="version" id="hexrays-version"></div><div class="editor" id="hexrays-editor"></div></div>
<div class="editor-col" id="jeb-col"><h3>JEB</h3><div class="version" id="jeb-version"></div><div class="editor" id="jeb-editor"></div></div>
<div class="editor-col" id="r2dec-col"><h3>r2dec</h3><div class="version" id="r2dec-version"></div><div class="editor" id="r2dec-editor"></div></div>
<div class="editor-col" id="r2ghidra-col"><h3>r2ghidra</h3><div class="version" id="r2ghidra-version"></div><div class="editor" id="r2ghidra-editor"></div></div>
<div class="editor-col" id="reko-col"><h3>Reko</h3><div class="version" id="reko-version"></div><div class="editor" id="reko-editor"></div></div>
<div class="editor-col" id="retdec-col"><h3>RetDec</h3><div class="version" id="retdec-version"></div><div class="editor" id="retdec-editor"></div></div>
<div class="editor-col" id="snowman-col"><h3>Snowman</h3><div class="version" id="snowman-version"></div><div class="editor" id="snowman-editor"></div></div>
Expand All @@ -180,7 +187,7 @@ <h1><a href="https://github.com/mborgerson/mdec">mdec</a></h1>

<script type="text/javascript">

var services = ['angr', 'binja', 'ghidra', 'hexrays', 'jeb', 'r2dec', 'reko', 'retdec', 'snowman'];
var services = ['angr', 'binja', 'ghidra', 'hexrays', 'jeb', 'r2dec', 'r2ghidra', 'reko', 'retdec', 'snowman'];
var editors = {};
var version_boxes = {};
var options = [];
Expand Down
2 changes: 1 addition & 1 deletion tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_all_services(self):
src_path = os.path.join(TEST_ROOT, 'src', 'fib.c')
bin_path = os.path.join(working_dir, 'fib')
subprocess.run(['gcc', '-o', bin_path, src_path], check=True)
for service_name in ['angr', 'r2dec', 'reko', 'retdec', 'snowman']:
for service_name in ['angr', 'r2dec', 'r2ghidra', 'reko', 'retdec', 'snowman']:
self._test_service(service_name, bin_path)


Expand Down