Skip to content

Commit afdf3ab

Browse files
committed
ripped out all the memcache session stuff, not worth it with the side effects of caching things
cleaned up config/checks
1 parent 8487a8d commit afdf3ab

10 files changed

+60
-292
lines changed

public/include/admin_checks.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
if (mysqli_connect_errno() || !array_key_exists('client_info', $db_connect)) {
7979
$error[] = "Unable to connect to mysql using provided credentials";
8080
}
81-
if (($config['strict'] || $config['mc_antidos']) && !$config['memcache']['enabled']) {
82-
$error[] = "strict or mc_antidos are enabled and memcache is not, <u>memcache is required</u> to use these.";
81+
if ($config['mc_antidos'] && !$config['memcache']['enabled']) {
82+
$error[] = "mc_antidos is enabled and memcache is not, <u>memcache is required</u> to use this";
8383
}
8484
// poke stratum using gettingstarted details -> enotice
8585
if (substr_count(strtolower(PHP_OS), 'nix') > 0) {
@@ -102,10 +102,6 @@
102102
}
103103

104104
// security checks
105-
// strict not on -> notice
106-
if (!$config['strict']) {
107-
$notice[] = "Strict is <u>disabled</u> - if you have memcache, you should turn this on.";
108-
}
109105
// salts too short -> notice, salts default -> error
110106
if ((strlen($config['SALT']) < 24) || (strlen($config['SALTY']) < 24) || $config['SALT'] == 'PLEASEMAKEMESOMETHINGRANDOM' || $config['SALTY'] == 'THISSHOULDALSOBERRAANNDDOOM') {
111107
if ($config['SALT'] == 'PLEASEMAKEMESOMETHINGRANDOM' || $config['SALTY'] == 'THISSHOULDALSOBERRAANNDDOOM') {

public/include/autoloader.inc.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// Default classes
1313
require_once(CLASS_DIR . '/debug.class.php');
1414
require_once(INCLUDE_DIR . '/lib/KLogger.php');
15-
if ($config['strict']) {
15+
if ($config['mysql_filter']) {
1616
require_once(CLASS_DIR . '/strict.class.php');
1717
}
1818
require_once(INCLUDE_DIR . '/database.inc.php');

public/include/classes/memcache_ad.class.php

+24-61
Original file line numberDiff line numberDiff line change
@@ -7,116 +7,79 @@ class MemcacheAntiDos
77
public $rate_limit_this_request = false;
88
public $rate_limit_api_request = false;
99
public $rate_limit_site_request = false;
10-
public function __construct($config, &$memcache, $userORip, $request='', $mcSettings) {
10+
public function __construct($config, &$memcache, $request='') {
1111
$this->cache = $memcache;
1212
// set our config options
13-
$per_page = '';
14-
$flush_sec_api = $config['flush_seconds_api'];
15-
$rate_limit_api = $config['rate_limit_api'];
16-
$flush_sec_site = $config['flush_seconds_site'];
17-
$rate_limit_site = $config['rate_limit_site'];
18-
$ajax_add = $config['ajax_hits_additive'];
19-
unset($config);
13+
$userORip = $_SERVER['REMOTE_ADDR'].$_SERVER['HTTP_USER_AGENT'];
2014
// prep stuff we need to check this request
21-
$key_md5 = md5($mcSettings['keyprefix'].$userORip);
15+
$key_md5 = $config['memcache']['keyprefix'].md5($userORip);
2216
$request_data = $this->cache->get($key_md5);
2317
$now = time();
24-
$max_req_flush = max(array($flush_sec_api,$flush_sec_site));
18+
$max_req_flush = max(array($config['mc_antidos']['flush_seconds_api'],$config['mc_antidos']['flush_seconds_site']));
2519
// check the request
2620
if (is_array($request_data)) {
2721
// this request key already exists, update it
2822
$request_data['la'] = $now;
2923
if ($request == 'api') {
3024
$request_data['ha'] += 1;
31-
if ($ajax_add) {
25+
if ($config['mc_antidos']['ajax_hits_additive']) {
3226
$request_data['hn'] += 1;
3327
}
3428
} else {
3529
$request_data['hn'] += 1;
3630
}
3731
// not rate limited yet, update the rest of the object
38-
if (($request_data['hn'] < $rate_limit_site) && ($request_data['ha'] < $rate_limit_api)) {
39-
40-
if (((($request_data['hnl'] + $flush_sec_site) <= $now) || ($request_data['hal'] + $flush_sec_api) <= $now) || (($request_data['la'] + $max_req_flush) <= $now)) {
32+
if (($request_data['hn'] < $config['mc_antidos']['rate_limit_site']) && ($request_data['ha'] < $config['mc_antidos']['rate_limit_api'])) {
33+
if (((($request_data['hnl'] + $config['mc_antidos']['flush_seconds_site']) <= $now) || ($request_data['hal'] + $config['mc_antidos']['flush_seconds_api']) <= $now) || (($request_data['la'] + $max_req_flush) <= $now)) {
4134
// needs to be flushed & updated
4235
$new = $this->getRequestBase();
4336
$new['key'] = $key_md5;
44-
$new['sid'] = session_id();
45-
$new['ua'] = md5($_SERVER['HTTP_USER_AGENT']);
46-
$new['ip'] = $key_md5;
4737
$new['la'] = $now;
48-
$new['hal'] = ((($request_data['hal'] + $flush_sec_api) <= $now)) ? $now : 1;
49-
$new['hnl'] = ((($request_data['hnl'] + $flush_sec_site) <= $now)) ? $now : 1;
50-
$this->cache->set($key_md5, $new, $max_req_flush);
51-
$this->rate_limit_api_request = ($request_data['ha'] >= $rate_limit_api) ? true : false;
52-
$this->rate_limit_site_request = ($request_data['hn'] >= $rate_limit_site) ? true : false;
53-
//$this->rate_limit_this_request = false;
38+
$new['hal'] = ((($request_data['hal'] + $config['mc_antidos']['flush_seconds_api']) <= $now)) ? $now : 1;
39+
$new['hnl'] = ((($request_data['hnl'] + $config['mc_antidos']['flush_seconds_site']) <= $now)) ? $now : 1;
40+
$this->cache->set($key_md5, $new, $config['memcache']['expiration']);
41+
$this->rate_limit_api_request = ($request_data['ha'] >= $config['mc_antidos']['rate_limit_api']) ? true : false;
42+
$this->rate_limit_site_request = ($request_data['hn'] >= $config['mc_antidos']['rate_limit_site']) ? true : false;
5443
} else {
5544
// no flush, just update
5645
$new = $this->getRequestBase();
57-
$new['key'] = $key_md5;
58-
$new['sid'] = session_id();
59-
$new['ua'] = md5($_SERVER['HTTP_USER_AGENT']);
60-
$new['ip'] = $key_md5;
46+
$new['key'] = $request_data['key'];
6147
$new['la'] = time();
6248
$new['ha'] = $request_data['ha'];
6349
$new['hal'] = $request_data['hal'];
6450
$new['hn'] = $request_data['hn'];
6551
$new['hnl'] = $request_data['hnl'];
66-
$this->cache->set($key_md5, $new, $max_req_flush);
67-
//$this->rate_limit_this_request = false;
68-
$this->rate_limit_api_request = ($request_data['ha'] >= $rate_limit_api) ? true : false;
69-
$this->rate_limit_site_request = ($request_data['hn'] >= $rate_limit_site) ? true : false;
52+
$this->cache->set($key_md5, $new, $config['memcache']['expiration']);
53+
$this->rate_limit_api_request = ($request_data['ha'] >= $config['mc_antidos']['rate_limit_api']) ? true : false;
54+
$this->rate_limit_site_request = ($request_data['hn'] >= $config['mc_antidos']['rate_limit_site']) ? true : false;
7055
}
7156
} else {
7257
// too many hits, we should rate limit this
73-
//$this->rate_limit_this_request = true;
74-
$this->rate_limit_api_request = ($request_data['ha'] >= $rate_limit_api) ? true : false;
75-
$this->rate_limit_site_request = ($request_data['hn'] >= $rate_limit_site) ? true : false;
58+
$this->rate_limit_api_request = ($request_data['ha'] >= $config['mc_antidos']['rate_limit_api']) ? true : false;
59+
$this->rate_limit_site_request = ($request_data['hn'] >= $config['mc_antidos']['rate_limit_site']) ? true : false;
7660
}
7761
} else {
7862
// doesn't exist for this request_key, create one
7963
$new = $this->getRequestBase();
80-
$new['key'] = $key_md5;
81-
$new['sid'] = session_id();
82-
$new['ua'] = md5($_SERVER['HTTP_USER_AGENT']);
83-
$new['ip'] = $key_md5;
64+
$new['key'] = $config['memcache']['keyprefix'].md5($userORip);
8465
$new['la'] = time();
8566
if ($request == 'api') {
8667
$new['ha'] += 1;
87-
if ($ajax_add) {
68+
if ($config['mc_antidos']['ajax_hits_additive']) {
8869
$new['hn'] += 1;
8970
}
9071
} else {
9172
$new['hn'] += 1;
9273
}
93-
$this->cache->set($key_md5, $new, $max_req_flush);
94-
$this->rate_limit_this_request = false;
74+
$this->cache->set($key_md5, $new, $config['memcache']['expiration']);
75+
$this->rate_limit_api_request = false;
76+
$this->rate_limit_site_request = false;
9577
}
9678
}
9779
public function getRequestBase() {
98-
$new = array(
99-
'key' => '',
100-
'sid' => '',
101-
'ua' => '',
102-
'ip' => '',
103-
'la' => 0,
104-
'hn' => 0,
105-
'hnl' => 0,
106-
'ha' => 0,
107-
'hal' => 0
108-
);
80+
$new = array('key' => '','la' => 0,'hn' => 0,'hnl' => 0,'ha' => 0,'hal' => 0);
10981
return $new;
11082
}
111-
public function rateLimitRequest() {
112-
return $this->rate_limit_this_request;
113-
}
114-
public function rateLimitSite() {
115-
return $this->rate_limit_site_request;
116-
}
117-
public function rateLimitAPI() {
118-
return $this->rate_limit_api_request;
119-
}
12083
}
12184

12285
?>

public/include/classes/strict.class.php

-137
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,6 @@
11
<?php
22
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
33

4-
class strict_session {
5-
private $memcache = null;
6-
private $validate_client = false;
7-
private $validate_client_ip = false;
8-
private $validate_client_ua = false;
9-
private $validate_client_sid = false;
10-
private $validate_client_num = 0;
11-
private $valid_server = '';
12-
private $memcache_key = '';
13-
public function valid_session_id($id) {
14-
return preg_match('#^[a-zA-Z0-9]{26}$#', $id);
15-
}
16-
public function session_delete_key($key) {
17-
$read = $this->memcache->delete($key);
18-
}
19-
private $validation_misses = 0;
20-
private $initial_ua;
21-
public function create_or_update_client($client, $force=false, $login=false) {
22-
$read = $this->memcache->get($client['key']);
23-
// this needs to be available later
24-
$update = array('key' => '','sid' => '','ua' => '','ip' => '','la' => 0,'hn' => 0,'hnl' => 0,'ha' => 0,'hal' => 0);
25-
$update['sid'] = $client['sid'];
26-
$update['ua'] = md5($this->initial_ua);
27-
$update['ip'] = $client['ip'];
28-
$update['la'] = time();
29-
$update['key'] = md5($this->memcache_key.$client['ip']);
30-
$validation_misses = 0;
31-
if ($read !== false) {
32-
$read_model = array('key' => '','sid' => '','ua' => '','ip' => '','la' => 0,'hn' => 0,'hnl' => 0,'ha' => 0,'hal' => 0);
33-
$read_model['sid'] = @$read['sid'];
34-
$read_model['ip'] = @$read['ip'];
35-
$read_model['ua'] = @$read['ua'];
36-
$read_model['la'] = @$read['la'];
37-
$read_model['key'] = md5($this->memcache_key.$read['ip']);
38-
// key already exists, update
39-
if ($this->validate_client) {
40-
if ($this->verify_client($read_model, $update, $login)) {
41-
$update_client = $this->memcache->set($update['key'], $update);
42-
}
43-
}
44-
} else {
45-
$update_client = $this->memcache->set($client['key'], $client);
46-
if ($force && $login) {
47-
$update_client = $this->memcache->set($update['key'], $update);
48-
}
49-
}
50-
}
51-
public function verify_client($client_model, $data, $login=false) {
52-
$fails = 0;
53-
$fails += ((count($client_model)) !== (count($data)) && $this->validate_client) ? 1 : 0;
54-
$fails += ($client_model['ua'] !== $data['ua'] && $this->validate_client && $this->validate_client_ua) ? 1 : 0;
55-
$fails += ($client_model['ip'] !== $data['ip'] && $this->validate_client && $this->validate_client_ip) ? 1 : 0;
56-
$now = time();
57-
$this->validation_misses = $fails;
58-
if ($fails > $this->validate_client_num && $login == false && $this->validate_client) {
59-
// something changed
60-
$port = ($_SERVER["SERVER_PORT"] == "80" || $_SERVER["SERVER_PORT"] == "443") ? "" : (":".$_SERVER["SERVER_PORT"]);
61-
$location = (@$_SERVER['HTTPS'] == "on") ? 'https://' : 'http://';
62-
$location .= $_SERVER['SERVER_NAME'] . $port . $_SERVER['SCRIPT_NAME'];
63-
$this->session_delete_key($client_model['key']);
64-
$this->session_delete_key($data['key']);
65-
@session_start();
66-
@session_regenerate_id(true);
67-
$_SESSION = null;
68-
$_SESSION['POPUP'][] = array('CONTENT' => "Session revoked due to a change in your client. You may have a plugin messing with your useragent, or your IP address may have changed.", 'TYPE' => 'warning');
69-
$location.= '?page=login';
70-
if (!headers_sent()) exit(header('Location: ' . $location));
71-
exit('<meta http-equiv="refresh" content="0; url=' . htmlspecialchars($location) . '"/>');
72-
}
73-
return ($fails > 0) ? false : true;
74-
}
75-
public function read_if_client_exists($client_key) {
76-
if ($this->memcache !== null) {
77-
$exists = $this->memcache->get($client_key);
78-
}
79-
return ($exists !== null) ? $exists : false;
80-
}
81-
public function regen_session_id() {
82-
$sidbefore = @session_id();
83-
@session_regenerate_id(true);
84-
$sid = session_id();
85-
return $sid;
86-
}
87-
public function __construct($config, &$memcache) {
88-
$this->initial_ua = $_SERVER['HTTP_USER_AGENT'];
89-
$this->memcache = $memcache;
90-
$this->memcache_key = $config['memcache']['keyprefix'];
91-
if ($config['strict__verify_client']) {
92-
$this->validate_client = true;
93-
$this->validate_client_ip = $config['strict__verify_client_ip'];
94-
$this->validate_client_ua = $config['strict__verify_client_useragent'];
95-
$this->validate_client_sid = $config['strict__verify_client_sessionid'];
96-
$this->validate_client_num = 0;
97-
if ($config['strict__verify_server']) {
98-
$proto = (@$_SERVER['HTTPS'] == "on") ? 'https' : 'http';
99-
$location = $proto."://".$_SERVER['SERVER_NAME'] . $_SERVER['SERVER_PORT'];
100-
if ($config['strict__verify_server']) {
101-
if ($config['strict__bind_protocol']."://".$config['strict__bind_host'].$config['strict__bind_port'] !== $location) {
102-
return false;
103-
}
104-
}
105-
}
106-
$client = array('key' => '','sid' => '','ua' => '','ip' => '','la' => 0,'hn' => 0,'hnl' => 0,'ha' => 0,'hal' => 0);
107-
$client['ua'] = md5($_SERVER['HTTP_USER_AGENT']);
108-
$client['ip'] = md5($_SERVER['REMOTE_ADDR']);
109-
$client['la'] = time();
110-
$client['key'] = md5($this->memcache_key.$client['ip']);
111-
$read = $this->read_if_client_exists($client['key']);
112-
}
113-
session_set_cookie_params((time()+$config['cookie']['duration']), $config['cookie']['path'], $config['cookie']['domain'], false, true);
114-
$session_start = @session_start();
115-
$client['sid'] = session_id();
116-
$valid_session_id = $this->valid_session_id($client['sid']);
117-
if (!$valid_session_id || !$session_start) {
118-
@session_destroy();
119-
$client['sid'] = $this->regen_session_id();
120-
session_start();
121-
}
122-
if ($read !== null) {
123-
// client exists, verify
124-
$this->create_or_update_client($client, true, false);
125-
126-
} else {
127-
// doesn't exist
128-
$this->create_or_update_client($client, true, true);
129-
}
130-
@setcookie(session_name(), $client['sid'], (time()+$config['cookie']['duration']), $config['cookie']['path'], $config['cookie']['domain'], false, true);
131-
// post changes validate
132-
if ($this->validate_client) {
133-
$read_post = $this->read_if_client_exists($client['key']);
134-
if ($read_post !== null) {
135-
$this->verify_client($client, $read_post, true);
136-
}
137-
}
138-
}
139-
}
140-
1414
class mysqli_strict extends mysqli {
1425
public function bind_param($paramTypes) {
1436
if (!is_string($paramTypes)) {

public/include/classes/user.class.php

+8-22
Original file line numberDiff line numberDiff line change
@@ -503,17 +503,10 @@ private function createSession($username, $lastIP='', $lastLoginTime='') {
503503
if (!empty($lastIP) && (!empty($lastLoginTime))) {
504504
$_SESSION['last_ip_pop'] = array($lastIP, $lastLoginTime);
505505
}
506-
if ($this->config['strict'] && $this->config['memcache']['enabled']) {
507-
session_regenerate_id(true);
508-
$_SESSION['AUTHENTICATED'] = '1';
509-
// $this->user from checkUserPassword
510-
$_SESSION['USERDATA'] = $this->user;
511-
} else {
512-
session_regenerate_id(true);
513-
$_SESSION['AUTHENTICATED'] = '1';
514-
// $this->user from checkUserPassword
515-
$_SESSION['USERDATA'] = $this->user;
516-
}
506+
session_regenerate_id(true);
507+
$_SESSION['AUTHENTICATED'] = '1';
508+
// $this->user from checkUserPassword
509+
$_SESSION['USERDATA'] = $this->user;
517510
}
518511

519512
/**
@@ -814,17 +807,10 @@ public function initResetPassword($username) {
814807
**/
815808
public function isAuthenticated($logout=true) {
816809
$this->debug->append("STA " . __METHOD__, 4);
817-
if (!$this->config['strict']) {
818-
if (@$_SESSION['AUTHENTICATED'] == true &&
819-
!$this->isLocked($_SESSION['USERDATA']['id']) &&
820-
$this->getUserIp($_SESSION['USERDATA']['id']) == $_SERVER['REMOTE_ADDR']
821-
) return true;
822-
} else {
823-
if (@$_SESSION['AUTHENTICATED'] && $_SESSION['AUTHENTICATED'] == '1' &&
824-
(!$this->isLocked($_SESSION['USERDATA']['id'])) &&
825-
($this->getUserIp($_SESSION['USERDATA']['id']) == $_SERVER['REMOTE_ADDR']))
826-
return true;
827-
}
810+
if (@$_SESSION['AUTHENTICATED'] == true &&
811+
!$this->isLocked($_SESSION['USERDATA']['id']) &&
812+
$this->getUserIp($_SESSION['USERDATA']['id']) == $_SERVER['REMOTE_ADDR']
813+
) return true;
828814
// Catchall
829815
if ($logout == true) $this->logoutUser($_SERVER['REQUEST_URI']);
830816
return false;

public/include/config/security.inc.dist.php

+5-15
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,12 @@
22
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
33

44
/**
5-
* Strict Mode
6-
* Extra security options that can help protect against a few different types of attacks
7-
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#wiki-strict-mode
5+
* Misc
6+
* Extra security settings
7+
*
88
**/
9-
$config['strict'] = true;
10-
$config['strict__https_only'] = false;
11-
$config['strict__mysql_filter'] = true;
12-
$config['strict__verify_client'] = true;
13-
$config['strict__verify_client_ip'] = true;
14-
$config['strict__verify_client_useragent'] = true;
15-
$config['strict__verify_client_sessionid'] = true;
16-
$config['strict__verify_client_fails'] = 0;
17-
$config['strict__verify_server'] = false;
18-
$config['strict__bind_protocol'] = 'https';
19-
$config['strict__bind_host'] = '';
20-
$config['strict__bind_port'] = 443;
9+
$config['https_only'] = false;
10+
$config['mysql_filter'] = true;
2111

2212
/**
2313
* Memcache Rate Limiting

public/include/database.inc.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
33

44
// Instantiate class, we are using mysqlng
5-
if ($config['strict'] && $config['strict__mysql_filter']) {
5+
if ($config['mysql_filter']) {
66
$mysqli = new mysqli_strict($config['db']['host'], $config['db']['user'], $config['db']['pass'], $config['db']['name'], $config['db']['port']);
77
} else {
88
$mysqli = new mysqli($config['db']['host'], $config['db']['user'], $config['db']['pass'], $config['db']['name'], $config['db']['port']);

0 commit comments

Comments
 (0)