-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_csrf.php
More file actions
executable file
·270 lines (237 loc) · 7.38 KB
/
write_csrf.php
File metadata and controls
executable file
·270 lines (237 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{"docu": {"Type": "snippet",
"Short Purpose": "PHP File Header","Version": "2022.02.05",
"Old Versions": "2019.07.11,2019.08.23"}}
*/
/*
* make accessible from anywhere with CORS
* //todo API only
header('Access-Control-Allow-Origin:*');
/*
{"docu": {"Type": "snippet",
"Short Purpose": "Scheduled Jobs Ignore Abort", "Version": "2019.06.29",
"Old Versions": ""}}
*/
/*
* if user is not waiting for a return then continue running even if user disconnects
* and increase mex execution time to 60 minutes
*
ob_end_clean();
header('Connection: close\r\n');
header('Content-Encoding: none\r\n');
header('Content-Length: 1');
ignore_user_abort(true);
ini_set('max_execution_time', 3600);
/*
* Check if file was called directly and error out because file should never be called directly
*
* //todo class files only
*
if (realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])){
echo 'Something went terribly wrong. :(';
die;
}
/*
* get file head
*/
if (
(!file_exists('file_head.inc.php'))
||
(!include_once('file_head.inc.php'))
){
$tmp = basename(__FILE__) . ':' . __LINE__;
error_log("[ LAKEBED ][ {$tmp} ] Failed to include file_head.inc.php.");
echo 'Something went terribly wrong. :(';
die;
}
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* SETUP CLASSES CODE BLOCK
* Everything below this point is setting up classes used in this file
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/*
* Classes used in this file
*/
if (
(!$_SERVER['class']['sanitize'] = new sanitize)
||
(!$_SERVER['class']['microservices'] = new microservices)
||
(!$_SERVER['class']['csrf'] = new csrf_new)
){
$tmp = basename(__FILE__) . ':' . __LINE__;
error_log("[ LAKEBED ][ {$tmp} ] Failed to init classes.");
echo 'Something went terribly wrong. :(';
die;
}
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* INIT LOG CODE BLOCK
* Ensure log security token is init'd for today so we can write to log after this point
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
//todo change everything in file_head.inc.php and base.inc.php to error_log()
if (!file_exists("{$_SERVER['class']['constants']->docroot}/API/microservices/log/{$_SERVER['class']['constants']->day['today']}.security.php")){
$_SERVER['class']['microservices']->call(array(
'url' => "{$_SERVER['class']['constants']->server_url}/API/microservices/log/add.php",
'security_token' => '',
'await_response' => 0,
));
}
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* CSRF/TOKEN CODE BLOCK
* Everything below this point is checking CSRF/API token
* No point going further if token is not provided or not valid
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/**
* check simple security token just to reduce robots
*/
if (
(empty($_GET['simple_security_token']))
||
($_GET['simple_security_token'] != gmdate('ymdH'))
){
$_SERVER['class']['microservices']->call(array(
'url' => "{$_SERVER['class']['constants']->server_url}/API/microservices/log/add.php",
'security_token' => md5(file_get_contents("{$_SERVER['class']['constants']->docroot}/API/microservices/log/{$_SERVER['class']['constants']->day['today']}.security.php")),
'message' => 'Something went terribly wrong.',
'admin_only_message' => 'Simple security token is not set or not valid.',
'await_response' => 0,
));
header('Status: 404 Not Found', false);
header("Refresh:2;url=https://lakebed.io", false);
echo $_SERVER['class']['microservices']->last_request['message'];
die;
}
/**
* confirm referer matches
* //note we do this first b/c there's no point going further if HTTP_REF doesn't match
* //note no point waiting for log response since we have to error out anyway
*/
if (
(empty($_SERVER['HTTP_REFERER']))
||
(!$tmp = str_replace('://', '/', $_SERVER['class']['constants']->server_url))
||
(!$tmp = explode('/', $tmp))
||
(!count($tmp))
||
(empty($tmp[1]))
||
(strpos($_SERVER['HTTP_REFERER'], $tmp[1]) === false)
){
$_SERVER['class']['microservices']->call(array(
'url' => "{$_SERVER['class']['constants']->server_url}/API/microservices/log/add.php",
'security_token' => md5(file_get_contents("{$_SERVER['class']['constants']->docroot}/API/microservices/log/{$_SERVER['class']['constants']->day['today']}.security.php")),
'message' => 'Something went terribly wrong.',
'admin_only_message' => 'HTTP Referer does not match.',
'await_response' => 0,
));
header('Status: 404 Not Found', false);
header("Refresh:2;url=https://lakebed.io", false);
echo $_SERVER['class']['microservices']->last_request['message'];
die;
}
/**
* check simple security token
*/
//leftoff handle this after we setup redirect
/**
* set CSRF browser flag
* generate a new token if none available
* and renew if valid and expired
*/
$_SERVER['class']['csrf']->browser_flag=1;
if (!$_SERVER['class']['csrf']->check_if_valid()){
$new_token = $_SERVER['class']['csrf']->new_hash();
$_SERVER['class']['csrf']->write_and_save($new_token);
}
$_SERVER['class']['csrf']->renew_if_expired();
/**
* get/set next_url
* //todo switch everything to _SESSION[next_url]
* //future if JWT valid set next_url to /account
*/
$next_url = 'login/';
if (!empty($_SESSION['next_url'])){
$next_url = $_SESSION['next_url'];
}
/*
{"docu": {"Type": "ChangeLog",
"2021-10-20 12:14:49": {
"Time": "2021-10-20 12:14:49",
"Type": "Added",
"Components": "write_csrf",
"Description": "added HTTPS check since cookies are assumed to be secure if over https.",
"Made By": "slepine"
}
}}
*/
if (stripos($_SERVER['class']['constants']->server_url, 'https://') === false){
header("Set-Cookie:PHPSESSID={$_COOKIE['PHPSESSID']};{$_SERVER['class']['constants']->cookie}", false);
}
/*
* header redirect in case JS failed
*/
if (strpos($next_url, $_SERVER['class']['constants']->server_url) === false){
$next_url = "{$_SERVER['class']['constants']->server_url}/{$next_url}";
}
header("Refresh:2;url={$next_url}", false);
/*
* init _COOKIE if not
*/
if (
(!isset($_COOKIE['csrf']))
||
(!is_string($_COOKIE['csrf']))
){
$_COOKIE['csrf']='';
}
/*
* handle hide_text
*/
$text_html = '<p>Getting your security ready</p>';
if (
(isset($_GET['hide_text']))
&&
(strlen($_GET['hide_text']))
&&
($_GET['hide_text'])
){
$text_html = '';
}
/*
* output iframe check
*/
echo <<<m_echo
<html>
<script id='{$_COOKIE['csrf']}' >
if (
(typeof window.self != 'object')
||
(typeof window.top != 'object')
||
(window.self !== window.top)
||
(window.frameElement !== null)
){
alert('This site is loaded in an iFrame. This is likely a hacking attempt!!!');
window.location.replace('{$_SERVER['class']['constants']->server_url}/login/logout.php');
}
if (
(!'{$_COOKIE['csrf']}')
||
('{$_COOKIE['csrf']}' == '0')
){
window.location.reload();
}
</script>
{$text_html}
</html>
m_echo;