-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
227 lines (209 loc) · 8.56 KB
/
index.php
File metadata and controls
227 lines (209 loc) · 8.56 KB
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
<?php
// -------------------------------------- //
// parametri di connessione al database
$host = "mysql-db";
$user = "root";
$pass = "123456";
$db = "mysql";
// -------------------------------------- //
// --------------------------------------------------------------- //
// tentativo di connessione al database
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connessione fallita: $conn->connect_error");
}
// --------------------------------------------------------------- //
// --------------------------------------------------------------- //
// parametri per la ricerca di elementi nel database
$search = $_POST["search"] ?? "";
$user_results = [];
$product_results = [];
$message = "";
// --------------------------------------------------------------- //
// ----------------------------------------------------------------------------------------------------------------------- //
// query volutamente vulnerabile ad attacchi di SQL Injection (di tipo In-Band), per la ricerca di elementi nel database
if ($_SERVER["REQUEST_METHOD"] === "POST" && !empty($search)) {
$query1 = "SELECT 'utente' AS tipo,
id,
nome,
email,
pass_word,
registrazione,
NULL AS descrizione
FROM utenti
WHERE nome = '$search'";
$result1 = $conn->query($query1);
if ($result1) {
$user_results = $result1->fetch_all(MYSQLI_ASSOC);
$result1->free();
} else {
$message = "Errore query utenti: " . $conn->error;
}
// Query 2: Prodotti
$query2 = "SELECT 'prodotto' AS tipo,
id,
nome,
NULL AS email,
NULL as pass_word,
rilascio AS registrazione,
descrizione
FROM prodotti
WHERE nome = '$search'";
$result2 = $conn->query($query2);
if ($result2) {
$product_results = $result2->fetch_all(MYSQLI_ASSOC);
$result2->free();
} else {
$message = "Errore query prodotti: " . $conn->error;
}
if (empty($user_results) && empty($product_results)) {
$message = "Nessun risultato trovato.";
}
// ----------------------------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------- //
// tentativo di esecuzione della query
if ($conn->multi_query($query1 ?? $query2)) {
do {
if ($result = $conn->store_result()) {
$current_results = $result->fetch_all(MYSQLI_ASSOC);
if (!empty($current_results)) {
if (
isset($current_results[0]["tipo"]) &&
$current_results[0]["tipo"] === "utente"
) {
$user_results = $current_results;
} elseif (
isset($current_results[0]["tipo"]) &&
$current_results[0]["tipo"] === "prodotto"
) {
$product_results = $current_results;
}
}
$result->free();
}
} while ($conn->more_results() && $conn->next_result());
} else {
$message = "Errore nell'esecuzione della query: $conn->error";
}
if (empty($user_results) && empty($product_results)) {
$message = "Nessun risultato trovato.";
}
}
// ------------------------------------------------------------------------- //
$conn->close();
?>
<!-- formattazione della pagina web -->
<!DOCTYPE html>
<html>
<head>
<title>In-Band SQL Injection</title>
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<link rel="shortcut icon" type="image/x-icon" href="https://cdn-icons-png.flaticon.com/512/6843/6843633.png" />
</head>
<body>
<div class="container">
<h1>Accesso Database</h1>
<form method="POST" action="">
<div class="search-container">
<input type="text"
name="search"
placeholder="esempi: utenteX, prodottoY"
value="<?php echo htmlspecialchars($search); ?>">
<button type="submit">
<i class="fa fa-search"></i>
</button>
</div>
</form>
<?php if (!empty($message)): ?>
<p><?php echo $message; ?></p>
<?php endif; ?>
<!-- Tabella Utenti -->
<?php if (!empty($user_results)): ?>
<br>
<h3> Utenti Trovati:</h3>
<div class="table-wrapper">
<table border="1">
<thead>
<tr>
<?php foreach (
array_keys($user_results[0])
as $header
): ?>
<?php if (
$header !== "tipo" &&
$header !== "descrizione"
): ?>
<th><?php echo htmlspecialchars(
$header,
); ?></th>
<?php endif; ?>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach ($user_results as $row): ?>
<tr>
<?php foreach ($row as $key => $cell): ?>
<?php if (
$key !== "tipo" &&
$key !== "descrizione"
): ?>
<td><?php echo htmlspecialchars(
$cell ?? "NULL",
); ?></td>
<?php endif; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
<!-- Tabella Prodotti -->
<?php if (!empty($product_results)): ?>
<br>
<h3>Prodotti Trovati:</h3>
<div class="table-wrapper">
<table border="1">
<thead>
<tr>
<?php foreach (
array_keys($product_results[0])
as $header
): ?>
<?php if (
$header !== "tipo" &&
$header !== "email" &&
$header !== "pass_word"
): ?>
<th><?php echo htmlspecialchars(
$header,
); ?></th>
<?php endif; ?>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach ($product_results as $row): ?>
<tr>
<?php foreach ($row as $key => $cell): ?>
<?php if (
$key !== "tipo" &&
$key !== "email" &&
$key !== "pass_word"
): ?>
<td><?php echo htmlspecialchars(
$cell ?? "NULL",
); ?></td>
<?php endif; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</body>
</html>