Subversion Repositories filter_foundry

Rev

Rev 309 | Rev 312 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
292 daniel-mar 1
/*
2
    This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
3
    Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au
4
    Copyright (C) 2018-2021 Daniel Marschall, ViaThinkSoft
5
 
6
    This program is free software; you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
8
    the Free Software Foundation; either version 2 of the License, or
9
    (at your option) any later version.
10
 
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public License
17
    along with this program; if not, write to the Free Software
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
*/
20
 
21
#include <stddef.h>
22
#include <stdint.h>
23
#include <assert.h>
24
#include <time.h>
25
 
26
#include "ff.h"
27
 
28
const volatile uint32_t cObfuscV4Seed = 0x52830517; // this value will be manipulated during the building of each individual filter (see make_win.c)
29
 
30
int rand_msvcc(unsigned int* seed) {
31
        *seed = *seed * 214013L + 2531011L;
32
        return (*seed >> 16) & 0x7fff; /* Scale between 0 and RAND_MAX */
33
}
34
 
35
int rand_openwatcom(unsigned int* seed) {
36
        // https://github.com/open-watcom/open-watcom-v2/blob/master/bld/clib/math/c/rand.c
37
        *seed = *seed * 1103515245L + 12345L;
38
        return (*seed >> 16) & 0x7fff; /* Scale between 0 and RAND_MAX */
39
}
40
 
293 daniel-mar 41
void xorshift(unsigned char** p, uint32_t* x32, size_t num) {
292 daniel-mar 42
        size_t i;
43
        unsigned char* x = *p;
44
        for (i = 0; i < num; i++) {
45
                // https://de.wikipedia.org/wiki/Xorshift
46
                *x32 ^= *x32 << 13;
47
                *x32 ^= *x32 >> 17;
48
                *x32 ^= *x32 << 5;
49
                *x++ ^= *x32;
50
        }
51
        *p = x;
52
}
53
 
293 daniel-mar 54
int obfuscation_version(PARM_T* pparm) {
309 daniel-mar 55
        uint32_t obfusc_info = pparm->unknown2;
292 daniel-mar 56
 
57
        if (obfusc_info == 0x00000000) { // 00 00 00 00
58
                // Photoshop FilterFactory default initialization
59
                // (no obfuscation)
60
                return 0;
61
        }
62
        else if (obfusc_info == 0x00000001) { // 01 00 00 00
63
                // Premiere FilterFactory default initialization
64
                // (no obfuscation)
65
                return 0;
66
        }
67
        else if (obfusc_info == 0x90E364A3) { // A3 64 E3 90
68
                // Version 1 obfuscation (Filter Foundry 1.4b8,9,10)
69
                return 1;
70
        }
71
        else if (obfusc_info == 0xE2CFCA34) { // 34 CA CF E2
72
                // Version 2 obfuscation (Filter Foundry 1.7b1)
73
                return 2;
74
        }
293 daniel-mar 75
        else if ((obfusc_info >= 4) && (obfusc_info <= 0xFF)) { // xx 00 00 00
292 daniel-mar 76
                // Version 4 obfuscation (Filter Foundry 1.7.0.7)
77
                // Version 5 obfuscation (Filter Foundry 1.7.0.8)
293 daniel-mar 78
                // Future: Version 6, 7, 8, ... 255
292 daniel-mar 79
                return obfusc_info;
80
        }
81
        else {
82
                // Version 3 obfuscation (Filter Foundry 1.7.0.5)
83
                // obfusc_info is the srand() seed and is equal to the time(0) build timestamp
84
                return 3;
85
        }
86
}
87
 
311 daniel-mar 88
uint32_t crc32b(char *data, int nLength) {
89
        int i, j, k;
90
        unsigned int byte, crc, mask;
91
 
92
        i = 0;
93
        crc = 0xFFFFFFFF;
94
 
95
        for(k=0;k<nLength;k++) {
96
                byte = data[k];
97
                crc = crc ^ byte;
98
                for (j = 7; j >= 0; j--) {
99
                        mask = -(crc & 1);
100
                        crc = (crc >> 1) ^ (0xEDB88320 & mask);
101
                }
102
                i++;
103
        }
104
        return ~crc;
105
}
106
 
309 daniel-mar 107
uint32_t obfusc(PARM_T* pparm) {
293 daniel-mar 108
        // Windows:   Version 5 obfuscation (Introduced in Filter Foundry 1.7.0.8)
109
        // Macintosh: Version 4 obfuscation (Introduced in Filter Foundry 1.7.0.7)
292 daniel-mar 110
 
111
        unsigned char* p;
112
        size_t size, seed_position;
309 daniel-mar 113
        uint32_t seed, initial_seed;
114
        uint32_t obfusc_version;
292 daniel-mar 115
 
116
#ifdef MAC_ENV
293 daniel-mar 117
        // For Mac, we use obfuscation version 4, because the placing the seed into the produced executable code is not implemented in Mac!
118
        // (It needs to be implemented in make_mac.c)
119
        initial_seed = OBFUSC_V4_DEFAULT_SEED;
120
        obfusc_version = 4;
292 daniel-mar 121
#else
293 daniel-mar 122
        // In obfuscation version 5, the seed is also the checksum. It will be verified at deobfusc()!
311 daniel-mar 123
        initial_seed = crc32b(pparm,sizeof(PARM_T));
293 daniel-mar 124
        obfusc_version = 5;
292 daniel-mar 125
#endif
126
 
127
        seed_position = offsetof(PARM_T, unknown2);
128
        size = sizeof(PARM_T);
293 daniel-mar 129
        seed = initial_seed;
292 daniel-mar 130
 
309 daniel-mar 131
        if (obfusc_version == 5) {
132
                // make v4 and v5 intentionally incompatible to avoid a downgrade-attack
133
                seed ^= 0xFFFFFFFF;
134
        }
135
 
292 daniel-mar 136
        p = (unsigned char*)pparm;
293 daniel-mar 137
        xorshift(&p, &seed, seed_position);
309 daniel-mar 138
        *((uint32_t*)p) = obfusc_version;
292 daniel-mar 139
        p += 4;
293 daniel-mar 140
        xorshift(&p, &seed, size - seed_position - 4);
292 daniel-mar 141
 
142
        return initial_seed;
143
}
144
 
