Rizin
unix-like reverse engineering framework and cli tools
cmd_remote.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2020 nibble <nibble.ds@gmail.com>
2 // SPDX-FileCopyrightText: 2009-2020 pancake <pancake@nopcode.org>
3 // SPDX-FileCopyrightText: 2020 ret2libc <sirmy15@gmail.com>
4 // SPDX-License-Identifier: LGPL-3.0-only
5 
6 #include "rz_cmd.h"
7 #include "rz_core.h"
8 
9 static const char *help_msg_equal[] = {
10  "Usage:", " R[:!+-=ghH] [...]", " # connect with other instances of rizin",
11  "\nremote commands:", "", "",
12  "R", "", "list all open connections",
13  "R<", "[fd] cmd", "send output of local command to remote fd", // XXX may not be a special char
14  "R", "[fd] cmd", "exec cmd at remote 'fd' (last open is default one)",
15  "R!", " cmd", "run command via rz_io_system",
16  "R+", " [proto://]host:port", "connect to remote host:port (*rap://, raps://, tcp://, udp://, http://)",
17  "R-", "[fd]", "remove all hosts or host 'fd'",
18  "R=", "[fd]", "open remote session with host 'fd', 'q' to quit",
19  "R!=", "", "disable remote cmd mode",
20  "R=!", "", "enable remote cmd mode",
21  "\nservers:", "", "",
22  ".:", "9000", "start the tcp server (echo x|nc ::1 9090 or curl ::1:9090/cmd/x)",
23  "R:", "port", "start the rap server (o rap://9999)",
24  "Rg", "[?]", "start the gdbserver",
25  "Rh", "[?]", "start the http webserver",
26  "RH", "[?]", "start the http webserver (and launch the web browser)",
27  "\nother:", "", "",
28  "R&", ":port", "start rap server in background (same as '& Rr')",
29  "R", ":host:port cmd", "run 'cmd' command on remote server",
30  "\nexamples:", "", "",
31  "R+", "tcp://localhost:9090/", "connect to: rizin -c.:9090 ./bin",
32  "R+", "rap://localhost:9090/", "connect to: rizin rap://:9090",
33  "R+", "http://localhost:9090/cmd/", "connect to: rizin -c'Rh 9090' bin",
34  "o ", "rap://:9090/", "start the rap server on tcp port 9090",
35  NULL
36 };
37 
38 static const char *help_msg_equalh[] = {
39  "Usage:", " R[hH] [...]", " # http server",
40  "http server:", "", "",
41  "Rh", " port", "listen for http connections (rizin -qcRH /bin/ls)",
42  "Rh-", "", "stop background webserver",
43  "Rh--", "", "stop foreground webserver",
44  "Rh*", "", "restart current webserver",
45  "Rh&", " port", "start http server in background",
46  "RH", " port", "launch browser and listen for http",
47  "RH&", " port", "launch browser and listen for http in background",
48  NULL
49 };
50 
51 static const char *help_msg_equalg[] = {
52  "Usage:", " R[g] [...]", " # gdb server",
53  "gdbserver:", "", "",
54  "Rg", " port file [args]", "listen on 'port' debugging 'file' using gdbserver",
55  "Rg!", " port file [args]", "same as above, but debug protocol messages (like gdbserver --remote-debug)",
56  NULL
57 };
58 
59 static int getArg(char ch, int def) {
60  switch (ch) {
61  case '&':
62  case '-':
63  return ch;
64  }
65  return def;
66 }
67 
68 RZ_IPI int rz_equal_g_handler_old(void *data, const char *input) {
69  RzCore *core = (RzCore *)data;
70  if (input[0] == '?') {
72  } else {
73  rz_core_rtr_gdb(core, getArg(input[0], 'g'), input);
74  }
75  return 0;
76 }
77 
78 RZ_IPI int rz_equal_h_handler_old(void *data, const char *input) {
79  RzCore *core = (RzCore *)data;
80  if (input[0] == '?') {
82  } else {
83  rz_core_rtr_http(core, getArg(input[0], 'h'), 'h', input);
84  }
85  return 0;
86 }
87 
88 RZ_IPI int rz_equal_H_handler_old(void *data, const char *input) {
89  RzCore *core = (RzCore *)data;
90  if (input[0] == '?') {
92  } else {
93  const char *arg = rz_str_trim_head_ro(input);
94  rz_core_rtr_http(core, getArg(input[0], 'H'), 'H', arg);
95  }
96  return 0;
97 }
98 
99 RZ_IPI int rz_cmd_remote(void *data, const char *input) {
100  RzCore *core = (RzCore *)data;
101  switch (*input) {
102  case '\0': // "R"
103  rz_core_rtr_list(core);
104  break;
105  case 'j': // "Rj"
106  eprintf("TODO: list connections in json\n");
107  break;
108  case '!': // "R!"
109  if (input[1] == 'q') {
110  RZ_FREE(core->cmdremote);
111  } else if (input[1] == '=') { // R!=0 or R!= for iosystem
112  RZ_FREE(core->cmdremote);
113  core->cmdremote = rz_str_trim_dup(input + 2);
114  } else {
115  char *res = rz_io_system(core->io, input + 1);
116  if (res) {
117  rz_cons_printf("%s\n", res);
118  free(res);
119  }
120  }
121  break;
122  case '+': // "R+"
123  rz_core_rtr_add(core, input + 1);
124  break;
125  case '-': // "R-"
126  rz_core_rtr_remove(core, input + 1);
127  break;
128  // case ':': rz_core_rtr_cmds (core, input + 1); break;
129  case '<': // "R<"
130  rz_core_rtr_pushout(core, input + 1);
131  break;
132  case '=': // "R="
133  rz_core_rtr_session(core, input + 1);
134  break;
135  case 'g': // "Rg"
136  rz_equal_g_handler_old(core, input + 1);
137  break;
138  case 'h': // "Rh"
139  rz_equal_h_handler_old(core, input + 1);
140  break;
141  case 'H': // "RH"
142  rz_equal_H_handler_old(core, input + 1);
143  break;
144  case '?': // "R?"
146  break;
147  default:
148  rz_core_rtr_cmd(core, input);
149  break;
150  }
151  return 0;
152 }
153 
154 RZ_IPI RzCmdStatus rz_remote_handler(RzCore *core, int argc, const char **argv) {
155  if (argc == 1) {
156  rz_core_rtr_list(core);
157  return RZ_CMD_STATUS_OK;
158  } else if (argc == 3) {
159  char *args = rz_str_array_join(argv + 1, argc - 1, " ");
160  rz_core_rtr_cmd(core, args);
161  free(args);
162  return RZ_CMD_STATUS_OK;
163  }
164  return RZ_CMD_STATUS_ERROR;
165 }
166 
167 RZ_IPI RzCmdStatus rz_remote_send_handler(RzCore *core, int argc, const char **argv) {
168  char *args = rz_str_array_join(argv + 1, argc - 1, " ");
169  rz_core_rtr_pushout(core, args);
170  free(args);
171  return RZ_CMD_STATUS_OK;
172 }
173 
174 RZ_IPI int rz_io_system_run_oldhandler(void *data, const char *input) {
175  RzCore *core = (RzCore *)data;
176  char *res = rz_io_system(core->io, input);
177  if (res) {
178  rz_cons_printf("%s\n", res);
179  free(res);
180  }
181  return 0;
182 }
183 
184 RZ_IPI RzCmdStatus rz_remote_add_handler(RzCore *core, int argc, const char **argv) {
185  char *args = rz_str_array_join(argv + 1, argc - 1, " ");
186  rz_core_rtr_add(core, args);
187  free(args);
188  return RZ_CMD_STATUS_OK;
189 }
190 
191 RZ_IPI RzCmdStatus rz_remote_del_handler(RzCore *core, int argc, const char **argv) {
192  char *args = rz_str_array_join(argv + 1, argc - 1, " ");
193  rz_core_rtr_remove(core, args);
194  free(args);
195  return RZ_CMD_STATUS_OK;
196 }
197 
198 RZ_IPI RzCmdStatus rz_remote_open_handler(RzCore *core, int argc, const char **argv) {
199  char *args = rz_str_array_join(argv + 1, argc - 1, " ");
200  rz_core_rtr_session(core, args);
201  free(args);
202  return RZ_CMD_STATUS_OK;
203 }
204 
206  RZ_FREE(core->cmdremote);
207  core->cmdremote = rz_str_trim_dup(argc > 1 ? argv[1] : "0");
208  return RZ_CMD_STATUS_OK;
209 }
210 
212  RZ_FREE(core->cmdremote);
213  return RZ_CMD_STATUS_OK;
214 }
215 
216 RZ_IPI RzCmdStatus rz_remote_rap_handler(RzCore *core, int argc, const char **argv) {
217  char *args = rz_str_array_join(argv + 1, argc - 1, " ");
218  args = rz_str_prepend(args, ":");
219  rz_core_rtr_cmd(core, args);
220  free(args);
221  return RZ_CMD_STATUS_OK;
222 }
223 
224 RZ_IPI RzCmdStatus rz_remote_rap_bg_handler(RzCore *core, int argc, const char **argv) {
225  char *args = rz_str_array_join(argv + 1, argc - 1, " ");
226  args = rz_str_prepend(args, "&:");
227  rz_core_rtr_cmd(core, args);
228  free(args);
229  return RZ_CMD_STATUS_OK;
230 }
231 
232 RZ_IPI RzCmdStatus rz_remote_tcp_handler(RzCore *core, int argc, const char **argv) {
233  if (argc == 2) {
234  rz_core_rtr_cmds(core, argv[1]);
235  return RZ_CMD_STATUS_OK;
236  } else if (argc == 3) {
237  char *host, *port = strchr(argv[1], ':');
238  if (port) {
239  host = rz_str_ndup(argv[1], port - argv[1]);
240  port = strdup(port + 1);
241  } else {
242  host = strdup("localhost");
243  port = strdup(argv[1]);
244  }
245  char *rbuf = rz_core_rtr_cmds_query(core, host, port, argv[2]);
246  if (rbuf) {
247  rz_cons_print(rbuf);
248  free(rbuf);
249  }
250  free(host);
251  free(port);
252  return RZ_CMD_STATUS_OK;
253  }
254  return RZ_CMD_STATUS_ERROR;
255 }
#define RZ_IPI
Definition: analysis_wasm.c:11
RZ_API void rz_core_cmd_help(const RzCore *core, const char *help[])
Definition: cmd.c:163
RZ_IPI RzCmdStatus rz_remote_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_remote.c:154
RZ_IPI RzCmdStatus rz_remote_rap_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_remote.c:216
RZ_IPI RzCmdStatus rz_remote_mode_enable_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_remote.c:205
RZ_IPI RzCmdStatus rz_remote_tcp_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_remote.c:232
RZ_IPI RzCmdStatus rz_remote_del_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_remote.c:191
RZ_IPI int rz_io_system_run_oldhandler(void *data, const char *input)
Definition: cmd_remote.c:174
RZ_IPI int rz_equal_h_handler_old(void *data, const char *input)
Definition: cmd_remote.c:78
static const char * help_msg_equalh[]
Definition: cmd_remote.c:38
static int getArg(char ch, int def)
Definition: cmd_remote.c:59
RZ_IPI RzCmdStatus rz_remote_add_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_remote.c:184
RZ_IPI int rz_equal_H_handler_old(void *data, const char *input)
Definition: cmd_remote.c:88
static const char * help_msg_equalg[]
Definition: cmd_remote.c:51
RZ_IPI int rz_cmd_remote(void *data, const char *input)
Definition: cmd_remote.c:99
RZ_IPI RzCmdStatus rz_remote_open_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_remote.c:198
RZ_IPI int rz_equal_g_handler_old(void *data, const char *input)
Definition: cmd_remote.c:68
RZ_IPI RzCmdStatus rz_remote_rap_bg_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_remote.c:224
RZ_IPI RzCmdStatus rz_remote_send_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_remote.c:167
static const char * help_msg_equal[]
Definition: cmd_remote.c:9
RZ_IPI RzCmdStatus rz_remote_mode_disable_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_remote.c:211
RZ_API int rz_cons_printf(const char *format,...)
Definition: cons.c:1202
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
int args
Definition: mipsasm.c:18
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API void rz_core_rtr_session(RzCore *core, const char *input)
Definition: rtr.c:814
RZ_API void rz_core_rtr_list(RzCore *core)
Definition: rtr.c:631
RZ_API void rz_core_rtr_add(RzCore *core, const char *_input)
Definition: rtr.c:651
RZ_API char * rz_core_rtr_cmds_query(RzCore *core, const char *host, const char *port, const char *cmd)
Definition: rtr.c:983
RZ_API int rz_core_rtr_gdb(RzCore *core, int launch, const char *path)
Definition: rtr.c:571
RZ_API void rz_core_rtr_remove(RzCore *core, const char *input)
Definition: rtr.c:793
RZ_API int rz_core_rtr_cmds(RzCore *core, const char *port)
Definition: rtr.c:1225
RZ_API void rz_core_rtr_pushout(RzCore *core, const char *input)
Definition: rtr.c:582
RZ_API void rz_core_rtr_cmd(RzCore *core, const char *input)
Definition: rtr.c:850
RZ_API int rz_core_rtr_http(RzCore *core, int launch, int browse, const char *path)
Definition: rtr_http.c:491
enum rz_cmd_status_t RzCmdStatus
@ RZ_CMD_STATUS_OK
command handler exited in the right way
Definition: rz_cmd.h:24
@ RZ_CMD_STATUS_ERROR
command handler had issues while running (e.g. allocation error, etc.)
Definition: rz_cmd.h:26
RZ_API char * rz_io_system(RzIO *io, const char *cmd)
Definition: io.c:411
RZ_API char * rz_str_ndup(RZ_NULLABLE const char *ptr, int len)
Create new copy of string ptr limited to size len.
Definition: str.c:1006
RZ_API char * rz_str_trim_dup(const char *str)
Definition: str_trim.c:78
RZ_API char * rz_str_array_join(const char **a, size_t n, const char *sep)
Definition: str.c:3861
RZ_API char * rz_str_prepend(char *ptr, const char *string)
Definition: str.c:1027
RZ_API const char * rz_str_trim_head_ro(const char *str)
Definition: str_trim.c:86
#define RZ_FREE(x)
Definition: rz_types.h:369
RzIO * io
Definition: rz_core.h:313
char * cmdremote
Definition: rz_core.h:369
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
int def(FILE *source, FILE *dest, int level)
Definition: zpipe.c:36