forked from php/pecl-php-blenc
-
Notifications
You must be signed in to change notification settings - Fork 6
/
blenc.c
720 lines (544 loc) · 18.1 KB
/
blenc.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/*
+----------------------------------------------------------------------+
| PHP Version 7.4 - 8.2 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
| Copyright (c) 2022-2023 Tuukka Pasanen |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: John Coggeshall <[email protected]> |
| Giuseppe Chiesa <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/md5.h"
#include "ext/standard/base64.h"
#ifdef PHP_WIN32
# include "win32/time.h"
#endif
#include "php_blenc.h"
#include "blenc_protect.h"
#include "bf_algo.h"
ZEND_DECLARE_MODULE_GLOBALS(blenc)
typedef struct _blenc_header {
b_byte ident[8];
b_byte version[16];
b_byte md5[32];
b_byte reserved[16];
} blenc_header;
/* True global - no need for thread safety here */
HashTable *php_bl_keys;
/* {{{ argument information */
ZEND_BEGIN_ARG_INFO_EX(arginfo_blenc_encrypt, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, content, IS_STRING, 1)
ZEND_ARG_TYPE_INFO(0, output, IS_STRING, 1)
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 1)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ blenc_functions[] */
zend_function_entry blenc_functions[] = {
PHP_FE(blenc_encrypt, arginfo_blenc_encrypt)
{
NULL, NULL, NULL
}
};
/* }}} */
/* {{{ blenc_module_entry
*/
zend_module_entry blenc_module_entry = {
STANDARD_MODULE_HEADER,
"blenc",
blenc_functions,
PHP_MINIT(blenc),
PHP_MSHUTDOWN(blenc),
PHP_RINIT(blenc),
NULL,
PHP_MINFO(blenc),
PHP_BLENC_VERSION, /* Replace with version number for your extension */
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_BLENC
ZEND_GET_MODULE(blenc)
#endif
/* {{{ PHP_INI */
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("blenc.key_file", "/usr/local/etc/blenckeys", PHP_INI_ALL, OnUpdateString, key_file,
zend_blenc_globals, blenc_globals)
PHP_INI_END()
/* }}} */
/* {{{ php_blenc_init_globals */
static void php_blenc_init_globals(zend_blenc_globals *blenc_globals)
{
blenc_globals->key_file = NULL;
blenc_globals->keys_loaded = FALSE;
blenc_globals->decoded = NULL;
blenc_globals->decoded_len = 0;
blenc_globals->index = 0;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(blenc)
{
time_t now;
struct tm *today;
char today_char[16];
char expire_char[] = BLENC_PROTECT_EXPIRE;
char buff[10];
int k = 0, j = 0, i = 0;
ZEND_INIT_MODULE_GLOBALS(blenc, php_blenc_init_globals, NULL);
REGISTER_INI_ENTRIES();
php_bl_keys = pemalloc(sizeof(HashTable), TRUE);
zend_hash_init(php_bl_keys, 0, NULL, ZVAL_PTR_DTOR, TRUE);
zend_compile_file_old = zend_compile_file;
zend_compile_file = blenc_compile;
REGISTER_STRING_CONSTANT("BLENC_EXT_VERSION", PHP_BLENC_VERSION, CONST_CS | CONST_PERSISTENT);
/*
* check expire extension
*/
memset(today_char, '\0', sizeof(today_char));
now = time(NULL);
today = localtime(&now);
strftime(today_char, 16, "%Y%m%d", today);
#ifndef BLENC_PROTECT_COMP3
BL_G(expire_date) = pemalloc(sizeof(expire_char), TRUE);
strcpy(BL_G(expire_date), expire_char);
strncpy(buff, &expire_char[6], 4);
strncpy(&buff[4], &expire_char[3], 2);
strncpy(&buff[6], &expire_char[0], 2);
buff[8] = '\0';
#else
j = 0;
for(k = 0; k < 4; k++) {
if(expire_char[k] == 0) {
buff[j++] = '0';
buff[j++] = '0';
} else {
i = expire_char[k];
if(i < 0) {
i += 256;
}
buff[j++] = i / 16 + '0';
buff[j++] = i % 16 + '0';
}
} // for
buff[j] = '\0';
BL_G(expire_date) = pemalloc(11, TRUE);
strncpy(&BL_G(expire_date)[0], &buff[6], 2);
BL_G(expire_date)[2] = '-';
strncpy(&BL_G(expire_date)[3], &buff[4], 2);
BL_G(expire_date)[5] = '-';
strncpy(&BL_G(expire_date)[6], &buff[0], 4);
BL_G(expire_date)[10] = '\0';
#endif
if(atol(today_char) > atol(buff)) {
BL_G(expired) = TRUE;
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(blenc)
{
UNREGISTER_INI_ENTRIES();
zend_hash_destroy(php_bl_keys);
pefree(php_bl_keys, TRUE);
// FREE_HASHTABLE(php_bl_keys);
zend_compile_file = zend_compile_file_old;
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(blenc)
{
if(!BL_G(keys_loaded)) {
if(php_blenc_load_keyhash() == FAILURE) {
zend_error(E_WARNING, "BLENC: Could not load some or all of the Keys");
BL_G(keys_loaded) = FALSE;
} else {
BL_G(keys_loaded) = TRUE;
}
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(blenc)
{
php_info_print_table_start();
if(BL_G(expired)) {
php_info_print_table_row(2, "Blenc support", "Expired");
} else {
php_info_print_table_row(2, "Blenc support", "Enabled");
}
php_info_print_table_row(2, "Blenc version", PHP_BLENC_VERSION);
php_info_print_table_row(2, "Blenc expire date", BL_G(expire_date));
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
PHP_FUNCTION(blenc_encrypt)
{
unsigned char *key = NULL, *retval = NULL;
char *data = NULL, *output_file = NULL;
size_t output_len = 0, key_len = 0, data_len = 0, output_file_len = 0;
php_stream *stream;
char main_key[] = BLENC_PROTECT_MAIN_KEY;
char main_hash[33];
b_byte *bfdata = NULL;
size_t bfdata_len = 0;
zend_string *b64data = NULL;
char *rtnstr = NULL;
size_t b64data_len = 0;
blenc_header header = {BLENC_IDENT, PHP_BLENC_VERSION};
memset(main_hash, '\0', sizeof(main_hash));
php_blenc_make_md5(main_hash, main_key, strlen(main_key));
if(zend_parse_parameters(ZEND_NUM_ARGS(), "ss|s", &data, &data_len,
&output_file, &output_file_len,
&key, &key_len) == FAILURE) {
RETURN_FALSE;
}
if(key == NULL) {
/* If key if not provided and there is key file then assume that
* First key wanted to be used. If not then generate key
*/
if(BL_G(keys_loaded)
&& zend_hash_num_elements(php_bl_keys) >= 1) {
key = (unsigned char *) zend_hash_index_find_ptr(php_bl_keys, 0);
} else {
key = php_blenc_gen_key();
/* Add generated key to key chain that script that one
* can also decode encoded stuff in same php instance
*
* Add notice that we are using new that user understands
* use that one
*/
zend_hash_next_index_insert_ptr(php_bl_keys, key);
zend_error(E_NOTICE, "blenc_encode: generate new key encoding key.");
}
key_len = strlen((char *)key);
}
php_blenc_make_md5((char *)&header.md5, data, data_len);
retval = php_blenc_encode(data, key, data_len, &output_len);
bfdata = php_blenc_encode(key, (unsigned char *)main_hash, key_len, &bfdata_len);
b64data = php_base64_encode(bfdata, bfdata_len);
if((stream = php_stream_open_wrapper(output_file, "wb", REPORT_ERRORS, NULL))) {
_php_stream_write(stream, (void *)&header, (int)sizeof(blenc_header));
_php_stream_write(stream, (char *)retval, output_len);
php_stream_close(stream);
rtnstr = strdup(ZSTR_VAL(b64data));
zend_string_release(b64data);
RETVAL_STRINGL((char *)rtnstr, ZSTR_LEN(b64data));
}
efree(retval);
}
static void php_blenc_make_md5(char *result, void *data, size_t data_len)
{
PHP_MD5_CTX context;
unsigned char digest[16];
PHP_MD5Init(&context);
PHP_MD5Update(&context, data, data_len);
PHP_MD5Final(digest, &context);
make_digest(result, digest);
}
static char *php_blenc_file_to_mem(char *filename)
{
php_stream *stream;
size_t len;
zend_string *data = NULL;
char *rtnstr = NULL;
zval *stringvalue = NULL;
if (!(stream = php_stream_open_wrapper(filename, "rb", REPORT_ERRORS, NULL))) {
zend_error(E_WARNING, "php_blenc_file_to_mem: Can't find or open file: '%s'.", filename);
return NULL;
}
data = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
if (data == NULL || ZSTR_LEN(data) == 0) {
data = zend_string_init("", 0, 0);
}
php_stream_close(stream);
if(data == NULL) {
return NULL;
}
rtnstr = estrdup(ZSTR_VAL(data));
zend_string_release(data);
return rtnstr;
}
static int php_blenc_load_keyhash()
{
char *strtok_buf = NULL;
char *key = NULL;
char *keys = NULL;
char main_key[] = BLENC_PROTECT_MAIN_KEY;
unsigned char main_hash[33];
b_byte *buff = NULL;
size_t buff_len = 0;
unsigned char *bfdata = NULL;
zend_string *zstring_bfdata = NULL;
zval *rtncode = NULL;
size_t bfdata_len = 0;
keys = php_blenc_file_to_mem(BL_G(key_file));
if(!keys) {
return FAILURE;
}
memset(main_hash, '\0', sizeof(main_hash));
php_blenc_make_md5((char *)main_hash, main_key, strlen(main_key));
if(keys) {
char *t = keys;
char *temp = NULL;
while((key = php_strtok_r(t, "\n", &strtok_buf))) {
temp = NULL;
t = NULL;
if(!key) {
continue;
}
zstring_bfdata = php_base64_decode((unsigned char *)key, strlen(key));
bfdata = estrdup(ZSTR_VAL(zstring_bfdata));
bfdata_len = zstring_bfdata->len;
zend_string_release(zstring_bfdata);
buff = php_blenc_decode(bfdata, main_hash, bfdata_len, &buff_len);
temp = pestrdup((char *)buff, TRUE);
efree(bfdata);
bfdata = NULL;
rtncode = zend_hash_next_index_insert_ptr(php_bl_keys, temp);
if(Z_TYPE_P(rtncode) == IS_FALSE) {
zend_error(E_WARNING, "Could not add a key to the keyhash!");
}
temp = NULL;
}
efree(keys);
}
return SUCCESS;
}
b_byte *php_blenc_encode(void *script, unsigned char *key, size_t in_len, size_t *out_len)
{
BLOWFISH_CTX *ctx = NULL;
unsigned long hi = 0, low = 0;
size_t i = 0;
char pad_size = 0;
b_byte *input = NULL;
ctx = emalloc(sizeof(BLOWFISH_CTX));
Blowfish_Init (ctx, (unsigned char *)key, strlen((char *)key));
if((pad_size = in_len % 8)) {
pad_size = 8 - pad_size;
input = (b_byte *) estrdup(script);
input = erealloc(input, in_len + pad_size);
memset(&input[in_len], '\0', pad_size);
} else {
input = (b_byte *) estrdup(script);
pad_size = 0;
}
hi = 0x0L;
low = 0x0L;
for(i = 0; i < (in_len + pad_size); i += 8) {
hi |= (unsigned int)((char *)input)[i] & 0xFF;
hi = hi << 8;
hi |= (unsigned int)((char *)input)[i + 1] & 0xFF;
hi = hi << 8;
hi |= (unsigned int)((char *)input)[i + 2] & 0xFF;
hi = hi << 8;
hi |= (unsigned int)((char *)input)[i + 3] & 0xFF;
low |= (unsigned int)((char *)input)[i + 4] & 0xFF;
low = low << 8;
low |= (unsigned int)((char *)input)[i + 5] & 0xFF;
low = low << 8;
low |= (unsigned int)((char *)input)[i + 6] & 0xFF;
low = low << 8;
low |= (unsigned int)((char *)input)[i + 7] & 0xFF;
Blowfish_Encrypt(ctx, &hi, &low);
input[i] = hi >> 24;
input[i + 1] = hi >> 16;
input[i + 2] = hi >> 8;
input[i + 3] = hi;
input[i + 4] = low >> 24;
input[i + 5] = low >> 16;
input[i + 6] = low >> 8;
input[i + 7] = low;
hi = 0x0L;
low = 0x0L;
}
*out_len = in_len + pad_size;
efree(ctx);
return input;
}
b_byte *php_blenc_decode(void *input, unsigned char *key, size_t in_len, size_t *out_len)
{
BLOWFISH_CTX ctx;
size_t hi = 0, low = 0;
size_t retval_size = 0;
size_t i = 0;
b_byte *retval;
Blowfish_Init (&ctx, (unsigned char *)key, strlen((char *)key));
if(in_len % 8) {
zend_error(E_WARNING, "Attempted to decode non-blenc encrypted file.");
return (b_byte *)estrdup("");
} else {
retval = emalloc(in_len + 1);
}
retval_size = sizeof(retval);
memset(retval, '\0', retval_size);
hi = 0x0L;
low = 0x0L;
for(i = 0; i < in_len; i += 8) {
hi |= (unsigned int)((char *)input)[i] & 0xFF;
hi = hi << 8;
hi |= (unsigned int)((char *)input)[i + 1] & 0xFF;
hi = hi << 8;
hi |= (unsigned int)((char *)input)[i + 2] & 0xFF;
hi = hi << 8;
hi |= (unsigned int)((char *)input)[i + 3] & 0xFF;
low |= (unsigned int)((char *)input)[i + 4] & 0xFF;
low = low << 8;
low |= (unsigned int)((char *)input)[i + 5] & 0xFF;
low = low << 8;
low |= (unsigned int)((char *)input)[i + 6] & 0xFF;
low = low << 8;
low |= (unsigned int)((char *)input)[i + 7] & 0xFF;
Blowfish_Decrypt(&ctx, &hi, &low);
retval[i] = hi >> 24;
retval[i + 1] = hi >> 16;
retval[i + 2] = hi >> 8;
retval[i + 3] = hi;
retval[i + 4] = low >> 24;
retval[i + 5] = low >> 16;
retval[i + 6] = low >> 8;
retval[i + 7] = low;
hi = 0x0L;
low = 0x0L;
}
retval[in_len] = '\0';
*out_len = strlen((char *)retval);
return retval;
}
static unsigned char *php_blenc_gen_key()
{
int sec = 0, usec = 0;
struct timeval tv;
char *retval = NULL, *tmp = NULL;
PHP_MD5_CTX context;
unsigned char digest[16];
gettimeofday((struct timeval *) &tv, (struct timezone *) NULL);
sec = (int) tv.tv_sec;
usec = (int) (tv.tv_usec % 0x100000);
spprintf(&tmp, 0, "%08x%05x", sec, usec);
retval = emalloc(33);
PHP_MD5Init(&context);
PHP_MD5Update(&context, tmp, strlen(tmp));
PHP_MD5Final(digest, &context);
make_digest(retval, digest);
efree(tmp);
return (unsigned char *)retval;
}
zend_op_array *blenc_compile(zend_file_handle *file_handle, int type)
{
int i = 0;
size_t bytes;
php_stream *stream;
char *script = NULL;
b_byte *decoded = NULL;
size_t decoded_len = 0;
size_t index = 0;
size_t script_len = 0;
zend_op_array *retval = NULL;
blenc_header *header;
zend_string *decoded_code = NULL;
zend_bool validated = FALSE;
const char *file_name = NULL;
#if PHP_MAJOR_VERSION == 7 || (PHP_MAJOR_VERSION == 8 && PHP_MINOR_VERSION < 1)
file_name = file_handle->filename;
#else
file_name = ZSTR_VAL(file_handle->filename);
#endif
/*
* using php_stream instead zend internals
*/
if( (stream = php_stream_open_wrapper(file_name, "r", REPORT_ERRORS, NULL)) == NULL) {
zend_error(E_NOTICE, "blenc_compile: unable to open stream, compiling with default compiler.");
return zend_compile_file_old(file_handle, type);
}
script = emalloc(BLENC_BUFSIZE);
for(i = 2; (bytes = php_stream_read(stream, &script[index], BLENC_BUFSIZE)) > 0; i++) {
script_len += bytes;
if(bytes == BLENC_BUFSIZE) {
script = erealloc(script, BLENC_BUFSIZE * i);
index += bytes;
}
}
script_len += bytes;
php_stream_close(stream);
if(!script_len) {
zend_error(E_NOTICE, "blenc_compile: unable to read stream, compiling with default compiler.");
return zend_compile_file_old(file_handle, type);
}
/*
* check if it's a blenc script
*/
header = (blenc_header *)script;
if(!strncmp(script, BLENC_IDENT, strlen(BLENC_IDENT))) {
char *md5;
char *encoded = &script[sizeof(blenc_header)];
unsigned char *key = NULL;
if(BL_G(expired)) {
zend_error(E_ERROR, "blenc_compile: Module php_blenc was expired. Please buy a new license key or disable the module.");
return NULL;
}
ZEND_HASH_FOREACH_PTR(php_bl_keys, key)
decoded = php_blenc_decode(encoded, key, script_len - sizeof(blenc_header), &decoded_len);
md5 = emalloc(33);
php_blenc_make_md5(md5, decoded, decoded_len);
if(!strncmp(md5, (char *)header->md5, 32)) {
validated = TRUE;
efree(md5);
break;
}
efree(md5);
md5 = NULL;
efree(decoded);
decoded_len = 0;
ZEND_HASH_FOREACH_END();
if(!validated) {
zend_error(E_ERROR, "blenc_compile: Validation of script '%s' failed, cannot execute.", file_name);
return NULL;
}
}
/* File was not decoded, use standard compiler */
if (!validated || !decoded) {
return zend_compile_file_old(file_handle, type);
}
decoded_code = zend_string_init((char *)decoded, decoded_len, 0);
#if PHP_MAJOR_VERSION == 7
zval code;
ZVAL_STR(&code, decoded_code);
retval = zend_compile_string(&code, (char *)file_name);
#else
#if PHP_MINOR_VERSION < 2
retval = zend_compile_string(decoded_code, file_name);
#else
retval = zend_compile_string(decoded_code, file_name, ZEND_COMPILE_POSITION_AFTER_OPEN_TAG);
#endif
#endif
zend_string_release(decoded_code);
return retval;
}
void _php_blenc_pefree_wrapper(void **data)
{
pefree(*data, TRUE);
}