Subversion Repositories filter_foundry

Rev

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