PluginProbe
WebTotem Security / 1.0
WebTotem Security v1.0
3.0.2 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 All 110 releases
← All changes | library/WT.php +450 -391 2.1.91.0 View file →
@@ -1,392 +1,451 @@
1 -<?php defined('ABSPATH') or die("Protected By WT!");
2 -
3 -
4 -class WTSEC_LIBRARY_WT
5 -{
6 - const URL = "https://api.wtotem.com/graphql";
7 -// const URL = "https://api-1.2ke2xgwx4.wtotem.com/graphql";
8 -
9 - public static function auth($key)
10 - {
11 - $payload = '{"query":"mutation{\n guest{\n apiKeys{\n auth(apiKey:\"' . $key . '\"),{\n token{\n value,refreshToken,expiresIn\n }\n }\n }\n }\n}"}';
12 - return self::requestApi($payload);
13 - }
14 -
15 - protected static function requestApi($payload, $token = false, $repeat = false)
16 - {
17 - if ($token) {
18 - $token = WTSEC_LIBRARY_App::getToken();
19 - }
20 - $args = [
21 - 'body' => $payload,
22 - 'timeout' => '30',
23 - 'sslverify' => false,
24 - 'headers' => ['Content-Type:application/json', 'Content-Type' => 'application/json', 'source:WORDPRESS', 'Accept: application/json'],
25 - ];
26 - if (!is_null($token) && $token) {
27 - $authorization = "Bearer " . $token;
28 - $args['headers'] = array_merge($args['headers'], ["Authorization" => $authorization]);
29 - }
30 - $response = wp_remote_post(self::URL, $args);
31 - $httpcode = wp_remote_retrieve_response_code($response);
32 -
33 - if ($httpcode < 200) {
34 - WTSEC_LIBRARY_Session::setNotification("error", WTSEC_LIBRARY_Localization::lmsg('could_not_connect_to_the_server')." requestApi");
35 - }
36 - $response = wp_remote_retrieve_body($response);
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']['guest']['apiKeys']['auth']['token']['value'])) {
43 - $token_ = $result['data']['guest']['apiKeys']['auth']['token']['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 'Invalid token':
72 - $definition = WTSEC_LIBRARY_Localization::lmsg('invalid_token');
73 - break;
74 - case 'USER_ALREADY_REGISTERED':
75 - $definition = WTSEC_LIBRARY_Localization::lmsg('user_already_exist');
76 - break;
77 - case 'DUPLICATE_HOST':
78 - $definition = WTSEC_LIBRARY_Localization::lmsg('duplicate_host');
79 - break;
80 - case 'INVALID_DOMAIN_NAME':
81 - $definition = WTSEC_LIBRARY_Localization::lmsg('invalid_domain_name');
82 - break;
83 - }
84 - return $definition;
85 - }
86 -
87 - //del
88 - public static function requestURL($url)
89 - {
90 - $args = [
91 - 'timeout' => '30',
92 - 'sslverify' => false,
93 - ];
94 - $response = wp_remote_get($url, $args);
95 - $httpcode = wp_remote_retrieve_response_code($response);
96 - if ($httpcode < 200) {
97 - WTSEC_LIBRARY_Session::setNotification("error", WTSEC_LIBRARY_Localization::lmsg('could_not_connect_to_the_server')." requestURL");
98 - }
99 - $response = wp_remote_retrieve_body($response);
100 - return $response;
101 - }
102 -
103 -
104 - public static function getFileByUrl($url)
105 - {
106 - $args = [
107 - 'timeout' => '30',
108 - 'sslverify' => false,
109 - ];
110 - $response = wp_remote_get($url, $args);
111 - $httpcode = wp_remote_retrieve_response_code($response);
112 - if ($httpcode < 200) {
113 - WTSEC_LIBRARY_Session::setNotification("error", WTSEC_LIBRARY_Localization::lmsg('could_not_connect_to_the_server')." getFileByUrl");
114 - }
115 - $filename = null;
116 - $metas = explode(";",$response['headers']["content-disposition"]);
117 - foreach ($metas as $meta){
118 - if(stripos($meta,"filename") !== false){
119 - $filename = trim(str_replace(["filename=","=","\""],"",$meta));
120 - }
121 - }
122 - $response = wp_remote_retrieve_body($response);
123 - return ["body" => $response, "filename" => $filename];
124 - }
125 -
126 - public static function getOwnSite($attempt = false)
127 - {
128 - $payload = '{"query":"query getSites { auth { viewer { sites { ...sites __typename } __typename } __typename }}fragment sites on SiteQueries { list(filter: {}) { edges { node { id hostname title ssl { status __typename } availability { status __typename } reputation { status __typename } ports { status __typename } deface { status __typename } domain { status __typename } antivirus { status __typename } firewall { status __typename } maliciousScript { stack { name __typename } __typename } __typename } __typename } __typename } __typename}"}';
129 -
130 - $result = self::requestApi($payload, true);
131 - if (isset($result['data']['auth']['viewer']['sites']['list']['edges'])) {
132 - //mutator
133 - foreach ($result['data']['auth']['viewer']['sites']['list']['edges'] as &$m) {
134 - if (isset($m['node']['hostname'])) {
135 - $m['node']['hostname'] = WTSEC_LIBRARY_Idn::idn_to_utf8($m['node']['hostname']);
136 -// if ($m['node']['id'] == "c2l0ZV8yNTg=") {
137 -// return $m['node'];
138 -// }
139 - if (self::isSiteUrl($m['node']['hostname'])) {
140 - return $m['node'];
141 - }
142 - }
143 - }
144 - $add_site = self::addSite(WTSEC_SITE_URL);
145 - if (isset($add_site['errors'])) {
146 - return $result['data']['node'] = [];
147 - } else {
148 - if (!$attempt) {
149 - return self::getOwnSite(true);
150 - }
151 - }
152 - }
153 - return [];
154 - }
155 -
156 - public static function isSiteUrl($url)
157 - {
158 - return WTSEC_LIBRARY_Idn::idn_to_utf8(WTSEC_SITE_URL) === $url;
159 - }
160 -
161 - public static function addSite($url)
162 - {
163 - $payload = '{"variables":{"input":{"title":"' . $url . '","hostname":"' . $url . '","configs":{"scheme":"http","port":80,"wa":{},"dec":{},"ps":{}}}},"query":"mutation ($input: CreateSiteInput) {\n auth {\n sites {\n create(input: $input) {\n id\n hostname\n title\n __typename\n }\n __typename\n }\n __typename\n }\n}\n"}';
164 - return self::requestApi($payload, true);
165 - }
166 -
167 - public static function getAllChecks($host_id)
168 - {
169 - $from = time() - (60 * 60 * 24);
170 - $to = time();
171 - $from_waf = time() - (60 * 60 * 24 * 7);
172 - $payload = '{"query":"query($id: ID!, $dateRange: DateRangeInput!) { auth { viewer { sites { one(id: $id) { ports { status ip tcp lastTest { time } } availability { status lastTest { time } responseTime downTime(dateRange: $dateRange) percent(dateRange: $dateRange) } deface { status lastTest { time } words count } domain { status registrar owner email createdDate expiredDate } ports { status lastTest { time } ip tcp country } ssl { status daysLeft expiryDate issueDate } reputation { status lastTest { time } virusList { virus{ type path } antiVirus } } firewall { lastTest { time } status chart(dateRange: $dateRange) { time attacks blocked } report(dateRange: $dateRange) { time attacks ip } } maliciousScript { lastTest { time } status } antivirus { status stats { changed deleted scaned infected error } lastTest { time } isFirstCheck } } } } } }","variables":{"id":"' . $host_id . '","dateRange":{"to":' . $to . ',"from":' . $from_waf . '}}}';
173 -// $payload = '{"query":"query($id: ID!, $dateRange:DateRangeInput!) {\n auth {\n viewer {\n sites {\n one(id: $id) {\n ports{\n status\n ip\n tcp\n lastTest{\n time\n }\n }\n availability {\n status\n lastTest {\n time\n }\n responseTime\n downTime(dateRange: $dateRange)\n percent(dateRange: $dateRange)\n }\n deface {\n status\n lastTest {\n time\n }\n words\n count\n }\n domain {\n status\n registrar\n owner\n email\n createdDate\n expiredDate\n }\n ports {\n status\n lastTest {\n time\n }\n ip\n tcp\n country\n }\n ssl {\n status\n daysLeft\n expiryDate\n issueDate\n }\n reputation {\n status\n lastTest {\n time\n }\n virusList {\n viruses\n antiVirus\n }\n }\n firewall {\n lastTest { \n time\n } \n status\n logs{\n edges\n{\n node{\n status\n country\n type\n}\n}\n}\n chart(dateRange: $dateRange) {\n time\n attacks\n blocked\n }\n report(dateRange: $dateRange) {\n time\n attacks\n ip\n }\n }\n maliciousScript{\n lastTest{\n time\n }\n status\n }\n antivirus{\n status \n stats { \n changed\n deleted\n scaned\n infected\n error\n } \n lastTest { \n time\n } \n isFirstCheck\n }\n }\n }\n }\n }\n}","variables":{"id":"' . $host_id . '","dateRange":{"to":' . $to . ',"from":' . $from_waf . '}}}';
174 - $response = self::requestApi($payload, true);
175 - if (isset($response['data']['auth']['viewer']['sites']['one'])) {
176 - return $response['data']['auth']['viewer']['sites']['one'];
177 - }
178 - return [];
179 - }
180 -
181 - public static function changeStatus($config_id, $host_id)
182 - {
183 - $payload = '{"query":"\n mutation {\n toggleServiceConfig(\n id: ' . $config_id . '\n userhostId: ' . $host_id . '\n ) {\n isActive\n }\n }\n "}';
184 - return self::requestApi($payload, true);
185 - }
186 -
187 - public static function serviceConnect($id, $service)
188 - {
189 - $payload = '{"query":"mutation {\n auth {\n agents{\n check(siteId:\"' . $id . '\",service:' . strtolower($service) . ',plugin:WORDPRESS)\n }\n }\n}\n"}';
190 - return self::requestApi($payload, true);
191 - }
192 -
193 - public static function generateFile($id, $service)
194 - {
195 - $payload = '{"query":"mutation {\n auth {\n agents{\n generate(siteId:\"' . $id . '\",service:' . strtolower($service) . '){\n agentName\n }\n }\n }\n}\n"}';
196 - return self::requestApi($payload, true);
197 - }
198 -
199 - public static function generateAmFile($id)
200 - {
201 - $payload = '{"operationName":null,"variables":{},"query":"mutation {\n auth {\n am {\n install(siteId: \"'.$id.'\")\n }\n }\n}\n"}';
202 - return self::requestApi($payload, true);
203 - }
204 -
205 - public static function checkStatus($id, $service)
206 - {
207 - $payload = '{"operationName":null,"variables":{"id":"' . $id . '"},"query":"query ($id: ID!) {\n auth {\n viewer {\n sites {\n one(id: $id) {\n ... on Site {\n configs {\n ... on ' . $service . 'Config {\n isActive\n id\n }\n }\n }\n }\n }\n }\n }\n}\n"}';
208 - return self::requestApi($payload, true);
209 - }
210 -
211 -
212 - public static function getAntivirus($host_id, $limit = 10, $cursor = null, $days = 365, $event = 'infected')
213 - {
214 - $from = (is_array($days)) ? $days['begin'] : time() - (60 * 60 * 24 * $days);
215 - $to = (is_array($days)) ? $days['end'] : time();
216 - $cursor = ($cursor == null) ? 'null' : '"' . $cursor . '"';
217 - $payload = '{"operationName":null,"variables":{"id":"' . $host_id . '","avLogFilter":{"event":"'.$event.'","dateRange":{"to":'.$to.',"from":'.$from.'},"order":{"direction":"DESC","field":"time"},"pagination":{"first":' . $limit . ',"cursor":' . $cursor . '}}},"query":"query ($id: ID!, $avLogFilter: AvLogFilter!) { auth { viewer { sites { one(id: $id) { id ... on Site { configs { ... on AvConfig { isActive id } } } antivirus { status log(avLogFilter: $avLogFilter) { edges { node { filePath matches event signatures time } } pageInfo { endCursor hasNextPage __typename } } lastTest { time } stats { changed deleted scaned infected } } } } } } }"}';
218 - return self::requestApi($payload, true);
219 - }
220 -
221 - public static function getOptions($host_id)
222 - {
223 - $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}"}';
224 - return self::requestApi($payload, true);
225 - }
226 -
227 - public static function getFirewall($host_id, $limit = 20, $cursor = null, $days = 365)
228 - {
229 - $from = (is_array($days)) ? $days['begin'] : time() - (60 * 60 * 24 * $days);
230 - $to = (is_array($days)) ? $days['end'] : time();
231 - $cursor = ($cursor == null) ? 'null' : '"'.$cursor.'"';
232 -
233 - $payload = '{"operationName":"FirewallAttackLog","variables":{"dateRange":{"to":'.$to.',"from":'.$from.'},"id":"'.$host_id.'","wafLogFilter":{"dateRange":{"to":'.$to.',"from":'.$from.'},"order":{"direction":"DESC","field":"time"},"pagination":{"first":'.$limit.',"cursor":' . $cursor . '}}},"query":"query FirewallAttackLog($id: ID!, $wafLogFilter: WafLogFilter!, $dateRange: DateRangeInput!) { auth { viewer { sites { one(id: $id) { firewall { lastTest { time } status... FirewallLogFragment map(dateRange: $dateRange) { attacks, country } __typename } __typename } __typename } __typename } __typename } } fragment FirewallLogFragment on Waf { logs(wafLogFilter: $wafLogFilter) { edges { cursor node { type blocked payload ip location { country { nameEn __typename } __typename } time request status country category __typename } __typename } pageInfo { endCursor hasNextPage __typename } __typename } __typename }"}';
234 - $res = self::requestApi($payload, true);
235 -
236 - return $res;
237 - }
238 -
239 - public static function getFirewallChart($host_id, $days = 7)
240 - {
241 - $to = time();
242 - $from_waf = ($days <= 1) ? strtotime(date('Y-m-d 00:00:01')) : time() - (60 * 60 * 24 * $days);
243 -
244 - $payload = '{ "query":"query($id: ID!, $dateRange: DateRangeInput!, $wafLogFilter: WafLogFilter!) { auth { viewer { sites { one(id: $id) { firewall { lastTest { time } status logs(wafLogFilter: $wafLogFilter) { edges { node { status country type userAgent } } } chart(dateRange: $dateRange) { time attacks blocked } report(dateRange: $dateRange) { time attacks ip } } } } } } }", "operationName":null,"variables":{"id":"' . $host_id . '","dateRange":{"to":' . $to . ',"from":' . $from_waf . '}, "wafLogFilter": { "dateRange": { "from": ' .$from_waf. ', "to": ' .$to. ' }, "pagination": { "cursor": null }, "order": { "direction": "DESC", "field": "time" } } } }';
245 -
246 - return self::requestApi($payload, true);
247 - }
248 -
249 - public static function getStatusIcon($status)
250 - {
251 - $statuses = [
252 - "clean" => [
253 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.ok'),
254 - "color" => self::getColor("success"),
255 - "image" => "check-mark.svg"
256 - ],
257 - "pending" => [
258 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.pending'),
259 - "color" => self::getColor("grey"),
260 - "image" => "loading.svg"
261 - ],
262 - "expired" => [
263 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.expired'),
264 - "color" => self::getColor("orange"),
265 - "image" => "warning.svg"
266 -
267 - ],
268 - "invalid" => [
269 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.invalid'),
270 - "color" => self::getColor("error"),
271 - "image" => "warning.svg"
272 - ],
273 - "no_cert" => [
274 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.no_cert'),
275 - "color" => self::getColor("orange"),
276 - "image" => "warning.svg"
277 - ],
278 - "error" => [
279 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.error'),
280 - "color" => self::getColor("error"),
281 - "image" => "warning.svg"
282 - ],
283 - "expires" => [
284 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.expires'),
285 - "color" => self::getColor("orange"),
286 - "image" => "warning.svg"
287 - ],
288 - "expires_today" => [
289 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.expires_today'),
290 - "color" => self::getColor("error"),
291 - "image" => "warning.svg"
292 - ],
293 - "down" => [
294 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.down'),
295 - "color" => self::getColor("error"),
296 - "image" => "warning.svg"
297 - ],
298 - "up" => [
299 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.up'),
300 - "color" => self::getColor("success"),
301 - "image" => "check-mark.svg"
302 - ],
303 - "infected" => [
304 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.infected'),
305 - "color" => self::getColor("error"),
306 - "image" => "warning.svg"
307 - ],
308 - "open_ports" => [
309 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.open'),
310 - "color" => self::getColor("orange"),
311 - "image" => "warning.svg"
312 - ],
313 - "deface" => [
314 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.deface'),
315 - "color" => self::getColor("error"),
316 - "image" => "warning.svg"
317 - ],
318 - "modified" => [
319 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.modified'),
320 - "color" => self::getColor("grey"),
321 - "image" => "warning.svg"
322 - ],
323 - "not_supported" => [
324 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.unsupported'),
325 - "color" => self::getColor("orange"),
326 - "image" => "warning.svg"
327 - ],
328 - "not_registered" => [
329 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.not_registered'),
330 - "color" => self::getColor("orange"),
331 - "image" => "warning.svg"
332 - ],
333 - "not_installed" => [
334 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.not_installed'),
335 - "color" => self::getColor("error"),
336 - "image" => "warning.svg"
337 - ],
338 - "working" => [
339 - "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.working'),
340 - "color" => self::getColor("success"),
341 - "image" => "check-mark.svg"
342 - ],
343 - ];
344 - if (isset($statuses[$status])) {
345 - return ["image" => $statuses[$status]["image"], "icon" => $statuses[$status]["color"]["icon"], "color" => $statuses[$status]["color"]["color"], "text" => "<span class='" . $statuses[$status]["color"]["color"] . "'>" . $statuses[$status]["text"] . "</span>"];
346 - } else {
347 - return ["image" => "warning.svg", "icon" => "ww-icon--status_unknow", "color" => "ww--status_unknow", "text" => "<span class='ww--status_unknow'>Unknown status</span>"];
348 - }
349 - }
350 -
351 - public static function getColor($type)
352 - {
353 - $types = [
354 - "green" => "is--status--ok",
355 - "red" => "is--status--error",
356 - "success" => "is--status--ok",
357 - "error" => "is--status--error",
358 - "orange" => "is--status--warning",
359 - "warning" => "is--status--warning",
360 - "grey" => "ww--status_unknow"
361 - ];
362 - $icon_types = [
363 - "green" => "ww-icon--status_ok",
364 - "red" => "ww-icon--status_error",
365 - "success" => "ww-icon--status_ok",
366 - "error" => "ww-icon--status_error",
367 - "orange" => "ww-icon--status_warning",
368 - "warning" => "ww-icon--status_warning",
369 - "grey" => "ww-icon--status_unknow"
370 - ];
371 - return ["icon" => $icon_types[$type], "color" => $types[$type]];
372 - }
373 -
374 - public static function getConfigId($configs)
375 - {
376 - $id = 0;
377 - $is_active = false;
378 - foreach ($configs as &$config) {
379 - if (!empty($config)) {
380 - $id = $config['id'];
381 - $is_active = $config['isActive'];
382 - break;
383 - }
384 - }
385 - return compact('id', 'is_active');
386 - }
387 -
388 - public static function getScore(){
389 - return self::requestURL("https://api.wtotem.com/site/score?url=".WTSEC_SITE_URL);
390 - }
391 -
1 +<?php defined('ABSPATH') or die("Protected By WT!");
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 + $definition = '';
193 + switch ($service) {
194 + case 'wa':
195 + switch ((string)$status) {
196 + case '-1':
197 + $color = self::getColor("error");
198 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
199 + break;
200 + case '-200':
201 + $color = self::getColor("grey");
202 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
203 + break;
204 + case '0':
205 + $color = self::getColor("success");
206 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
207 + break;
208 + case '1':
209 + $color = self::getColor("error");
210 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.unavailable');
211 + break;
212 + }
213 + break;
214 + case 'ssl':
215 + switch ((string)$status) {
216 + case '-1':
217 + $color = self::getColor("grey");
218 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
219 + break;
220 + case '-200':
221 + $color = self::getColor("grey");
222 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
223 + break;
224 + case '0':
225 + $color = self::getColor("green");
226 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
227 + break;
228 + case '1':
229 + $color = self::getColor("red");
230 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.invalid');
231 + break;
232 + case '2':
233 + $color = self::getColor("red");
234 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.expired');
235 + break;
236 + case '3':
237 + $color = self::getColor("orange");
238 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.expires');
239 + break;
240 + case '4':
241 + $color = self::getColor("red");
242 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.expires_today');
243 + break;
244 + }
245 + break;
246 + case 'dec':
247 + switch ((string)$status) {
248 + case '-3':
249 + $color = self::getColor("grey");
250 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.not_registered');
251 + break;
252 + case '-2':
253 + $color = self::getColor("grey");
254 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.unsupported');
255 + break;
256 + case '-1':
257 + $color = self::getColor("grey");
258 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
259 + break;
260 + case '-200':
261 + $color = self::getColor("grey");
262 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
263 + break;
264 + case '0':
265 + $color = self::getColor("green");
266 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
267 + break;
268 + case '1':
269 + $color = self::getColor("orange");
270 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.expires');
271 + break;
272 + case '2':
273 + $color = self::getColor("orange");
274 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.expired');
275 + break;
276 + case '3':
277 + $color = self::getColor("red");
278 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.expires_today');
279 + break;
280 + }
281 + break;
282 + case 'av':
283 + switch ((string)$status) {
284 + case '-1':
285 + $color = self::getColor("grey");
286 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
287 + break;
288 + case '-200':
289 + $color = self::getColor("grey");
290 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
291 + break;
292 + case '0':
293 + $color = self::getColor("green");
294 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
295 + break;
296 + case '1':
297 + $color = self::getColor("red");
298 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.blacklisted');
299 + break;
300 + }
301 + break;
302 + case 'cms':
303 + switch ((string)$status) {
304 + case '-1':
305 + $color = self::getColor("grey");
306 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
307 + break;
308 + case '-200':
309 + $color = self::getColor("grey");
310 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
311 + break;
312 + case '0':
313 + $color = self::getColor("green");
314 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
315 + break;
316 + case '1':
317 + $color = self::getColor("red");
318 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.miner_detected');
319 + break;
320 + }
321 + break;
322 + case 'dc':
323 + switch ((string)$status) {
324 + case '-1':
325 + $color = self::getColor("grey");
326 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
327 + break;
328 + case '-200':
329 + $color = self::getColor("grey");
330 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
331 + break;
332 + case '0':
333 + $color = self::getColor("green");
334 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
335 + break;
336 + case '1':
337 + $color = self::getColor("red");
338 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.deface');
339 + break;
340 + case '2':
341 + $color = self::getColor("orange");
342 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.modified');
343 + break;
344 + }
345 + break;
346 + case 'ps':
347 + switch ((string)$status) {
348 + case '-1':
349 + $color = self::getColor("grey");
350 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
351 + break;
352 + case '-200':
353 + $color = self::getColor("grey");
354 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
355 + break;
356 + case '0':
357 + $color = self::getColor("green");
358 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
359 + break;
360 + case '1':
361 + $color = self::getColor("orange");
362 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.open');
363 + break;
364 + }
365 + break;
366 + case 'waf':
367 + switch ((string)$status) {
368 + case '-400':
369 + $color = self::getColor("red");
370 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.blocked');
371 + break;
372 + case '-300':
373 + $color = self::getColor("orange");
374 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.not_installed');
375 + break;
376 + case '-1':
377 + $color = self::getColor("grey");
378 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
379 + break;
380 + case '-200':
381 + $color = self::getColor("grey");
382 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
383 + break;
384 + case '0':
385 + $color = self::getColor("green");
386 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
387 + break;
388 + case '1':
389 + $color = self::getColor("red");
390 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.attacks_detected');
391 + break;
392 + }
393 + break;
394 + case 'vc':
395 + switch ((string)$status) {
396 + case '-400':
397 + $color = self::getColor("red");
398 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.blocked');
399 + break;
400 + case '-300':
401 + $color = self::getColor("orange");
402 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.not_installed');
403 + break;
404 + case '-1':
405 + $color = self::getColor("grey");
406 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.error');
407 + break;
408 + case '-200':
409 + $color = self::getColor("grey");
410 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.pending');
411 + break;
412 + case '0':
413 + $color = self::getColor("green");
414 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.ok');
415 + break;
416 + case '1':
417 + $color = self::getColor("orange");
418 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.file_changes');
419 + break;
420 + case '2':
421 + $color = self::getColor("red");
422 + $definition = WTSEC_LIBRARY_Localization::lmsg('statuses.signature_found');
423 + break;
424 + }
425 + break;
426 + }
427 + return ["icon" => $color["icon"], "color" => $color["color"], "text" => "<span class='" . $color["color"] . "'>" . $definition . "</span>"];
428 + }
429 +
430 + public static function getColor($type)
431 + {
432 + $types = [
433 + "green" => "is--status--ok",
434 + "red" => "is--status--error",
435 + "success" => "is--status--ok",
436 + "error" => "is--status--error",
437 + "orange" => "is--status--warning",
438 + "grey" => "ww--status_unknow"
439 + ];
440 + $icon_types = [
441 + "green" => "ww-icon--status_ok",
442 + "red" => "ww-icon--status_error",
443 + "success" => "ww-icon--status_ok",
444 + "error" => "ww-icon--status_error",
445 + "orange" => "ww-icon--status_warning",
446 + "grey" => "ww-icon--status_unknow"
447 + ];
448 + return ["icon" => $icon_types[$type], "color" => $types[$type]];
449 + }
450 +
392 451 }