Example #1
0
// Finds the offset of a given value in a cyclic pattern of an integer.
R_API int r_debruijn_offset(ut64 value, bool is_big_endian) {
	char* needle, *pattern, buf[9];
	int retval = -1;
	char* pch;
	// 0x10000 should be long enough. This is how peda works, and nobody complains
	// ... but is slow. Optimize for common case.
	int lens[2] = {0x1000, 0x10000};
	int j;

	if (value == 0) {
		return -1;
	}

	for (j = 0; j <= 2 && retval == -1; ++j) {
		pattern = r_debruijn_pattern (lens[j], 0, debruijn_charset);

		buf[8] = '\0';
		if (is_big_endian) {
			r_write_be64 (buf, value);
		} else {
			r_write_le64 (buf, value);
		}
		for (needle = buf; !*needle; needle++) {
			/* do nothing here */
		}

		pch = strstr (pattern, needle);

		if (pch) {
			retval = (int)(size_t)(pch - pattern);
		}
		free (pattern);
	}
	return retval;
}
Example #2
0
static void cmd_write_op (RCore *core, const char *input) {
	ut8 *buf;
	int len;
	int value;
	if (!input[0])
		return;
	switch (input[1]) {
	case 'e':
		if (input[2]!=' ') {
			r_cons_printf ("Usage: 'woe from-to step'\n");
			return;
		}
		/* fallthru */
	case 'a':
	case 's':
	case 'A':
	case 'x':
	case 'r':
	case 'l':
	case 'm':
	case 'd':
	case 'o':
	case 'w':
	case '2':
	case '4':
		if (input[2]) {  // parse val from arg
			r_core_write_op (core, input+3, input[1]);
			r_core_block_read (core);
		} else {  // use clipboard instead of val
			r_core_write_op (core, NULL, input[1]);
			r_core_block_read (core);
		}
		break;
	case 'R':
		r_core_cmd0 (core, "wr $b");
		break;
	case 'n':
		r_core_write_op (core, "ff", 'x');
		r_core_block_read (core);
		break;
	case 'E': // "woE" encrypt
	case 'D': // "woD" decrypt
		{
			int direction = (input[1] == 'E') ? 0 : 1;
			const char *algo = NULL;
			const char *key = NULL;
			const char *iv = NULL;
			char *space, *args = strdup (r_str_trim_ro (input+2));
			space = strchr (args, ' ');
			if (space) {
				*space++ = 0;
				key = space;
				space = strchr (key, ' ');
				if (space) {
					*space++ = 0;
					iv = space;
				}
			}
			algo = args;
			if (algo && *algo && key) {
				encrypt_or_decrypt_block (core, algo, key, direction, iv);
			} else {
				eprintf ("Usage: wo%c [algo] [key] [IV]\n", ((!direction)?'E':'D'));
				eprintf ("Currently supported hashes:\n");
				ut64 bits;
				int i;
				for (i = 0; ; i++) {
					bits = ((ut64)1) << i;
					const char *name = r_hash_name (bits);
					if (!name || !*name) break;
					printf ("  %s\n", name);
				}
				eprintf ("Available Encoders/Decoders: \n");
				// TODO: do not hardcode
				eprintf ("  base64\n");
				eprintf ("  base91\n");
				eprintf ("  punycode\n");
				eprintf ("Currently supported crypto algos:\n");
				for (i = 0; ; i++) {
					bits = ((ut64)1) << i;
					const char *name = r_crypto_name (bits);
					if (!name || !*name) break;
					printf ("  %s\n", name);
				}
			}
			free (args);
		}
		break;
	case 'p': // debrujin patterns
		switch (input[2]) {
		case 'D': // "wopD"
			len = (int)(input[3]==' ')
				? r_num_math (core->num, input + 3)
				: core->blocksize;
			if (len > 0) {
				/* XXX This seems to fail at generating long patterns (wopD 512K) */
				buf = (ut8*)r_debruijn_pattern (len, 0, NULL); //debruijn_charset);
				if (buf) {
					const ut8 *ptr = buf;
					ut64 addr = core->offset;
					if (input[3] == '*') {
						int i;
						r_cons_printf ("wx ");
						for (i = 0; i < len; i++) {
							r_cons_printf ("%02x", buf[i]);
						}
						r_cons_newline ();
					} else {
						while (true) {
							int res = r_core_write_at (core, addr, ptr, len);
							if (res < 1 || len == res) {
								break;
							}
							if (res < len) {
								ptr += res;
								len -= res;
								addr += res;
							}
						} 
					}
					free (buf);
				} else {
					eprintf ("Couldn't generate pattern of length %d\n", len);
				}
			}
			break;
		case 'O': // "wopO"
			if (strlen (input) > 4 && strncmp (input + 4, "0x", 2)) {
				eprintf ("Need hex value with `0x' prefix e.g. 0x41414142\n");
			} else if (input[3] == ' ') {
				value = r_num_get (core->num, input + 4);
				core->num->value = r_debruijn_offset (value, r_config_get_i (core->config, "cfg.bigendian"));
				r_cons_printf ("%"PFMT64d"\n", core->num->value);
			}
			break;
		case '\0':
		case '?':
		default:
			r_core_cmd_help (core, help_msg_wop);
			break;
		}
		break;
	case '\0':
	case '?':
	default:
		r_core_cmd_help (core, help_msg_wo);
		break;
	}
}
Example #3
0
static void cmd_write_op (RCore *core, const char *input) {
	ut8 *buf;
  char *sequence_err = NULL;
	int len;
	const char* help_msg[] = {
		"Usage:","wo[asmdxoArl24]"," [hexpairs] @ addr[!bsize]",
		"wow"," [val]", "==  write looped value (alias for 'wb')",
		"woa"," [val]", "+=  addition (f.ex: woa 0102)",
		"wos"," [val]", "-=  substraction",
		"wom"," [val]", "*=  multiply",
		"wod"," [val]", "/=  divide",
		"woe"," [from-to] [step]","..  create sequence",
		"wox"," [val]","^=  xor  (f.ex: wox 0x90)",
		"woo"," [val]","|=  or",
		"woA"," [val]","&=  and",
		"woR","","random bytes (alias for 'wr $b')",
		"wor"," [val]", ">>= shift right",
		"wol"," [val]","<<= shift left",
		"wo2"," [val]","2=  2 byte endian swap",
		"wo4"," [val]", "4=  4 byte endian swap",
		"woD"," [len]","De Bruijn Pattern (syntax woD length @ addr)",
		"woO"," [len]", "De Bruijn Pattern Offset (syntax: woO value)",
		NULL
	};
	if (!input[0])
		return;
	switch (input[1]) {
	case 'a':
	case 's':
	case 'e':
	case 'A':
	case 'x':
	case 'r':
	case 'l':
	case 'm':
	case 'd':
	case 'o':
	case 'w':
		if (input[2]!=' ') {
			if (input[1]=='e') r_cons_printf ("Usage: 'woe from-to step'\n");
			else r_cons_printf ("Usage: 'wo%c 00 11 22'\n", input[1]);
			return;
		}
	case '2':
	case '4':
		if (input[2]){
			r_core_write_op (core, input+3, input[1]);
			r_core_block_read (core, 0);
		} else eprintf ("Missing argument\n");
		break;
	case 'R':
		r_core_cmd0 (core, "wr $b");
		break;
	case 'n':
		r_core_write_op (core, "ff", 'x');
		r_core_block_read (core, 0);
		break;
	case 'D':
        len = strtoul (input+2, &sequence_err, 0);
        if (*sequence_err) {
          printf ("Invalid length: %s\n", sequence_err);
          break;
        }
        if (len > 0) {
          buf = (ut8*)r_debruijn_pattern (len, 0, NULL); //debruijn_charset);
          if (buf) {
              r_core_write_at (core, core->offset, buf, len);
              free (buf);
          } else {
            eprintf ("Couldn't generate pattern of length %d\n", len);
          }
        }
        break;
	case 'O':
        len = strtoul (input+2, &sequence_err, 16);
        if (*sequence_err) {
          eprintf ("Invalid sequence: %s\n", sequence_err);
          break;
        }
		core->num->value = r_debruijn_offset (len, !core->assembler->big_endian);
		r_cons_printf ("%d\n", core->num->value);
		break;
	case '\0':
	case '?':
	default:
		r_core_cmd_help (core, help_msg);
		break;
	}
}
Example #4
0
static void cmd_write_op (RCore *core, const char *input) {
	ut8 *buf;
	int len;
	const char* help_msg[] = {
		"Usage:","wo[asmdxoArl24]"," [hexpairs] @ addr[!bsize]",
		"wo[aAdlmorwx24]","", "without hexpair values, clipboard is used",
		"woa"," [val]", "+=  addition (f.ex: woa 0102)",
		"woA"," [val]","&=  and",
		"wod"," [val]", "/=  divide",
		"woD","[algo] [key]","decrypt current block with given algo and key",
		"woe"," [from to] [step] [wsz=1]","..  create sequence",
		"woE"," [algo] [key]", "encrypt current block with given algo and key",
		"wol"," [val]","<<= shift left",
		"wom"," [val]", "*=  multiply",
		"woo"," [val]","|=  or",
		"wopD"," [len]","De Bruijn Pattern (syntax wopD length @ addr)",
		"wopO"," [len]", "De Bruijn Pattern Offset (syntax: wopO value)",
		"wor"," [val]", ">>= shift right",
		"woR","","random bytes (alias for 'wr $b')",
		"wos"," [val]", "-=  substraction",
		"wow"," [val]", "==  write looped value (alias for 'wb')",
		"wox"," [val]","^=  xor  (f.ex: wox 0x90)",
		"wo2"," [val]","2=  2 byte endian swap",
		"wo4"," [val]", "4=  4 byte endian swap",
		NULL
	};
	if (!input[0])
		return;
	switch (input[1]) {
	case 'e':
		if (input[2]!=' ') {
			r_cons_printf ("Usage: 'woe from-to step'\n");
			return;
		}
		/* fallthru */
	case 'a':
	case 's':
	case 'A':
	case 'x':
	case 'r':
	case 'l':
	case 'm':
	case 'd':
	case 'o':
	case 'w':
	case '2':
	case '4':
		if (input[2]) {  // parse val from arg
			r_core_write_op (core, input+3, input[1]);
			r_core_block_read (core, 0);
		} else {  // use clipboard instead of val
			r_core_write_op (core, NULL, input[1]);
			r_core_block_read (core, 0);
		}
		break;
	case 'R':
		r_core_cmd0 (core, "wr $b");
		break;
	case 'n':
		r_core_write_op (core, "ff", 'x');
		r_core_block_read (core, 0);
		break;
	case 'E': // encrypt
	case 'D': // decrypt
		{
			int direction = (input[1] == 'E') ? 0 : 1;
			const char *algo = NULL;
			const char *key = NULL;
			char *space, *args = strdup (r_str_chop_ro (input+2));
			space = strchr (args, ' ');
			if (space) {
				*space++ = 0;
				key = space;
			}
			algo = args;
			if (algo && *algo) {
				encrypt_or_decrypt_block (core, algo, key, direction);
			} else {
				eprintf ("Usage: wo%c [algo] [key]\n", ((!direction)?'E':'D'));
				eprintf ("TODO: list currently supported crypto algorithms\n");
				eprintf ("  rc2, rc4, xor, blowfish, aes, rot, ror, rol\n");
			}
			free (args);
		}
		break;
	case 'p': // debrujin patterns
		switch (input[2]) {
		case 'D':
			len = (int)(input[3]==' ')?
				r_num_math (core->num, input + 3): core->blocksize;
			if (len > 0) {
				buf = (ut8*)r_debruijn_pattern (len, 0, NULL); //debruijn_charset);
				if (buf) {
					r_core_write_at (core, core->offset, buf, len);
					free (buf);
				} else {
					eprintf ("Couldn't generate pattern of length %d\n", len);
				}
			}
			break;
		case 'O':
			len = (int)(input[3]==' ')?
				r_num_math (core->num, input + 3): core->blocksize;
			core->num->value = r_debruijn_offset (len, !core->assembler->big_endian);
			r_cons_printf ("%"PFMT64d"\n", core->num->value);
			break;
		default:
			eprintf ("Invalid arguments for wop\n");
			break;
		}
		break;
	case '\0':
	case '?':
	default:
		r_core_cmd_help (core, help_msg);
		break;
	}
}
Example #5
0
static void cmd_write_op (RCore *core, const char *input) {
	ut8 *buf;
	int len;
	const char* help_msg[] = {
		"Usage:","wo[asmdxoArl24]"," [hexpairs] @ addr[!bsize]",
		"wo[aAdlmorwx24]","", "without hexpair values, clipboard is used",
		"woa"," [val]", "+=  addition (f.ex: woa 0102)",
		"woA"," [val]","&=  and",
		"wod"," [val]", "/=  divide",
		"woD","[algo] [key] [IV]","decrypt current block with given algo and key",
		"woe"," [from to] [step] [wsz=1]","..  create sequence",
		"woE"," [algo] [key] [IV]", "encrypt current block with given algo and key",
		"wol"," [val]","<<= shift left",
		"wom"," [val]", "*=  multiply",
		"woo"," [val]","|=  or",
		"wop[DO]"," [arg]","De Bruijn Patterns",
		"wor"," [val]", ">>= shift right",
		"woR","","random bytes (alias for 'wr $b')",
		"wos"," [val]", "-=  substraction",
		"wow"," [val]", "==  write looped value (alias for 'wb')",
		"wox"," [val]","^=  xor  (f.ex: wox 0x90)",
		"wo2"," [val]","2=  2 byte endian swap",
		"wo4"," [val]", "4=  4 byte endian swap",
		NULL
	};
	if (!input[0])
		return;
	switch (input[1]) {
	case 'e':
		if (input[2]!=' ') {
			r_cons_printf ("Usage: 'woe from-to step'\n");
			return;
		}
		/* fallthru */
	case 'a':
	case 's':
	case 'A':
	case 'x':
	case 'r':
	case 'l':
	case 'm':
	case 'd':
	case 'o':
	case 'w':
	case '2':
	case '4':
		if (input[2]) {  // parse val from arg
			r_core_write_op (core, input+3, input[1]);
			r_core_block_read (core);
		} else {  // use clipboard instead of val
			r_core_write_op (core, NULL, input[1]);
			r_core_block_read (core);
		}
		break;
	case 'R':
		r_core_cmd0 (core, "wr $b");
		break;
	case 'n':
		r_core_write_op (core, "ff", 'x');
		r_core_block_read (core);
		break;
	case 'E': // encrypt
	case 'D': // decrypt
		{
			int direction = (input[1] == 'E') ? 0 : 1;
			const char *algo = NULL;
			const char *key = NULL;
			const char *iv = NULL;
			char *space, *args = strdup (r_str_chop_ro (input+2));
			space = strchr (args, ' ');
			if (space) {
				*space++ = 0;
				key = space;
				space = strchr (key, ' ');
				if (space) {
					*space++ = 0;
					iv = space;
				}
			}
			algo = args;
			if (algo && *algo) {
				encrypt_or_decrypt_block (core, algo, key, direction, iv);
			} else {
				eprintf ("Usage: wo%c [algo] [key] [IV]\n", ((!direction)?'E':'D'));
				eprintf ("Currently supported hashes:\n");
				ut64 bits;
				int i;
				for (i = 0; ; i++) {
					bits = ((ut64)1) << i;
					const char *name = r_hash_name (bits);
					if (!name || !*name) break;
					printf ("  %s\n", name);
				}
				eprintf ("Available Encoders/Decoders: \n");
				// TODO: do not hardcode
				eprintf ("  base64\n");
				eprintf ("  base91\n");
				eprintf ("  punycode\n");
				eprintf ("Currently supported crypto algos:\n");
				for (i = 0; ; i++) {
					bits = ((ut64)1) << i;
					const char *name = r_crypto_name (bits);
					if (!name || !*name) break;
					printf ("  %s\n", name);
				}
			}
			free (args);
		}
		break;
	case 'p': // debrujin patterns
		switch (input[2]) {
		case 'D': // "wopD"
			len = (int)(input[3]==' ')
				? r_num_math (core->num, input + 3)
				: core->blocksize;
			if (len > 0) {
				/* XXX This seems to fail at generating long patterns (wopD 512K) */
				buf = (ut8*)r_debruijn_pattern (len, 0, NULL); //debruijn_charset);
				if (buf) {
					const ut8 *ptr = buf;
					ut64 addr = core->offset;
					while (true) {
						int res = r_core_write_at (core, addr, ptr, len);
						if (res < 1 || len == res) {
							break;
						}
						if (res < len) {
							ptr += res;
							len -= res;
							addr += res;
						}
					} 
					free (buf);
				} else {
					eprintf ("Couldn't generate pattern of length %d\n", len);
				}
			}
			break;
		case 'O': // "wopO"
			len = (int)(input[3]==' ')
				? r_num_math (core->num, input + 3)
				: core->blocksize;
			core->num->value = r_debruijn_offset (len, r_config_get_i (core->config, "cfg.bigendian"));
			r_cons_printf ("%"PFMT64d"\n", core->num->value);
			break;
		case '\0':
		case '?':
		default:
			{
				const char* wop_help_msg[] = {
					"Usage:","wop[DO]"," len @ addr | value",
					"wopD"," len [@ addr]","Write a De Bruijn Pattern of length 'len' at address 'addr'",
					"wopO"," value", "Finds the given value into a De Bruijn Pattern at current offset",
					NULL
				};
				r_core_cmd_help (core, wop_help_msg);
				break;
			}
		}
		break;
	case '\0':
	case '?':
	default:
		r_core_cmd_help (core, help_msg);
		break;
	}
}