Skip to content

Commit

Permalink
Convert pk, pK, and pkill commands to the rzshell (#3178)
Browse files Browse the repository at this point in the history
  • Loading branch information
XVilka authored Nov 11, 2022
1 parent 5611bc3 commit 685035c
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 61 deletions.
109 changes: 48 additions & 61 deletions librz/core/cmd/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -4545,67 +4545,6 @@ RZ_IPI int rz_cmd_print(void *data, const char *input) {
case 'f': // "pf"
cmd_print_format(core, input, block, len);
break;
case 'k': // "pk"
if (input[1] == '?') {
rz_cons_printf("|Usage: pk [len] print key in randomart\n");
rz_cons_printf("|Usage: pkill [process-name]\n");
} else if (!strncmp(input, "kill", 4)) {
RzListIter *iter;
RzDebugPid *pid;
const char *arg = strchr(input, ' ');
RzList *pids = (core->dbg->cur && core->dbg->cur->pids)
? core->dbg->cur->pids(core->dbg, 0)
: NULL;
if (arg && *++arg) {
rz_list_foreach (pids, iter, pid) {
if (strstr(pid->path, arg)) {
rz_cons_printf("dk 9 %d\n", pid->pid);
}
// rz_debug_kill (core->dbg, pid->pid, pid->pid, 9); // kill -9
}
}
rz_list_free(pids);
} else if (l > 0) {
len = len > core->blocksize ? core->blocksize : len;
char *s = rz_hash_cfg_randomart(block, len, core->offset);
rz_cons_println(s);
free(s);
}
break;
case 'K': // "pK"
if (input[1] == '?') {
rz_cons_printf("|Usage: pK [len] print key in randomart mosaic\n");
} else if (l > 0) {
len = len > core->blocksize ? core->blocksize : len;
int w, h;
RzConsCanvas *c;
w = rz_cons_get_size(&h);
ut64 offset0 = core->offset;
int cols = (w / 20);
int rows = (h / 12);
int i, j;
char *s;
if (rows < 1) {
rows = 1;
}
c = rz_cons_canvas_new(w, rows * 11);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
rz_cons_canvas_gotoxy(c, j * 20, i * 11);
core->offset += len;
rz_io_read_at(core->io, core->offset, core->block, len);
s = rz_hash_cfg_randomart(core->block, len, core->offset);
rz_cons_canvas_write(c, s);
free(s);
}
}
rz_cons_canvas_print(c);
rz_cons_canvas_free(c);
rz_io_read_at(core->io, offset0, core->block, len);
core->offset = offset0;
rz_cons_printf("\n");
}
break;
default:
rz_core_cmd_help(core, help_msg_p);
break;
Expand Down Expand Up @@ -6518,3 +6457,51 @@ RZ_IPI RzCmdStatus rz_print_operation_sub_handler(RzCore *core, int argc, const
RZ_IPI RzCmdStatus rz_print_operation_xor_handler(RzCore *core, int argc, const char **argv) {
return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_XOR, argv[1]));
}

RZ_IPI RzCmdStatus rz_print_key_randomart_handler(RzCore *core, int argc, const char **argv) {
ut64 len = argc > 1 ? rz_num_math(core->num, argv[1]) : core->blocksize;
if (len == 0) {
return RZ_CMD_STATUS_ERROR;
}
len = len > core->blocksize ? core->blocksize : len;
char *s = rz_hash_cfg_randomart(core->block, len, core->offset);
rz_cons_println(s);
free(s);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_print_key_mosaic_handler(RzCore *core, int argc, const char **argv) {
ut64 len = argc > 1 ? rz_num_math(core->num, argv[1]) : core->blocksize;
if (len == 0) {
return RZ_CMD_STATUS_ERROR;
}
len = len > core->blocksize ? core->blocksize : len;
int w, h;
RzConsCanvas *c;
w = rz_cons_get_size(&h);
ut64 offset0 = core->offset;
int cols = (w / 20);
int rows = (h / 12);
int i, j;
char *s;
if (rows < 1) {
rows = 1;
}
c = rz_cons_canvas_new(w, rows * 11);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
rz_cons_canvas_gotoxy(c, j * 20, i * 11);
core->offset += len;
rz_io_read_at(core->io, core->offset, core->block, len);
s = rz_hash_cfg_randomart(core->block, len, core->offset);
rz_cons_canvas_write(c, s);
free(s);
}
}
rz_cons_canvas_print(c);
rz_cons_canvas_free(c);
rz_io_read_at(core->io, offset0, core->block, len);
core->offset = offset0;
rz_cons_printf("\n");
return RZ_CMD_STATUS_OK;
}
16 changes: 16 additions & 0 deletions librz/core/cmd/cmd_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,19 @@ RZ_IPI RzCmdStatus rz_cmd_shell_date_handler(RzCore *core, int argc, const char
free(now);
return RZ_CMD_STATUS_OK;
}

