New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate with r2 #23

Merged
merged 9 commits into from Aug 12, 2018
View
@@ -18,6 +18,7 @@ base64 = "0.9.2"
rustyline = "1.0.0"
[dependencies.r2pipe]
# path = "../r2pipe.rs"
git = "https://github.com/radareorg/r2pipe.rs"
[dependencies.r2api]
View
@@ -1,8 +1,17 @@
all:
cargo build
make -C plugin build
run:
cargo run
install:
cargo install
make -C plugin
uninstall:
cargo uninstall
make -C plugin uninstall
clean:
cargo clean
View
@@ -40,6 +40,12 @@ fn sym.main () {
>>
```
## Installation
```shell
make install
```
## License
Licensed under The BSD 3-Clause License. Please check [COPYING](https://github.com/radare/radeco/blob/master/COPYING) file for
complete license.
View
@@ -0,0 +1,23 @@
CFLAGS=$(shell pkg-config --cflags r_core)
PLUGDIR=$(shell r2 -H R2_USER_PLUGINS)
LIBEXT=$(shell r2 -H LIBEXT)
PLUGNAME=radeco_pde
TARGET=$(PLUGNAME).$(LIBEXT)
all:
$(MAKE) build
$(MAKE) install
build:
$(CC) $(CFLAGS) $(LDFLAGS) -shared -fPIC $(PLUGNAME).c -o $(TARGET)
install:
mkdir -p $(PLUGDIR)
rm -rf $(PLUGDIR)/$(TARGET)
cp -rf $(TARGET) $(PLUGDIR)/
uninstall:
rm -rf $(PLUGDIR)/$(TARGET)
clean:
rm -rf $(TARGET)
View
@@ -0,0 +1,98 @@
#include <r_core.h>
#include <stdlib.h>
#include <string.h>
#define SETDESC(x, y) r_config_node_desc(x, y)
#define SETPREF(x, y, z) SETDESC(r_config_set(core->config, x, y), z)
const int PORT = 11111;
static char *radeco_path = "radeco";
static FILE *radeco_p = NULL;
void spawn_radeco() {
if (radeco_p) {
pclose(radeco_p);
}
radeco_p = popen(radeco_path, "w");
setbuf(radeco_p, NULL);
}
void spawn_http_srv(RCore *core) {
static bool is_called = false;
if (core == NULL) {
return;
}
if (!is_called) {
char port_str[10];
SETPREF("http.log", "false", "Show HTTP requests processed");
SETPREF("http.sandbox", "false", "Show HTTP requests processed");
snprintf(port_str, 9, " %d", PORT);
r_core_rtr_http(core, '&', '\0', port_str);
}
is_called = true;
}
void usage() {
eprintf("Usage: pde[ ?ac] <func> plugin for radeco\n");
eprintf("| pde <func> decompile current function\n");
eprintf("| pde? show this help\n");
eprintf("| pdea <func> analyze current function with radeco\n");
eprintf("| pdec send information to radeco\n");
eprintf("| pder <cmd> send <cmd> to radeco directly\n");
eprintf("| pdes respawn radeco subprocess\n");
}
int cmd_pdd(const char *input) {
if (input == NULL) {
return true;
}
const char *query = input + 1;
switch (input[0]) {
case ' ':
fprintf(radeco_p, "decompile %s\n", query);
break;
case 'a':
fprintf(radeco_p, "analyze %s\n", query);
break;
case 'c':
fprintf(radeco_p, "connect http://localhost:%d\n", PORT);
break;
case 'r':
fprintf(radeco_p, "%s\n", query);
break;
case 's':
spawn_radeco();
break;
case '\0':
case '?':
default:
usage();
}
return true;
}
int cmd(void *user, const char *input) {
if (strncmp("pde", input, 3)) {
return false;
}
spawn_http_srv((RCore *)user);
cmd_pdd(input + 3);
return true;
}
int init(void *user, const char *_input) {
spawn_radeco();
return true;
};
RCorePlugin r_core_plugin_test = {.name = "radeco",
.desc = "r2 interface for radeco",
.license = "BSD 3-Clause",
.call = cmd,
.init = init};
#ifndef CORELIB
RLibStruct radare_plugin = {.type = R_LIB_TYPE_CORE,
.data = &r_core_plugin_test,
.version = R2_VERSION};
#endif
View
@@ -79,8 +79,13 @@ fn cmd(op1: Option<&str>, op2: Option<&str>, proj_opt: &mut Option<RadecoProject
*proj_opt = Some(load_proj_by_path(path));
return;
}
(Some(command::CONNECT), Some(port)) => {
match load_proj_by_tcp(port) {
(Some(command::CONNECT), Some(url)) => {
let p_opt = if url.starts_with("http://") {
load_proj_http(&url[7..])
} else {
load_proj_tcp(&url[6..])
};
match p_opt {
Ok(proj) => *proj_opt = Some(proj),
Err(msg) => println!("{}", msg),
}
@@ -170,16 +175,25 @@ fn load_proj_by_path(path: &str) -> RadecoProject {
p
}
fn load_proj_by_tcp(port: &str) -> Result<RadecoProject, &'static str> {
let r2p = R2Pipe::tcp(&format!("localhost:{}", port))?;
fn load_proj_tcp(url: &str) -> Result<RadecoProject, &'static str> {
let r2p = R2Pipe::tcp(url)?;
Ok(load_project_by_r2pipe(r2p))
}
fn load_proj_http(url: &str) -> Result<RadecoProject, &'static str> {
let r2p = R2Pipe::http(url)?;
Ok(load_project_by_r2pipe(r2p))
}
fn load_project_by_r2pipe(r2p: R2Pipe) -> RadecoProject {
let r2 = R2::from(r2p);
let r2w = Rc::new(RefCell::new(r2));
let mut p = ProjectLoader::new().source(Rc::new(r2w)).load();
let regfile = p.regfile().clone();
for mut xy in p.iter_mut() {
analyze_mod(regfile.clone(), xy.module);
}
Ok(p)
p
}
fn fn_list(proj: &RadecoProject) -> Vec<String> {
ProTip! Use n and p to navigate between commits in a pull request.