Difference between revisions of "KBAG"

From The iPhone Wiki
Jump to: navigation, search
m (Fix spelling & make consistant)
m (info about 2nd cryptState)
Line 19: Line 19:
 
uint32_t tagDataSize; // size of KBAG without this 0xC header
 
uint32_t tagDataSize; // size of KBAG without this 0xC header
 
uint32_t cryptState; // 1 if the key and IV in the KBAG are encrypted with the [[GID Key]]
 
uint32_t cryptState; // 1 if the key and IV in the KBAG are encrypted with the [[GID Key]]
// 2 is used with a second KBAG for the [[S5L8920]], use is unknown.
+
// 2 if the key and IV are encrypted with the Development [[GID Key]], used when the [[Security Fusings|processor is demoted.]]
 
uint32_t aesType; // 0x80 = aes128 / 0xc0 = aes192 / 0x100 = aes256
 
uint32_t aesType; // 0x80 = aes128 / 0xc0 = aes192 / 0x100 = aes256
 
uint8_t encIV[16]; // IV for the firmware file, encrypted with the [[GID Key]]
 
uint8_t encIV[16]; // IV for the firmware file, encrypted with the [[GID Key]]
Line 31: Line 31:
 
uint32_t tagDataSize; // size of KBAG without this 0xC header
 
uint32_t tagDataSize; // size of KBAG without this 0xC header
 
uint32_t cryptState; // 1 if the key and IV in the KBAG are encrypted with the [[GID Key]]
 
uint32_t cryptState; // 1 if the key and IV in the KBAG are encrypted with the [[GID Key]]
// 2 is used with a second KBAG for the [[S5L8920]], use is unknown.
+
// 2 if the key and IV are encrypted with the Development [[GID Key]], used when the [[Security Fusings|processor is demoted.]]
 
uint32_t aesType; // 0x80 = aes128 / 0xc0 = aes192 / 0x100 = aes256
 
uint32_t aesType; // 0x80 = aes128 / 0xc0 = aes192 / 0x100 = aes256
 
uint8_t encIV[16]; // IV for the firmware file, encrypted with the [[GID Key]]
 
uint8_t encIV[16]; // IV for the firmware file, encrypted with the [[GID Key]]
Line 43: Line 43:
 
uint32_t tagDataSize; // size of KBAG without this 0xC header
 
uint32_t tagDataSize; // size of KBAG without this 0xC header
 
uint32_t cryptState; // 1 if the key and IV in the KBAG are encrypted with the [[GID Key]]
 
uint32_t cryptState; // 1 if the key and IV in the KBAG are encrypted with the [[GID Key]]
// 2 is used with a second KBAG for the [[S5L8920]], use is unknown.
+
// 2 if the key and IV are encrypted with the Development [[GID Key]], used when the [[Security Fusings|processor is demoted.]]
 
uint32_t aesType; // 0x80 = aes128 / 0xc0 = aes192 / 0x100 = aes256
 
uint32_t aesType; // 0x80 = aes128 / 0xc0 = aes192 / 0x100 = aes256
 
uint8_t encIV[16]; // IV for the firmware file, encrypted with the [[GID Key]]
 
uint8_t encIV[16]; // IV for the firmware file, encrypted with the [[GID Key]]

Revision as of 09:38, 28 September 2021

Apple's IMG3 and IMG4 security scheme uses a data format called a KBAG. At the bottom of a firmware file, you will see something that will, on the ASCII side of your hex editor, say "GABK", which, as ARM is little-endian based, is "KBAG" flipped. Look on the hex side and you will see the KBAG according to this format:

How it works

It boils down to using the GID Key to decrypt encIV and encKey, then using that key and IV to decrypt the DATA section of the file (the code itself).

Because of the circumstances with the IMG3 File Format, the kernel never needs to even touch the GID Key anymore, as its job is to just flash the image to the NOR as is, with container and all.

In order to decrypt the KBAG for img3, you need to remove them using this command: dd if=IMG3_FILE bs=1 skip=4741424B count=0x70 or for img4, dd if=IMG4FILE bs=44 skip=1 | openssl enc -aes-256-cbc -d -nopad -iv IV -K KEY > OUTPUTFILE

To grab the KBAG for img3 files, you'd run xpwntool /path/to/img3/ /dev/null.

This is different with img4 files. For these, you can use img4lib and run the following command: img4 -i /path/to/image.im4p -b.

KBAG Format

KBAG128

typedef struct Unparsed_KBAG_AES128 {
     uint32_t magic;       // string with bytes flipped ("KBAG" in little endian)
     uint32_t fullSize;    // size of KBAG from beyond that point to the end of it
     uint32_t tagDataSize; // size of KBAG without this 0xC header
     uint32_t cryptState;  // 1 if the key and IV in the KBAG are encrypted with the GID Key
                           // 2 if the key and IV are encrypted with the Development GID Key, used when the processor is demoted.
     uint32_t aesType;     // 0x80 = aes128 / 0xc0 = aes192 / 0x100 = aes256
     uint8_t encIV[16];    // IV for the firmware file, encrypted with the GID Key
     uint8_t encKey[16];   // Key for the firmware file, encrypted with the GID Key
} UnparsedKbagAes128_t;

KBAG192

typedef struct Unparsed_KBAG_AES192 {
     uint32_t magic;       // string with bytes flipped ("KBAG" in little endian)
     uint32_t fullSize;    // size of KBAG from beyond that point to the end of it
     uint32_t tagDataSize; // size of KBAG without this 0xC header
     uint32_t cryptState;  // 1 if the key and IV in the KBAG are encrypted with the GID Key
                           // 2 if the key and IV are encrypted with the Development GID Key, used when the processor is demoted.
     uint32_t aesType;     // 0x80 = aes128 / 0xc0 = aes192 / 0x100 = aes256
     uint8_t encIV[16];    // IV for the firmware file, encrypted with the GID Key
     uint8_t encKey[24];   // Key for the firmware file, encrypted with the GID Key
} UnparsedKbagAes192_t;

KBAG256

typedef struct Unparsed_KBAG_256 {
     uint32_t magic;       // string with bytes flipped ("KBAG" in little endian)
     uint32_t fullSize;    // size of KBAG from beyond that point to the end of it
     uint32_t tagDataSize; // size of KBAG without this 0xC header
     uint32_t cryptState;  // 1 if the key and IV in the KBAG are encrypted with the GID Key
                           // 2 if the key and IV are encrypted with the Development GID Key, used when the processor is demoted.
     uint32_t aesType;     // 0x80 = aes128 / 0xc0 = aes192 / 0x100 = aes256
     uint8_t encIV[16];    // IV for the firmware file, encrypted with the GID Key
     uint8_t encKey[32];   // Key for the firmware file, encrypted with the GID Key
} UnparsedKbagAes256_t;