Ejemplo n.º 1
0
/*
 * Write some data to a file. May be "-" to write to stdout.
 */
static void write_file(const char *filename, const char *data) {
	FILE *fp = stdout;
	size_t len;
	size_t nw;
	int err;


	if (strcmp(filename, "-") != 0) {
		fp = fopen(filename, "w");
		if (fp == NULL)
			system_error(filename);
	}

	len = strlen(data);

	nw = fwrite(data, 1, len, fp);
	if (ferror(fp))
		system_error("fwrite");

	if (nw != len)
		custom_error("Dafuq? Didn't write everything we should.");

	if (strcmp(filename, "-") != 0) {
		err = fclose(fp);
		if (err == -1)
			system_error("fclose");
	}
}
Ejemplo n.º 2
0
static void simple_action(struct fs_ctx *s, const char *key, enum action act) {
	if (act == ACTION_ENCRYPT)
		vig_encrypt(s, key);
	else if (act == ACTION_DECRYPT)
		vig_decrypt(s, key);
	else
		custom_error("Dafuq? simple_action called with an unknown action");
}
Ejemplo n.º 3
0
size_t decode_append(char *append, enum decode appenddecode) {
	size_t inlen;
	size_t i, o;
	unsigned x;

	inlen = strlen(append);

	if (appenddecode == DECODE_RAW)
		return inlen;

	/* Additional requirement for DECODE_HEX */
	if (appenddecode == DECODE_HEX && inlen % 2 != 0)
		custom_error("The append string must have an even length to hex-decode it.");

	for (i = 0, o = 0; i < inlen; o++) {
		int n = 0;

		/* Special case for DECODE_HEX */
		if (appenddecode == DECODE_HEX) {
			sscanf(&append[i], "%02x%n", &x, &n);
			if (n != 2)
				custom_error("Error while hex-decoding %s", &append[i]);
			append[o] = x;
			i += 2;
			continue;
		}

		if (append[i] != '\\') {
			append[o] = append[i];
			i++;
			continue;
		}

		switch (append[i + 1]) {
		case 'a':
			append[o] = '\x07';
			i += 2;
			break;
		case 'b':
			append[o] = '\x08';
			i += 2;
			break;
		case 't':
			append[o] = '\x09';
			i += 2;
			break;
		case 'n':
			append[o] = '\x0a';
			i += 2;
			break;
		case 'v':
			append[o] = '\x0b';
			i += 2;
			break;
		case 'f':
			append[o] = '\x0c';
			i += 2;
			break;
		case 'r':
			append[o] = '\x0d';
			i += 2;
			break;
		case 'e':
			append[o] = '\x1b';
			i += 2;
			break;
		case 'x':
			sscanf(&append[i + 2], "%02x%n", &x, &n);
			append[o] = x;
			i += 2 + n;
			break;
		case '0':
			sscanf(&append[i + 2], "%03o%n", &x, &n);
			append[o] = x;
			i += 2 + n;
			break;

		case '\\':
		case '\0':
		default:
			append[o] = '\\';
			i += 2;
			break;
		}
	}

	append[o] = '\0';

	return o;
}
Ejemplo n.º 4
0
int main(int argc, char **argv) {
	struct goh_state st;
	int opt;
	const char *prefixhash;
	char *append;
	size_t appendlen;
	enum decode appenddecode = DECODE_ECHO;
	size_t minlen = 0, maxlen = SHA_CBLOCK;


	/* Options parsing */
	goh_init(&st, opt_desc, ARRAY_LENGTH(opt_desc), argc, argv, 1);
	st.usagehelp = "[options] sha1(prefix) hex(append)\n";

	while ((opt = goh_nextoption(&st)) >= 0) {
		switch (opt) {
		case 'm':
			minlen = strtol(st.argval, NULL, 0);
			break;

		case 'M':
			maxlen = strtol(st.argval, NULL, 0);
			break;

		case 'd':
			if (strcmp(st.argval, "raw") == 0)
				appenddecode = DECODE_RAW;
			else if (strcmp(st.argval, "hex") == 0)
				appenddecode = DECODE_HEX;
			else if (strcmp(st.argval, "echo") == 0)
				appenddecode = DECODE_ECHO;
			else
				custom_error("%s not a valid value for --append-decode", st.argval);
			break;

		default:
			custom_error("Option declared but not handled");
		}
	}

	/* Common command line error */
	if (st.argidx + 2 != argc) {
		custom_warn("Missing mandatory arguments");
		goh_printhelp(&st, stderr);
		return EXIT_FAILURE;
	}

	prefixhash = argv[st.argidx];
	append = argv[st.argidx + 1];

	goh_fini(&st);


	/* Command line checking */
	if (strlen(prefixhash) != 2 * SHA_DIGEST_LENGTH)
		custom_error("sha1(prefix) must be %d hex characters long", 2 * SHA_DIGEST_LENGTH);

	/* Decode the append string */
	appendlen = decode_append(append, appenddecode);

	sha1append(prefixhash, append, appendlen, minlen, maxlen);

	return EXIT_SUCCESS;
}
Ejemplo n.º 5
0
 bool operator()(T& t, M& method, int code, const std::string& message, int id)
 {
   return this->_send( t, method, custom_error_json(), custom_error(code, message), id );
 }
