Add ragg2 -n and -N and fix some bugs in r_buf and r_egg

This commit is contained in:
pancake 2014-10-29 02:44:21 +01:00
parent 8aa76e644c
commit 69b40d1afa
4 changed files with 40 additions and 8 deletions

View File

@ -28,6 +28,8 @@ static int usage (int v) {
" -p [padding] add padding after compilation (padding=n10s32)\n"
" ntas : begin nop, trap, 'a', sequence\n"
" NTAS : same as above, but at the end\n"
" -n [dword] append 32bit number (4 bytes)\n"
" -N [dword] append 64bit number (8 bytes)\n"
" -P [size] prepend debrujn pattern\n"
" -s show assembler\n"
" -r show raw bytes instead of hexpairs\n"
@ -99,6 +101,7 @@ int main(int argc, char **argv) {
int show_hex = 1;
int show_asm = 0;
int show_raw = 0;
int append = 0;
int show_str = 0;
char *shellcode = NULL;
char *encoder = NULL;
@ -110,7 +113,9 @@ int main(int argc, char **argv) {
int c, i;
REgg *egg = r_egg_new ();
while ((c = getopt (argc, argv, "he:a:b:f:o:sxrk:FOI:Li:c:p:P:B:C:vd:D:w:z")) != -1) {
//egg->bin = r_buf_new ();
while ((c = getopt (argc, argv, "n:N:he:a:b:f:o:sxrk:FOI:Li:c:p:P:B:C:vd:D:w:z")) != -1) {
switch (c) {
case 'a':
arch = optarg;
@ -144,6 +149,16 @@ int main(int argc, char **argv) {
} else eprintf ("Missing colon in -w\n");
}
break;
case 'n': {
ut32 n = r_num_math (NULL, optarg);
append = 1;
r_egg_patch (egg, -1, (const ut8*)&n, 4);
} break;
case 'N': {
ut64 n = r_num_math (NULL, optarg);
r_egg_patch (egg, -1, (const ut8*)&n, 8);
append = 1;
} break;
case 'd':
{
ut32 off, n;
@ -241,7 +256,7 @@ int main(int argc, char **argv) {
}
}
if (optind == argc && !shellcode && !bytes && !contents && !encoder && !padding && !pattern) {
if (optind == argc && !shellcode && !bytes && !contents && !encoder && !padding && !pattern && !append) {
return usage (0);
} else file = argv[optind];
@ -345,6 +360,9 @@ int main(int argc, char **argv) {
if (pattern)
r_egg_pattern (egg, r_num_math (NULL, pattern));
if (!egg->bin) {
egg->bin = r_buf_new ();
}
if (!(b = r_egg_get_bin (egg))) {
eprintf ("r_egg_get_bin: invalid egg :(\n");
@ -353,7 +371,7 @@ int main(int argc, char **argv) {
r_egg_finalize (egg); // apply patches
if (show_execute)
return r_egg_run (egg);
b = r_egg_get_bin (egg);
if (show_raw) {
write (1, b->buf, b->length);
} else {

View File

@ -412,12 +412,19 @@ R_API int r_egg_patch(REgg *egg, int off, const ut8 *buf, int len) {
R_API void r_egg_finalize(REgg *egg) {
RBuffer *b;
RListIter *iter;
if (!egg->bin->buf)
egg->bin = r_buf_new ();
r_list_foreach (egg->patches, iter, b) {
if (b->length+b->cur > egg->bin->length) {
eprintf ("Fuck this shit. Cant patch outside\n");
if (b->cur <0) {
r_buf_append_bytes (egg->bin, b->buf, b->length);
} else {
// TODO: use r_buf_cpy_buf or what
if (b->length+b->cur > egg->bin->length) {
eprintf ("Fuck this shit. Cant patch outside\n");
return;
}
memcpy (egg->bin->buf + b->cur, b->buf, b->length);
}
// TODO: use r_buf_cpy_buf or what
memcpy (egg->bin->buf + b->cur, b->buf, b->length);
}
}

View File

@ -106,8 +106,9 @@ R_API char *r_buf_to_string(RBuffer *b) {
R_API int r_buf_append_bytes(RBuffer *b, const ut8 *buf, int length) {
if (!b) return R_FALSE;
if (b->empty) b->length = b->empty = 0;
if (!(b->buf = realloc (b->buf, b->length+length)))
if (!(b->buf = realloc (b->buf, b->length+length))) {
return R_FALSE;
}
memmove (b->buf+b->length, buf, length);
b->length += length;
return R_TRUE;

View File

@ -16,6 +16,8 @@
.Op Fl B Ar hexpairs
.Op Fl c Ar k=v
.Op Fl C Ar file
.Op Fl n Ar num32
.Op Fl N Ar num64
.Op Fl d Ar off:dword
.Op Fl D Ar off:qword
.Op Fl w Ar off:hexpair
@ -62,6 +64,10 @@ Patch final buffer with given dword at specified offset
Patch final buffer with given qword at specified offset
.It Fl w Ar off:hexpairs
Patch final buffer with given hexpairs at specified offset
.It Fl n Ar num32
Append a 32bit number in little endian
.It Fl N Ar num64
Append a 64bit number in little endian
.It Fl p Ar padding
Specify generic paddings with a format string.
.It Fl P Ar size