int main(int argc, char **argv) {

    unsigned char sha1_digest[40]={0};
    unsigned char ssid[8]={0},buf[8]={0},year,week,x1,x2,x3;
    unsigned int keys = 0,ssidLen = 0,verbose = 0, opt = 0;
    unsigned char *strId = NULL;
    FILE *ofile = NULL;
    unsigned int year_target = 0;
    unsigned int year_max = 0; 

    SHA_CTX sha1_ctx;

    if(argc > 1) {
      while( (opt = getopt(argc, argv,"vuy:o:i:")) != -1) {

        switch(opt) {

          case 'i' :
            strId = (unsigned char *)optarg;
            break;

          case 'o' :
            if((ofile = fopen(optarg,"wb")) == NULL) {
              fprintf(stderr,"\nCannot open %s for output.\n",optarg);
              return(0);
            }
            break;

          case 'v' :
            verbose++;
            break;

          case 'u' :
            // Hex 0x30 is the specific ascii byte ('0') for the UPC variant
            serial[0] = 0x30;
            serial[1] = 0x30;
            break;
          case 'y' :
            sscanf(optarg, "%u", &year_target);
            if(year_target >= 5 && year_target <= 10) {
              break;
            }
            else {
              fprintf(stderr, "Invalid year selected: %u\n", year_target);
              usage(argv);
            }
          default:
            usage(argv);
        }
      }

      if(!strId) usage(argv);

      if(!(ssidLen = str2ssid(ssid,strId))) usage(argv);

      if(verbose)
        fprintf(stdout,"Generating keys..please wait\n");
        if (year_target) {
          year = year_target;
          year_max = year_target;
        } else {
          year = 5;
          year_max = 10;
        }
        // generate values only for 2005 - 2010
        for(;year <= year_max;year++) {
          if(verbose)
            fprintf(stdout,"Calculating keys for 20%02d...\n",year);

          serial[2] = (year / 10) + 48;
          serial[3] = (year % 10) + 48;

          // 52 weeks of the year
          for(week = 1;week <= 52;week++) {

            serial[4] = (week / 10) + 48;
            serial[5] = (week % 10) + 48;

            for(x1 = 0;x1 < 36;x1++) {

              serial[6] = hexmsb(charTable[x1]);
              serial[7] = hexlsb(charTable[x1]);

              for(x2 = 0;x2 < 36;x2++) {

                serial[8] = hexmsb(charTable[x2]);
                serial[9] = hexlsb(charTable[x2]);

                for(x3 = 0;x3 < 36;x3++) {

                  serial[10] = hexmsb(charTable[x3]);
                  serial[11] = hexlsb(charTable[x3]);

                  // hash serial number with sha-1
                  SHA1_Init(&sha1_ctx);
                  SHA1_Update(&sha1_ctx,serial,SERIAL_LENGTH);
                  SHA1_Final(&sha1_ctx,sha1_digest);

                  // compare SSID octets with last number of bytes supplied
                  if(memcmp(&sha1_digest[(SHA1_LENGTH-ssidLen)],ssid,ssidLen) == 0) {

                    keys++;

                    if(verbose) {

                      memcpy(buf,serial,6);

                      fprintf(stdout,
                              "Serial Number Year 20%02d: %s**%C%C%C - potential key = ",year,
                              buf,charTable[x1],charTable[x2],charTable[x3]);

                      dump_key(stdout,sha1_digest);
                    } else {
                      dump_key(stdout,sha1_digest);
                    }
                    if(ofile) {
                      dump_key(ofile,sha1_digest);
                    }
                  }
                }
              }
            }
          }
        }
      if(verbose)
        fprintf(stdout,"Found %d potential keys.\n",keys);

      if(ofile) fclose(ofile);
    }
    else {
        usage(argv);
    }
    return(0);
}
示例#2
0
文件: stkeys.c 项目: xant/istkey
int
compute_key(char *strId, int year, char *output, int len) {

    unsigned char sha1_digest[40]={0};
    char ssid[8]={0},buf[8]={0},week,x1,x2,x3;
    int keys = 0,ssidLen = 0;
    int offset = 0;
    if(!(ssidLen = str2ssid(ssid,strId)))
        return -99;
    
    //fprintf(stdout,"\nGenerating keys..please wait\n\n");

    serial[3] = year | '0';

    // 52 weeks of the year

    for(week = 1;week <= 52;week++) {

      serial[4] = (week / 10) + '0';
      serial[5] = (week % 10) + '0';

      for(x1 = 0;x1 < 36;x1++) {

        serial[6] = hexmsb(charTable[x1]);
        serial[7] = hexlsb(charTable[x1]);

        for(x2 = 0;x2 < 36;x2++) {

          serial[8] = hexmsb(charTable[x2]);
          serial[9] = hexlsb(charTable[x2]);

          for(x3 = 0;x3 < 36;x3++) {
            
            serial[10] = hexmsb(charTable[x3]);
            serial[11] = hexlsb(charTable[x3]);

            // hash serial number with sha-1
            CC_SHA1(serial, SERIAL_LENGTH, sha1_digest);
            // compare SSID octets with last number of bytes supplied

            if(memcmp(&sha1_digest[(20-ssidLen)],ssid,ssidLen) == 0) {
              // check if more keys will fit in the provided buffer
              if (keys >= len/(OUTPUT_KEY_SIZE))
                  goto __done;

              keys++;
              memcpy(buf,serial,6);
              sprintf(&output[offset], "[%s**%C%C%C]: ", buf, charTable[x1],charTable[x2],charTable[x3]);
              offset += OUTPUT_OVERHEAD;
              dump_key(&output[offset],sha1_digest);
              offset += DEFAULT_KEY_SIZE*2;
              strcat(&output[offset], "\n");
              output[++offset] = 0; // ensure to null-terminate the string
            }
          }
        }
      }
    }
__done:
    //fprintf(stdout,"\nFound %d potential keys.\n",keys);
    return keys;
}