rev-eng van CRC checksum voor firmware

Pagina: 1
Acties:

Vraag


Acties:
  • 0 Henk 'm!

  • Tafelpoowt
  • Registratie: Juli 2013
  • Laatst online: 17-11-2020
Ik ben de firmware van mijn WD PR4100 aan het modden, waarbij het rootfs als een squashfs image op het interne flash geheugen staat. De firmware voegt ook een checksum toe aan de squashfs image zodat bij eventuele corruptie de backup image kan ingeladen worden.
WD biedt een GPL pakket aan die mits heel veel tweaken de firmware kan bouwen.
https://support.wdc.com/downloads.aspx?p=276&lang=en

Ik wil nu enkel de bestaande image uitpakken, licht wijzigen en weer een image ervan maken met mksquashfs.
Ik zou daarbij graag deze tool die de checksum maakt omzetten in python voor wat meer transparantie.

http://s000.tinyupload.co...e_id=01947567422758218230

Deze binary zit in het GPL pakket en zal een checksum in-place toevoegen aan de file die je als argument meegeeft.

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
tfl@reveng:~$ dd if=/dev/urandom of=foo.bin bs=1 count=1k
tfl@reveng:~$ ls -l foo.bin
rwxrwxrwx 1 tfl tfl 1024 jun 24 14:56 foo.bin
tfl@reveng:~$./image_checksum foo.bin
image len = 1024
image checksum = 935c9f23
tfl@reveng:~$ ls -l foo.bin
rwxrwxrwx 1 tfl tfl 3072 jun 24 14:59 foo.bin
tfl@reveng:~$ head -c 2k foo.bin | hd
00000000  00 04 00 00 23 9f 5c 93  00 00 00 00 00 00 00 00  |....#.\.........|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000800


De tool voegt 2048 bytes toe aan het begin van de binary, die allemaal 0 zijn behalve de eerste 8 bytes.
De eerste 4 bytes komen overeen met de size van de binary (0x00004000 little endian), de andere 4 bytes zijn een checksum... maar welke??? |:(

Ik heb CRC32 en een hoop online checksums nagekeken maar geen enkele die matcht...

Enige mede tweaker met raad of zin om me te helpen deze puzzel te kraken?

Beste antwoord (via Tafelpoowt op 27-06-2017 10:36)


  • ripperke
  • Registratie: Augustus 2003
  • Laatst online: 19-08 16:06

ripperke

w00t!

De hele file wordt ingelezen en elke 4 bytes worden geXORd met de vorige 4 bytes. Niets super speciaal eigenlijk :)

Zie hier:

code:
1
2
3
4
5
6
7
8
9
10
~# dd if=/dev/urandom of=test.bin bs=1 count=1k && cp test.bin test.orig
..

~# ./image_checksum test.bin
image len = 1024
image checksum = 685fc250

~# gcc -Wall -o xor xor.c
~# ./xor test.orig
685FC250



C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>

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

        unsigned char checksum[4] = { 0x00, 0x00, 0x00, 0x00 };
        unsigned char buffer[4] = { 0x00, 0x00, 0x00, 0x00 };

        FILE *fp;
        char *inputFile = argv[1];
        fp = fopen (inputFile , "rb");
        if(!fp) perror("input"),exit(1);

        fseek(fp, 0, SEEK_END);
        int fileSize = ftell(fp);
        int i,j;

        for (i=0;i<fileSize;i+=4){
                fseek (fp, i, SEEK_SET);
                fread(buffer, 4, 1, fp);
                for(j=0;j<4;j++) {
                        checksum[j] = checksum[j] ^ buffer[j];
                }
        }

        for(j=3;j>=0;j--) { printf("%02X", checksum[j]); }
        printf("\n");

        return 0;
}

If TCP/IP handshaking was less formal, perhaps SYN/ACK would be YO/WASSUP

Alle reacties


Acties:
  • +1 Henk 'm!

  • Mijzelf
  • Registratie: September 2004
  • Niet online
Dit lijkt het te doen:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function 0x804866d (.text) {
    0x804866d: push ebp
    0x804866e: ebp = esp
    0x8048670: esp -= 16
    0x8048673: *(ebp - 4) = 0
    0x804867a: jmp loop_0x804869d
    loop {
        loop_0x804869d:
        0x804869d: eax = *(ebp + 12)
        0x80486a0: eax >>= 2
        # 0x80486a3: cmp eax, dword ptr [ebp - 4]
        # 0x80486a6: ja 0x804867c
        if (eax (unsigned) <= *(ebp - 4))  goto ret_0x80486a8
        0x804867c: eax = *(ebp + 16)
        0x804867f: edx = *(eax)
        0x8048681: eax = *(ebp - 4)
        0x8048684: ecx = (eax*4)
        0x804868b: eax = *(ebp + 8)
        0x804868e: eax += ecx
        0x8048690: eax = *(eax)
        0x8048692: edx ^= eax
        0x8048694: eax = *(ebp + 16)
        0x8048697: *(eax) = edx
        0x8048699: *(ebp - 4) += 1
    }
    ret_0x80486a8:
    0x80486a8: leave
    0x80486a9: ret
}
Heb je daar wat aan? De binary is al in het geheugen geladen als deze routine wordt aangeroepen. Ik heb dit overigens gedisassembleerd met plasma. De binary lijkt wat vreemde jumps te bevatten. Obfuscation?

Acties:
  • 0 Henk 'm!

  • Tafelpoowt
  • Registratie: Juli 2013
  • Laatst online: 17-11-2020
hmmm assembler is lang geleden voor mij... maar ik zal het eens proberen vertalen :)
alvast bedankt!

Acties:
  • Beste antwoord
  • +1 Henk 'm!

  • ripperke
  • Registratie: Augustus 2003
  • Laatst online: 19-08 16:06

ripperke

w00t!

De hele file wordt ingelezen en elke 4 bytes worden geXORd met de vorige 4 bytes. Niets super speciaal eigenlijk :)

Zie hier:

code:
1
2
3
4
5
6
7
8
9
10
~# dd if=/dev/urandom of=test.bin bs=1 count=1k && cp test.bin test.orig
..

~# ./image_checksum test.bin
image len = 1024
image checksum = 685fc250

~# gcc -Wall -o xor xor.c
~# ./xor test.orig
685FC250



C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>

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

        unsigned char checksum[4] = { 0x00, 0x00, 0x00, 0x00 };
        unsigned char buffer[4] = { 0x00, 0x00, 0x00, 0x00 };

        FILE *fp;
        char *inputFile = argv[1];
        fp = fopen (inputFile , "rb");
        if(!fp) perror("input"),exit(1);

        fseek(fp, 0, SEEK_END);
        int fileSize = ftell(fp);
        int i,j;

        for (i=0;i<fileSize;i+=4){
                fseek (fp, i, SEEK_SET);
                fread(buffer, 4, 1, fp);
                for(j=0;j<4;j++) {
                        checksum[j] = checksum[j] ^ buffer[j];
                }
        }

        for(j=3;j>=0;j--) { printf("%02X", checksum[j]); }
        printf("\n");

        return 0;
}

If TCP/IP handshaking was less formal, perhaps SYN/ACK would be YO/WASSUP


Acties:
  • 0 Henk 'm!

  • Tafelpoowt
  • Registratie: Juli 2013
  • Laatst online: 17-11-2020
OMG. Super bedankt! Ik was al met pen en papier aan de assembler code begonnen maar zat met de handen in het haar. Hiermee kan ik weer verder.

Allebei super bedankt nogmaals. <3 tweakers