// pkill
RZ_IPI RzCmdStatus rz_cmd_shell_pkill_handler(RzCore *core, int argc, const char **argv) {
RzListIter *iter;
RzDebugPid *pid;
RzList *pids = (core->dbg->cur && core->dbg->cur->pids)
? core->dbg->cur->pids(core->dbg, 0)
: NULL;
rz_list_foreach (pids, iter, pid) {
if (strstr(pid->path, argv[1])) {
rz_debug_kill(core->dbg, pid->pid, 0, 9);
}
}
rz_list_free(pids);
return RZ_CMD_STATUS_OK;
}
56 changes: 56 additions & 0 deletions librz/core/cmd_descs/cmd_descs.c
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ static const RzCmdDescArg print_pattern_latin_alphabet_args[2];
static const RzCmdDescArg print_pattern_debrujin_args[2];
static const RzCmdDescArg print_pattern_oxff_args[2];
static const RzCmdDescArg print_pattern_num_args[2];
static const RzCmdDescArg print_key_randomart_args[2];
static const RzCmdDescArg print_key_mosaic_args[2];
static const RzCmdDescArg cmd_print_magic_args[2];
static const RzCmdDescArg print_operation_add_args[2];
static const RzCmdDescArg print_operation_and_args[2];
Expand Down Expand Up @@ -724,6 +726,7 @@ static const RzCmdDescArg cmd_shell_mv_args[3];
static const RzCmdDescArg cmd_shell_mkdir_args[3];
static const RzCmdDescArg cmd_shell_sort_args[2];
static const RzCmdDescArg cmd_shell_which_args[2];
static const RzCmdDescArg cmd_shell_pkill_args[2];

