-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBitSensor.php
More file actions
426 lines (373 loc) · 11 KB
/
BitSensor.php
File metadata and controls
426 lines (373 loc) · 11 KB
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
<?php
namespace BitSensor\Core;
use BitSensor\Blocking\Blocking;
use BitSensor\Connector\Connector;
use BitSensor\Handler\AfterRequestHandler;
use BitSensor\Handler\CodeErrorHandler;
use BitSensor\Handler\ExceptionHandler;
use BitSensor\Handler\Handler;
use BitSensor\Handler\HttpRequestHandler;
use BitSensor\Handler\InterfaceHandler;
use BitSensor\Handler\IpHandler;
use BitSensor\Handler\ModSecurityHandler;
use BitSensor\Handler\PluginHandler;
use BitSensor\Handler\RequestInputHandler;
use BitSensor\Hook\MysqliHook;
use BitSensor\Hook\PDOHook;
use BitSensor\Util\Log;
use Proto\Datapoint;
use Proto\GeneratedBy;
use Proto\Invocation;
/**
* Entry point for starting the BitSensor Web Application Security plugin.
* @package BitSensor\Core
*/
class BitSensor
{
const VERSION = '1.0.5';
/**
* Reference to the global Datapoint instance.
*
* @var Datapoint
*/
private static $datapoint;
/**
* @var Connector to remote endpoint
*/
private static $connector;
/**
* @var Blocking $blocking
*/
private static $blocking;
/**
* Handlers that should run on each request
*
* @var Handler[]
*/
private static $handlers = [];
/**
* ErrorHandler method set by the application to be called
*
* @var mixed
**/
public static $errorHandler;
/**
* ExceptionHandler method set by the application to be called
*
* @var callable
**/
public static $exceptionHandler;
/**
* To be called on request finish if {@see setEnbaleShutdownHandler} is true
*
* @var AfterRequestHandler to be called on request finish
*/
private static $afterRequestHandler;
/**
* Enable processing after request is finished, in general this
* is used to signal the {@see Connector} to send the {@see Datapoint}
*
* @var boolean
*/
private static $enbaleShutdownHandler = true;
/**
* Uopz Hooking. Turning this on enables BitSensor to hook into function calls.
*/
private static $enbaleUopzHook = false;
/**
* Running mode of BitSensor pipeline. Default is {@see MODE_IDS}.
*
* @var string
*/
private static $mode = self::MODE_IDS;
/**
* Process pipeline, false positive detection, auto-blocking
*/
const MODE_IDS = 'ids';
/**
* Process pipeline, false positive detection
*/
const MODE_MONITORING = 'monitoring';
/**
* Only log raw input datapoints.
*/
const MODE_OFF = 'off';
/**
* Log level.
*
* @var string
*/
public static $logLevel = E_ERROR;
private static $config;
/**
* BitSensor is static.
*/
private function __construct()
{
}
static function init()
{
self::initializeDatapoint();
self::$handlers = [];
self::$afterRequestHandler = new AfterRequestHandler();
self::setBlocking(new Blocking());
if (!defined('BITSENSOR_WORKING_DIR')) {
/**
* Working directory when the application started.
*/
define('BITSENSOR_WORKING_DIR', getcwd());
}
if (!isset(self::$errorHandler)) {
self::$errorHandler = set_error_handler([CodeErrorHandler::class, 'handle']);
Log::d("Previous error handler is: " . (is_null(self::$errorHandler) ?
"not defined" : (is_array(self::$errorHandler) ?
(is_string(self::$errorHandler[0]) ? implode(self::$errorHandler) : get_class(self::$errorHandler[0])) : self::$errorHandler)));
}
if (!isset(self::$exceptionHandler)) {
self::$exceptionHandler = set_exception_handler([ExceptionHandler::class, 'handle']);
/** @noinspection PhpParamsInspection */
Log::d("Previous exception handler is: " . (is_null(self::$exceptionHandler) ?
"not defined" : (is_array(self::$exceptionHandler) ?
(is_string(self::$exceptionHandler[0]) ? implode(self::$exceptionHandler) : get_class(self::$exceptionHandler[0])) : self::$exceptionHandler)));
}
}
/**
* @param string|string[] $configOrPath Object with configuration.
*/
public static function configure($configOrPath = 'config.json')
{
if (is_array($configOrPath)) {
$config = $configOrPath;
} else {
$config = json_decode(file_get_contents($configOrPath), true);
Log::d('BitSensor loaded configuration from location ' . $configOrPath);
}
self::$config = $config;
if (array_key_exists('uopzHook', $config))
self::setEnableUopzHook($config['uopzHook'] == 'on' ? true : false);
if (array_key_exists('mode', $config))
self::setMode($config['mode']);
if (array_key_exists('logLevel', $config))
self::setLogLevel($config['logLevel']);
self::$afterRequestHandler->configure($config);
if (array_key_exists('connector', $config))
self::createConnector($config['connector']);
if (array_key_exists('blocking', $config))
self::createBlocking($config['blocking']);
}
/**
* @throws \Exception
*/
public static function run()
{
//Add handlers
self::addHandler(PluginHandler::class);
self::addHandler(IpHandler::class);
self::addHandler(HttpRequestHandler::class);
self::addHandler(RequestInputHandler::class);
self::addHandler(ModSecurityHandler::class);
self::addHandler(InterfaceHandler::class);
// Post request handling
if (self::$enbaleShutdownHandler)
register_shutdown_function([self::$afterRequestHandler, 'handle'], self::$datapoint);
// Load plugins
if (self::$enbaleUopzHook) {
PDOHook::instance()->start();
MysqliHook::instance()->start();
}
self::runHandlers();
if (isset(self::$blocking)) {
self::getBlocking()->handle(self::$datapoint);
}
}
/**
* Run all registered Handlers to collect data about the current request.
*/
private static function runHandlers()
{
if (empty(self::$handlers))
return;
foreach (self::$handlers as $handler) {
$handler->handle(self::$datapoint);
}
}
private static function initializeDatapoint()
{
$datapoint = new Datapoint();
$invocation = new Invocation();
$datapoint->setInvocation($invocation);
self::$datapoint = $datapoint;
}
/**
* @param string[]|string $connectorConfiguration Can be of type string with name,
* or of type string[] with configuration. In the latter case, uses [type] to create
* Connector object.
*/
public static function createConnector($connectorConfiguration)
{
if (empty($connectorConfiguration))
return;
/** If configuration is set using an assoc string[] array, pass it along */
if (is_string($connectorConfiguration)) {
$type = $connectorConfiguration;
$passConfiguration = false;
} else {
$type = $connectorConfiguration['type'];
$passConfiguration = true;
}
if (strpos($type, '\\')) {
self::setConnector(new $type($passConfiguration ? $connectorConfiguration : null));
} else {
$bitSensorType = '\\BitSensor\\Connector\\' . ucfirst($type) . "Connector";
self::setConnector(new $bitSensorType ($passConfiguration ? $connectorConfiguration : null));
}
}
/**
* @param string[] $blockingConfiguration
*/
public static function createBlocking($blockingConfiguration)
{
Blocking::configure($blockingConfiguration);
}
/**
* Adds a new {@link Handler} that should run to collect data about the current request.
*
* @param String|Handler $handler
* @return mixed
*/
public static function addHandler($handler)
{
if (is_string($handler))
return self::$handlers[] = new $handler(self::$config);
return self::$handlers[] = $handler;
}
/**
* Adds a new entry to the context map.
*
* @param $key
* @param $value
*/
public static function putContext($key, $value)
{
self::$datapoint->getContext()[$key] = $value;
}
/**
* Adds a new entry to the endpoint map.
*
* @param $key
* @param $value
*/
public static function putEndpoint($key, $value)
{
self::$datapoint->getEndpoint()[$key] = $value;
}
/**
* Adds a new {@link Error} to the error collection.
*
* @param \Proto\Error $error
*/
public static function addError(\Proto\Error $error)
{
$error->setGeneratedBy(GeneratedBy::PLUGIN);
self::$datapoint->getErrors()[] = $error;
}
/**
* Adds a new entry to the input map.
*
* @param $key
* @param $value
*/
public static function putInput($key, $value)
{
self::$datapoint->getInput()[$key] = $value;
}
/**
* @return Datapoint
*/
public static function getDatapoint()
{
return self::$datapoint;
}
/**
* The Invocation object acts as a namespace for specialized invocation objects.
* These can be added by user-defined Handlers.
*
* @return Invocation object holding specialized invocation objects.
*/
public static function getInvocations()
{
return self::$datapoint->getInvocation();
}
/**
* @return Connector
*/
public static function getConnector()
{
return self::$connector;
}
/**
* @return Blocking
*/
private static function getBlocking()
{
return self::$blocking;
}
/**
* @param Blocking $blocking
*/
public static function setBlocking($blocking)
{
self::$blocking = $blocking;
}
/**
* @param Connector $connector
*/
public static function setConnector($connector)
{
self::$connector = $connector;
}
/**
* @param bool $enbaleShutdownHandler
*/
public static function setEnbaleShutdownHandler($enbaleShutdownHandler)
{
self::$enbaleShutdownHandler = $enbaleShutdownHandler;
}
/**
* @return string
*/
public static function getMode()
{
return self::$mode;
}
/**
* @param string $mode
*/
public static function setMode($mode)
{
self::$mode = $mode;
}
/**
* @param mixed $enbaleUopzHook
*/
public static function setEnableUopzHook($enbaleUopzHook)
{
self::$enbaleUopzHook = $enbaleUopzHook;
}
/**
* @param string $logLevel
*/
public static function setLogLevel($logLevel)
{
self::$logLevel = $logLevel;
}
/**
* @return string Log level.
*/
public static function getLogLevel()
{
return self::$logLevel;
}
}
// Execute static initialization
BitSensor::init();