Few more cleanups for rahash2 and add the strhash crypto plugin

This commit is contained in:
pancake 2024-03-21 11:16:16 +01:00 committed by GitHub
parent 44b6a981a3
commit 757cbcef2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 122 additions and 54 deletions

View File

@ -17,7 +17,7 @@ flags.
### radare2
```
```sh
r2 - # same as r2 malloc://4096; "the playground"
r2 /bin/ls # standard way to run r2
r2 -w ls # open in read-write
@ -26,7 +26,7 @@ r2 -d ls # start debugging the ls in PATH
### rasm2
```
```sh
rasm2 -L # list all supported assembler/disassembler/emulator plugins
rasm2 -a arm -b 64 'nop' # assemble a nop in 64-bit ARM
rasm2 -d 90 # disassemble 0x90; nop, if you're using x86
@ -34,13 +34,20 @@ rasm2 -d 90 # disassemble 0x90; nop, if you're using x86
### rabin2
```
```sh
rabin2 -s /bin/ls # list symbols in a binary
rabin2 -z /bin/ls # find strings
```
### rax2
### rafind2
```sh
rafind2 -s lib /bin/ls # search for strings matching 'lib' in /bin/ls
```
### rax2
```sh
rax2 '10+0x20' # compute the result
rax2 -k 10+32 # keep the same base as input (10)
rax2 -h # convert between (hex, octal, decimal.. bases)

View File

@ -186,6 +186,7 @@ crypto.rot
crypto.serpent
crypto.sm4
crypto.xor
crypto.strhash
debug.bf
debug.evm
debug.rv32ima

View File

