PluginProbe
WebTotem Security / 1.3.3
WebTotem Security v1.3.3
3.0.1 3.0.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 2.0 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.1 2.2.2 2.2.3 2.2.4 All 109 releases
wt-security / library / WT.php

WT.php in WebTotem Security 1.3.3, at library/WT.php

454 lines 22.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php defined('ABSPATH') or die("Protected By Webtotem Wordpress Security!");
2
3
4 class WTSEC_LIBRARY_WT
5 {
6 const URL = "https://api.wtotem.com/v1/graphql";
7
8 public static function auth($key)
9 {
10 $payload = '{"query":"mutation{\n apiServiceMutation{\n auth(apiKey:\"' . $key . '\"){\n value\n refreshToken\n expiresIn\n }\n }\n}\n\n"}';
11 return self::requestApi($payload);
12 }
13
14 protected static function requestApi($payload, $token = false, $repeat = false)
15 {
16 if ($token) {
17 $token = WTSEC_LIBRARY_App::getToken();
18 }
19 $args = [
20 'body' => $payload,
21 'timeout' => '15',
22 'sslverify' => false,
23 'headers' => ['Content-Type:application/json'],
24 ];
25 if (!is_null($token) && $token) {
26 $authorization = "Bearer " . $token;
27 $args['headers'] = array_merge($args['headers'], ["Authorization" => $authorization]);
28 }
29 $response = wp_remote_post(self::URL, $args);
30 $httpcode = wp_remote_retrieve_response_code($response);
31
32 if ($httpcode < 200) {
33 WTSEC_LIBRARY_Session::setNotification("error", WTSEC_LIBRARY_Localization::lmsg('could_not_connect_to_the_server'));
34 }
35 $response = wp_remote_retrieve_body($response);
36
37 $result = json_decode($response, true);
38 if (isset($result['errors'][0]['message'])) {
39 $message = self::diffMesageForHuman($result['errors'][0]['message']);
40 if (stripos($result['errors'][0]['message'], "token") !== false && !$repeat) {
41 $result = WTSEC_LIBRARY_WT::auth(wtsec_app()->get("api_key"));
42 if (isset($result['data']['apiServiceMutation']['auth']['value'])) {
43 $token_ = $result['data']['apiServiceMutation']['auth']['value'];
44 WTSEC_LIBRARY_App::login($token_);
45 return self::requestApi($payload, $token, true);
46 } else {
47 WTSEC_LIBRARY_App::logout();
48 }
49 } else {
50 if ($message !== false) {
51 WTSEC_LIBRARY_Session::setNotification("warning", $message);
52 }
53 }
54 }
55 return $result;
56 }
57
58 public static function diffMesageForHuman($message)
59 {
60 $definition = $message;
61 $excepts = [
62 "RESOURCE_NOT_FOUND", "DUPLICATE_HOST", "INVALID_CREDENTIALS", "INVALID_API_KEY", "Invalid token",
63 ];
64 if (in_array($message, $excepts)) {
65 return false;
66 }
67 switch ($message) {
68 case 'HOSTS_LIMIT_EXCEEDED':
69 $definition = WTSEC_LIBRARY_Localization::lmsg('hosts_limit_exceed');
70 break;
71 case 'RESOURCE_NOT_FOUND':
72 $definition = WTSEC_LIBRARY_Localization::lmsg('resource_not_found');
73 break;
74 case 'Invalid token':
75 $definition = WTSEC_LIBRARY_Localization::lmsg('invalid_token');
76 break;
77 case 'USER_ALREADY_REGISTERED':
78 $definition = WTSEC_LIBRARY_Localization::lmsg('user_already_exist');
79 break;
80 case 'DUPLICATE_HOST':
81 $definition = WTSEC_LIBRARY_Localization::lmsg('duplicate_host');
82 break;
83 case 'Agent does not exist or has been already verified':
84 $definition = WTSEC_LIBRARY_Localization::lmsg('agent_does_not_exist_or_has_been_already_verified');
85 break;
86 case 'INVALID_DOMAIN_NAME':
87 $definition = WTSEC_LIBRARY_Localization::lmsg('invalid_domain_name');
88 break;
89 }
90 return $definition;
91 }
92
93 public static function requestURL($url)
94 {
95 $args = [
96 'timeout' => '15',
97 'sslverify' => false,
98 ];
99 $response = wp_remote_post($url, $args);
100 $httpcode = wp_remote_retrieve_response_code($response);
101 if ($httpcode < 200) {
102 WTSEC_LIBRARY_Session::setNotification("error", WTSEC_LIBRARY_Localization::lmsg('could_not_connect_to_the_server'));
103 }
104 $response = wp_remote_retrieve_body($response);
105 return $response;
106 }
107
108 public static function getOwnSite()
109 {
110 $payload = '{"query":"{\n userHostsList{\n id\n hostname\n }\n}"}';
111 $result = self::requestApi($payload, true);
112 if (isset($result['data']['userHostsList'])) {
113 //mutator
114 foreach ($result['data']['userHostsList'] as &$m) {
115 if (isset($m['hostname'])) {
116 $m['hostname'] = WTSEC_LIBRARY_Idn::idn_to_utf8($m['hostname']);
117 if (self::isSiteUrl($m['hostname'])) {
118 return $result['data']['userHostsList'] = $m;
119 break;
120 }
121 }
122 }
123 $add_site = self::addSite(WTSEC_SITE_URL);
124 if (isset($add_site['errors'])) {
125 return $result['data']['userHostsList'] = [];
126 } else {
127 return self::getOwnSite();
128 }
129 }
130 return [];
131 }
132
133 public static function isSiteUrl($url)
134 {
135 return WTSEC_LIBRARY_Idn::idn_to_utf8(WTSEC_SITE_URL) === $url;
136 }
137
138 public static function addSite($url)
139 {
140 $payload = '{"query":"\n mutation($input: AddUserHostInput!) {\n addUserHost(input: $input) {\n id\n title\n hostname\n tags\n stack\n services {\n id\n name\n configs {\n id\n data\n isActive\n createdAt\n }\n }\n isActive\n createdAt\n }\n }\n ","variables":{"input":{"hostname":"' . $url . '","services":[{"id":1,"configs":[{"scheme":"http","port":80,"check_interval":1,"responsetime_threshold":30,"path":"/","http_errors":[400,401,402,403,404,500,501,502,503],"alert_after":0}]},{"id":2,"configs":[{"check_interval":5,"notify_expiry_day":true,"notify_expiry_month":true,"notify_expiry_week":true,"port":443}]},{"id":4,"configs":[{"check_interval":5,"path":"/","scheme":"http","port":80}]},{"id":5,"configs":[{"check_interval":5,"path":"/","scheme":"http","port":80}]},{"id":6,"configs":[{"check_interval":5,"path":"/","scheme":"http","port":80}]},{"id":7,"configs":[{"check_interval":5,"ports_udp":[18,19,53,27,29,31,71,74],"ports_tcp":[21,22,25,3306,5432,80,443,88,8000,8080]}]},{"id":9,"configs":[{"scheme":"http","port":80,"path":"/"}]},{"id":8,"configs":[{"check_interval":30,"scheme":"http","port":80,"path":"/"}]},{"id":3,"configs":[{"check_interval":60,"notify_expiry_day":true,"notify_expiry_month":true,"notify_expiry_week":true}]},{"id":10,"configs":[{"check_interval":30,"path":"/","scheme":"http","port":80}]}],"title":"' . $url . '"}}}';
141 return self::requestApi($payload, true);
142 }
143
144 public static function getAllChecks($host_id)
145 {
146 $from = time() - (60 * 60 * 24);
147 $to = time();
148 $from_waf = time() - (60 * 60 * 24 * 7);
149 $payload = '{"query":"query getAllChecks( $hostId:Int!, $dateRange: DateRangeInput!, $dateRangeWaf:DateRangeInput! ){\n waServiceChecks(userHostId:$hostId){\n config{\n id\n isActive\n }\n isDown\n status\n average(dateRange: $dateRange)\n responseTime(dateRange: $dateRange)\n testsResults(dateRange: $dateRange)\n }\n sslServiceChecks(userHostId:$hostId){\n config{\n id\n isActive\n }\n status\n issued\n expires\n daysLeft\n tls\n }\n decServiceChecks(userHostId:$hostId){\n config{\n id\n isActive\n }\n email\n daysLeft\n created\n expires\n registrar\n owner\n status\n }\n avServiceChecks(userHostId:$hostId){\n config{\n id\n isActive\n }\n lastTestId\n lastTestTime\n status\n count\n list\n }\n cmsServiceChecks(userHostId:$hostId){\n config{\n id\n isActive\n }\n lastTestId\n lastTestTime\n status\n count\n list\n }\n dcServiceChecks(userHostId:$hostId){\n config{\n id\n isActive\n }\n lastTestTime\n status\n count\n list\n }\n psServiceChecks(userHostId:$hostId){\n config{\n id\n isActive\n }\n ip\n lastTestId\n lastTestTime\n status\n count\n openTCPs\n openUDPs\n }\n wafServiceChecks(userHostId:$hostId){\n config{\n id\n isActive\n }\n lastTestTime\n status\n count\n countIP \n chart(dateRangeWaf:$dateRangeWaf){\n date\n count\n }\n }\n vcServiceChecks(userHostId:$hostId){\n config{\n id\n isActive\n }\n status\n list\n fileChangesCount\n errorsCount\n signaturesCount\n }\n \n}\n\n\n\n","variables":{"hostId":' . $host_id . ',"dateRange":{"from":' . $from . ',"to":' . $to . '},"dateRangeWaf":{"to":' . $to . ',"from":' . $from_waf . '}}}';
150 return self::requestApi($payload, true);
151 }
152
153 public static function changeStatus($config_id, $host_id)
154 {
155 $payload = '{"query":"\n mutation {\n toggleServiceConfig(\n id: ' . $config_id . '\n userhostId: ' . $host_id . '\n ) {\n isActive\n }\n }\n "}';
156 return self::requestApi($payload, true);
157 }
158
159 public static function serviceConnect($id, $service)
160 {
161 $payload = '{"query":"{ checkAgent(userHostId: ' . $id . ', service: ' . strtoupper($service) . ') }"}';
162 return self::requestApi($payload, true);
163 }
164
165 public static function generateFile($id, $service)
166 {
167 $payload = '{"query":"{ generateAgent(userHostId: ' . $id . ', service: ' . strtoupper($service) . ') }"}';
168 return self::requestApi($payload, true);
169 }
170
171 public static function checkStatus($id, $service)
172 {
173 $payload = '{"query":"{\n ' . strtolower($service) . 'ServiceChecks(userHostId:' . $id . '){\n status\n config{\n isActive\n id\n }\n }\n}"}';
174 return self::requestApi($payload, true);
175 }
176
177 public static function getOptions($host_id)
178 {
179 $payload = '{"query":"query{\nuserHost(id:' . $host_id . '){\n id\n title\n hostname\n stack\n createdAt\n services {\n id\n name\n configs {\n id\n \tdata\n isActive\n }\n }\n }\n}"}';
180 return self::requestApi($payload, true);
181 }
182
183 public static function getAntivirus($host_id)
184 {
185 $payload = '{"query":"query{\n vcServiceChecks(userHostId:' . $host_id . '){\n config{\n id,\n isActive\n }\n status\n list\n fileChangesCount\n errorsCount\n signaturesCount\n }\n}\n\n"}';
186 return self::requestApi($payload, true);
187 }
188
189 public static function getStatusIcon($status, $service)
190 {
191 $color = [
192 "icon" => "",
193 "color" => "",
194 ];
195 $definition = '';
196 switch ($service) {
197 case 'wa':
198 switch ((string)$status) {
199 case '-1':
200 $color = self::getColor("error");
201 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
202 break;
203 case '-200':
204 $color = self::getColor("grey");
205 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
206 break;
207 case '0':
208 $color = self::getColor("success");
209 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
210 break;
211 case '1':
212 $color = self::getColor("error");
213 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.unavailable');
214 break;
215 }
216 break;
217 case 'ssl':
218 switch ((string)$status) {
219 case '-1':
220 $color = self::getColor("grey");
221 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
222 break;
223 case '-200':
224 $color = self::getColor("grey");
225 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
226 break;
227 case '0':
228 $color = self::getColor("green");
229 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
230 break;
231 case '1':
232 $color = self::getColor("red");
233 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.invalid');
234 break;
235 case '2':
236 $color = self::getColor("red");
237 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.expired');
238 break;
239 case '3':
240 $color = self::getColor("orange");
241 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.expires');
242 break;
243 case '4':
244 $color = self::getColor("red");
245 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.expires_today');
246 break;
247 }
248 break;
249 case 'dec':
250 switch ((string)$status) {
251 case '-3':
252 $color = self::getColor("grey");
253 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.not_registered');
254 break;
255 case '-2':
256 $color = self::getColor("grey");
257 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.unsupported');
258 break;
259 case '-1':
260 $color = self::getColor("grey");
261 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
262 break;
263 case '-200':
264 $color = self::getColor("grey");
265 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
266 break;
267 case '0':
268 $color = self::getColor("green");
269 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
270 break;
271 case '1':
272 $color = self::getColor("orange");
273 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.expires');
274 break;
275 case '2':
276 $color = self::getColor("orange");
277 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.expired');
278 break;
279 case '3':
280 $color = self::getColor("red");
281 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.expires_today');
282 break;
283 }
284 break;
285 case 'av':
286 switch ((string)$status) {
287 case '-1':
288 $color = self::getColor("grey");
289 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
290 break;
291 case '-200':
292 $color = self::getColor("grey");
293 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
294 break;
295 case '0':
296 $color = self::getColor("green");
297 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
298 break;
299 case '1':
300 $color = self::getColor("red");
301 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.blacklisted');
302 break;
303 }
304 break;
305 case 'cms':
306 switch ((string)$status) {
307 case '-1':
308 $color = self::getColor("grey");
309 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
310 break;
311 case '-200':
312 $color = self::getColor("grey");
313 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
314 break;
315 case '0':
316 $color = self::getColor("green");
317 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
318 break;
319 case '1':
320 $color = self::getColor("red");
321 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.miner_detected');
322 break;
323 }
324 break;
325 case 'dc':
326 switch ((string)$status) {
327 case '-1':
328 $color = self::getColor("grey");
329 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
330 break;
331 case '-200':
332 $color = self::getColor("grey");
333 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
334 break;
335 case '0':
336 $color = self::getColor("green");
337 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
338 break;
339 case '1':
340 $color = self::getColor("red");
341 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.deface');
342 break;
343 case '2':
344 $color = self::getColor("orange");
345 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.modified');
346 break;
347 }
348 break;
349 case 'ps':
350 switch ((string)$status) {
351 case '-1':
352 $color = self::getColor("grey");
353 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
354 break;
355 case '-200':
356 $color = self::getColor("grey");
357 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
358 break;
359 case '0':
360 $color = self::getColor("green");
361 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
362 break;
363 case '1':
364 $color = self::getColor("orange");
365 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.open');
366 break;
367 }
368 break;
369 case 'waf':
370 switch ((string)$status) {
371 case '-400':
372 $color = self::getColor("red");
373 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.blocked');
374 break;
375 case '-300':
376 $color = self::getColor("orange");
377 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.not_installed');
378 break;
379 case '-1':
380 $color = self::getColor("grey");
381 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
382 break;
383 case '-200':
384 $color = self::getColor("grey");
385 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
386 break;
387 case '0':
388 $color = self::getColor("green");
389 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
390 break;
391 case '1':
392 $color = self::getColor("red");
393 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.attacks_detected');
394 break;
395 }
396 break;
397 case 'vc':
398 switch ((string)$status) {
399 case '-400':
400 $color = self::getColor("red");
401 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.blocked');
402 break;
403 case '-300':
404 $color = self::getColor("orange");
405 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.not_installed');
406 break;
407 case '-1':
408 $color = self::getColor("grey");
409 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
410 break;
411 case '-200':
412 $color = self::getColor("grey");
413 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
414 break;
415 case '0':
416 $color = self::getColor("green");
417 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
418 break;
419 case '1':
420 $color = self::getColor("orange");
421 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.file_changes');
422 break;
423 case '2':
424 $color = self::getColor("red");
425 $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.signature_found');
426 break;
427 }
428 break;
429 }
430 return ["icon" => $color["icon"], "color" => $color["color"], "text" => "<span class='" . $color["color"] . "'>" . $definition . "</span>"];
431 }
432
433 public static function getColor($type)
434 {
435 $types = [
436 "green" => "is--status--ok",
437 "red" => "is--status--error",
438 "success" => "is--status--ok",
439 "error" => "is--status--error",
440 "orange" => "is--status--warning",
441 "grey" => "ww--status_unknow"
442 ];
443 $icon_types = [
444 "green" => "ww-icon--status_ok",
445 "red" => "ww-icon--status_error",
446 "success" => "ww-icon--status_ok",
447 "error" => "ww-icon--status_error",
448 "orange" => "ww-icon--status_warning",
449 "grey" => "ww-icon--status_unknow"
450 ];
451 return ["icon" => $icon_types[$type], "color" => $types[$type]];
452 }
453
454 }