1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Shieldon package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* php version 7.1.0 |
11
|
|
|
* |
12
|
|
|
* @category Web-security |
13
|
|
|
* @package Shieldon |
14
|
|
|
* @author Terry Lin <[email protected]> |
15
|
|
|
* @copyright 2019 terrylinooo |
16
|
|
|
* @license https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT |
17
|
|
|
* @link https://github.com/terrylinooo/shieldon |
18
|
|
|
* @see https://shieldon.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace Shieldon\Firewall\Component; |
24
|
|
|
|
25
|
|
|
use Shieldon\Firewall\Component\ComponentProvider; |
26
|
|
|
use Shieldon\Firewall\Component\DeniedTrait; |
27
|
|
|
use Shieldon\Firewall\IpTrait; |
28
|
|
|
|
29
|
|
|
use function gethostbyname; |
30
|
|
|
use function implode; |
31
|
|
|
use function preg_match; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Rdns component. |
35
|
|
|
*/ |
36
|
|
|
class Rdns extends ComponentProvider |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Public methods | Desctiotion |
40
|
|
|
* ----------------------|--------------------------------------------- |
41
|
|
|
* setIp | Set an IP address. |
42
|
|
|
* getIp | Get current set IP. |
43
|
|
|
* setRdns | Set a RDNS record for the check. |
44
|
|
|
* getRdns | Get IP resolved hostname. |
45
|
|
|
* ----------------------|--------------------------------------------- |
46
|
|
|
*/ |
47
|
|
|
use IpTrait; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Public methods | Desctiotion |
51
|
|
|
* ----------------------|--------------------------------------------- |
52
|
|
|
* setDeniedItems | Add items to the blacklist pool. |
53
|
|
|
* setDeniedItem | Add an item to the blacklist pool. |
54
|
|
|
* getDeniedItems | Get items from the blacklist pool. |
55
|
|
|
* getDeniedItem | Get items from the blacklist pool. |
56
|
|
|
* removeDeniedItem | Remove a denied item if exists. |
57
|
|
|
* removeDeniedItems | Remove all denied items. |
58
|
|
|
* hasDeniedItem | Check if a denied item exists. |
59
|
|
|
* getDenyWithPrefix | Check if a denied item exists have the same prefix. |
60
|
|
|
* removeDenyWithPrefix | Remove denied items with the same prefix. |
61
|
|
|
* isDenied | Check if an item is denied? |
62
|
|
|
* ----------------------|--------------------------------------------- |
63
|
|
|
*/ |
64
|
|
|
use DeniedTrait; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Constant |
68
|
|
|
*/ |
69
|
|
|
const STATUS_CODE = 82; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Constructor. |
73
|
|
|
*/ |
74
|
112 |
|
public function __construct() |
75
|
|
|
{ |
76
|
|
|
// RDNS for robot's IP address. |
77
|
112 |
|
$this->deniedList = [ |
78
|
112 |
|
'unknown_1' => '.webcrawler.link', |
79
|
112 |
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritDoc} |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
19 |
|
public function isDenied(): bool |
88
|
|
|
{ |
89
|
19 |
|
if (!empty($this->deniedList)) { |
90
|
19 |
|
if (preg_match('/(' . implode('|', $this->deniedList). ')/i', $this->rdns)) { |
91
|
1 |
|
return true; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
19 |
|
if ($this->strictMode) { |
96
|
|
|
// If strict mode is on, this value can not be empty. |
97
|
|
|
if (empty($this->rdns)) { |
98
|
3 |
|
return true; |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
// If the RDNS is an IP adress, not a FQDN. |
102
|
|
|
if ($this->ip === $this->rdns) { |
103
|
3 |
|
return true; |
104
|
1 |
|
} |
105
|
|
|
|
106
|
|
|
// confirm hostname's IP again |
107
|
|
|
$ip = gethostbyname($this->rdns); |
108
|
2 |
|
|
109
|
|
|
// If the IP is different as hostname's resolved IP. |
110
|
|
|
if ($ip !== $this->ip) { |
111
|
2 |
|
return true; |
112
|
2 |
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return false; |
116
|
17 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Unique deny status code. |
120
|
|
|
* |
121
|
|
|
* @return int |
122
|
|
|
*/ |
123
|
|
|
public function getDenyStatusCode(): int |
124
|
1 |
|
{ |
125
|
|
|
return self::STATUS_CODE; |
126
|
1 |
|
} |
127
|
|
|
} |
128
|
|
|
|