-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
132 lines (114 loc) · 3.5 KB
/
main.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "common.h"
#include <openssl/aes.h>
void hextou8array(char* hex, u8* buffer, u32 bytes)
{
bytes = strlen(hex) / 2;
for(int i = 0; i < bytes; i++)
{
u8 byte;
sscanf(hex, "%2hhx", &byte);
buffer[i] = byte;
hex += 2;
}
}
void decrypt(uint8_t* ciphertext, uint32_t length, uint8_t* key,
uint8_t* iv, uint8_t* plaintext)
{
AES_KEY aes_key;
AES_set_decrypt_key(key, 128, &aes_key);
memset(plaintext, 0, length);
AES_cbc_encrypt(ciphertext, plaintext, length, &aes_key, iv, AES_DECRYPT);
}
int main(int ac, char** av)
{
if(ac < 4) return 1;
FILE* infp = fopen(av[1], "rb");
if(!infp) return 2;
u8* indata = (u8*)malloc(0x04000000);
u8* outdata = (u8*)malloc(0x04000000);
u32 filesize = 0;
fseek(infp, 0, SEEK_END);
filesize = ftell(infp);
fseek(infp, 0, SEEK_SET);
char magicimg3[4];
magicimg3[0] = fgetc(infp);
magicimg3[1] = fgetc(infp);
magicimg3[2] = fgetc(infp);
magicimg3[3] = fgetc(infp);
if(magicimg3[0] == '3' && magicimg3[1] == 'g' && magicimg3[2] == 'm' && magicimg3[3] == 'I')
{
fseek(infp, 0x10, SEEK_CUR);
filesize -= 0x14;
char magicimg3tag[4];
magicimg3tag[0] = fgetc(infp);
magicimg3tag[1] = fgetc(infp);
magicimg3tag[2] = fgetc(infp);
magicimg3tag[3] = fgetc(infp);
if(magicimg3tag[0] == 'A' && magicimg3tag[1] == 'T' && magicimg3tag[2] == 'A' && magicimg3tag[3] == 'D')
{
uint32_t totallength;
fread(&totallength, 1, 4, infp);
fseek(infp, 4, SEEK_CUR);
filesize = totallength - 0xc;
if(fread(indata, 1, filesize, infp) != filesize)
{
free(indata);
free(outdata);
fclose(infp);
return 4;
}
}
u8 key[16];
u8 iv[16];
hextou8array(av[3], key, 16);
if(ac > 4) hextou8array(av[4], iv, 16);
else memset(iv, 0, 16);
decrypt(indata, filesize, key, iv, outdata);
FILE* outfp = fopen(av[2], "wb");
fwrite(outdata, 1, filesize, outfp);
fclose(outfp);
fclose(infp);
free(indata);
free(outdata);
}
else
{
fseek(infp, 0, SEEK_SET);
char magic8900[4];
magic8900[0] = fgetc(infp);
magic8900[1] = fgetc(infp);
magic8900[2] = fgetc(infp);
magic8900[3] = fgetc(infp);
u32 addr = 0x4;
if(magic8900[0] == '8' && magic8900[1] == '9' && magic8900[2] == '0' && magic8900[3] == '0')
{
fseek(infp, 3, SEEK_CUR);
addr += 0x3;
u8 format = fgetc(infp);
addr += 0x1;
fseek(infp, 4, SEEK_CUR);
addr += 0x4;
u32 datalength;
fread(&datalength, 1, 4, infp);
filesize = datalength;
fseek(infp, 0x800, SEEK_SET);
fread(indata, 1, filesize, infp);
if((format & 7) == 3)
{
u8 key_837[16];
u8 empty_iv[16];
memset(empty_iv, 0, 16);
hextou8array(av[3], key_837, 16);
decrypt(indata, filesize, key_837, empty_iv, outdata);
}
filesize -= 0x400;
FILE* outfp = fopen(av[2], "wb");
fwrite(outdata + 0x400, 1, filesize, outfp);
fclose(outfp);
}
free(indata);
free(outdata);
fclose(infp);
return 0;
}
}