-
Notifications
You must be signed in to change notification settings - Fork 25
/
php_suhosin7.h
452 lines (379 loc) · 12.2 KB
/
php_suhosin7.h
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
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2015 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 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_01.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: |
+----------------------------------------------------------------------+
*/
#pragma once
extern zend_module_entry suhosin7_module_entry;
#define phpext_suhosin7_ptr &suhosin7_module_entry
#define SUHOSIN7_EXT_VERSION "0.10.0dev"
#if PHP_VERSION_ID < 70000 | PHP_VERSION_ID >= 70200
#error Suhosin7 works with PHP 7.0 and 7.1 only! Looking for Suhosin for PHP 5.x? Take a look at https://www.suhosin.org/
#endif
#ifdef PHP_WIN32
# define SUHOSIN7_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
# define SUHOSIN7_API __attribute__ ((visibility("default")))
#else
# define SUHOSIN7_API
#endif
#ifdef ZTS
#include "TSRM.h"
#endif
/* -------------- */
#define SUHOSIN_LOG "/tmp/suhosin_log.txt"
#ifdef PHP_WIN32
#define SDEBUG
#else
#ifdef SUHOSIN7_DEBUG
// #define SDEBUG(msg...) \
// {FILE *f;f=fopen(SUHOSIN_LOG, "a+");if(f){fprintf(f,"[%u] ",getpid());fprintf(f, msg);fprintf(f,"\n");fclose(f);}}
#define SDEBUG(msg...) \
{FILE *f;f=fopen(SUHOSIN_LOG, "a+");if(f){fprintf(f,"[%u] %s:%u %s #> ",getpid(), __FILE__, __LINE__, __func__);fprintf(f, msg);fprintf(f,"\n");fclose(f);}}
#else
#define SDEBUG(msg...)
#endif
#endif
/* -------------- */
#define BYTE unsigned char /* 8 bits */
#define WORD unsigned int /* 32 bits */
// PHP_MINIT_FUNCTION(suhosin);
// PHP_MSHUTDOWN_FUNCTION(suhosin);
// PHP_RINIT_FUNCTION(suhosin);
// PHP_RSHUTDOWN_FUNCTION(suhosin);
// PHP_MINFO_FUNCTION(suhosin);
#include "ext/standard/basic_functions.h"
#ifdef HAVE_PHP_SESSION
#include "ext/session/php_session.h"
#endif
static inline int suhosin_is_protected_varname(char *var, int var_len)
{
switch (var_len) {
case 18:
if (memcmp(var, "HTTP_RAW_POST_DATA", 18)==0) goto protected_varname;
break;
case 17:
if (memcmp(var, "HTTP_SESSION_VARS", 17)==0) goto protected_varname;
break;
case 16:
if (memcmp(var, "HTTP_SERVER_VARS", 16)==0) goto protected_varname;
if (memcmp(var, "HTTP_COOKIE_VARS", 16)==0) goto protected_varname;
break;
case 15:
if (memcmp(var, "HTTP_POST_FILES", 15)==0) goto protected_varname;
break;
case 14:
if (memcmp(var, "HTTP_POST_VARS", 14)==0) goto protected_varname;
break;
case 13:
if (memcmp(var, "HTTP_GET_VARS", 13)==0) goto protected_varname;
if (memcmp(var, "HTTP_ENV_VARS", 13)==0) goto protected_varname;
break;
case 8:
if (memcmp(var, "_SESSION", 8)==0) goto protected_varname;
if (memcmp(var, "_REQUEST", 8)==0) goto protected_varname;
break;
case 7:
if (memcmp(var, "GLOBALS", 7)==0) goto protected_varname;
if (memcmp(var, "_COOKIE", 7)==0) goto protected_varname;
if (memcmp(var, "_SERVER", 7)==0) goto protected_varname;
break;
case 6:
if (memcmp(var, "_FILES", 6)==0) goto protected_varname;
break;
case 5:
if (memcmp(var, "_POST", 5)==0) goto protected_varname;
break;
case 4:
if (memcmp(var, "_ENV", 4)==0) goto protected_varname;
if (memcmp(var, "_GET", 4)==0) goto protected_varname;
break;
}
return 0;
protected_varname:
return 1;
}
ZEND_BEGIN_MODULE_GLOBALS(suhosin7)
zend_bool protectkey;
zend_bool simulation;
// zend_bool stealth;
// zend_bool already_scanned;
zend_bool abort_request;
//
/* executor */
zend_ulong in_code_type;
zend_bool executor_allow_symlink;
long execution_depth;
long max_execution_depth;
long executor_include_max_traversal;
zend_bool executor_include_allow_writable_files;
// char *filter_action;
HashTable *include_whitelist;
HashTable *include_blacklist;
HashTable *func_whitelist;
HashTable *func_blacklist;
HashTable *eval_whitelist;
HashTable *eval_blacklist;
zend_bool executor_disable_eval;
zend_bool executor_disable_emod;
/* request variables */
zend_long max_request_variables;
zend_long cur_request_variables;
zend_long att_request_variables;
zend_long max_varname_length;
zend_long max_totalname_length;
zend_long max_value_length;
zend_long max_array_depth;
zend_long max_array_index_length;
char* array_index_whitelist;
char* array_index_blacklist;
zend_bool disallow_nul;
zend_bool disallow_ws;
/* cookie variables */
zend_long max_cookie_vars;
zend_long cur_cookie_vars;
zend_long att_cookie_vars;
zend_long max_cookie_name_length;
zend_long max_cookie_totalname_length;
zend_long max_cookie_value_length;
zend_long max_cookie_array_depth;
zend_long max_cookie_array_index_length;
zend_bool disallow_cookie_nul;
zend_bool disallow_cookie_ws;
/* get variables */
zend_long max_get_vars;
zend_long cur_get_vars;
zend_long att_get_vars;
zend_long max_get_name_length;
zend_long max_get_totalname_length;
zend_long max_get_value_length;
zend_long max_get_array_depth;
zend_long max_get_array_index_length;
zend_bool disallow_get_nul;
zend_bool disallow_get_ws;
/* post variables */
zend_long max_post_vars;
zend_long cur_post_vars;
zend_long att_post_vars;
zend_long max_post_name_length;
zend_long max_post_totalname_length;
zend_long max_post_value_length;
zend_long max_post_array_depth;
zend_long max_post_array_index_length;
zend_bool disallow_post_nul;
zend_bool disallow_post_ws;
/* fileupload */
zend_long upload_max_newlines;
zend_long upload_limit;
zend_long num_uploads;
zend_bool upload_disallow_elf;
zend_bool upload_disallow_binary;
zend_bool upload_remove_binary;
#ifdef SUHOSIN7_EXPERIMENTAL
zend_bool upload_allow_utf8;
#endif
char *upload_verification_script;
zend_bool no_more_variables;
zend_bool no_more_get_variables;
zend_bool no_more_post_variables;
zend_bool no_more_cookie_variables;
zend_bool no_more_uploads;
/* session */
#ifdef HAVE_PHP_SESSION
void *s_module;
void *s_original_mod;
int (*old_s_read)(PS_READ_ARGS);
int (*old_s_write)(PS_WRITE_ARGS);
int (*old_s_destroy)(PS_DESTROY_ARGS);
#endif
/* encryption */
BYTE fi[24],ri[24];
WORD fkey[120];
WORD rkey[120];
zend_bool session_encrypt;
char* session_cryptkey;
zend_bool session_cryptua;
zend_bool session_cryptdocroot;
long session_cryptraddr;
long session_checkraddr;
long session_max_id_length;
char* decrypted_cookie;
char* raw_cookie;
zend_bool cookie_encrypt;
char* cookie_cryptkey;
zend_bool cookie_cryptua;
zend_bool cookie_cryptdocroot;
long cookie_cryptraddr;
long cookie_checkraddr;
HashTable *cookie_plainlist;
HashTable *cookie_cryptlist;
/* misc */
zend_bool coredump;
// zend_bool apc_bug_workaround;
// zend_bool do_not_scan;
//
zend_bool server_encode;
zend_bool server_strip;
//
zend_bool disable_display_errors;
/* random number generator */
php_uint32 r_state[625];
php_uint32 *r_next;
int r_left;
zend_bool srand_ignore;
zend_bool mt_srand_ignore;
php_uint32 mt_state[625];
php_uint32 *mt_next;
int mt_left;
char *seedingkey;
zend_bool reseed_every_request;
//
zend_bool r_is_seeded;
zend_bool mt_is_seeded;
/* memory_limit */
zend_long memory_limit;
zend_long hard_memory_limit;
/* PERDIR Handling */
// char *perdir;
zend_bool log_perdir;
zend_bool exec_perdir;
zend_bool get_perdir;
zend_bool post_perdir;
zend_bool cookie_perdir;
zend_bool request_perdir;
zend_bool upload_perdir;
zend_bool sql_perdir;
zend_bool misc_perdir;
/* log */
zend_bool log_use_x_forwarded_for;
// long log_syslog;
// long log_syslog_facility;
// long log_syslog_priority;
// long log_script;
long log_sapi;
long log_stdout;
// char *log_scriptname;
// long log_phpscript;
// char *log_phpscriptname;
// zend_bool log_phpscript_is_safe;
long log_file;
char *log_filename;
zend_bool log_file_time;
/* header handler */
zend_bool allow_multiheader;
/* mailprotect */
// long mailprotect;
/* sqlprotect */
// zend_bool sql_bailout_on_error;
// char *sql_user_prefix;
// char *sql_user_postfix;
// char *sql_user_match;
// long sql_comment;
// long sql_opencomment;
// long sql_union;
// long sql_mselect;
// int (*old_php_body_write)(const char *str, unsigned int str_length);
ZEND_END_MODULE_GLOBALS(suhosin7)
#define SUHOSIN7_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(suhosin7, v)
#if defined(ZTS) && defined(COMPILE_DL_SUHOSIN7)
ZEND_TSRMLS_CACHE_EXTERN();
#endif
/* Error Constants */
#ifndef S_MEMORY
#define S_MEMORY (1<<0L)
#define S_MISC (1<<1L)
#define S_VARS (1<<2L)
#define S_FILES (1<<3L)
#define S_INCLUDE (1<<4L)
#define S_SQL (1<<5L)
#define S_EXECUTOR (1<<6L)
#define S_MAIL (1<<7L)
#define S_SESSION (1<<8L)
#define S_INTERNAL (1<<29L)
#define S_ALL (S_MEMORY | S_VARS | S_INCLUDE | S_FILES | S_MAIL | S_SESSION | S_MISC | S_SQL | S_EXECUTOR)
#endif
#ifndef S_GETCALLER
#define S_GETCALLER (1<<30L)
#endif
#define SUHOSIN_NORMAL 0
#define SUHOSIN_EVAL 1
#define SUHOSIN_FLAG_CREATED_BY_EVAL 1
#define SUHOSIN_FLAG_NOT_EVALED_CODE 2
ZEND_EXTERN_MODULE_GLOBALS(suhosin7)
/* functions */
// unsigned int suhosin_input_filter(int arg, char *var, char **val, size_t val_len, size_t *new_val_len);
// unsigned int suhosin_input_filter_wrapper(int arg, char *var, char **val, size_t val_len, size_t *new_val_len);
SUHOSIN7_API void suhosin_log(int loglevel, char *fmt, ...);
// extern unsigned int (*orig_input_filter)(int arg, char *var, char **val, size_t val_len, size_t *new_val_len);
char *suhosin_getenv(char *name, size_t name_len);
// hooks
void suhosin_hook_memory_limit();
void suhosin_hook_treat_data();
void suhosin_hook_input_filter();
void suhosin_hook_register_server_variables();
void suhosin_hook_header_handler();
void suhosin_unhook_header_handler();
void suhosin_hook_execute();
// void suhosin_hook_sha256();
void suhosin_hook_ex_imp();
#ifdef HAVE_PHP_SESSION
void suhosin_hook_session();
#endif
void suhosin_hook_post_handlers();
// ifilter.c
void suhosin_normalize_varname(char *varname);
size_t suhosin_strnspn(const char *input, size_t n, const char *accept);
size_t suhosin_strncspn(const char *input, size_t n, const char *reject);
// cookiecrypt.c
char *suhosin_cookie_decryptor(char *raw_cookie);
zend_string *suhosin_encrypt_single_cookie(char *name, int name_len, char *value, int value_len, char *key);
char *suhosin_decrypt_single_cookie(char *name, int name_len, char *value, int value_len, char *key, char **out);
// crypt.c
zend_string *suhosin_encrypt_string(char *str, int len, char *var, int vlen, char *key);
zend_string *suhosin_decrypt_string(char *str, int padded_len, char *var, int vlen, char *key, int check_ra);
char *suhosin_generate_key(char *key, zend_bool ua, zend_bool dr, long raddr, char *cryptkey);
#define S7_GENERATE_KEY(type, keyvar) suhosin_generate_key(SUHOSIN7_G(type ## _cryptkey), SUHOSIN7_G(type ## _cryptua), SUHOSIN7_G(type ## _cryptdocroot), SUHOSIN7_G(type ## _cryptraddr), (char *)keyvar);
// aes.c
void suhosin_aes_gentables();
void suhosin_aes_gkey(int nb,int nk,char *key);
void suhosin_aes_encrypt(char *buff);
void suhosin_aes_decrypt(char *buff);
//
static inline void suhosin_bailout()
{
if (!SUHOSIN7_G(simulation)) {
zend_bailout();
}
}
static inline char *suhosin_get_active_function_name() {
char *fn = (char *)get_active_function_name();
if (fn == NULL) {
return "unknown";
}
return fn;
}
#ifdef SUHOSIN_STRCASESTR
char *suhosin_strcasestr(char *haystack, char *needle);
#else
#define suhosin_strcasestr(a, b) strcasestr(a, b)
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/