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
|
<?php
final class ITSEC_Log { /* Critical issues are very important events that administrators should be notified about, such as finding malware * on the site or detecting a security breach. */ public static function add_critical_issue( $module, $code, $data = false, $overrides = array() ) { return self::add( $module, $code, $data, 'critical-issue', 0, $overrides ); }
/* Actions are noteworthy automated events that change the functionality of the site based upon certain criteria, * such as locking out an IP address due to bruteforce attempts. */ public static function add_action( $module, $code, $data = false, $overrides = array() ) { return self::add( $module, $code, $data, 'action', 0, $overrides ); }
/* Fatal errors are critical problems detected in the code that could and should be reserved for very rare but * highly problematic situations, such as a catch handler in a try/catch block or a shutdown handler running before * a process finishes. */ public static function add_fatal_error( $module, $code, $data = false, $overrides = array() ) { return self::add( $module, $code, $data, 'fatal', 0, $overrides ); }
/* Errors are events that indicate a failure of some sort, such as failure to write to a file or an inability to * request a remote URL. */ public static function add_error( $module, $code, $data = false, $overrides = array() ) { return self::add( $module, $code, $data, 'error', 0, $overrides ); }
/* Warnings are noteworthy events that might indicate an issue, such as finding changed files. */ public static function add_warning( $module, $code, $data = false, $overrides = array() ) { return self::add( $module, $code, $data, 'warning', 0, $overrides ); }
/* Notices keep track of events that should be tracked but do not necessarily indicate an issue, such as requests * for files that do not exist and completed scans that did not find any issues. */ public static function add_notice( $module, $code, $data = false, $overrides = array() ) { return self::add( $module, $code, $data, 'notice', 0, $overrides ); }
/* Debug events are to be used in situations where extra information about a specific process could be helpful to * have when investigating an issue but the information would typically be uninteresting to the user, such as * noting the use of a compatibility function. */ public static function add_debug( $module, $code, $data = false, $overrides = array() ) { return self::add( $module, $code, $data, 'debug', 0, $overrides ); }
/* Process events allow for creating single entries that have a start, zero or more updates, and a stopping point. * This allows for benchmarking performance of long-running code in addition to finding issues such as terminated * execution due to the missing process-stop entry. */ public static function add_process_start( $module, $code, $data = false, $overrides = array() ) { $id = self::add( $module, $code, $data, 'process-start', 0, $overrides );
return compact( 'module', 'code', 'id' ); }
public static function add_process_update( $reference, $data = false, $overrides = array() ) { self::add( $reference['module'], $reference['code'], $data, 'process-update', $reference['id'], $overrides ); }
public static function add_process_stop( $reference, $data = false, $overrides = array() ) { self::add( $reference['module'], $reference['code'], $data, 'process-stop', $reference['id'], $overrides ); }
private static function add( $module, $code, $data, $type, $parent_id = 0, $overrides = array() ) { if ( defined( 'WP_CLI' ) && WP_CLI ) { $url = 'wp-cli'; } else if ( ( is_callable( 'wp_doing_cron' ) && wp_doing_cron() ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { $url = 'wp-cron'; } else if ( isset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] ) ) { $url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; } else { $url = 'unknown'; }
$data = array_merge( array( 'parent_id' => $parent_id, 'module' => $module, 'code' => $code, 'data' => $data, 'type' => $type, 'timestamp' => gmdate( 'Y-m-d H:i:s' ), 'init_timestamp' => gmdate( 'Y-m-d H:i:s', ITSEC_Core::get_current_time_gmt() ), 'memory_current' => memory_get_usage(), 'memory_peak' => memory_get_peak_usage(), 'url' => $url, 'blog_id' => get_current_blog_id(), 'user_id' => get_current_user_id(), 'remote_ip' => ITSEC_Lib::get_ip(), ), $overrides );
$log_type = ITSEC_Modules::get_setting( 'global', 'log_type' );
if ( 'database' === $log_type ) { $id = self::add_to_db( $data ); } else if ( 'file' === $log_type ) { $id = self::add_to_file( $data ); } else { $id = self::add_to_db( $data ); self::add_to_file( $data, $id ); }
do_action( 'itsec_log_add', $data, $id, $log_type );
return $id; }
private static function add_to_db( $data ) { global $wpdb;
$format = array();
foreach ( $data as $key => $value ) { if ( is_int( $value ) ) { $format[] = '%d'; } else { $format[] = '%s';
if ( ! is_string( $value ) ) { $data[$key] = serialize( $value ); } } }
$result = $wpdb->insert( "{$wpdb->base_prefix}itsec_logs", $data, $format );
if ( false === $result ) { error_log( "Failed to insert log entry: {$wpdb->last_error}" ); return new WP_Error( 'itsec-log-failed-db-insert', sprintf( esc_html__( 'Failed to insert log entry: %s', 'better-wp-security' ), $wpdb->last_error ) ); }
return $wpdb->insert_id; }
private static function add_to_file( $data, $id = false ) { if ( false === $id ) { $id = microtime( true ); }
$file = self::get_log_file_path();
if ( is_wp_error( $file ) ) { return $file; }
$entries = array();
foreach ( $data as $value ) { if ( is_object( $value ) || is_array( $value ) ) { $value = serialize( $value ); } else { $value = (string) $value; }
$value = str_replace( '"', '""', $value );
if ( preg_match( '/[", ]/', $value ) ) { $value = "\"$value\""; }
$entries[] = $value; }
$entry = implode( ',', $entries ) . "\n";
$result = file_put_contents( $file, $entry, FILE_APPEND );
if ( false === $result ) { return new WP_Error( 'itsec-log-failed-to-write-to-file', __( 'Unable to write to the log file. This could indicate that there is no space available, that there is a permissions issue, or that the server is not configured properly.', 'better-wp-security' ) ); }
return $id; }
public static function get_log_file_path() { static $log_file = false;
if ( false !== $log_file ) { return $log_file; }
$log_location = ITSEC_Modules::get_setting( 'global', 'log_location' ); $log_info = ITSEC_Modules::get_setting( 'global', 'log_info' );
if ( empty( $log_info ) ) { $log_info = substr( sanitize_title( get_bloginfo( 'name' ) ), 0, 20 ) . '-' . wp_generate_password( 30, false );
ITSEC_Modules::set_setting( 'global', 'log_info', $log_info ); }
$log_file = "$log_location/event-log-$log_info.log";
if ( ! file_exists( $log_file ) ) { $header = "parent_id,module,code,data,type,timestamp,init_timestamp,memory_current,memory_peak,user_id,remote_ip\n";
file_put_contents( $log_file, $header ); }
return $log_file; }
public static function get_entries( $filters = array(), $limit = 0, $page = 1, $sort_by_column = 'id', $sort_direction = 'DESC', $columns = false ) { require_once( dirname( __FILE__ ) . '/log-util.php' );
return ITSEC_Log_Util::get_entries( $filters, $limit, $page, $sort_by_column, $sort_direction, $columns ); }
public static function get_entry( $id ) { require_once( dirname( __FILE__ ) . '/log-util.php' );
$entries = ITSEC_Log_Util::get_entries( array( 'id' => $id ), 0, 1, 'id', 'DESC', 'all' );
return isset( $entries[0] ) ? $entries[0] : array(); }
public static function get_number_of_entries( $filters = array() ) { $filters['__get_count'] = true; return self::get_entries( $filters ); }
public static function get_type_counts( $min_timestamp = 0 ) { require_once( dirname( __FILE__ ) . '/log-util.php' );
return ITSEC_Log_Util::get_type_counts( $min_timestamp ); }
public static function get_types_for_display() { return array( 'critical-issue' => esc_html__( 'Critical Issue', 'better-wp-security' ), 'action' => esc_html__( 'Action', 'better-wp-security' ), 'fatal' => esc_html__( 'Fatal Error', 'better-wp-security' ), 'error' => esc_html__( 'Error', 'better-wp-security' ), 'warning' => esc_html__( 'Warning', 'better-wp-security' ), 'notice' => esc_html__( 'Notice', 'better-wp-security' ), 'debug' => esc_html__( 'Debug', 'better-wp-security' ), 'process-start' => esc_html__( 'Process', 'better-wp-security' ), 'process-update' => esc_html__( 'Process Update', 'better-wp-security' ), 'process-stop' => esc_html__( 'Process Stop', 'better-wp-security' ), ); }
public static function register_events( $scheduler ) { $scheduler->schedule( ITSEC_Scheduler::S_DAILY, 'purge-log-entries' ); }
public static function purge_entries() { global $wpdb;
$database_entry_expiration = date( 'Y-m-d H:i:s', ITSEC_Core::get_current_time_gmt() - ( ITSEC_Modules::get_setting( 'global', 'log_rotation' ) * DAY_IN_SECONDS ) ); $query = $wpdb->prepare( "DELETE FROM `{$wpdb->base_prefix}itsec_logs` WHERE timestamp<%s", $database_entry_expiration ); $wpdb->query( $query );
$log_type = ITSEC_Modules::get_setting( 'global', 'log_type' );
if ( 'database' !== $log_type ) { self::rotate_log_files(); } }
public static function rotate_log_files() {
if ( $days_to_keep = ITSEC_Modules::get_setting( 'global', 'file_log_rotation' ) ) { self::delete_old_logs( $days_to_keep ); }
$log = self::get_log_file_path(); $max_file_size = 10 * 1024 * 1024; // 10MiB
if ( ! file_exists( $log ) || filesize( $log ) < $max_file_size ) { return; }
$files = glob( "$log.*" );
foreach ( $files as $index => $file ) { if ( ! preg_match( '/^' . preg_quote( $log, '/' ) . '\.\d+$/', $file ) ) { unset( $files[$index] ); } }
natsort( $files ); $files = array_values( $files );
$files_to_delete = array(); $files_to_rotate = array(); $max_files = apply_filters( 'itsec_log_max_log_files', 100 );
foreach ( $files as $index => $file ) { $number = intval( pathinfo( $file, PATHINFO_EXTENSION ) );
if ( $number > $max_files ) { $files_to_delete[] = $file; } else if ( $number === $index + 1 && $number !== $max_files ) { $files_to_rotate[] = $file; } }
array_unshift( $files_to_rotate, $log ); krsort( $files_to_rotate );
foreach ( $files_to_rotate as $index => $file ) { rename( $file, "$log." . ( $index + 1 ) ); }
touch( $log );
foreach ( $files_to_delete as $file ) { unlink( $file ); } }
private static function delete_old_logs( $days_to_keep ) {
$log = self::get_log_file_path();
if ( ! file_exists( $log ) ) { return; }
$seconds = $days_to_keep * DAY_IN_SECONDS;
$files = glob( "$log.*" );
foreach ( $files as $file ) { if ( ! $time = self::get_latest_write_for_file( $file ) ) { continue; }
if ( $time + $seconds > ITSEC_Core::get_current_time_gmt() ) { continue; }
unlink( $file ); } }
private static function get_latest_write_for_file( $file ) {
$line = self::tail( $file );
if ( ! $line ) { return false; }
return self::get_date( $line ); }
private static function get_date( $line ) { if ( ! $parsed = self::parse_csv_line( $line ) ) { return false; }
if ( ! isset( $parsed[5] ) ) { return false; }
return strtotime( $parsed[5] ); }
private static function parse_csv_line( $line ) {
if ( function_exists( 'str_getcsv' ) ) { return str_getcsv( $line ); }
require_once( ITSEC_Core::get_core_dir() . '/lib/class-itsec-lib-file.php' );
if ( ! function_exists( 'wp_tempnam' ) ) { require_once( ABSPATH . 'wp-admin/includes/file.php' ); }
$temp = wp_tempnam();
$success = ITSEC_Lib_File::write( $temp, $line );
if ( true !== $success ) { return false; }
if ( ! $fh = fopen( $temp, 'rb' ) ) { return false; }
$parsed = fgetcsv( $fh );
if ( ! is_array( $parsed ) ) { return false; }
return $parsed; }
/** * Get the last n lines of a file. * * @link https://www.geekality.net/2011/05/28/php-tail-tackling-large-files/ * * @param string $filename * @param int $lines * @param int $buffer * * @return bool|string */ private static function tail( $filename, $lines = 1, $buffer = 4096 ) { // Open the file $f = fopen( $filename, "rb" );
// Jump to last character fseek( $f, - 1, SEEK_END );
// Read it and adjust line number if necessary // (Otherwise the result would be wrong if file doesn't end with a blank line) if ( fread( $f, 1 ) !== "\n" ) { -- $lines; }
// Start reading $output = ''; $chunk = '';
// While we would like more while ( ftell( $f ) > 0 && $lines >= 0 ) { // Figure out how far back we should jump $seek = min( ftell( $f ), $buffer );
// Do the jump (backwards, relative to where we are) fseek( $f, - $seek, SEEK_CUR );
// Read a chunk and prepend it to our output $output = ( $chunk = fread( $f, $seek ) ) . $output;
// Jump back to where we started reading fseek( $f, - mb_strlen( $chunk, '8bit' ), SEEK_CUR );
// Decrease our line counter $lines -= substr_count( $chunk, "\n" ); }
// While we have too many lines // (Because of buffer size we might have read too many) while ( $lines ++ < 0 ) { // Find first newline and remove all text before that $output = substr( $output, strpos( $output, "\n" ) + 1 ); }
// Close file and return fclose( $f );
return $output; } }
add_action( 'itsec_scheduler_register_events', array( 'ITSEC_Log', 'register_events' ) ); add_action( 'itsec_scheduled_purge-log-entries', array( 'ITSEC_Log', 'purge_entries' ) );
|