Fix w6e and w6d, Add w6x, p6[e|d][s|z] + tests ##print

* encode, decode, hex buffers, argument strings null terminated strings, ..
This commit is contained in:
pancake 2022-08-23 20:05:06 +02:00
parent 85b63cd6a6
commit 89a6b0eb83
6 changed files with 182 additions and 41 deletions

View File

@ -95,9 +95,13 @@ static const char *help_msg_pc[] = {
};
static const char *help_msg_p6[] = {
"Usage: p6[de]", "[len]", "base64 decoding/encoding",
"p6d", "[len]", "decode base64",
"p6e", "[len]", "encode base64",
"Usage: p6[d|e][s|z]", " [len]", "base64 decoding/encoding",
"p6d", "[s|z] [len]", "decode current block as base64",
"p6e", "[s|z][len]", "encode current block in base64",
"p6ez", "", "encode base64 zero-terminated string",
"p6es", " hello world", "encode given string to base64",
"p6ds", " AAA=", "decode given base64 string",
"p6dz", "", "decode null-terminated base64 string in block",
NULL
};
@ -1135,7 +1139,7 @@ static int cmd_pdu(RCore *core, const char *input) {
if (buf) {
r_io_read_at (core->io, addr, buf, len);
} else {
eprintf ("Cannot allocate %d byte(s)\n", len);
R_LOG_ERROR ("Cannot allocate %d byte(s)", len);
return 1;
}
@ -1150,15 +1154,15 @@ static int cmd_pdu(RCore *core, const char *input) {
ut64 to = r_num_get (core->num, arg);
if (!to) {
eprintf ("Couldn't parse address \"%s\"\n", arg);
R_LOG_ERROR ("Couldn't parse address \"%s\"", arg);
ret = 1;
break;
} else if (to < addr) {
eprintf ("Can't print until an earlier address\n");
R_LOG_ERROR ("Can't print until an earlier address");
ret = 2;
break;
} else if (to == addr) {
eprintf ("Can't print until the start address\n");
R_LOG_ERROR ("Can't print until the start address");
ret = 2;
break;
}
@ -1236,7 +1240,7 @@ static void cmd_pDj(RCore *core, const char *arg) {
r_core_print_disasm_json (core, core->offset, buf, bsize, 0, pj);
free (buf);
} else {
eprintf ("Cannot allocate %d byte(s)\n", bsize);
R_LOG_ERROR ("Cannot allocate %d byte(s)", bsize);
}
pj_end (pj);
r_cons_println (pj_string (pj));
@ -1299,7 +1303,7 @@ static void cmd_print_fromage(RCore *core, const char *input, const ut8* data, i
free (res);
}
} else {
eprintf ("Malformed object: did you supply enough data?\ntry to change the block size (see b?)\n");
R_LOG_ERROR ("Malformed object: did you supply enough data?\ntry to change the block size (see b?)");
}
}
break;
@ -1326,7 +1330,7 @@ static void cmd_print_fromage(RCore *core, const char *input, const ut8* data, i
}
r_x509_free_certificate (x509);
} else {
eprintf ("Malformed object: did you supply enough data?\ntry to change the block size (see b?)\n");
R_LOG_ERROR ("Malformed object: did you supply enough data?\ntry to change the block size (see b?)");
}
}
break;
@ -1341,7 +1345,7 @@ static void cmd_print_fromage(RCore *core, const char *input, const ut8* data, i
}
r_pkcs7_free_cms (cms);
} else {
eprintf ("Malformed object: did you supply enough data?\ntry to change the block size (see b?)\n");
R_LOG_ERROR ("Malformed object: did you supply enough data?\ntry to change the block size (see b?)");
}
}
break;
@ -1361,7 +1365,7 @@ static void cmd_print_fromage(RCore *core, const char *input, const ut8* data, i
r_cons_printf ("%s", s);
free (s);
} else {
eprintf ("Malformed object: did you supply enough data?\ntry to change the block size (see b?)\n");
R_LOG_ERROR ("Malformed object: did you supply enough data?\ntry to change the block size (see b?)");
}
}
break;
@ -1421,7 +1425,7 @@ static void cmd_print_gadget(RCore *core, const char *_input) {
r_cons_printf ("\"pg %d %d %d %d %s\"\n", g->x, g->y, g->w, g->h, g->cmd);
}
} else if (*_input == 'b') { // "pgb"
eprintf ("TODO: Change gadget background color\n");
R_LOG_INFO ("TODO: Change gadget background color");
} else if (*_input == 'm') { // "pgm"
int nth = atoi (_input + 1);
RCoreGadget *g = r_list_get_n (core->gadgets, nth);
@ -1759,7 +1763,7 @@ static void cmd_print_format(RCore *core, const char *_input, const ut8* block,
if (val) {
r_cons_printf ("%s\n", val);
} else {
eprintf ("Struct %s is not defined\n", _input);
R_LOG_ERROR ("Struct %s is not defined", _input);
}
}
} else {
@ -7700,7 +7704,7 @@ static int cmd_print(void *data, const char *input) {
}
break;
case '6': // "p6"
if (l) {
if (1) {
int malen = (core->blocksize * 4) + 1;
ut8 *buf = malloc (malen);
if (!buf) {
@ -7709,20 +7713,84 @@ static int cmd_print(void *data, const char *input) {
memset (buf, 0, malen);
switch (input[1]) {
case 'd': // "p6d"
if (r_base64_decode (buf, (const char *) block, len)) {
r_cons_println ((const char *) buf);
} else {
R_LOG_ERROR ("r_base64_decode: invalid stream");
switch (input[2]) {
case '?':
r_core_cmd_help_match (core, help_msg_p6, "p6d", true);
break;
case 's': // "p6ds"
if (input[3] == '?') {
r_core_cmd_help_match (core, help_msg_p6, "p6ds", true);
} else {
char *a = r_str_trim_dup (input + 3);
char *out = malloc ((4 + strlen (a)) * 4);
if (r_base64_decode ((ut8 *)out,(const char *) a, strlen (a))) {
r_cons_println ((const char *) out);
} else {
R_LOG_ERROR ("r_base64_decode: invalid stream");
}
free (a);
free (out);
}
break;
case 'z': // "p6dz"
if (input[3] == '?') {
r_core_cmd_help_match (core, help_msg_p6, "p6dz", true);
} else {
len = r_str_nlen ((const char *)block, len);
if (r_base64_decode (buf, (const char *) block, len)) {
r_cons_println ((const char *) buf);
} else {
R_LOG_ERROR ("r_base64_decode: invalid stream");
}
r_cons_println ((const char *) buf);
}
break;
default:
len = len > core->blocksize? core->blocksize: len;
if (r_base64_decode (buf, (const char *) block, len)) {
r_cons_println ((const char *) buf);
} else {
R_LOG_ERROR ("r_base64_decode: invalid stream");
}
break;
}
break;
case 'e': // "p6e"
switch (input[2]) {
case '?':
r_core_cmd_help_match (core, help_msg_p6, "p6e", true);
break;
case 's': // "p6es"
if (input[3] == '?') {
r_core_cmd_help_match (core, help_msg_p6, "p6es", true);
} else {
char *a = r_str_trim_dup (input + 3);
char *out = calloc ((4 + strlen (a)), 4);
r_base64_encode ((char *) out, (const ut8*)a, strlen (a));
r_cons_println ((const char *) out);
free (a);
free (out);
}
break;
case 'z': // "p6ez"
if (input[3] == '?') {
r_core_cmd_help_match (core, help_msg_p6, "p6ez", true);
} else {
len = r_str_nlen ((const char *)block, len);
r_base64_encode ((char *) buf, block, len);
r_cons_println ((const char *) buf);
}
break;
default:
len = len > core->blocksize? core->blocksize: len;
r_base64_encode ((char *) buf, block, len);
r_cons_println ((const char *) buf);
break;
}
break;
case '?':
default:
r_core_cmd_help(core, help_msg_p6);
r_core_cmd_help (core, help_msg_p6);
break;
}
free (buf);

View File

@ -8,7 +8,7 @@ static const char *help_msg_w[] = {
"w"," foobar","write string 'foobar'",
"w+","string","write string and seek at the end of it",
"w0"," [len]","write 'len' bytes with value 0x00",
"w6","[de] base64/hex","write base64 [d]ecoded or [e]ncoded string",
"w6","[d|e|x] base64/string/hex","write base64 [d]ecoded or [e]ncoded string",
"wa","[?] push ebp","write opcode, separated by ';' (use '\"' around the command)",
"waf"," f.asm","assemble file and write bytes",
"waF"," f.asm","assemble file and write bytes and show 'wx' op with hexpair bytes of assembled code",
@ -918,7 +918,9 @@ static int cmd_w6(void *data, const char *input) {
int len = 0, str_len;
if (input[0] && input[1] != ' ') {
fail = true;
if (input[0] != 'e' && input[0] != 'd') {
fail = true;
}
}
const char *str = (input[0] && input[1] && input[2])? input + 2: "";
str_len = strlen (str) + 1;
@ -926,16 +928,16 @@ static int cmd_w6(void *data, const char *input) {
switch (input[0]) {
case 'd': // "w6d"
buf = malloc (str_len);
if (!buf) {
break;
}
len = r_base64_decode (buf, str, -1);
if (len < 0) {
free (buf);
fail = true;
if (buf) {
len = r_base64_decode (buf, str, -1);
if (len < 0) {
R_LOG_WARN ("Invalid hexpair string");
R_FREE (buf);
fail = true;
}
}
break;
case 'e': { // "w6e"
case 'x': { // "w6x"
ut8 *bin_buf = malloc (str_len);
if (!bin_buf) {
break;
@ -947,11 +949,28 @@ static int cmd_w6(void *data, const char *input) {
buf = calloc (str_len + 1, 4);
len = r_base64_encode ((char *)buf, bin_buf, bin_len);
if (len == 0) {
free (buf);
R_FREE (buf);
fail = true;
}
}
free (bin_buf);
}
break;
case 'e': { // "w6e"
ut8 *bin_buf = malloc (str_len);
if (!bin_buf) {
break;
}
char *s = r_str_trim_dup (input + 1);
int slen = strlen (s);
free (buf);
buf = malloc ((4+slen) * 4);
len = r_base64_encode ((char *)buf, (const ut8*)s, slen);
if (len == 0) {
R_FREE (buf);
fail = true;
}
free (bin_buf);
break;
}
default:
@ -967,7 +986,7 @@ static int cmd_w6(void *data, const char *input) {
r_core_block_read (core);
free (buf);
} else {
eprintf ("Usage: w6[de] base64/hex\n");
eprintf ("Usage: w6[d|e|x] base64/string/hex\n");
}
return 0;
}

View File

@ -464,7 +464,7 @@ NAME=pf? struct not defined
FILE=-
CMDS=pf?cat_sat_on_keyboard
EXPECT_ERR=<<EOF
Struct cat_sat_on_keyboard is not defined
ERROR: Struct cat_sat_on_keyboard is not defined
EOF
RUN

View File

@ -123,12 +123,12 @@ EXPECT=<<EOF
EOF
RUN
NAME=w6
NAME=w6x
FILE=-
CMDS=<<EOF
w6d ESIz
p8 3
w6e 112233
w6x 112233
ps
EOF
EXPECT=<<EOF
@ -137,6 +137,22 @@ ESIz
EOF
RUN
NAME=w6e
FILE=-
CMDS=<<EOF
w6d ESIz
p8 3
w6x 112233
psz
p6e 3
EOF
EXPECT=<<EOF
112233
ESIz
RVNJ
EOF
RUN
NAME=wh
FILE=-
CMDS=<<EOF

View File

@ -21,3 +21,42 @@ EXPECT=<<EOF
hello world
EOF
RUN
NAME=both bsae64
FILE=-
CMDS=<<EOF
p6ds `p6es hello world`
EOF
EXPECT=<<EOF
hello world
EOF
RUN
NAME=base64 zero-terminated
FILE=-
CMDS=<<EOF
b 16
w `p6es hello world`
p6d
p6dz
EOF
EXPECT=<<EOF
hello world
hello world
hello world
EOF
RUN
NAME=w6e/w6d
FILE=-
CMDS=<<EOF
w6e hello world
psz
w6d `psz`
psz
EOF
EXPECT=<<EOF
aGVsbG8gd29ybGQ=
hello worldybGQ=
EOF
RUN

View File

@ -5,8 +5,8 @@ pf?cat_sat_on_keyboard
pf?CAT_SAT_ON_KEYBOARD
EOF
EXPECT_ERR=<<EOF
Struct cat_sat_on_keyboard is not defined
Struct CAT_SAT_ON_KEYBOARD is not defined
ERROR: Struct cat_sat_on_keyboard is not defined
ERROR: Struct CAT_SAT_ON_KEYBOARD is not defined
EOF
RUN
@ -16,11 +16,10 @@ CMDS=<<EOF
pf?cat_sat_on_keyboard
pf?CAT_SAT_ON_KEYBOARD
EOF
EXPECT=<<EOF
EOF
EXPECT=
EXPECT_ERR=<<EOF
Struct cat_sat_on_keyboard is not defined
Struct CAT_SAT_ON_KEYBOARD is not defined
ERROR: Struct cat_sat_on_keyboard is not defined
ERROR: Struct CAT_SAT_ON_KEYBOARD is not defined
EOF
RUN