@ -105,7 +105,7 @@ R_API bool r_crypto_job_set_key(RCryptoJob *cj, const ut8* key, int keylen, int
keylen = strlen ((const char *)key);
}
if (!cj->h || !cj->h->set_key) {
return false;
return true;
}
cj->key_len = keylen;
cj->key = calloc (1, cj->key_len);
@ -222,7 +222,7 @@ R_API void r_crypto_list(RCrypto *cry, PrintfCallback cb_printf, int mode) {
pj_ks (pj, "type", "hash");
pj_ks (pj, "name", cp->meta.name);
switch (cp->type) {
case R_CRYPTO_TYPE_HASH:
case R_CRYPTO_TYPE_HASHER:
pj_ks (pj, "type", "hash");
break;
case R_CRYPTO_TYPE_ENCRYPT:

View File

@ -53,7 +53,7 @@ RCryptoPlugin r_crypto_plugin_entropy = {
.author = "pancake",
.license = "MIT",
},
.type = R_CRYPTO_TYPE_HASH,
.type = R_CRYPTO_TYPE_HASHER,
.update = update,
.end = end
};

View File

@ -51,7 +51,7 @@ static bool ror_check(const char *algo) {
}
static bool update(RCryptoJob *cj, const ut8 *buf, int len) {
if (!cj->flag) {
if (cj->flag != R_CRYPTO_DIR_ENCRYPT) {
return false;
}
ut8 *obuf = calloc (1, len);

View File

@ -53,7 +53,7 @@ static void rot_decrypt(ut8 key, const ut8 *inbuf, ut8 *outbuf, int buflen) {
}
static bool rot_set_key(RCryptoJob *cj, const ut8 *key, int keylen, int mode, int direction) {
cj->flag = direction;
cj->flag = direction == R_CRYPTO_DIR_ENCRYPT;
return rot_init (&cj->rot_key, key, keylen);
}
@ -67,10 +67,13 @@ static bool update(RCryptoJob *cj, const ut8 *buf, int len) {
if (!obuf) {
return false;
}
if (cj->flag == 0) {
switch (cj->flag) {
case R_CRYPTO_DIR_ENCRYPT:
rot_crypt (cj->rot_key, buf, obuf, len);
} else if (cj->flag == 1) {
break;
case R_CRYPTO_DIR_DECRYPT:
rot_decrypt (cj->rot_key, buf, obuf, len);
break;
}
r_crypto_job_append (cj, obuf, len);
free (obuf);

View File

@ -0,0 +1,35 @@
/* radare - LGPL - Copyright 2024 - pancake */
#include <r_lib.h>
#include <r_crypto.h>
static bool update(RCryptoJob *cj, const ut8 *buf, int len) {
char *s = r_str_ndup ((const char *)buf, len);
int n = r_str_hash (s);
free (s);
cj->output = malloc (4);
r_write_ble32 (cj->output, n, cj->c->bigendian);
eprintf ("0x%x\n", n);
cj->output_len = 4;
return true;
}
RCryptoPlugin r_crypto_plugin_strhash = {
.meta = {
.name = "strhash",
.author = "pancake",
.license = "MIT",
},
.type = R_CRYPTO_TYPE_HASHER,
.implements = "strhash",
.update = update,
.end = update
};
#ifndef R2_PLUGIN_INCORE
R_API RLibStruct radare_plugin = {
.type = R_LIB_TYPE_CRYPTO,
.data = &r_crypto_plugin_strhash,
.version = R2_VERSION
};
#endif

9
libr/crypto/p/strhash.mk Normal file
View File

@ -0,0 +1,9 @@
OBJ_STRHASH=crypto_strhash.o
STATIC_OBJ+=${OBJ_STRHASH}
TARGET_STRHASH=crypto_strhash.${EXT_SO}
ALL_TARGETS+=${TARGET_STRHASH}
${TARGET_STRHASH}: ${OBJ_STRHASH}
$(CC) $(call libname,crypto_strhash) ${LDFLAGS} ${CFLAGS} -o ${TARGET_STRHASH} ${OBJ_STRHASH}

View File

@ -4,7 +4,6 @@
#define R2_CRYPTO_H
#include <r_types.h>
#include <r_list.h>
#include <r_th.h>
#include <r_crypto/r_des.h>
#include <r_hash.h>
@ -24,10 +23,12 @@ enum {
R_CRYPTO_MODE_CFB,
};
// TODO: use encode/decode wordings
// TODO: use encode/decode wordings?
enum {
R_CRYPTO_DIR_DECRYPT = 0,
R_CRYPTO_DIR_ENCRYPT = 1,
R_CRYPTO_DIR_NONE = -1,
R_CRYPTO_DIR_HASH = 0,
R_CRYPTO_DIR_DECRYPT = 1,
R_CRYPTO_DIR_ENCRYPT = 2,
};
typedef struct r_crypto_t {
@ -41,6 +42,7 @@ typedef struct r_crypto_t {
int output_size;
int dir;
#endif
bool bigendian;
void *user;
RList *plugins;
} RCrypto;
@ -66,8 +68,8 @@ typedef struct r_crypto_job_t {
typedef enum {
R_CRYPTO_TYPE_ENCODER = 'e',
R_CRYPTO_TYPE_HASH = 'h',
R_CRYPTO_TYPE_ENCRYPT = 'c',
R_CRYPTO_TYPE_HASHER = 'h',
R_CRYPTO_TYPE_ENCRYPT = 'c', // CIPHER
} RCryptoType;
typedef bool (*RCryptoJobSetIVCallback)(RCryptoJob *ci, const ut8 *iv, int ivlen);
@ -118,6 +120,7 @@ R_API ut8 *r_crypto_job_get_output(RCryptoJob *cry, int *size);
#endif
/* plugin pointers */
extern RCryptoPlugin r_crypto_plugin_strhash;
extern RCryptoPlugin r_crypto_plugin_aes;
extern RCryptoPlugin r_crypto_plugin_des;
extern RCryptoPlugin r_crypto_plugin_rc4;

View File

@ -4,7 +4,6 @@
#include <r_io.h>
#include <r_main.h>
#include <r_hash.h>
#include <r_util/r_print.h>
#include <r_crypto.h>
@ -20,6 +19,7 @@ typedef struct {
ut64 to;
RHashSeed *_s;
RHashSeed s;
const char *algorithm;
} RahashOptions;
static void compare_hashes(const RHash *ctx, RahashOptions *ro, const ut8 *compare, int length, int *ret, int rad) {
@ -364,14 +364,14 @@ static bool is_power_of_two(const ut64 x) {
return x && !(x & (x - 1));
}
static void print_result(RahashOptions *ro, const char *algo, const ut8 *result, int result_size) {
static void print_result(RahashOptions *ro, const ut8 *result, int result_size) {
int i;
switch (ro->mode) {
case 'j':
{
PJ *pj = pj_new ();
pj_o (pj);
pj_ks (pj, "algo", algo);
pj_ks (pj, "algo", ro->algorithm);
pj_ks (pj, "mode", ro->direction? "encrypt": "decrypt");
pj_ka (pj, "data");
for (i = 0; i < result_size; i++) {
@ -398,8 +398,9 @@ static void print_result(RahashOptions *ro, const char *algo, const ut8 *result,
}
}
// direction: 0 => decrypt, 1 => encrypt
static int encrypt_or_decrypt(RahashOptions *ro, const char *algo, int direction, const char *hashstr, int hashstr_len, const ut8 *iv, int ivlen, int mode) {
static int encrypt_or_decrypt(RahashOptions *ro, const char *hashstr, int hashstr_len, const ut8 *iv, int ivlen, int mode) {
const int direction = ro->direction;
const char *algo = ro->algorithm;
// TODO: generalise this for all non key encoding/decoding.
bool no_key_mode = !strcmp ("base64", algo) || !strcmp ("base91", algo) || !strcmp ("punycode", algo);
if (no_key_mode || ro->s.len > 0) {
@ -420,7 +421,7 @@ static int encrypt_or_decrypt(RahashOptions *ro, const char *algo, int direction
int result_size = 0;
ut8 *result = r_crypto_job_get_output (cj, &result_size);
if (result) {
print_result (ro, algo, result, result_size);
print_result (ro, result, result_size);
free (result);
}
} else {
@ -438,7 +439,9 @@ static int encrypt_or_decrypt(RahashOptions *ro, const char *algo, int direction
return 1;
}
static int encrypt_or_decrypt_file(RahashOptions *ro, const char *algo, int direction, const char *filename, const ut8 *iv, int ivlen, int mode) {
static int encrypt_or_decrypt_file(RahashOptions *ro, const char *filename, const ut8 *iv, int ivlen, int mode) {
const int direction = ro->direction;
const char *algo = ro->algorithm;
// TODO: generalise this for all non key encoding/decoding. aka crypto vs encoder plugins after moving all those hash algos to crypto plugins
bool no_key_mode = !strcmp ("base64", algo) || !strcmp ("base91", algo) || !strcmp ("punycode", algo);
if (no_key_mode || ro->s.len > 0) {
@ -470,7 +473,7 @@ static int encrypt_or_decrypt_file(RahashOptions *ro, const char *algo, int dire
int result_size = 0;
ut8 *result = r_crypto_job_get_output (cj, &result_size);
if (result) {
print_result (ro, algo, result, result_size);
print_result (ro, result, result_size);
free (result);
}
free (buf);
@ -490,12 +493,14 @@ static int encrypt_or_decrypt_file(RahashOptions *ro, const char *algo, int dire
}
static void add_algo(RList *algos, const char *a) {
R_RETURN_IF_FAIL (algos);
if (R_STR_ISEMPTY (a)) {
return;
}
RListIter *iter;
const char *ua;
char *ha = strdup (a);
// TODO: Use a set
RList *words = r_str_split_list (ha, ",", 0);
r_list_foreach (words, iter, ua) {
if (!r_list_find (algos, ua, (RListComparator)strcmp)) {
@ -506,6 +511,16 @@ static void add_algo(RList *algos, const char *a) {
free (ha);
}
static bool check_base_flags(RahashOptions *ro) {
const char *algo = ro->algorithm;
switch (ro->direction) {
case R_CRYPTO_DIR_ENCRYPT:
case R_CRYPTO_DIR_DECRYPT:
return !strcmp (algo, "base64") || !strcmp (algo, "base91");
}
return false;
}
R_API int r_main_rahash2(int argc, const char **argv) {
ut64 i;
int c, rad = 0, bsize = 0, numblocks = 0, ule = 0;
@ -513,8 +528,6 @@ R_API int r_main_rahash2(int argc, const char **argv) {
char *algo = NULL;
const char *seed = NULL;
bool show_version = false;
const char *decrypt = NULL;
const char *encrypt = NULL;
char *hashstr = NULL;
ut8 *iv = NULL;
int ivlen = -1;
@ -534,8 +547,8 @@ R_API int r_main_rahash2(int argc, const char **argv) {
bool listplugins = false;
int _ret = 0;
ro->direction = -1;
ro->incremental = true;
// #define ret(x) {_ret=x;printf("%d\n", __LINE__);goto beach;}
#define ret(x) {_ret=x;goto beach;}
RGetopt opt;
r_getopt_init (&opt, argc, argv, "p:jJD:rveE:a:i:I:S:s:x:b:nBhf:t:kLqc:X");
@ -557,8 +570,22 @@ R_API int r_main_rahash2(int argc, const char **argv) {
case 'S': seed = opt.arg; break;
case 'I': ivseed = opt.arg; break;
case 'n': numblocks = 1; break;
case 'D': ro->direction = R_CRYPTO_DIR_DECRYPT; decrypt = opt.arg; break;
case 'E': ro->direction = R_CRYPTO_DIR_ENCRYPT; encrypt = opt.arg; break;
case 'D':
if (ro->direction != -1) {
R_LOG_ERROR ("Cannot use -D and -E at the same time");
ret (1);
}
ro->direction = R_CRYPTO_DIR_DECRYPT;
ro->algorithm = opt.arg;
break;
case 'E':
if (ro->direction != -1) {
R_LOG_ERROR ("Cannot use -D and -E at the same time");
ret (1);
}
ro->direction = R_CRYPTO_DIR_ENCRYPT;
ro->algorithm = opt.arg;
break;
case 'L': listplugins = true; break;
case 'e': ule = 1; ro->endian = !ro->endian; break;
case 'r': rad = 1; break;
@ -581,11 +608,6 @@ R_API int r_main_rahash2(int argc, const char **argv) {
ret (r_main_version_print ("rahash2", rad));
}
algo = r_list_empty (algos) ? strdup ("sha1") : r_str_list_join (algos, ",");
if (encrypt && decrypt) {
R_LOG_ERROR ("Option -E and -D are incompatible with each other");
ret (1);
}
if (listplugins) {
if (rad == 'j' && ro->quiet) {
rad = 'J';
@ -593,19 +615,14 @@ R_API int r_main_rahash2(int argc, const char **argv) {
algolist (rad);
ret (0);
}
algo = r_list_empty (algos) ? strdup ("sha1") : r_str_list_join (algos, ",");
if (compareStr) {
int compareBin_len;
if (bsize && !ro->incremental) {
R_LOG_ERROR ("Option -c incompatible with -b and -B options");
ret (1);
}
bool flag = false;
if (encrypt) {
flag = !strcmp (encrypt, "base64") || !strcmp (encrypt, "base91");
} else if (decrypt) {
flag = !strcmp (decrypt, "base64") || !strcmp (decrypt, "base91");
}
if (flag) {
if (check_base_flags (ro)) {
R_LOG_ERROR ("Option -c incompatible with -E base64, -E base91, -D base64 or -D base91 options");
ret (1);
}
@ -720,10 +737,8 @@ R_API int r_main_rahash2(int argc, const char **argv) {
if (!bytes_read && !hashstr_hex) {
hashstr_len = r_str_unescape (nhashstr);
}
if (encrypt) {
ret (encrypt_or_decrypt (ro, encrypt, R_CRYPTO_DIR_ENCRYPT, nhashstr, hashstr_len, iv, ivlen, 0));
} else if (decrypt) {
ret (encrypt_or_decrypt (ro, decrypt, R_CRYPTO_DIR_DECRYPT, nhashstr, hashstr_len, iv, ivlen, 0));
if (ro->direction != -1) {
ret (encrypt_or_decrypt (ro, nhashstr, hashstr_len, iv, ivlen, 0));
} else {
char *str = (char *) nhashstr;
int strsz = hashstr_len;
@ -815,14 +830,8 @@ R_API int r_main_rahash2(int argc, const char **argv) {
ret (1);
}
if (encrypt) {// for encrytion when files are provided
int rt = encrypt_or_decrypt_file (ro, encrypt, 0, argv[i], iv, ivlen, 0);
if (rt == -1) {
continue;
}
ret (rt);
} else if (decrypt) {
int rt = encrypt_or_decrypt_file (ro, decrypt, 1, argv[i], iv, ivlen, 0);
if (ro->direction != -1) {
int rt = encrypt_or_decrypt_file (ro, argv[i], iv, ivlen, 0);
if (rt == -1) {
continue;
}

View File

@ -180,6 +180,7 @@ c ror LGPL pancake
c rot MIT pancake
c serpent-ecb LGPL pancake
c sm4-ecb LGPL3 Sylvain Pelissier
h strhash MIT pancake
c xor MIT pancake
h md5
h sha1