1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spinzar\Firewall\Middleware; |
4
|
|
|
|
5
|
|
|
use Spinzar\Firewall\Abstracts\Middleware; |
6
|
|
|
use Spinzar\Firewall\Events\AttackDetected; |
7
|
|
|
use Jenssegers\Agent\Agent as Parser; |
8
|
|
|
|
9
|
|
|
class Agent extends Middleware |
10
|
|
|
{ |
11
|
|
|
protected $parser; |
12
|
|
|
|
13
|
|
|
public function check($patterns) |
14
|
|
|
{ |
15
|
|
|
$status = false; |
16
|
|
|
|
17
|
|
|
$this->parser = new Parser(); |
18
|
|
|
|
19
|
|
|
if ($this->isMalicious()) { |
20
|
|
|
$status = true; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
if (!$status && $this->isBrowser()) { |
24
|
|
|
$status = true; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if (!$status && $this->isPlatform()) { |
28
|
|
|
$status = true; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (!$status && $this->isDevice()) { |
32
|
|
|
$status = true; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (!$status && $this->isProperty()) { |
36
|
|
|
$status = true; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ($status) { |
40
|
|
|
$log = $this->log(); |
41
|
|
|
|
42
|
|
|
event(new AttackDetected($log)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $status; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function isMalicious() |
49
|
|
|
{ |
50
|
|
|
$agent = $this->parser->getUserAgent(); |
51
|
|
|
|
52
|
|
|
if (empty($agent) || ($agent == '-') || strstr($agent, '<?')) { |
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$patterns = [ |
57
|
|
|
'@"feed_url@', |
58
|
|
|
'@}__(.*)|O:@', |
59
|
|
|
'@J?Simple(p|P)ie(Factory)?@', |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
foreach ($patterns as $pattern) { |
63
|
|
|
if (!preg_match($pattern, $agent) == 1) { |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function isBrowser() |
74
|
|
|
{ |
75
|
|
|
if (!$browsers = config('firewall.middleware.' . $this->middleware . '.browsers')) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (!empty($browsers['allow']) && !in_array((string) $this->parser->browser(), (array) $browsers['allow'])) { |
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (in_array((string) $this->parser->browser(), (array) $browsers['block'])) { |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function isPlatform() |
91
|
|
|
{ |
92
|
|
|
if (!$platforms = config('firewall.middleware.' . $this->middleware . '.platforms')) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (!empty($platforms['allow']) && !in_array((string) $this->parser->platform(), (array) $platforms['allow'])) { |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (in_array((string) $this->parser->platform(), (array) $platforms['block'])) { |
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function isDevice() |
108
|
|
|
{ |
109
|
|
|
if (!$devices = config('firewall.middleware.' . $this->middleware . '.devices')) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$list = ['Desktop', 'Mobile', 'Tablet']; |
114
|
|
|
|
115
|
|
|
foreach ((array) $devices['allow'] as $allow) { |
116
|
|
|
if (!in_array($allow, $list)) { |
117
|
|
|
continue; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$function = 'is' . ucfirst($allow); |
121
|
|
|
|
122
|
|
|
if ($this->$function()) { |
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
foreach ((array) $devices['block'] as $block) { |
130
|
|
|
if (!in_array($block, $list)) { |
131
|
|
|
continue; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$function = 'is' . ucfirst($block); |
135
|
|
|
|
136
|
|
|
if (!$this->$function()) { |
137
|
|
|
continue; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
protected function isProperty() |
147
|
|
|
{ |
148
|
|
|
if (!$agents = config('firewall.middleware.' . $this->middleware . '.properties')) { |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
foreach ((array) $agents['allow'] as $allow) { |
153
|
|
|
if ($this->parser->is((string) $allow)) { |
154
|
|
|
continue; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
foreach ((array) $agents['block'] as $block) { |
161
|
|
|
if (!$this->parser->is((string) $block)) { |
162
|
|
|
continue; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return true; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|