1
|
|
|
<?php |
2
|
|
|
defined('ROOT_PATH') or exit('Access denied'); |
3
|
|
|
/** |
4
|
|
|
* TNH Framework |
5
|
|
|
* |
6
|
|
|
* A simple PHP framework using HMVC architecture |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2017 TNH Framework |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
class Security extends BaseClass { |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Construct new instance |
35
|
|
|
*/ |
36
|
|
|
public function __construct() { |
37
|
|
|
parent::__construct(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* This method is used to generate the CSRF token |
42
|
|
|
* @return string the generated CSRF token |
43
|
|
|
*/ |
44
|
|
|
public function generateCSRF() { |
45
|
|
|
$this->logger->debug('Generation of CSRF ...'); |
46
|
|
|
$key = get_config('csrf_key', 'csrf_key'); |
47
|
|
|
$expire = get_config('csrf_expire', 60); |
48
|
|
|
$keyExpire = 'csrf_expire'; |
49
|
|
|
$currentTime = time(); |
50
|
|
|
$sessionInstance = get_instance()->session; |
51
|
|
|
if ( |
52
|
|
|
$sessionInstance->exists($key) |
53
|
|
|
&& $sessionInstance->exists($keyExpire) |
54
|
|
|
&& $sessionInstance->get($keyExpire) > $currentTime |
55
|
|
|
) { |
56
|
|
|
$this->logger->info('The CSRF token not yet expire just return it'); |
57
|
|
|
return $sessionInstance->get($key); |
58
|
|
|
} else { |
59
|
|
|
$newTime = $currentTime + $expire; |
60
|
|
|
$token = sha1(uniqid()) . sha1(uniqid()); |
61
|
|
|
$this->logger->info('The CSRF informations are listed below: ' |
62
|
|
|
. 'key [' . $key . '], key expire [' . $keyExpire . '], ' |
63
|
|
|
. 'expire time [' . $expire . '], token [' . $token . ']'); |
64
|
|
|
$sessionInstance->set($keyExpire, $newTime); |
65
|
|
|
$sessionInstance->set($key, $token); |
66
|
|
|
return $sessionInstance->get($key); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* This method is used to check the CSRF if is valid, not yet expire, etc. |
72
|
|
|
* @return boolean true if valid, false if not valid |
73
|
|
|
*/ |
74
|
|
|
public function validateCSRF() { |
75
|
|
|
$this->logger->debug('Validation of CSRF ...'); |
76
|
|
|
$key = get_config('csrf_key', 'csrf_key'); |
77
|
|
|
$expire = get_config('csrf_expire', 60); |
78
|
|
|
$keyExpire = 'csrf_expire'; |
79
|
|
|
$currentTime = time(); |
80
|
|
|
$sessionInstance = get_instance()->session; |
81
|
|
|
$this->logger->info('The CSRF informations are listed below: key [' . $key . '], key expire [' . $keyExpire . '], expire time [' . $expire . ']'); |
82
|
|
|
if (!$sessionInstance->exists($key) || $sessionInstance->get($keyExpire) <= $currentTime) { |
83
|
|
|
$this->logger->warning('The CSRF session data is not valide'); |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
//perform form data |
87
|
|
|
$token = get_instance()->request->post($key); |
88
|
|
|
if ($token !== $sessionInstance->get($key) || $sessionInstance->get($keyExpire) <= $currentTime) { |
89
|
|
|
$this->logger->warning('The CSRF data [' . $token . '] is not valide may be attacker do his job'); |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
$this->logger->info('The CSRF data [' . $token . '] is valide the form data is safe continue'); |
93
|
|
|
//remove the token from session and data |
94
|
|
|
$sessionInstance->clear($key); |
95
|
|
|
$sessionInstance->clear($keyExpire); |
96
|
|
|
get_instance()->globalvar->removePost($key); |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* This method is used to check the whitelist IP address access |
102
|
|
|
* |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
|
|
public function checkWhiteListIpAccess() { |
106
|
|
|
$this->logger->debug('Validation of the IP address access ...'); |
107
|
|
|
$this->logger->debug('Check if whitelist IP access is enabled in the configuration ...'); |
108
|
|
|
$isEnable = get_config('white_list_ip_enable', false); |
109
|
|
|
if (!$isEnable) { |
110
|
|
|
$this->logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
$this->logger->info('Whitelist IP access is enabled in the configuration'); |
114
|
|
|
$list = get_config('white_list_ip_addresses', array()); |
115
|
|
|
if (empty($list)) { |
116
|
|
|
$this->logger->info('The list of whitelist IP is empty, ignore checking'); |
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
//Can't use Loader::functions() at this time because teh "Loader" library is loader after the security prossessing |
120
|
|
|
require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
121
|
|
|
$ip = get_ip(); |
122
|
|
|
if ((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)) { |
123
|
|
|
$this->logger->info('IP address ' . $ip . ' is allowed using the wildcard "*" or the full IP address'); |
124
|
|
|
//wildcard to access all ip address |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
// go through all whitelisted ips |
128
|
|
|
foreach ($list as $ipaddr) { |
129
|
|
|
// find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
130
|
|
|
$wildcardPosition = strpos($ipaddr, '*'); |
131
|
|
|
if ($wildcardPosition === false) { |
132
|
|
|
// no wild card in whitelisted ip --continue searching |
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
// cut ip at the position where we got the wild card on the whitelisted ip |
136
|
|
|
// and add the wold card to get the same pattern |
137
|
|
|
if (substr($ip, 0, $wildcardPosition) . '*' === $ipaddr) { |
138
|
|
|
// f.e. we got |
139
|
|
|
// ip "127.0.0.1" |
140
|
|
|
// whitelisted ip "127.0.*" |
141
|
|
|
// then we compared "127.0.*" with "127.0.*" |
142
|
|
|
// return success |
143
|
|
|
$this->logger->info('IP address ' . $ip . ' is allowed using the wildcard address like "x.x.x.*"'); |
144
|
|
|
return true; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
$this->logger->warning('IP address ' . $ip . ' is not allowed to access to this application'); |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|