Esempio n. 1
0
int main(int argc, char **argv, char **envp) {
  array_t array;
  bio_t bin;
  char bin_data[8192];
  bio_t *bio;
  int fd;
  char *name;
  int opt;
  ssize_t r;
  struct stat st;
  char *type = 0;
  
  while ((opt = sgetopt(argc, argv, "t:")) != -1) {
    switch (opt) {
      case '?':
        usage();
      case 't':
        type = soptarg;
        break;
    }
  }
  argv += soptind;
  
  if (!type)
    usage();
    
  array_init(&array, 1);
  
  /* Set the initial buffered io pointer to use stdin and set the default name
   * to '-'. */
  bio = bio_0;
  name = "-";
  for (;;) {
    if (*argv) {
      /* If a path was given then check to see if it's a directory and skip
       * over it if it is. */
      if (stat(*argv, &st))
        err(1, "fatal");
      if (S_ISDIR(st.st_mode)) {
        errno = EISDIR;
        warn(*argv);
        argv++;
        continue;
      }
      
      /* Open the file for reading and construct our own buffered io struct to
       * use for reading in the file and hashing it.  Also set the default
       * buffered io pointer to our own and update the name to reflect the
       * current file. */
      fd = open_read(*argv);
      if (fd == -1)
        err(1, "fatal");
      bio_init(&bin, read, fd, bin_data, sizeof(bin_data));
      bio = &bin;
      name = *argv;
    }
    /* If there's no more arguments passed on the command line and the default
     * buffered io pointer doesn't point to stdin then that means all of the
     * files have been hashed and it's time to exit. */
    else if ((bio != bio_0))
      break;
    
    if (!strcmp(type, "md4"))
      handle_md4(&array, bio);
    else if (!strcmp(type, "md5"))
      handle_md5(&array, bio);
    else if (!strcmp(type, "sha1"))
      handle_sha1(&array, bio);
    else if (!strcmp(type, "sha256"))
      handle_sha256(&array, bio);
    else if (!strcmp(type, "sha512"))
      handle_sha512(&array, bio);
    else
      errx(1, "fatal: %s", "unknown type");
      
    bio_put_str(bio_1, array_start(&array));
    bio_put_str(bio_1, "  ");
    bio_put_str(bio_1, name);
    bio_put_str(bio_1, "\n");
    bio_flush(bio_1);
    
    array_trunc(&array);
    
    if (*argv)
      close(fd);
    if ((!*argv) || (!*++argv))
      break;
  }
  
  return 0;
}
Esempio n. 2
0
int main(int argc, char **argv) {
	const char *addr = NULL;
	RCore *c, *c2;
	RDiff *d;
	const char *arch = NULL;
	int bits = 0;
	char *file, *file2;
	ut8 *bufa, *bufb;
	int o, sza, szb, /*diffmode = 0,*/ delta = 0;
	int mode = MODE_DIFF;
	int diffops = 0;
	int threshold = -1;
	int gdiff_mode = 0;
	double sim;

	while ((o = getopt (argc, argv, "a:b:Cnpg:Ojrhcdsvxt:")) != -1) {
		switch (o) {
		case 'a':
			arch = optarg;
			break;
		case 'b':
			bits = atoi (optarg);
			break;
		case 'p':
			useva = R_FALSE;
			break;
		case 'r':
			diffmode = 'r';
			break;
		case 'g':
			mode = MODE_GRAPH;
			addr = optarg;
			break;
		case 'c':
			showcount = 1;
			break;
		case 'C':
			mode = MODE_CODE;
			gdiff_mode++;
			break;
		case 'n':
			showbare = R_TRUE;
			break;
		case 'O':
			diffops = 1;
			break;
		case 't':
			threshold = atoi (optarg);
			printf ("%s\n", optarg);
			break;
		case 'd':
			delta = 1;
			break;
		case 'h':
			return show_help (1);
		case 's':
			mode = MODE_DIST;
			break;
		case 'x':
			mode = MODE_COLS;
			break;
		case 'v':
			printf ("radiff2 v"R2_VERSION"\n");
			return 0;
		case 'j':
			diffmode = 'j';
			break;
		default:
			return show_help (0);
		}
	}
	
	if (argc<3 || optind+2>argc)
		return show_help (0);

	if (optind<argc) {
		file = argv[optind];
	} else {
		file = NULL;
	}
	
	if (optind+1<argc) {
		file2 = argv[optind+1];
	} else {
		file2 = NULL;
	}

	switch (mode) {
	case MODE_GRAPH:
	case MODE_CODE:
		c = opencore (file);
		if (!c) eprintf ("Cannot open '%s'\n", r_str_get (file));
		c2 = opencore (file2);
		if (!c || !c2) {
			eprintf ("Cannot open '%s'\n", r_str_get (file2));
			return 1;
		}
		if (arch) {
			r_config_set (c->config, "asm.arch", arch);
			r_config_set (c2->config, "asm.arch", arch);
		}
		if (bits) {
			r_config_set_i (c->config, "asm.bits", bits);
			r_config_set_i (c2->config, "asm.bits", bits);
		}
		r_config_set_i (c->config, "diff.bare", showbare);
		r_config_set_i (c2->config, "diff.bare", showbare);
		r_anal_diff_setup_i (c->anal, diffops, threshold, threshold);
		r_anal_diff_setup_i (c2->anal, diffops, threshold, threshold);
		if (mode == MODE_GRAPH) {
			char *words = strdup (addr);
			char *second = strstr (words, ",");
			if (second) {
				ut64 off;
				*second++ = 0;
				off = r_num_math (c->num, words);
				// define the same function at each offset
				r_core_anal_fcn (c, off, UT64_MAX, R_ANAL_REF_TYPE_NULL, 0);
				r_core_anal_fcn (c2, r_num_math (c2->num, second),
						UT64_MAX, R_ANAL_REF_TYPE_NULL, 0);
				r_core_gdiff (c, c2, R_FALSE); // compute the diff
				r_core_anal_graph (c, off, R_CORE_ANAL_GRAPHBODY | R_CORE_ANAL_GRAPHDIFF);
			} else {
				r_core_anal_fcn (c, r_num_math (c->num, words), UT64_MAX, R_ANAL_REF_TYPE_NULL, 0);
				r_core_anal_fcn (c2, r_num_math (c2->num, words), UT64_MAX, R_ANAL_REF_TYPE_NULL, 0);
				r_core_gdiff (c, c2, gdiff_mode);
				r_core_anal_graph (c, r_num_math (c->num, addr),
					R_CORE_ANAL_GRAPHBODY | R_CORE_ANAL_GRAPHDIFF);
			}
			free (words);
		} else {
			r_core_gdiff (c, c2, gdiff_mode);
			r_core_diff_show (c, c2);
		}
		r_cons_flush ();
		return 0;
	}

	bufa = (ut8*)r_file_slurp (file, &sza);
	if (!bufa) {
		eprintf ("radiff2: Cannot open %s\n", r_str_get (file));
		return 1;
	}
	bufb = (ut8*)r_file_slurp (file2, &szb);
	if (!bufb) {
		eprintf ("radiff2: Cannot open: %s\n", r_str_get (file2));
		free (bufa);
		return 1;
	}

	switch (mode) {
	case MODE_COLS:
		{
			int cols = (r_cons_get_size (NULL)>112)?16:8;
			dump_cols (bufa, sza, bufb, szb, cols);
		}
		break;
	case MODE_DIFF:
		d = r_diff_new (0LL, 0LL);
		r_diff_set_delta (d, delta);
		if (diffmode == 'j') {
			printf("{\"files\":[{\"filename\":\"%s\", \"size\":%d, \"sha256\":\"", file, sza); 
			handle_sha256 (bufa, sza);
			printf("\"},\n{\"filename\":\"%s\", \"size\":%d, \"sha256\":\"", file2, szb);
			handle_sha256 (bufb, szb);
			printf("\"}],\n");
			printf("\"changes\":[");
		}
		r_diff_set_callback (d, &cb, 0);//(void *)(size_t)diffmode);
		r_diff_buffers (d, bufa, sza, bufb, szb);
		if (diffmode == 'j')
			printf("]\n");
		r_diff_free (d);
		break;
	case MODE_DIST:
		r_diff_buffers_distance (NULL, bufa, sza, bufb, szb, &count, &sim);
		printf ("similarity: %.2f\n", sim);
		printf ("distance: %d\n", count);
		break;
	}

	if (diffmode == 'j' && showcount)
		printf (",\"count\":%d}\n",count);
	else if (showcount && diffmode != 'j')
		printf ("%d\n", count);
	else if (!showcount && diffmode == 'j')
		printf ("}\n");
	free (bufa);
	free (bufb);

	return 0;
}