Browse Source

#137 Non-user based DDoS check

added a new table for login_attempts (in both SQL files)
added 2 config vars `login_attempts`(db) & `remove_successful_attempts`
changed function `reset_login_attempts()` (removed user_id and changed where to ip_address and timestamp from user_id only)
changed function `update_login_attempts()` (removed user_id and changed where to ip_address and timestamp from email/user_id only)
changed function `login()` (removed arguments from changed functions, added abilty to enable/disable removing login attempt after successful login)
develop
REJack 9 years ago
parent
commit
34f66afe5e
  1. 6
      application/config/aauth.php
  2. 62
      application/libraries/Aauth.php
  3. 15
      sql/Aauth_v2.sql
  4. 15
      sql/Aauth_v2_BCrypt.sql

6
application/config/aauth.php

@ -31,6 +31,7 @@ defined('BASEPATH') OR exit('No direct script access allowed');
| ['perm_to_user'] The table which contains permissions for users | ['perm_to_user'] The table which contains permissions for users
| ['pms'] The table which contains private messages | ['pms'] The table which contains private messages
| ['user_variables'] The table which contains users variables | ['user_variables'] The table which contains users variables
| ['login_attempts'] The table which contains login attempts
| |
| ['remember'] Remember time elapsed after connecting and automatic LogOut | ['remember'] Remember time elapsed after connecting and automatic LogOut
| |
@ -49,11 +50,12 @@ defined('BASEPATH') OR exit('No direct script access allowed');
| ['totp_active'] The Time-based One-time Password Algorithm | ['totp_active'] The Time-based One-time Password Algorithm
| ['totp_only_on_ip_change'] TOTP only on IP Change | ['totp_only_on_ip_change'] TOTP only on IP Change
| ['totp_reset_over_reset_password'] TOTP reset over reset Password | ['totp_reset_over_reset_password'] TOTP reset over reset Password
| ['totp_two_step_login'] enables TOTP two step login | ['totp_two_step_login'] Enables/Disables TOTP two step login
| ['totp_two_step_login_redirect'] Redirect path to TOTP Verification page used by control() & is_allowed() | ['totp_two_step_login_redirect'] Redirect path to TOTP Verification page used by control() & is_allowed()
| |
| ['max_login_attempt'] Login attempts time interval (default 10 times in one hour) | ['max_login_attempt'] Login attempts time interval (default 10 times in one hour)
| ['max_login_attempt_time_period'] Period of time for max login attempts (default "5 minutes") | ['max_login_attempt_time_period'] Period of time for max login attempts (default "5 minutes")
| ['remove_successful_attempts'] Enables/Disables removing login attempt after successful login
| |
| ['login_with_name'] Login Identificator, if TRUE username needed to login else email address. | ['login_with_name'] Login Identificator, if TRUE username needed to login else email address.
| |
@ -93,6 +95,7 @@ $config_aauth["default"] = array(
'perm_to_user' => 'aauth_perm_to_user', 'perm_to_user' => 'aauth_perm_to_user',
'pms' => 'aauth_pms', 'pms' => 'aauth_pms',
'user_variables' => 'aauth_user_variables', 'user_variables' => 'aauth_user_variables',
'login_attempts' => 'aauth_login_attempts',
'remember' => ' +3 days', 'remember' => ' +3 days',
@ -116,6 +119,7 @@ $config_aauth["default"] = array(
'max_login_attempt' => 10, 'max_login_attempt' => 10,
'max_login_attempt_time_period' => "5 minutes", 'max_login_attempt_time_period' => "5 minutes",
'remove_successful_attempts' => true,
'login_with_name' => false, 'login_with_name' => false,

62
application/libraries/Aauth.php

@ -174,7 +174,7 @@ class Aauth {
$row = $query->row(); $row = $query->row();
// only email found and login attempts exceeded // only email found and login attempts exceeded
if ($query->num_rows() > 0 && $this->config_vars['ddos_protection'] && ! $this->update_login_attempts($row->email)) { if ($query->num_rows() > 0 && $this->config_vars['ddos_protection'] && ! $this->update_login_attempts()) {
$this->error($this->CI->lang->line('aauth_error_login_attempts_exceeded')); $this->error($this->CI->lang->line('aauth_error_login_attempts_exceeded'));
return FALSE; return FALSE;
@ -352,7 +352,10 @@ class Aauth {
// update last login // update last login
$this->update_last_login($row->id); $this->update_last_login($row->id);
$this->update_activity(); $this->update_activity();
$this->reset_login_attempts($row->id);
if($this->config_vars['remove_successful_attempts'] == TRUE){
$this->reset_login_attempts();
}
return TRUE; return TRUE;
} }
@ -536,15 +539,18 @@ class Aauth {
/** /**
* Reset last login attempts * Reset last login attempts
* Sets a users 'last login attempts' to null * Removes a Login Attempt
* @param int $user_id User id to reset
* @return bool Reset fails/succeeds * @return bool Reset fails/succeeds
*/ */
public function reset_login_attempts($user_id) { public function reset_login_attempts() {
$ip_address = $this->CI->input->ip_address();
$data['login_attempts'] = null; $this->aauth_db->where(
$this->aauth_db->where('id', $user_id); array(
return $this->aauth_db->update($this->config_vars['users'], $data); 'ip_address'=>$ip_address,
'timestamp >='=>strtotime("-".$this->config_vars['max_login_attempt_time_period'])
)
);
return $this->aauth_db->delete($this->config_vars['login_attempts']);
} }
/** /**
@ -645,35 +651,39 @@ class Aauth {
//tested //tested
/** /**
* Update login attempt and if exceeds return FALSE * Update login attempt and if exceeds return FALSE
* Update user's last login attemp date and number date
* @param string $email User email
* @return bool * @return bool
*/ */
public function update_login_attempts($email) { public function update_login_attempts() {
$ip_address = $this->CI->input->ip_address();
$user_id = $this->get_user_id($email); $query = $this->aauth_db->where(
array(
'ip_address'=>$ip_address,
'timestamp >='=>strtotime("-".$this->config_vars['max_login_attempt_time_period'])
)
);
$query = $this->aauth_db->get( $this->config_vars['login_attempts'] );
$query = $this->aauth_db->where('id', $user_id); if($query->num_rows() == 0){
$query = $this->aauth_db->get( $this->config_vars['users'] ); $data = array();
$data['ip_address'] = $ip_address;
$data['timestamp']= date("Y-m-d H:i:s");
$data['login_attempts']= 1;
$this->aauth_db->insert($this->config_vars['login_attempts'], $data);
return TRUE;
}else{
$row = $query->row(); $row = $query->row();
$data = array(); $data = array();
$data['last_login_attempt'] = date("Y-m-d H:i:s"); $data['timestamp'] = date("Y-m-d H:i:s");
if (strtotime($row->last_login_attempt) > strtotime("-".$this->config_vars['max_login_attempt_time_period'])) {
$data['login_attempts'] = $row->login_attempts + 1; $data['login_attempts'] = $row->login_attempts + 1;
} else { $this->aauth_db->where('id', $row->id);
$data['login_attempts'] = 1; $this->aauth_db->update($this->config_vars['login_attempts'], $data);
}
$this->aauth_db->where('id', $user_id);
$this->aauth_db->update($this->config_vars['users'], $data);
if ( $data['login_attempts'] > $this->config_vars['max_login_attempt'] ) { if ( $data['login_attempts'] > $this->config_vars['max_login_attempt'] ) {
return FALSE; return FALSE;
} else { } else {
return TRUE; return TRUE;
} }
}
} }

15
sql/Aauth_v2.sql

@ -160,3 +160,18 @@ CREATE TABLE `aauth_group_to_group` (
-- Records of aauth_perm_to_group -- Records of aauth_perm_to_group
-- ---------------------------- -- ----------------------------
-- ----------------------------
-- Table structure for `aauth_login_attempts`
-- ----------------------------
CREATE TABLE IF NOT EXISTS `aauth_login_attempts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ip_address` varchar(39) DEFAULT '0',
`timestamp` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`login_attempts` tinyint(2) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of aauth_login_attempts
-- ----------------------------

15
sql/Aauth_v2_BCrypt.sql

@ -160,3 +160,18 @@ CREATE TABLE `aauth_group_to_group` (
-- Records of aauth_perm_to_group -- Records of aauth_perm_to_group
-- ---------------------------- -- ----------------------------
-- ----------------------------
-- Table structure for `aauth_login_attempts`
-- ----------------------------
CREATE TABLE IF NOT EXISTS `aauth_login_attempts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ip_address` varchar(39) DEFAULT '0',
`timestamp` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`login_attempts` tinyint(2) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of aauth_login_attempts
-- ----------------------------

Loading…
Cancel
Save