Ejemplo n.º 6
0
int main(int argc, char **argv) {
	struct goh_state st;
	int opt;
	struct fs_ctx s;
	enum action action = ACTION_CRACK;
	char *key = NULL;
	const char *filenamein = "-";
	const char *filenameout = "-";
	char *text = NULL;
	struct charset cs;
	struct crack_args cka;

	cs_init(&cs);
	memset(&cka, 0, sizeof(cka));

	/* Parse the options. */
	goh_init(&st, opt_desc, ARRAY_LENGTH(opt_desc), argc, argv, 1);
	st.usagehelp = "[options]\n";

	while ((opt = goh_nextoption(&st)) >= 0) {
		switch (opt) {
		case 'i':
			filenamein = st.argval;
			break;

		case 'o':
			filenameout = st.argval;
			break;

		case 'e':
			action = ACTION_ENCRYPT;
			break;

		case 'd':
			action = ACTION_DECRYPT;
			break;

		case 'k':
			key = st.argval;
			break;

		case 'l':
			cka.klen = atoi(st.argval);
			break;

		case OPT_KASISKI_MIN_LENGTH:
			cka.ka_minlen = atoi(st.argval);
			break;

		case OPT_SHOW_KASISKI_TABLE:
			cka.ka_show_table = 1;
			break;

		case OPT_SHOW_KASISKI_LENGTH:
			cka.ka_show_length = 1;
			break;

		case 'c':
			cs_add(&cs, st.argval);
			break;

		default:
			custom_error("Unrecognized option (shouldn't happen)");
			break;
		}
	}


	/* Common command line mistake. */
	if (st.argidx != argc)
		custom_error("Useless argument %s", argv[st.argidx]);

	goh_fini(&st);

	/* Check for some invalid options combinations. */
	if (action != ACTION_CRACK && key == NULL)
		custom_error("Encryption and decryption take a --key");

	if (action == ACTION_CRACK && key != NULL)
		custom_error("--key need either --encrypt or --decrypt");

	if (cka.klen > 0 && key != NULL)
		custom_warn("Unnecessary key length option with an actual key");

	if (cka.klen > 0 && key != NULL && cka.klen != strlen(key))
		custom_error("Key length option doesn't match "
		             "the length of the key");

	if (cka.ka_minlen > 0 && action != ACTION_CRACK)
		custom_error("--kasiski-min-length can only be used in "
		             "cracking mode");

	if (cka.ka_minlen > 0 && cka.klen > 0)
		custom_warn("Useless option --kasiski-min-length when the key "
		            "length is given");

	if (cka.ka_show_table != 0 && action != ACTION_CRACK)
		custom_error("--show-kasiski-table can only be used in "
		             "cracking mode");

	if (cka.ka_show_table != 0 && cka.klen > 0)
		custom_warn("Option --show-kasiski-table ignored when a key "
		            "length is given");

	if (cka.ka_show_length != 0 && action != ACTION_CRACK)
		custom_error("--show-kasiski-length can only be used in "
		             "cracking mode");

	if (cka.ka_show_length != 0 && cka.klen > 0)
		custom_warn("Option --show-kasiski-length ignored when a key "
		            "length is given");

	/* Default charset. */
	if (cs.chars_size == 0) {
		cs_add(&cs, CHARSET_UPPER);
		cs_add(&cs, CHARSET_LOWER);
	}


	/* Start to do the job. */
	text = read_file(filenamein);
	fs_init(&s, text, &cs);
	cka.str = &s;

	if (action == ACTION_CRACK)
		crack(&cka);
	else
		simple_action(&s, key, action);

	write_file(filenameout, text);

	fs_fini(&s);
	free(text);
	cs_fini(&cs);


	return EXIT_SUCCESS;
}