1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PragmaRX\Firewall; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use PragmaRX\Firewall\Repositories\DataRepository; |
7
|
|
|
use PragmaRX\Firewall\Repositories\Message; |
8
|
|
|
use PragmaRX\Firewall\Support\AttackBlocker; |
9
|
|
|
use PragmaRX\Firewall\Support\Redirectable; |
10
|
|
|
use PragmaRX\Firewall\Support\Responder; |
11
|
|
|
use PragmaRX\Support\CacheManager; |
12
|
|
|
use PragmaRX\Support\Config; |
13
|
|
|
use PragmaRX\Support\FileSystem; |
14
|
|
|
use PragmaRX\Support\GeoIp\Updater as GeoIpUpdater; |
15
|
|
|
|
16
|
|
|
class Firewall |
17
|
|
|
{ |
18
|
|
|
use Redirectable; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The IP adress. |
22
|
|
|
* |
23
|
|
|
* @var |
24
|
|
|
*/ |
25
|
|
|
private $ip; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The config object. |
29
|
|
|
* |
30
|
|
|
* @var Config |
31
|
|
|
*/ |
32
|
|
|
private $config; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The cache manager objetc. |
36
|
|
|
* |
37
|
|
|
* @var CacheManager |
38
|
|
|
*/ |
39
|
|
|
private $cache; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The file system object. |
43
|
|
|
* |
44
|
|
|
* @var FileSystem |
45
|
|
|
*/ |
46
|
|
|
private $fileSystem; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The data repository object. |
50
|
|
|
* |
51
|
|
|
* @var DataRepository |
52
|
|
|
*/ |
53
|
|
|
private $dataRepository; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The request. |
57
|
|
|
* |
58
|
|
|
* @var Request |
59
|
|
|
*/ |
60
|
|
|
private $request; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The geop ip object. |
64
|
|
|
* |
65
|
|
|
* @var GeoIp |
|
|
|
|
66
|
|
|
*/ |
67
|
|
|
public $geoIp; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The attack blocker. |
71
|
|
|
* |
72
|
|
|
* @var AttackBlocker |
73
|
|
|
*/ |
74
|
|
|
private $attackBlocker; |
75
|
|
|
/** |
76
|
|
|
* @var Message |
77
|
|
|
*/ |
78
|
|
|
private $messageRepository; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Initialize Firewall object. |
82
|
|
|
* |
83
|
|
|
* @param Config $config |
84
|
|
|
* @param DataRepository $dataRepository |
85
|
|
|
* @param CacheManager $cache |
86
|
|
|
* @param FileSystem $fileSystem |
87
|
|
|
* @param Request $request |
88
|
|
|
* @param AttackBlocker $attackBlocker |
89
|
|
|
* @param Message $messageRepository |
90
|
|
|
*/ |
91
|
|
|
public function __construct( |
92
|
|
|
Config $config, |
93
|
|
|
DataRepository $dataRepository, |
94
|
|
|
CacheManager $cache, |
95
|
|
|
FileSystem $fileSystem, |
96
|
|
|
Request $request, |
97
|
|
|
AttackBlocker $attackBlocker, |
98
|
|
|
Message $messageRepository |
99
|
|
|
) { |
100
|
|
|
$this->config = $config; |
101
|
|
|
|
102
|
|
|
$this->dataRepository = $dataRepository; |
103
|
|
|
|
104
|
|
|
$this->cache = $cache; |
105
|
|
|
|
106
|
|
|
$this->fileSystem = $fileSystem; |
107
|
|
|
|
108
|
|
|
$this->request = $request; |
109
|
|
|
|
110
|
|
|
$this->attackBlocker = $attackBlocker; |
111
|
|
|
|
112
|
|
|
$this->messageRepository = $messageRepository; |
113
|
|
|
|
114
|
|
|
$this->setIp(null); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get all IP addresses. |
119
|
|
|
* |
120
|
|
|
*/ |
121
|
|
|
public function all() |
122
|
|
|
{ |
123
|
|
|
return $this->dataRepository->all(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get all IP addresses by country. |
128
|
|
|
* |
129
|
|
|
* @param $country |
130
|
|
|
* @return static |
131
|
|
|
*/ |
132
|
|
|
public function allByCountry($country) |
133
|
|
|
{ |
134
|
|
|
return $this->dataRepository->allByCountry($country); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Blacklist an IP adress. |
139
|
|
|
* |
140
|
|
|
* @param $ip |
141
|
|
|
* @param bool $force |
142
|
|
|
* |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function blacklist($ip, $force = false) |
146
|
|
|
{ |
147
|
|
|
return $this->dataRepository->addToList(false, $ip, $force); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Create a blocked access response. |
152
|
|
|
* |
153
|
|
|
* @return \Illuminate\Http\Response|void |
154
|
|
|
* |
155
|
|
|
* @internal param null $content |
156
|
|
|
* @internal param null $status |
157
|
|
|
*/ |
158
|
|
|
public function blockAccess() |
159
|
|
|
{ |
160
|
|
|
return (new Responder())->respond( |
|
|
|
|
161
|
|
|
$this->config->get('response') |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Clear firewall table. |
167
|
|
|
* |
168
|
|
|
* @return mixed |
169
|
|
|
*/ |
170
|
|
|
public function clear() |
171
|
|
|
{ |
172
|
|
|
return $this->dataRepository->clear(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Find an IP address. |
177
|
|
|
* |
178
|
|
|
* @param string $ip |
179
|
|
|
* |
180
|
|
|
* @return object|array|null |
181
|
|
|
*/ |
182
|
|
|
public function find($ip) |
183
|
|
|
{ |
184
|
|
|
return $this->dataRepository->find($ip); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the IP address. |
189
|
|
|
* |
190
|
|
|
* @return mixed |
191
|
|
|
*/ |
192
|
|
|
public function getIp() |
193
|
|
|
{ |
194
|
|
|
return $this->ip; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get the messages. |
199
|
|
|
* |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
|
|
public function getMessages() |
203
|
|
|
{ |
204
|
|
|
return $this->messageRepository->getMessages(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Check if IP address is valid. |
209
|
|
|
* |
210
|
|
|
* @param $ip |
211
|
|
|
* |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
|
|
public function ipIsValid($ip) |
215
|
|
|
{ |
216
|
|
|
return $this->dataRepository->ipIsValid($ip); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Check if IP is blacklisted. |
221
|
|
|
* |
222
|
|
|
* @param null $ip |
|
|
|
|
223
|
|
|
* |
224
|
|
|
* @return bool |
225
|
|
|
*/ |
226
|
|
|
public function isBlacklisted($ip = null) |
227
|
|
|
{ |
228
|
|
|
$list = $this->whichList($ip); |
229
|
|
|
|
230
|
|
|
return !($list == 'whitelist') && |
231
|
|
|
$list == 'blacklist'; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Check if IP address is whitelisted. |
236
|
|
|
* |
237
|
|
|
* @param null $ip |
|
|
|
|
238
|
|
|
* |
239
|
|
|
* @return bool |
240
|
|
|
*/ |
241
|
|
|
public function isWhitelisted($ip = null) |
242
|
|
|
{ |
243
|
|
|
return $this->whichList($ip) == 'whitelist'; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Register messages in log. |
248
|
|
|
* |
249
|
|
|
* @param $message |
250
|
|
|
* @return void |
251
|
|
|
*/ |
252
|
|
|
public function log($message) |
253
|
|
|
{ |
254
|
|
|
if ($this->config->get('enable_log')) { |
255
|
|
|
app()->log->info("FIREWALL: $message"); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Remove IP from all lists. |
261
|
|
|
* |
262
|
|
|
* @param $ip |
263
|
|
|
* |
264
|
|
|
* @return bool |
265
|
|
|
*/ |
266
|
|
|
public function remove($ip) |
267
|
|
|
{ |
268
|
|
|
return $this->dataRepository->remove($ip); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Get the list of all IP addresses stored. |
273
|
|
|
* |
274
|
|
|
* @return mixed |
275
|
|
|
*/ |
276
|
|
|
public function report() |
277
|
|
|
{ |
278
|
|
|
return $this->dataRepository->all(); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Set the current IP address. |
283
|
|
|
* |
284
|
|
|
* @param $ip |
285
|
|
|
*/ |
286
|
|
|
public function setIp($ip) |
287
|
|
|
{ |
288
|
|
|
$this->ip = $ip |
289
|
|
|
?: ($this->ip |
290
|
|
|
?: $this->request->getClientIp()); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Check if a string is a valid country info. |
295
|
|
|
* |
296
|
|
|
* @param $country |
297
|
|
|
* |
298
|
|
|
* @return bool |
299
|
|
|
*/ |
300
|
|
|
public function validCountry($country) |
301
|
|
|
{ |
302
|
|
|
return $this->dataRepository->validCountry($country); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Tell in which list (black/white) an IP address is. |
307
|
|
|
* |
308
|
|
|
* @param $ip_address |
309
|
|
|
* |
310
|
|
|
* @return bool|string |
311
|
|
|
*/ |
312
|
|
|
public function whichList($ip_address) |
313
|
|
|
{ |
314
|
|
|
return $this->dataRepository->whichList($ip_address); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Whitelist an IP address. |
319
|
|
|
* |
320
|
|
|
* @param $ip |
321
|
|
|
* @param bool $force |
322
|
|
|
* |
323
|
|
|
* @return bool |
324
|
|
|
*/ |
325
|
|
|
public function whitelist($ip, $force = false) |
326
|
|
|
{ |
327
|
|
|
return $this->dataRepository->addToList(true, $ip, $force); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Update the GeoIp2 database. |
332
|
|
|
* |
333
|
|
|
* @return bool |
334
|
|
|
*/ |
335
|
|
|
public function updateGeoIp() |
336
|
|
|
{ |
337
|
|
|
$updater = new GeoIpUpdater(); |
338
|
|
|
|
339
|
|
|
$success = $updater->updateGeoIpFiles($this->config->get('geoip_database_path')); |
340
|
|
|
|
341
|
|
|
$this->messageRepository->addMessage($updater->getMessages()); |
342
|
|
|
|
343
|
|
|
return $success; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Check if the application is receiving some sort of attack. |
348
|
|
|
* |
349
|
|
|
* @return bool |
350
|
|
|
*/ |
351
|
|
|
public function isBeingAttacked($ipAddress = null) |
352
|
|
|
{ |
353
|
|
|
return $this->attackBlocker->isBeingAttacked($ipAddress ?: $this->ip); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Get a response to the attack. |
358
|
|
|
* |
359
|
|
|
* @return bool |
360
|
|
|
*/ |
361
|
|
|
public function responseToAttack() |
362
|
|
|
{ |
363
|
|
|
return $this->attackBlocker->responseToAttack(); |
|
|
|
|
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Get country code from an IP address. |
368
|
|
|
* |
369
|
|
|
* @param $ip_address |
370
|
|
|
* |
371
|
|
|
* @return bool|string |
372
|
|
|
*/ |
373
|
|
|
public function getCountryFromIp($ip_address) |
374
|
|
|
{ |
375
|
|
|
return $this->dataRepository->getCountryFromIp($ip_address); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Make a country info from a string. |
380
|
|
|
* |
381
|
|
|
* @param $country |
382
|
|
|
* @return bool|string |
383
|
|
|
*/ |
384
|
|
|
public function makeCountryFromString($country) |
385
|
|
|
{ |
386
|
|
|
return $this->dataRepository->makeCountryFromString($country); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Get the GeoIP instance. |
391
|
|
|
* |
392
|
|
|
* @return object |
393
|
|
|
*/ |
394
|
|
|
public function getGeoIp() |
395
|
|
|
{ |
396
|
|
|
return $this->dataRepository->getGeoIp(); |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths