Ecco cosa ho fatto:
Codice PHP:
/* Controlla se l'ip è gia presente nel database */
function brute_force(){
if (export_data(getenv('REMOTE_ADDR'), 1, 'logins.db')){
if ($export_data[2]<time()){
return true;
}
}
}
/* Salva i logins falliti */
function save_brute_force(){
/* L'ip esiste? */
if (export_data(getenv('REMOTE_ADDR'), 1, 'logins.db')){
/* I tentativi di login sono minori di 10 ? */
if ($export_data[3]<10){
/* Modifica la riga aggiungendo un log in più ai tentativi */
edit_line($export_data, 3, $export_data[3]+1);
/* Infine modifica la riga */
edit_file("{$board['database']}/ips.db", $export_data[0], $new_row);
}
else{
/* Se non lo sono blocca i tentivi di login per 15 minuti */
edit_line($export_data, 2, time()+900);
edit_file("{$board['database']}/ips.db", $export_data[0], $new_row);
}
}
else{
/* Aggiunge un record */
create_file('ips.db', count(file("{$board['database']}/ips.db"))+1 .'|'.getenv('REMOTE_ADDR')."||0|\n", 'a');
}
}
ed ecco le funzioni edit_line, edit_file, create_file e export_date:
Codice PHP:
// ---------------------------------
// Estrae i dati di un file
// ---------------------------------
function export_data($input, $row, $file) { // es. $input = 'nome_utent', $row = 1, $file = '/users.db';
global $export_data, $board;
$fp = fopen(SYSTEM_PATH . "{$board['database']}/$file", 'r');
while ( !feof($fp) ) {
$Line = fgets($f, 1024);
$Data = explode('|', $Line);
if ( strtolower($Data[$number]) == block_char(strtolower(trim($input))) ) {
foreach ( $Data as $Key => $Value ) {
$export_data[$Key] = $Value;
}
return true;
}
}
}
// ---------------------------------
// Modifica/Aggiorna file
// ---------------------------------
function edit_file($file, $row, $line) { // es. $file = 'user.db', $row = 3, $line = '3|username|password|etc...';
$edit = file(SYSTEM_PATH . $file);
unlink(SYSTEM_PATH . $file);
$f=fopen(SYSTEM_PATH . $file, 'a+');
for ( $i=0; $i < count($edit); $i++ ) {
if ( $i == $row - 1) {
fwrite($f, $line . "\n"); continue;
}
fwrite($f, $edit[$i]);
}
fclose($f);
}
function edit_line($array, $number, $value, $method) { global $new_row;
foreach ( $array as $Key => $Value ) {
if ( $Key == $number && empty($method) ) {
$new_row .= $value;
}
else {
$new_row .= $Value;
}
}
if ( isset($method) ) {
$new_row .= $value;
}
}
// Crea o aggiorna un file
function create_file($name, $string, $method) {
$fp = fopen($name, $method);
fwrite($fp, $string);
fclose($fp);
}
Ed ecco un tipico esempio di dato in cui viene salvato l'ip.
Codice PHP:
1|IP|TIME()|NUMERO LOGIN FALLITI|
2|80.201.195.23|12312|8|
C'è un modo per fare il tutto con:
Meno righe
Che ci metta di meno a scorrere il file
E magari con un codice più elegante?