|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
if (!defined('BASEPATH')) { |
|
4
|
|
|
exit('No direct script access allowed'); |
|
5
|
|
|
} |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Image CMS |
|
9
|
|
|
* CSRF Library Beta. |
|
10
|
|
|
*/ |
|
11
|
|
|
class Lib_csrf |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
public $ci = NULL; |
|
15
|
|
|
|
|
16
|
|
|
private $enc_key = ''; |
|
17
|
|
|
|
|
18
|
|
|
private $tokens = []; // User token. |
|
19
|
|
|
|
|
20
|
|
|
private $sess_id = NULL; // Session id. |
|
21
|
|
|
|
|
22
|
|
|
private $hidden_name = 'cms_token'; |
|
23
|
|
|
|
|
24
|
|
|
private $max_tokens = 10; |
|
25
|
|
|
|
|
26
|
|
|
public $log_errors = FALSE; |
|
27
|
|
|
|
|
28
|
|
|
public $log_ajax_requests = FALSE; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct() { |
|
31
|
|
|
$this->ci = &get_instance(); |
|
32
|
|
|
|
|
33
|
|
|
$this->_generate_token(); |
|
34
|
|
|
|
|
35
|
|
|
if ($this->check_token() === FALSE) { |
|
36
|
|
|
if ($this->log_errors === TRUE) { |
|
37
|
|
|
$this->_write_message('Wrong code.'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$err_text = 'Подозрение на атаку Cross-Site Request Forgery.'; |
|
41
|
|
|
show_error($err_text); |
|
42
|
|
|
die(); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function addDisabledCsrfUrls() { |
|
47
|
|
|
|
|
48
|
|
|
// Diable CSRF library form web money service |
|
49
|
|
|
$ci = $this->ci; |
|
50
|
|
View Code Duplication |
if ($ci->uri->segment(1) == 'shop' && $ci->uri->segment(2) == 'cart' && $ci->uri->segment(3) == 'view' && $ci->input->get('result') == 'true' && $ci->input->get('pm') > 0) { |
|
51
|
|
|
define('ICMS_DISBALE_CSRF', true); |
|
52
|
|
|
} |
|
53
|
|
|
// Support for robokassa |
|
54
|
|
View Code Duplication |
if ($ci->uri->segment(1) == 'shop' && $ci->uri->segment(2) == 'cart' && $ci->uri->segment(3) == 'view' && $ci->input->get('getResult') == 'true') { |
|
55
|
|
|
define('ICMS_DISBALE_CSRF', true); |
|
56
|
|
|
} |
|
57
|
|
|
if ($ci->uri->segment(1) == 'exchange') { |
|
58
|
|
|
define('ICMS_DISBALE_CSRF', true); |
|
59
|
|
|
} |
|
60
|
|
|
// Support for privat |
|
61
|
|
|
if ($ci->uri->segment(1) == 'shop' && $ci->uri->segment(2) == 'order' && $ci->uri->segment(3) == 'view' && $ci->input->post()) { |
|
62
|
|
|
define('ICMS_DISBALE_CSRF', true); |
|
63
|
|
|
} |
|
64
|
|
View Code Duplication |
if ($ci->uri->segment(1) == 'shop' && $ci->uri->segment(2) == 'cart' && $ci->uri->segment(3) == 'view' && $ci->input->get('succes') == 'true') { |
|
65
|
|
|
define('ICMS_DISBALE_CSRF', true); |
|
66
|
|
|
} |
|
67
|
|
View Code Duplication |
if ($ci->uri->segment(1) == 'shop' && $ci->uri->segment(2) == 'cart' && $ci->uri->segment(3) == 'view' && $ci->input->get('fail') == 'true') { |
|
68
|
|
|
define('ICMS_DISBALE_CSRF', true); |
|
69
|
|
|
} |
|
70
|
|
View Code Duplication |
if ($ci->input->server('HTTP_REFERER') AND strpos($ci->input->server('HTTP_REFERER') . '', 'facebook.com')) { |
|
71
|
|
|
define('ICMS_DISBALE_CSRF', true); |
|
72
|
|
|
} |
|
73
|
|
View Code Duplication |
if ($ci->input->server('HTTP_REFERER') AND strpos($ci->input->server('HTTP_REFERER') . '', 'facebook.com')) { |
|
74
|
|
|
define('ICMS_DISBALE_CSRF', true); |
|
75
|
|
|
} |
|
76
|
|
|
// Support for privat |
|
77
|
|
|
|
|
78
|
|
|
if ($ci->uri->segment(1) == 'shop' && $ci->uri->segment(2) == 'order' && $ci->uri->segment(3) == 'view') { |
|
79
|
|
|
define('ICMS_DISBALE_CSRF', true); |
|
80
|
|
|
} |
|
81
|
|
|
//new payment system |
|
82
|
|
|
if (preg_match('/payment_method_/i', $ci->uri->segment(1)) || preg_match('/payment_method_/i', $ci->uri->segment(2))) { |
|
83
|
|
|
define('ICMS_DISBALE_CSRF', true); |
|
84
|
|
|
} |
|
85
|
|
|
if ($ci->uri->segment(1) == 'facebook_store' && $ci->uri->segment(2) == 'auth_from_fb_store') { |
|
86
|
|
|
define('ICMS_DISBALE_CSRF', true); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($ci->uri->segment(4) == 'xbanners') { |
|
90
|
|
|
define('ICMS_DISBALE_CSRF', true); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function check_token() { |
|
95
|
|
|
$this->addDisabledCsrfUrls(); |
|
96
|
|
|
if (count($_POST) > 0) { |
|
97
|
|
|
if (defined('ICMS_DISBALE_CSRF') AND ICMS_DISBALE_CSRF === TRUE) { |
|
98
|
|
|
return TRUE; |
|
99
|
|
|
} |
|
100
|
|
|
// Don't check ajax requests |
|
101
|
|
|
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) { |
|
102
|
|
|
if (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { |
|
103
|
|
|
if ($this->log_ajax_requests === TRUE) { |
|
104
|
|
|
$this->_write_message('Ajax Request'); |
|
105
|
|
|
} |
|
106
|
|
|
return TRUE; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$post_token = $this->ci->input->post($this->hidden_name); |
|
111
|
|
|
|
|
112
|
|
|
if (array_search($post_token, $this->tokens) == FALSE) { |
|
113
|
|
|
if ($this->tokens[0] != $post_token) { |
|
114
|
|
|
return FALSE; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return TRUE; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Create input hidden |
|
124
|
|
|
*/ |
|
125
|
|
|
public function create_hidden_html() { |
|
126
|
|
|
return '<input type="hidden" value="' . $this->get_token() . '" name="' . $this->hidden_name . '" />'; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string $text |
|
131
|
|
|
*/ |
|
132
|
|
|
private function _write_message($text) { |
|
133
|
|
|
$this->ci->load->helper('file'); |
|
134
|
|
|
|
|
135
|
|
|
$post_data = '<br/>Post data:<br/>'; |
|
136
|
|
|
foreach ($_POST as $k => $v) { |
|
137
|
|
|
$post_data .= $k . ': ' . $v . '<br/>'; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$request_uri = 'Request uri: ' . $_SERVER['REQUEST_URI'] . '<br/><br/>'; |
|
141
|
|
|
|
|
142
|
|
|
$new_text = '<p>' . date('d-m-Y H:i:s') . ' IP:' . $_SERVER['REMOTE_ADDR'] . ' Referer: ' . $_SERVER['HTTP_REFERER'] . '<br/>' . $request_uri . $text . $post_data . '<br/>________________</p>'; |
|
143
|
|
|
|
|
144
|
|
|
@write_file('./application/logs/csrf.html', $new_text, 'a'); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
private function _generate_token() { |
|
148
|
|
|
$this->sess_id = $this->_get_sess_id(); |
|
149
|
|
|
$n_token = md5($this->sess_id . $this->enc_key); |
|
150
|
|
|
|
|
151
|
|
|
$this->tokens = $this->ci->session->userdata('ci_tokens'); |
|
152
|
|
|
|
|
153
|
|
|
if (is_array($this->tokens) AND count($this->tokens) > $this->max_tokens) { |
|
154
|
|
|
$this->tokens = array_slice($this->tokens, -3, 3); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
if (is_array($this->tokens)) { |
|
158
|
|
|
if (array_search($n_token, $this->tokens) === FALSE) { |
|
159
|
|
|
$this->tokens[] = $n_token; |
|
160
|
|
|
} |
|
161
|
|
|
} else { |
|
162
|
|
|
$this->tokens = []; |
|
163
|
|
|
$this->tokens[] = $n_token; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$this->ci->session->set_userdata('ci_tokens', $this->tokens); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function get_token() { |
|
170
|
|
|
if (count($this->tokens) == 0) { |
|
171
|
|
|
$this->_generate_token(); |
|
172
|
|
|
} |
|
173
|
|
|
return array_pop($this->tokens); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
private function _get_sess_id() { |
|
177
|
|
|
return $this->ci->session->userdata('session_id'); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/* End of file lib_csfr.php */ |
If you suppress an error, we recommend checking for the error condition explicitly: