* Cleanup for rsakey (still unused)

* Move sflib into libr/include
* Added r_file_size()

--HG--
rename : binr/ragg2/d/sflib/common/sfsocketcall.h => libr/include/sflib/common/sfsocketcall.h
rename : binr/ragg2/d/sflib/common/sftypes.h => libr/include/sflib/common/sftypes.h
rename : binr/ragg2/d/sflib/darwin-x86-32/sflib.h => libr/include/sflib/darwin-x86-32/sflib.h
rename : binr/ragg2/d/sflib/darwin-x86-32/sfsyscall.h => libr/include/sflib/darwin-x86-32/sfsyscall.h
rename : binr/ragg2/d/sflib/darwin-x86-32/sfsysnr.h => libr/include/sflib/darwin-x86-32/sfsysnr.h
rename : binr/ragg2/d/sflib/linux-x86-32/sflib.h => libr/include/sflib/linux-x86-32/sflib.h
rename : binr/ragg2/d/sflib/linux-x86-32/sfsyscall.h => libr/include/sflib/linux-x86-32/sfsyscall.h
rename : binr/ragg2/d/sflib/linux-x86-32/sfsysnr.h => libr/include/sflib/linux-x86-32/sfsysnr.h
rename : binr/ragg2/d/sflib/linux-x86-64/sflib.h => libr/include/sflib/linux-x86-64/sflib.h
rename : binr/ragg2/d/sflib/linux-x86-64/sfsyscall.h => libr/include/sflib/linux-x86-64/sfsyscall.h
rename : binr/ragg2/d/sflib/linux-x86-64/sfsysnr.h => libr/include/sflib/linux-x86-64/sfsysnr.h
This commit is contained in:
pancake 2011-12-01 10:53:02 +01:00
parent bc8f1e617b
commit 696961d15d
16 changed files with 29 additions and 79 deletions

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2010 pancake<nopcode.org> */
/* radare - LGPL - Copyright 2009-2011 pancake<nopcode.org> */
#include <stdio.h>
#include <stdlib.h>
@ -56,7 +56,7 @@ static int show_help(char *argv0, int line) {
" -s [str] search for zero-terminated strings (can be used multiple times)\n"
" -e [regex] search for regular expression string matches\n"
" -x [hex] search for hexpair string (909090) (can be used multiple times)\n"
" -m [str] set a mask\n"
" -m [str] set a binary mask to be applied on keywords\n"
" -f [from] start searching from address 'from'\n"
" -t [to] stop search at address 'to'\n"
" -X show hexdump of search results\n"
@ -90,6 +90,9 @@ static int rafind_open(char *file) {
r_search_set_callback (rs, &hit, buffer);
if (to == -1)
to = r_io_size(io);
if (mode == R_SEARCH_STRING) {
eprintf ("TODO: searchin stringz\n");
}
if (mode == R_SEARCH_KEYWORD) {
list_for_each(pos, &(kws_head)) {
BoxedString *kw = list_entry(pos, BoxedString, list);

View File

@ -33,6 +33,8 @@ install-pkgconfig:
install-includes:
${INSTALL_DIR} ${IFX}/libr
(cd include && for a in *.h ; do ${INSTALL_DATA} $$a ${IFX}/libr ; done)
${INSTALL_DIR} ${IFX}/libr/sflib
(cd include/sflib && for a in *.h ; do ${INSTALL_DATA} $$a ${IFX}/libr/sflib ; done)
install-symlink:
mkdir -p ${PFX}/bin

View File

@ -217,6 +217,7 @@ R_API void r_graph_plant(RGraph *t);
R_API void r_graph_push (RGraph *t, ut64 addr, void *data);
R_API RGraphNode* r_graph_pop(RGraph *t);
R_API int r_file_size(const char *str);
R_API RMmap *r_file_mmap (const char *file, boolt rw);
R_API void r_file_mmap_free (RMmap *m);

View File

@ -13,14 +13,6 @@
#include <r_search.h>
// XXX perror and exit are not portable
#ifdef __FreeBSD__
#include <err.h>
#else
#define err(x,y) { perror(y); exit(x); }
#endif
// Baby BER parser, just good enough for RSA keys.
//
// This is not robust to errors in the memory image, but if we added
@ -106,7 +98,7 @@ ut8 *r_find_key_start(ut8 *map, int offset) {
void r_find_keys(ut8 *image, int isize, ut8 *target, int target_size, boolt find_public) {
int i;
for (i = 0; i < isize - target_size; i++) {
if (memcmp(&image[i], target, target_size))
if (memcmp (&image[i], target, target_size))
continue;
ut8 *key = r_find_key_start(image, i);
@ -125,92 +117,37 @@ void r_find_keys(ut8 *image, int isize, ut8 *target, int target_size, boolt find
}
}
// Memory maps filename and return a pointer on success, setting len
// to the length of the file (does not return on error)
ut8 *r_map_file(char *filename, ut32 *len) {
int fd = open(filename, O_RDONLY);
if (fd < 0)
errx(1, "image open failed");
struct stat st;
if (fstat(fd, &st) != 0)
errx(1, "image fstat failed");
ut8 *map;
map = (ut8*)mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED)
errx(1, "image mmap failed");
*len = st.st_size;
return map;
}
// Returns a decoded byte from a file of hex values, ignoring whitespace
int r_get_hex_byte(int fd) {
for (;;) {
char a[3];
if (read(fd, &a[0], 1) < 1)
break;
if ((a[0] >= '0' && a[0] <= '9') || (a[0] >= 'a' && a[0] <= 'f')) {
if (read(fd, &a[1], 1) < 1)
break;
a[2] = '\0';
return strtol(a,NULL,16);
}
}
return -1;
}
// Reads hexadecimal bytes from filename and returns a byte array
// containing these values, setting len to the number of bytes (does not
// return on error)
ut8 *r_read_modulus(char *filename, ut32 *len) {
int fd = open(filename, O_RDONLY);
if (fd < 0)
err(1, "modulus open failed");
struct stat st;
if (fstat(fd, &st) != 0)
err(1, "modulus fstat failed");
ut8 *modulus = (ut8 *)malloc(st.st_size);
for (*len=0; ;*len++) {
int c = r_get_hex_byte(fd);
if (c == -1)
break;
modulus[*len] = c;
}
close(fd);
return modulus;
}
void r_usage() {
#if TEST
static void r_usage() {
fprintf(stderr, "USAGE: rsakeyfind MEMORY-IMAGE [MODULUS-FILE]\n"
"Locates BER-encoded RSA private keys in MEMORY-IMAGE.\n"
"If MODULUS-FILE is specified, it will locate private and public keys"
"matching the hex-encoded modulus read from this file.\n");
}
#if TEST
int main(int argc, char *argv[]) {
ut32 ilen;
ut8 *image;
RMmap *img;
if (argc < 2 || argc > 3) {
r_usage();
exit(1);
}
image = r_map_file(argv[1], &ilen);
img = r_file_mmap (argv[1], 0);
if (argc == 3) {
// method 1: searching for modulus
ut32 mlen;
ut8 *modulus = r_read_modulus(argv[2], &mlen);
r_find_keys(image, ilen, modulus, mlen, R_TRUE);
ut8 *modulus = r_file_slurp (argv[2], &mlen);
if (modulus)
r_find_keys (
img->buf, img->len,
modulus, mlen, R_TRUE);
} else {
// method 2: searching for versionmarker
ut8 versionmarker[4] = {0x02, 0x01, 0x00, 0x02};
r_find_keys(image, ilen, versionmarker, sizeof(versionmarker), R_FALSE);
r_find_keys (img->buf, img->len,
versionmarker, sizeof (versionmarker), R_FALSE);
}
r_file_mmap_free (img);
return 0;
}
#endif

View File

@ -26,6 +26,13 @@ R_API boolt r_file_exist(const char *str) {
return (S_ISREG (buf.st_mode))? R_TRUE: R_FALSE;
}
R_API int r_file_size(const char *str) {
struct stat buf;
if (stat (str, &buf)==-1)
return 0;
return buf.st_size;
}
R_API char *r_file_abspath(const char *file) {
char *ret = NULL;
char *cwd = r_sys_getdir ();