Skip to content

Commit

Permalink
Convert pF commands to the rzshell (#3071)
Browse files Browse the repository at this point in the history
* Convert `pF` commands to the rzshell
* Add json output for PKCS7 and x509 and rename test due FS issues

Co-authored-by: wargio <[email protected]>
  • Loading branch information
XVilka and wargio authored Oct 9, 2022
1 parent f3a4200 commit 24503df
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 103 deletions.
9 changes: 5 additions & 4 deletions librz/bin/p/bin_pe.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ static char *signature(RzBinFile *bf, bool json) {
}
struct PE_(rz_bin_pe_obj_t) *bin = bf->o->bin_obj;
if (json) {
PJ *pj = rz_pkcs7_cms_json(bin->cms);
if (pj) {
return pj_drain(pj);
PJ *pj = pj_new();
if (!pj) {
return strdup("{}");
}
return strdup("{}");
rz_pkcs7_cms_json(bin->cms, pj);
return pj_drain(pj);
}
return rz_pkcs7_cms_to_string(bin->cms);
}
Expand Down
191 changes: 104 additions & 87 deletions librz/core/cmd/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,6 @@ static const char *help_msg_pp[] = {
NULL
};

static const char *help_msg_pF[] = {
"Usage: pF[apdbA]", "[len]", "parse ASN1, PKCS, X509, DER, protobuf, axml",
"pFa", "[len]", "decode ASN1 from current block",
"pFaq", "[len]", "decode ASN1 from current block (quiet output)",
"pFb", "[len]", "decode raw proto buffers.",
"pFbv", "[len]", "decode raw proto buffers (verbose).",
"pFo", "[len]", "decode ASN1 OID",
"pFp", "[len]", "decode PKCS7",
"pFx", "[len]", "Same with X509",
"pFA", "[len]", "decode Android Binary XML from current block",
NULL
};

static const char *help_msg_pr[] = {
"Usage: pr[glx]", "[size]", "print N raw bytes",
"prc", "[=fep..]", "print bytes as colors in palette",
Expand Down Expand Up @@ -1070,77 +1057,6 @@ static void print_format_help_help_help_help(RzCore *core) {
rz_core_cmd_help(core, help_msg);
}

static void cmd_print_fromage(RzCore *core, const char *input, const ut8 *data, int size) {
switch (*input) {
case 'a': {
RzASN1Object *asn1 = rz_asn1_object_parse(data, size);
if (asn1) {
char *res = rz_asn1_to_string(asn1, 0, input[1] != 'q');
rz_asn1_object_free(asn1);
if (res) {
rz_cons_printf("%s", res);
free(res);
}
} else {
RZ_LOG_ERROR("core: Malformed object: did you supply enough data?\ntry to change the block size (see b?)\n");
}
} break;
case 'x': // "pFx" x509
{
RzASN1Object *object = rz_asn1_object_parse(data, size);
RzX509Certificate *x509 = rz_x509_certificate_parse(object);
if (x509) {
RzStrBuf *sb = rz_strbuf_new("");
rz_x509_certificate_dump(x509, NULL, sb);
char *res = rz_strbuf_drain(sb);
if (res) {
rz_cons_printf("%s", res);
free(res);
}
rz_x509_certificate_free(x509);
} else {
RZ_LOG_ERROR("core: Malformed object: did you supply enough data?\ntry to change the block size (see b?)\n");
}
} break;
case 'p': // "pFp"
{
RzCMS *cms = rz_pkcs7_cms_parse(data, size);
if (cms) {
char *res = rz_pkcs7_cms_to_string(cms);
if (res) {
rz_cons_printf("%s", res);
free(res);
}
rz_pkcs7_cms_free(cms);
} else {
RZ_LOG_ERROR("core: Malformed object: did you supply enough data?\ntry to change the block size (see b?)\n");
}
} break;
case 'b': // "pFb"
{
char *s = rz_protobuf_decode(data, size, input[1] == 'v');
if (s) {
rz_cons_printf("%s", s);
free(s);
}
} break;
case 'A': // "pFA"
{
char *s = rz_axml_decode(data, size);
if (s) {
rz_cons_printf("%s", s);
free(s);
} else {
RZ_LOG_ERROR("core: Malformed object: did you supply enough data?\ntry to change the block size (see b?)\n");
}
} break;
default:
case '?': // "pF?"
rz_core_cmd_help(core, help_msg_pF);
break;
}
}

/**
* \brief Frees a visual print gadget
*
Expand Down Expand Up @@ -5120,9 +5036,6 @@ RZ_IPI int rz_cmd_print(void *data, const char *input) {
case 'f': // "pf"
cmd_print_format(core, input, block, len);
break;
case 'F': // "pF"
cmd_print_fromage(core, input + 1, block, len);
break;
case 'k': // "pk"
if (input[1] == '?') {
rz_cons_printf("|Usage: pk [len] print key in randomart\n");
Expand Down Expand Up @@ -6598,3 +6511,107 @@ RZ_IPI RzCmdStatus rz_print_byte_bitstream_handler(RzCore *core, int argc, const
free(str_buf);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_cmd_print_asn1_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) {
RzASN1Object *asn1 = rz_asn1_object_parse(core->block, core->blocksize);
if (!asn1) {
RZ_LOG_ERROR("core: Malformed object: did you supply enough data?\ntry to change the block size (see b? or @!<size>)\n");
return RZ_CMD_STATUS_ERROR;
}
char *res = rz_asn1_to_string(asn1, 0, mode == RZ_OUTPUT_MODE_STANDARD);
rz_asn1_object_free(asn1);
if (!res) {
return RZ_CMD_STATUS_ERROR;
}
rz_cons_printf("%s", res);
free(res);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_cmd_print_protobuf_standard_handler(RzCore *core, int argc, const char **argv) {
char *s = rz_protobuf_decode(core->block, core->blocksize, false);
if (!s) {
RZ_LOG_ERROR("core: Malformed object: did you supply enough data?\ntry to change the block size (see b? or @!<size>)\n");
return RZ_CMD_STATUS_ERROR;
}
rz_cons_printf("%s", s);
free(s);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_cmd_print_protobuf_verbose_handler(RzCore *core, int argc, const char **argv) {
char *s = rz_protobuf_decode(core->block, core->blocksize, true);
if (!s) {
RZ_LOG_ERROR("core: Malformed object: did you supply enough data?\ntry to change the block size (see b? or @!<size>)\n");
return RZ_CMD_STATUS_ERROR;
}
rz_cons_printf("%s", s);
free(s);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_cmd_print_pkcs7_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state) {
char *res = NULL;
RzCMS *cms = rz_pkcs7_cms_parse(core->block, core->blocksize);
if (!cms) {
RZ_LOG_ERROR("core: Malformed object: did you supply enough data?\ntry to change the block size (see b? or @!<size>)\n");
}

switch (state->mode) {
case RZ_OUTPUT_MODE_JSON:
rz_pkcs7_cms_json(cms, state->d.pj);
break;
default:
res = rz_pkcs7_cms_to_string(cms);
if (res) {
rz_cons_printf("%s", res);
free(res);
}
break;
}
rz_pkcs7_cms_free(cms);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_cmd_print_x509_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state) {
char *res = NULL;
RzStrBuf *sb = NULL;
RzX509Certificate *x509 = rz_x509_certificate_parse2(core->block, core->blocksize);
if (!x509) {
RZ_LOG_ERROR("core: Malformed object: did you supply enough data?\ntry to change the block size (see b? or @!<size>)\n");
return RZ_CMD_STATUS_ERROR;
}

switch (state->mode) {
case RZ_OUTPUT_MODE_JSON:
rz_x509_certificate_json(state->d.pj, x509);
break;
default:
sb = rz_strbuf_new(NULL);
if (!sb) {
RZ_LOG_ERROR("core: failed to allocate RzStrBuf\n");
rz_x509_certificate_free(x509);
return RZ_CMD_STATUS_ERROR;
}
rz_x509_certificate_dump(x509, NULL, sb);
res = rz_strbuf_drain(sb);
if (res) {
rz_cons_printf("%s", res);
free(res);
}
break;
}
rz_x509_certificate_free(x509);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_cmd_print_axml_handler(RzCore *core, int argc, const char **argv) {
char *s = rz_axml_decode(core->block, core->blocksize);
if (!s) {
RZ_LOG_ERROR("core: Malformed object: did you supply enough data?\ntry to change the block size (see b? or @!<size>)\n");
return RZ_CMD_STATUS_ERROR;
}
rz_cons_printf("%s", s);
free(s);
return RZ_CMD_STATUS_OK;
}
73 changes: 73 additions & 0 deletions librz/core/cmd_descs/cmd_descs.c
Original file line number Diff line number Diff line change
Expand Up @@ -11876,6 +11876,60 @@ static const RzCmdDescHelp cmd_disassemble_summarize_block_help = {
.args = cmd_disassemble_summarize_block_args,
};

static const RzCmdDescHelp pF_help = {
.summary = "Print parsed ASN.1, PKCS, X509, ProtoBuf, AXML, etc.. formats",
};
static const RzCmdDescArg cmd_print_asn1_args[] = {
{ 0 },
};
static const RzCmdDescHelp cmd_print_asn1_help = {
.summary = "Decode ASN.1 from current block",
.args = cmd_print_asn1_args,
};

static const RzCmdDescHelp cmd_print_protobuf_help = {
.summary = "Decode raw protobuf from current block",
};
static const RzCmdDescArg cmd_print_protobuf_standard_args[] = {
{ 0 },
};
static const RzCmdDescHelp cmd_print_protobuf_standard_help = {
.summary = "Decode raw protobuf from current block",
.args = cmd_print_protobuf_standard_args,
};

static const RzCmdDescArg cmd_print_protobuf_verbose_args[] = {
{ 0 },
};
static const RzCmdDescHelp cmd_print_protobuf_verbose_help = {
.summary = "Decode raw protobuf from current block (verbose)",
.args = cmd_print_protobuf_verbose_args,
};

static const RzCmdDescArg cmd_print_pkcs7_args[] = {
{ 0 },
};
static const RzCmdDescHelp cmd_print_pkcs7_help = {
.summary = "Decode PKCS7 from current block",
.args = cmd_print_pkcs7_args,
};

static const RzCmdDescArg cmd_print_x509_args[] = {
{ 0 },
};
static const RzCmdDescHelp cmd_print_x509_help = {
.summary = "Decode X.509 from current block",
.args = cmd_print_x509_args,
};

static const RzCmdDescArg cmd_print_axml_args[] = {
{ 0 },
};
static const RzCmdDescHelp cmd_print_axml_help = {
.summary = "Decode Android Binary XML from current block",
.args = cmd_print_axml_args,
};

static const RzCmdDescHelp cmd_print_gadget_help = {
.summary = "Print gadgets",
};
Expand Down Expand Up @@ -18026,6 +18080,25 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) {
RzCmdDesc *cmd_disassemble_summarize_block_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_disassemble_summarize_cd, "pdsb", rz_cmd_disassemble_summarize_block_handler, &cmd_disassemble_summarize_block_help);
rz_warn_if_fail(cmd_disassemble_summarize_block_cd);

RzCmdDesc *pF_cd = rz_cmd_desc_group_new(core->rcmd, cmd_print_cd, "pF", NULL, NULL, &pF_help);
rz_warn_if_fail(pF_cd);
RzCmdDesc *cmd_print_asn1_cd = rz_cmd_desc_argv_modes_new(core->rcmd, pF_cd, "pFa", RZ_OUTPUT_MODE_STANDARD | RZ_OUTPUT_MODE_QUIET, rz_cmd_print_asn1_handler, &cmd_print_asn1_help);
rz_warn_if_fail(cmd_print_asn1_cd);

RzCmdDesc *cmd_print_protobuf_cd = rz_cmd_desc_group_new(core->rcmd, pF_cd, "pFb", rz_cmd_print_protobuf_standard_handler, &cmd_print_protobuf_standard_help, &cmd_print_protobuf_help);
rz_warn_if_fail(cmd_print_protobuf_cd);
RzCmdDesc *cmd_print_protobuf_verbose_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_protobuf_cd, "pFbv", rz_cmd_print_protobuf_verbose_handler, &cmd_print_protobuf_verbose_help);
rz_warn_if_fail(cmd_print_protobuf_verbose_cd);

RzCmdDesc *cmd_print_pkcs7_cd = rz_cmd_desc_argv_state_new(core->rcmd, pF_cd, "pFp", RZ_OUTPUT_MODE_STANDARD | RZ_OUTPUT_MODE_JSON, rz_cmd_print_pkcs7_handler, &cmd_print_pkcs7_help);
rz_warn_if_fail(cmd_print_pkcs7_cd);

RzCmdDesc *cmd_print_x509_cd = rz_cmd_desc_argv_state_new(core->rcmd, pF_cd, "pFx", RZ_OUTPUT_MODE_STANDARD | RZ_OUTPUT_MODE_JSON, rz_cmd_print_x509_handler, &cmd_print_x509_help);
rz_warn_if_fail(cmd_print_x509_cd);

RzCmdDesc *cmd_print_axml_cd = rz_cmd_desc_argv_new(core->rcmd, pF_cd, "pFA", rz_cmd_print_axml_handler, &cmd_print_axml_help);
rz_warn_if_fail(cmd_print_axml_cd);

RzCmdDesc *cmd_print_gadget_cd = rz_cmd_desc_group_new(core->rcmd, cmd_print_cd, "pg", rz_cmd_print_gadget_add_handler, &cmd_print_gadget_add_help, &cmd_print_gadget_help);
rz_warn_if_fail(cmd_print_gadget_cd);
RzCmdDesc *cmd_print_gadget_print_as_rizin_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_gadget_cd, "pg*", rz_cmd_print_gadget_print_as_rizin_handler, &cmd_print_gadget_print_as_rizin_help);
Expand Down
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 @@ -853,6 +853,12 @@ RZ_IPI RzCmdStatus rz_cmd_disassemble_recursively_no_function_handler(RzCore *co
RZ_IPI RzCmdStatus rz_cmd_disassemble_summarize_n_bytes_handler(RzCore *core, int argc, const char **argv);
RZ_IPI RzCmdStatus rz_cmd_disassemble_summarize_function_handler(RzCore *core, int argc, const char **argv);
RZ_IPI RzCmdStatus rz_cmd_disassemble_summarize_block_handler(RzCore *core, int argc, const char **argv);
RZ_IPI RzCmdStatus rz_cmd_print_asn1_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode);
RZ_IPI RzCmdStatus rz_cmd_print_protobuf_standard_handler(RzCore *core, int argc, const char **argv);
RZ_IPI RzCmdStatus rz_cmd_print_protobuf_verbose_handler(RzCore *core, int argc, const char **argv);
RZ_IPI RzCmdStatus rz_cmd_print_pkcs7_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state);
RZ_IPI RzCmdStatus rz_cmd_print_x509_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state);
RZ_IPI RzCmdStatus rz_cmd_print_axml_handler(RzCore *core, int argc, const char **argv);
RZ_IPI RzCmdStatus rz_cmd_print_gadget_add_handler(RzCore *core, int argc, const char **argv);
RZ_IPI RzCmdStatus rz_cmd_print_gadget_print_as_rizin_handler(RzCore *core, int argc, const char **argv);
RZ_IPI RzCmdStatus rz_cmd_print_gadget_remove_handler(RzCore *core, int argc, const char **argv);
Expand Down
44 changes: 43 additions & 1 deletion librz/core/cmd_descs/cmd_print.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,48 @@ commands:
summary: Summarize current block
cname: cmd_disassemble_summarize_block
args: []
- name: pF
summary: Print parsed ASN.1, PKCS, X509, ProtoBuf, AXML, etc.. formats
subcommands:
- name: pFa
summary: Decode ASN.1 from current block
cname: cmd_print_asn1
modes:
- RZ_OUTPUT_MODE_STANDARD
- RZ_OUTPUT_MODE_QUIET
args: []
- name: pFb
summary: Decode raw protobuf from current block
cname: cmd_print_protobuf
subcommands:
- name: pFb
summary: Decode raw protobuf from current block
cname: cmd_print_protobuf_standard
args: []
- name: pFbv
summary: Decode raw protobuf from current block (verbose)
cname: cmd_print_protobuf_verbose
args: []
- name: pFp
summary: Decode PKCS7 from current block
cname: cmd_print_pkcs7
type: RZ_CMD_DESC_TYPE_ARGV_STATE
modes:
- RZ_OUTPUT_MODE_STANDARD
- RZ_OUTPUT_MODE_JSON
args: []
- name: pFx
summary: Decode X.509 from current block
cname: cmd_print_x509
type: RZ_CMD_DESC_TYPE_ARGV_STATE
modes:
- RZ_OUTPUT_MODE_STANDARD
- RZ_OUTPUT_MODE_JSON
args: []
- name: pFA
summary: Decode Android Binary XML from current block
cname: cmd_print_axml
args: []
- name: pg
summary: Print gadgets
cname: cmd_print_gadget
Expand Down Expand Up @@ -816,4 +858,4 @@ commands:
cname: cmd_base64_decode
modes:
- RZ_OUTPUT_MODE_STANDARD
args: []
args: []
2 changes: 1 addition & 1 deletion librz/include/rz_util/rz_pkcs7.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ typedef struct rz_cms_indirect_data_content_t {
RZ_API RZ_OWN RzCMS *rz_pkcs7_cms_parse(RZ_NULLABLE const ut8 *buffer, ut32 length);
RZ_API void rz_pkcs7_cms_free(RZ_NULLABLE RzCMS *container);
RZ_API RZ_OWN char *rz_pkcs7_cms_to_string(RZ_NULLABLE RzCMS *container);
RZ_API RZ_OWN PJ *rz_pkcs7_cms_json(RZ_NULLABLE RzCMS *container);
RZ_API void rz_pkcs7_cms_json(RZ_NULLABLE RzCMS *container, RZ_NONNULL PJ *pj);
RZ_API RZ_OWN RzSpcIndirectDataContent *rz_pkcs7_spcinfo_parse(RZ_NONNULL RzCMS *cms);
RZ_API void rz_pkcs7_spcinfo_free(RZ_NULLABLE RzSpcIndirectDataContent *spcinfo);

Expand Down
Loading

0 comments on commit 24503df

Please sign in to comment.