145
void deobfusc(PARM_T* pparm) {
309 daniel-mar 146
        uint32_t obfusc_version;
292 daniel-mar 147
        size_t size = sizeof(PARM_T);
148
 
293 daniel-mar 149
        obfusc_version = obfuscation_version(pparm);
292 daniel-mar 150
 
151
        switch (obfusc_version) {
152
                case 0:
153
                        // no obfuscation
154
                        return;
155
                case 1: {
156
                        // Version 1 obfuscation (Filter Foundry 1.4b8,9,10)
157
                        // Filter built with VC++ (official release by Toby Thain)
158
 
159
                        unsigned char* p;
160
                        size_t i;
309 daniel-mar 161
                        uint32_t seed;
292 daniel-mar 162
 
163
                        seed = 0xdc43df3c;
164
 
165
                        for (i = size, p = (unsigned char*)pparm; i--;) {
166
                                *p++ ^= rand_msvcc(&seed);
167
                        }
168
                        break;
169
                }
170
                case 2: {
171
                        // Version 2 obfuscation (Filter Foundry 1.7b1)
172
                        // Compiler independent
173
 
174
                        unsigned char* p;
175
                        size_t i;
309 daniel-mar 176
                        uint32_t seed;
292 daniel-mar 177
 
178
                        seed = 0x95d4a68f;
179
 
180
                        for (i = size, p = (unsigned char*)pparm; i--;) {
181
                                seed ^= seed << 13;
182
                                seed ^= seed >> 17;
183
                                seed ^= seed << 5;
184
                                *p++ ^= seed;
185
                        }
186
                        break;
187
                }
188
                case 3: {
189
                        // Version 3 obfuscation (Filter Foundry 1.7.0.5)
190
                        // NO loading of other implementation supported, but that doesn't matter since
191
                        // obfuscation and protection is combined in Filter Factory >= 1.7.0.5.
192
                        // Using rand() is more secure, because it differs from compiler to compiler, so
193
                        // it is harder to read a protected 8BF plugin.
194
                        // Note that rand() in combination with srand() is deterministic, so it is safe
195
                        // to use it: https://stackoverflow.com/questions/55438293/does-rand-function-in-c-follows-non-determinstc-algorithm
196
                        // Note: 32-Bit FF is built using OpenWatcom (to support Win95), while 64-Bit FF is built using Microsoft Visual C++
197
 
198
                        unsigned char* p;
199
                        size_t i;
309 daniel-mar 200
                        uint32_t seed;
292 daniel-mar 201
                        size_t seed_position;
202
 
203
                        seed = pparm->unknown2;
204
                        seed_position = offsetof(PARM_T, unknown2); // = offsetof(PARM_T_PREMIERE, unknown1)
205
 
206
                        srand(seed);
207
                        p = (unsigned char*)pparm;
208
                        for (i = 0; i < seed_position; i++) *p++ ^= rand();
309 daniel-mar 209
                        *((uint32_t*)p) = 0; // here was the seed. Fill it with 0x00000000
292 daniel-mar 210
                        p += 4;
211
                        for (i = 0; i < size - seed_position - 4; i++) *p++ ^= rand();
212
                        break;
213
                }
214
                case 4:
215
                case 5: {
216
                        // Version 4 obfuscation (Filter Foundry 1.7.0.7)
217
                        // Version 5 obfuscation (Filter Foundry 1.7.0.8)
218
                        // Not compiler dependent, but individual for each build
219
                        // It is important that this code works for both x86 and x64 indepdently from the used compiler,
220
                        // otherwise, the cross-make x86/x64 won't work!
221
                        // Version 5 contains a seed requirement (checksum).
222
 
223
                        unsigned char* p;
224
                        size_t seed_position;
309 daniel-mar 225
                        uint32_t seed, initial_seed;
292 daniel-mar 226
 
309 daniel-mar 227
                        initial_seed = cObfuscV4Seed; // this value will be manipulated during the building of each individual filter (see make_win.c)
228
 
229
                        seed = initial_seed;
292 daniel-mar 230
                        seed_position = offsetof(PARM_T, unknown2); // = offsetof(PARM_T_PREMIERE, unknown1)
231
 
309 daniel-mar 232
                        if (obfusc_version == 5) {
233
                                // make v4 and v5 intentionally incompatible to avoid a downgrade-attack
234
                                seed ^= 0xFFFFFFFF;
235
                        }
236
 
292 daniel-mar 237
                        p = (unsigned char*)pparm;
293 daniel-mar 238
                        xorshift(&p, &seed, seed_position);
292 daniel-mar 239
                        p += 4; // obfusc info == 4
293 daniel-mar 240
                        xorshift(&p, &seed, size - seed_position - 4);
292 daniel-mar 241
 
242
                        if (obfusc_version == 5) {
311 daniel-mar 243
                                if (crc32b(pparm,sizeof(PARM_T)) != initial_seed) {
292 daniel-mar 244
                                        // Integrity check failed!
309 daniel-mar 245
                                        memset(pparm, 0, sizeof(PARM_T)); // invalidate everything
292 daniel-mar 246
                                }
247
                        }
248
 
249
                        break;
250
                }
251
                default: {
252
                        // Obfuscation version unexpected!
309 daniel-mar 253
                        memset(pparm, 0, sizeof(PARM_T)); // invalidate everything
311 daniel-mar 254
 
255
                        // If "return" is present: Calling function will receive an invalid cbSize value, hence showing "incompatible obfuscation"
256
                        // If "return" is not present: Then the code below will set a correct cbSize and iProtect flag if obfusc_version>=3, which will raise the error "filter is protected"
257
                        return;
292 daniel-mar 258
                }
259
        }
260
 
309 daniel-mar 261
        if ((pparm->cbSize != PARM_SIZE) &&
262
                //(pparm->cbSize != PARM_SIZE_PREMIERE) &&
263
                (pparm->cbSize != PARM_SIG_MAC)) {
264
                memset(pparm, 0, sizeof(PARM_T)); // invalidate everything
265
        }
266
 
292 daniel-mar 267
        if (obfusc_version >= 3) {
268
                // Filter Foundry >= 1.7.0.5 builds combines obfuscation and protection
269
                // when a standalone filter is built. Theoretically, you can un-protect a
270
                // plugin, even if it is obfuscated, just by bit-flipping the LSB of byte 0x164.
271
                // Therefore, we enforce that the plugin is protected!
272
                pparm->iProtected = 1;
273
 
274
                // Furthermore, if obfuscation 3+ failed (since the seed is individual for each 8BF file),
293 daniel-mar 275
                // we still want that load_*.c is able to detect pparm->iProtected instead
276
                // of throwing the error "Incompatible obfuscation".
292 daniel-mar 277
                pparm->cbSize = PARM_SIZE;
278
        }
279
}