Canon Firmware Decrypter and Unpacker

 

Fixed a bug in decryption routine that allowed for further unpacking of firmware. Thanks to iiewpie for demonstrating that further unpacking is possible and prompting me to look for the bug.

/*
 * Canon *.fir files decrypter/unpacker.
 *
 * Using this program to decrypt *.fir files provided by Canon may be in violation
 * of the license agreement between you and Canon.
 *
 * Copyright 2003 Alex Bernstein.
 */
#include <stdio.h>
#include <stddef.h>

typedef unsigned short uint16;
typedef unsigned long uint32;
typedef unsigned char uchar;
typedef unsigned char boolean;
#define TRUE  (1==1)
#define FALSE (!TRUE)
#define GETLENGTH(h) ((*(uchar *)&h.length) | \
                      (*((uchar *)&h.length + 1) << 8) | \
                      (*((uchar *)&h.length + 2) << 16) | \
                      (*((uchar *)&h.length + 3) << 24))

char crypt1[] = {
0x41, 0xCA, 0x42, 0xF9, 0x3C, 0x6D, 0x9D, 0xDF, 0xD8, 0x19, 0x84, 0x08, 0x50, 0xFB, 0x67, 0xA1,
0xAD, 0x35, 0xF2, 0xCE, 0xDF, 0x9A, 0xF3, 0xC4, 0xF5, 0x22, 0x1A, 0x8A, 0xD5, 0x64, 0x08, 0x65,
0xD3, 0x2C, 0x96, 0xB6, 0x08, 0x11, 0x3A, 0xB3, 0x90, 0x08, 0xE6, 0x2E, 0x88, 0xED, 0x24, 0xFB,
0x24, 0x28, 0x82, 0x3D, 0x39, 0xA8, 0xF3, 0x52, 0x98, 0xF9, 0xA7, 0x75, 0x52, 0x63, 0x0A, 0xF9,
0xED, 0x7A, 0x82, 0x02, 0x1F, 0x94, 0x76, 0xE1, 0xBA, 0xB6, 0xB9, 0x7B, 0x6C, 0xA2, 0xDA, 0x16,
0x5B, 0x58, 0x51, 0x20, 0xBC, 0xF1, 0xD8, 0x34, 0x36, 0xAA, 0xD1, 0xA5, 0xEF, 0xF4, 0xE8, 0x50,
0xDD, 0x09, 0x95, 0x8D, 0xC9, 0x06, 0x8D, 0xF0, 0x2D, 0xA2, 0xF1, 0x0D, 0x5E, 0x14, 0x09, 0x10,
0x3E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sizeof(crypt1[0]))
#define CRYPT2_SIZE (sizeof(crypt2)/sizeof(crypt2[0]))

typedef struct {
  uint32 length;
  uint32 field4;
  char type[32];
  char description[32];
  char name[80];
} header_t;

#define HEADER_SIZE (sizeof(header_t)/sizeof(char))
#define PAGE_DECRYPT_SIZE 0x4000

uint32   file_offset = 0;

/*
 * decryption routine
 * in: buffer to decrypt, length, offset into decryption table
 */
void decrypt(uchar *buf, uint32 len, uint32 offset) {
  uint32 i = offset % CRYPT1_SIZE;
  uint32 j = offset % CRYPT2_SIZE;
  uint32 counter = len;

  while (counter) {
    *buf = *buf ^ crypt1[i] ^ crypt2[j];
    i++;
    j++;
    if (i >= CRYPT1_SIZE) i=0;
    if (j >= CRYPT2_SIZE) j=0;
    buf++;
    counter--;
  }
}

uint32 read_and_decrypt_buffer(FILE *fp, uchar *buf, uint32 len) {
  uchar *decrypt_buf = buf;
  uint32 bytes_read;

  bytes_read = fread(buf, sizeof(uchar), len, fp);
  decrypt(decrypt_buf, bytes_read, file_offset);
  file_offset += bytes_read;

  return bytes_read;
}

int main(int argc, char *argv[])
{
  FILE     *in, *out;
  header_t header;
  uint32   length;
  uint32   bytes_read;
  uchar    *image;
  char     filename[100];


  if ((argc < 2) || (argc > 3)) {
    printf("Usage: decrypt inputfile [output_directory]\n");
    return -1;
  }

  if ((in = fopen(argv[1], "r")) == NULL) {
    printf("Can't open file name %s\n", argv[1]);
    return -1;
  }

  printf("Offset     Type     Description          Name         Length\n");
  printf("---------- -------- -------------------- ------------ ------\n");
  while(TRUE) {
    bytes_read = read_and_decrypt_buffer(in, (char *)&header, HEADER_SIZE);
    if (!bytes_read) break;
    length = GETLENGTH(header);
    printf("0x%08X %-8s %-20s %-12s %6d\n", file_offset, header.type,
           header.description, header.name, length);
    if (!length) continue;

    image = (uchar *)calloc(length, sizeof(uchar));
    if (!image) {
      printf("no memory!\n");
      goto __exit;
    }
    bytes_read = read_and_decrypt_buffer(in, image, length);

    if (argc == 3) {
      /* save found program */
      strcpy(filename, argv[2]);
      strcat(filename, "/");
      strcat(filename, header.name);
      if ((out = fopen(filename, "w")) == NULL) {
        printf("Can't open file name %s\n", filename);
        free(image);
        goto __exit;
      }
      fwrite(image, sizeof(char), bytes_read, out);
      fclose(out);
    }

    free(image);
    if (bytes_read != length) {
      printf("wrong number of bytes read\n");
      goto __exit;
    }
  }

__exit:
  fclose(in);
}

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>