#include #include #include #include #include "getargs.h" int from64tobits(char *out, const char *in); void to64frombits(unsigned char *out, const unsigned char *in, int inlen); int dumpReq; int dumpChal; int dumpResp; int genResp; int dumpRaw; int dumpb64only; int genReq; char *username = "joeuser"; char *password = "joespw"; argSpec argSpecArray[] = { {'q', OptionBoolean, &dumpReq, NULL, "dump NTLM request", NULL}, {'Q', OptionBoolean, &genReq, NULL, "generate (and dump) NTLM request", NULL}, {'c', OptionBoolean, &dumpChal, NULL, "dump NTLM challange", NULL}, {'g', OptionBoolean, &genResp, NULL, "generate (and dump) NTLM response given a challenge", NULL}, {'r', OptionBoolean, &dumpResp, NULL, "dump NTLM response", NULL}, {'R', OptionBoolean, &dumpRaw, NULL, "dump raw bytes", NULL}, {'6', OptionBoolean, &dumpb64only, NULL, "dump generated base64 only", NULL}, {'u', OptionString, &username, NULL, "username", NULL}, {'p', OptionString, &password, NULL, "password", NULL}, }; int argSpecCount = (sizeof argSpecArray / sizeof argSpecArray[0]); char *progName; void usage(void) { printf("usage: %s [options] [base-64-string]\n", progName); printf(" %s -? will display options\n", progName); } unsigned char buf[4096]; unsigned char buf2[4096]; int main(int argc, char *argv[]) { int rawLen = 0; int argsUsed; int i; progName = argv[0]; argsUsed = getargs(argc, argv, argSpecArray, argSpecCount); if (argsUsed < 0) { usage(); exit(1); } argc -= argsUsed; argv += argsUsed; if (argc != 1 && argc != 0) { usage(); exit(1); } if (argc == 1) { rawLen = from64tobits(buf,argv[0]); if (genReq) fprintf(stderr,"%s: extra argument with -Q ignored\n",progName); } else { if (dumpReq || dumpChal || dumpResp || dumpRaw) { fprintf(stderr,"%s: -q -r -c -R specified but no base64 data\n",progName); return 1; } } printf("Converted base64 string to %d data bytes\n",rawLen); if (dumpReq) dumpSmbNtlmAuthRequest(stdout,(tSmbNtlmAuthRequest*)buf); else if (dumpChal) dumpSmbNtlmAuthChallenge(stdout,(tSmbNtlmAuthChallenge*)buf); else if (dumpResp) dumpSmbNtlmAuthResponse(stdout,(tSmbNtlmAuthResponse*)buf); if (dumpRaw) for (i=0; i= 3; inlen -= 3) { *out++ = base64digits[in[0] >> 2]; *out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)]; *out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)]; *out++ = base64digits[in[2] & 0x3f]; in += 3; } if (inlen > 0) { unsigned char fragment; *out++ = base64digits[in[0] >> 2]; fragment = (in[0] << 4) & 0x30; if (inlen > 1) fragment |= in[1] >> 4; *out++ = base64digits[fragment]; *out++ = (inlen < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c]; *out++ = '='; } *out = '\0'; } int from64tobits(char *out, const char *in) /* base 64 to raw bytes in quasi-big-endian order, returning count of bytes */ { int len = 0; register unsigned char digit1, digit2, digit3, digit4; if (in[0] == '+' && in[1] == ' ') in += 2; if (*in == '\r') return(0); do { digit1 = in[0]; if (DECODE64(digit1) == BAD) return(-1); digit2 = in[1]; if (DECODE64(digit2) == BAD) return(-1); digit3 = in[2]; if (digit3 != '=' && DECODE64(digit3) == BAD) return(-1); digit4 = in[3]; if (digit4 != '=' && DECODE64(digit4) == BAD) return(-1); in += 4; *out++ = (DECODE64(digit1) << 2) | (DECODE64(digit2) >> 4); ++len; if (digit3 != '=') { *out++ = ((DECODE64(digit2) << 4) & 0xf0) | (DECODE64(digit3) >> 2); ++len; if (digit4 != '=') { *out++ = ((DECODE64(digit3) << 6) & 0xc0) | DECODE64(digit4); ++len; } } } while (*in && *in != '\r' && digit4 != '='); return (len); } /* base64.c ends here */