-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.php
335 lines (293 loc) · 11.1 KB
/
core.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
# Cette fonction synchronise la table dblist avec le fichier de conf de base de données
function SyncTable($conn,$dbTarget) {
foreach ($dbTarget as $db) {
$query = $conn->prepare('INSERT INTO `hdm_core_dblist` ( `db_name`, `db_type`, `db_host`, `db_port`, `db_user`, `db_is_ssl`) VALUES (:dbname, :dbtype, :dbhost, :dbport, :dbuser, :dbisssl);');
if ($db['ssl'] == NULL) {
$db['ssl'] = "false";
}
if (!$query->execute(array(':dbname' => $db['database'],
':dbtype' => $db['type'],
':dbhost' => $db['host'],
':dbport' => $db['port'],
':dbuser' => $db['user'],
':dbisssl' => $db['ssl']))) {
print_r($query->errorInfo());
}
#dropping duplicates :
$query = $conn->prepare('DELETE
FROM hdm_core_dblist
WHERE id NOT IN
(SELECT id
FROM
(SELECT MIN(id) AS id
FROM hdm_core_dblist
GROUP BY `db_name`,
`db_type`,
`db_host`,
`db_port`,
`db_user`,
`db_is_ssl` HAVING COUNT(*) >= 1) AS c);');
if (!$query->execute()) {
print_r($query->errorInfo());
}
}
}
# Fonction qui permet de requêter une page
function getSSLPage($url) {
$ch = curl_init($url);
//Set to simple authentification
curl_setopt($ch, CURLOPT_USERPWD, NEXUS_USER . ":" . NEXUS_PASSWORD);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
# Fonction qui récupère le contenu sur Nexus
function getNexusContent($query="") {
# on récupère l'url du répo à requêter
if(!isset($_SESSION['nexusUrl'])) {
$_SESSION['nexusUrl'] = NEXUS_API_URL;
}
# on récupère le répo à requêter
if (!isset($_SESSION['nexusRepository'])) {
$_SESSION['nexusRepository'] = NEXUS_DEFAULT_REPOSITORY;
}
// Affichage par défaut des data
# On redémarre la liste
if(isset($_POST['resetList'])) {
unset($_SESSION['continuationToken']);
}
# requête pour récup les metric packs du répo NEXUS : HARDCODED : hdm.metricpacks
$url = $_SESSION['nexusUrl']."search?q=".$query."&repository=".$_SESSION['nexusRepository'];
unset($_SESSION['continuationToken']);
# On GET le content
$contents = getSSLPage($url);
# Si il y a du contenu
$data = "{}";
if($contents !== false){
# On décode le json
$data = json_decode($contents,true);
}
return $data;
}
# Fonction qui récupère la liste des bases de données à scanner
function getDbList($conn) {
# Getting MySQL Metrics database version
$sql = 'SELECT * FROM hdm_core_dblist';
$sth = $conn->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute();
return $sth->fetchAll(PDO::FETCH_ASSOC);
}
# Fonction qui récupère la liste de correspondance entre les bases et les metricpacks
function getDbMpCorrList($conn) {
# Getting MySQL Metrics database version
$sql = 'SELECT * FROM hdm_core_table_corr_db_mp';
$sth = $conn->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute();
return $sth->fetchAll(PDO::FETCH_ASSOC);
}
# Fonction qui récupère la liste de correspondance entre les bases et les metricpacks
function getMailList($conn,$mail) {
# Getting MySQL Metrics database version
$sql = 'SELECT * FROM hdm_core_mail_list WHERE mail = :mail';
$sth = $conn->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute(array(':mail' => $mail));
return $sth->fetchAll(PDO::FETCH_ASSOC);
}
# Fonction qui récupère la liste de correspondance entre les bases et les metricpacks
function getDbRpCorrList($conn) {
# Getting MySQL Rule database version
$sql = 'SELECT * FROM hdm_core_table_corr_db_rp';
$sth = $conn->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute();
return $sth->fetchAll(PDO::FETCH_ASSOC);
}
// alert table Fonction php qui prend en entrée une ligne de donnée SQL et affiche en une ligne de tableau HTML en tenant compte des afficheurs de filtre de niveau d'alerte
function writeRow($row): string
{
$row_print = "";
$linkRule = "";
switch ($row['alert_scope']) {
case 'database':
case 'all':
$linkRule = "/rule-editor/rule.php?rulePack=rule_".$row['rule_pack']."&database=".$row['database']."&newFilter=#";
break;
case 'table':
$linkRule = "/rule-editor/rule.php?rulePack=rule_".$row['rule_pack']."&database=".$row['database']."&table=".$row['table']."&newFilter=#";
break;
case 'column':
$linkRule = "/rule-editor/rule.php?rulePack=rule_".$row['rule_pack']."&database=".$row['database']."&table=".$row['table']."&column=".$row['column']."&newFilter=#";
break;
}
$display = FALSE;
if(($_SESSION['alert-display-high'] == "True") && ($row['alert_level'] == "High")){
$row_print .= "<tr class=\"table-danger row\">";
$display = TRUE;
} elseif(($_SESSION['alert-display-warn'] == "True") && ($row['alert_level'] == "Warning")){
$row_print .= "<tr class=\"table-warning row\">";
$display = TRUE;
} elseif(($_SESSION['alert-display-info'] == "True") && ($row['alert_level'] == "Info")){
$row_print .= "<tr class=\"table-info row\">";
$display = TRUE;
}
if(($_SESSION['filter-display-METRICCOMPARE'] != "True") && ($row['alert_class'] == "METRICCOMPARE")){
$display = FALSE;
} elseif(($_SESSION['filter-display-SCHEMA'] != "True") && ($row['alert_class'] == "SCHEMA")){
$display = FALSE;
} elseif(($_SESSION['filter-display-METRIQUE'] != "True") && ($row['alert_class'] == "METRIQUE")){
$display = FALSE;
} elseif(($_SESSION['filter-display-DATA'] != "True") && ($row['alert_class'] == "DATA")){
$display = FALSE;
}
$messageClass = "";
switch ($row['alert_class']) {
case 'METRICCOMPARE':
$messageClass = '<span class="badge badge-secondary">METRIQUECOMPARE</span>';
break;
case 'SCHEMA':
$messageClass = '<span class="badge badge-success">SCHEMA</span>';
break;
case 'METRIQUE':
$messageClass = '<span class="badge badge-primary">METRIQUE</span>';
break;
case 'DATA':
$messageClass = '<span class="badge badge-dark">DATA</span>';
break;
}
switch ($row['alert_level']) {
case 'High':
$messageClass .= '<span class="badge badge-danger">High</span>';
break;
case 'Warning':
$messageClass .= '<span class="badge badge-warning">Warning</span>';
break;
case 'Info':
$messageClass .= '<span class="badge badge-info">Info</span>';
break;
}
# creation du lien paramètre pour l'explorer kibana :
$linkKibana = "/explorer/wrapper.php?filtered=true&database=".$row['database']."&version=".$row['dbversion']."&table=".$row['table']."&column=".$row['column'];
if($display){
$row_print .= "<td class=\"col-lg-1 col-sm-1\">". $messageClass . "</td>\n";
$row_print .= "<td class=\"col-lg-2 col-sm-2\">". $row['database'] . "</td>\n";
$row_print .= "<td class=\"col-lg-1 col-sm-1\">". $row['dbversion'] . "</td>\n";
$row_print .= "<td class=\"col-lg-2 col-sm-2\">". $row['table'] . "</td>\n";
$row_print .= "<td class=\"col-lg-3 col-sm-3\">". $row['column'] . "</td>\n";
$row_print .= "<td class=\"col-lg-2 col-sm-3\">". $row['alert_message'] . "</td>\n";
$row_print .= "<td class=\"col-lg-1 col-sm-1\">
<a href=\"".$linkKibana."\" role=\"button\" class=\"btn btn-light\" style=\"padding:0;\"><i class=\"fas fa-search\"></i></a>
<a href=\"".$linkRule."\" role=\"button\" class=\"btn btn-light\" style=\"padding:0;\"><i class=\"fas fa-pencil-alt\"></i></i></a>
</td>\n";
$row_print .= '</tr>';
}
return $row_print;
}
// alert table function
function printBadge($array,$selector,$scope){
if(!empty($array)) {
foreach ($array as $value) {
if ($value[$scope] == $selector) {
$i = 0;
foreach ($value as $indice => $count) {
if ($indice == "alert_level") {
echo('<span style="float: right;" class="badge badge-');
switch($count) {
case 'High':
echo "danger";
break;
case 'Warning':
echo "warning";
break;
case 'Info':
echo "info";
break;
}
echo('">'.$value['count'].'</span>');
}
$i++;
}
}
}
}
}
// alert table function
function printHeader($value='',$display=False): string
{
$valueDisplay = "";
if ($display) {
$valueDisplay = "active show";
}
return '<div class="tab-pane fade '.$valueDisplay.'" id="v-pills-'.$value.'" role="tabpanel" aria-labelledby="v-pills-'.$value.'-tab">
<a href="?#'.$value.'"><h4 id="'.$value.'">'.$value.'</h4></a>
<table class="table">
<thead><tr class="row">
<th class="col-lg-1"><a href="?orderBy=alertClass">Badges</a></th>
<th class="col-lg-2"><a href="?orderBy=database">database</a></th>
<th class="col-lg-1"><a href="?orderBy=version">version</a></th>
<th class="col-lg-2"><a href="?orderBy=table">table</a></th>
<th class="col-lg-3"><a href="?orderBy=column">column</a></th>
<th class="col-lg-2"><a href="?orderBy=alert_message">message</a></th>
<th class="col-lg-1"><a href="?orderBy=rule">Link</a></th>
</tr></thead>
<tbody>';
}
// fonction qui permet de détruire toutes les variables de session des formulaires d'ajout des règles
function unsetFormRule()
{
unset($_SESSION['form-step']);
unset($_SESSION['ruleType']);
unset($_SESSION['ruleName']);
unset($_SESSION['sqlRequestValue']);
unset($_SESSION['sqlRequest']);
unset($_SESSION['alertScope']);
unset($_SESSION['alertMessage']);
unset($_SESSION['alertLevel']);
unset($_SESSION['alertClass']);
unset($_SESSION['conditionTrigger']);
unset($_SESSION['metric']);
unset($_SESSION['condition']);
unset($_SESSION['conditionValue']);
unset($_SESSION['conditionScope']);
unset($_SESSION['metrics']);
}
# Fonction qui permet de requêter sans arguments
function simple_query_db($conn,$query_string) {
try {
# Préparation de la requête pour insertion des données dans la table de tokens
if ($conn != null) {
$query = $conn->prepare($query_string);
# Execution de la requête
if(!$query->execute()) {
print_r($query->errorInfo());
} else {
return $query->fetchAll();
}
} else {
return "";
}
} catch (PDOException $e) {
echo 'Connexion échouée : '. $e->getMessage();
}
return null;
}
# This function sanitize a string to slug
function sanitize($title): string
{
$title = strip_tags($title);
// Preserve escaped octets.
$title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
// Remove percent signs that are not part of an octet.
$title = str_replace('%', '', $title);
// Restore octets.
$title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
$title = strtolower($title);
$title = preg_replace('/&.+?;/', '', $title); // kill entities
$title = str_replace('.', '-', $title);
$title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
$title = preg_replace('/\s+/', '-', $title);
$title = preg_replace('|-+|', '-', $title);
$title = trim($title, '-');
return $title;
}