static const RzCmdDescHelp escl__help = {
.summary = "Run given commands as in system(3) or shows command history",
Expand Down Expand Up @@ -12495,6 +12498,36 @@ static const RzCmdDescHelp cmd_print_timestamp_ntfs_help = {
.args = cmd_print_timestamp_ntfs_args,
};

static const RzCmdDescArg print_key_randomart_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp print_key_randomart_help = {
.summary = "Print cryptographic key in randomart",
.args = print_key_randomart_args,
};

static const RzCmdDescArg print_key_mosaic_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp print_key_mosaic_help = {
.summary = "Print cryptographic key in randomart mosaic",
.args = print_key_mosaic_args,
};

static const RzCmdDescArg cmd_print_magic_args[] = {
{
.name = "file/directory",
Expand Down Expand Up @@ -16254,6 +16287,20 @@ static const RzCmdDescHelp cmd_shell_fortune_help = {
.args = cmd_shell_fortune_args,
};

static const RzCmdDescArg cmd_shell_pkill_args[] = {
{
.name = "name",
.type = RZ_CMD_ARG_TYPE_STRING,
.flags = RZ_CMD_ARG_FLAG_LAST,

},
{ 0 },
};
static const RzCmdDescHelp cmd_shell_pkill_help = {
.summary = "Kill process by name",
.args = cmd_shell_pkill_args,
};

RZ_IPI void rzshell_cmddescs_init(RzCore *core) {
RzCmdDesc *root_cd = rz_cmd_get_root(core->rcmd);
rz_cmd_batch_start(core->rcmd);
Expand Down Expand Up @@ -18869,6 +18916,12 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) {
RzCmdDesc *cmd_print_timestamp_ntfs_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_timestamp_cd, "ptn", rz_cmd_print_timestamp_ntfs_handler, &cmd_print_timestamp_ntfs_help);
rz_warn_if_fail(cmd_print_timestamp_ntfs_cd);

RzCmdDesc *print_key_randomart_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_cd, "pk", rz_print_key_randomart_handler, &print_key_randomart_help);
rz_warn_if_fail(print_key_randomart_cd);

RzCmdDesc *print_key_mosaic_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_cd, "pK", rz_print_key_mosaic_handler, &print_key_mosaic_help);
rz_warn_if_fail(print_key_mosaic_cd);

RzCmdDesc *cmd_print_magic_cd = rz_cmd_desc_argv_modes_new(core->rcmd, cmd_print_cd, "pm", RZ_OUTPUT_MODE_JSON, rz_cmd_print_magic_handler, &cmd_print_magic_help);
rz_warn_if_fail(cmd_print_magic_cd);

Expand Down Expand Up @@ -19595,5 +19648,8 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) {

RzCmdDesc *cmd_shell_fortune_cd = rz_cmd_desc_argv_new(core->rcmd, shell_cd, "fortune", rz_cmd_shell_fortune_handler, &cmd_shell_fortune_help);
rz_warn_if_fail(cmd_shell_fortune_cd);

RzCmdDesc *cmd_shell_pkill_cd = rz_cmd_desc_argv_new(core->rcmd, shell_cd, "pkill", rz_cmd_shell_pkill_handler, &cmd_shell_pkill_help);
rz_warn_if_fail(cmd_shell_pkill_cd);
rz_cmd_batch_end(core->rcmd);
}
6 changes: 6 additions & 0 deletions librz/core/cmd_descs/cmd_descs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,10 @@ RZ_IPI RzCmdStatus rz_cmd_print_timestamp_dos_handler(RzCore *core, int argc, co
RZ_IPI RzCmdStatus rz_cmd_print_timestamp_hfs_handler(RzCore *core, int argc, const char **argv);
// "ptn"
RZ_IPI RzCmdStatus rz_cmd_print_timestamp_ntfs_handler(RzCore *core, int argc, const char **argv);
// "pk"
RZ_IPI RzCmdStatus rz_print_key_randomart_handler(RzCore *core, int argc, const char **argv);
// "pK"
RZ_IPI RzCmdStatus rz_print_key_mosaic_handler(RzCore *core, int argc, const char **argv);
// "pm"
RZ_IPI RzCmdStatus rz_cmd_print_magic_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode);
// "po2"
Expand Down Expand Up @@ -2247,6 +2251,8 @@ RZ_IPI RzCmdStatus rz_cmd_shell_clear_handler(RzCore *core, int argc, const char
RZ_IPI RzCmdStatus rz_cmd_shell_which_handler(RzCore *core, int argc, const char **argv);
// "fortune"
RZ_IPI RzCmdStatus rz_cmd_shell_fortune_handler(RzCore *core, int argc, const char **argv);
// "pkill"
RZ_IPI RzCmdStatus rz_cmd_shell_pkill_handler(RzCore *core, int argc, const char **argv);

// Main function that initialize the entire commands tree
RZ_IPI void rzshell_cmddescs_init(RzCore *core);
14 changes: 14 additions & 0 deletions librz/core/cmd_descs/cmd_print.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,20 @@ commands:
summary: Print NTFS time (64 bit `cfg.bigendian`, since January 1, 1601)
cname: cmd_print_timestamp_ntfs
args: []
- name: pk
summary: Print cryptographic key in randomart
cname: print_key_randomart
args:
- name: len
type: RZ_CMD_ARG_TYPE_RZNUM
optional: true
- name: pK
summary: Print cryptographic key in randomart mosaic
cname: print_key_mosaic
args:
- name: len
type: RZ_CMD_ARG_TYPE_RZNUM
optional: true
- name: pm
summary: Print libmagic data
cname: cmd_print_magic
Expand Down
6 changes: 6 additions & 0 deletions librz/core/cmd_descs/cmd_shell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,9 @@ commands:
summary: Show the random fortune message
cname: cmd_shell_fortune
args: []
- name: pkill
cname: cmd_shell_pkill
summary: Kill process by name
args:
- name: name
type: RZ_CMD_ARG_TYPE_STRING

0 comments on commit 685035c

Please sign in to comment.