-
Notifications
You must be signed in to change notification settings - Fork 1
/
files.php
208 lines (161 loc) · 6.46 KB
/
files.php
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
<?php
// display all errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL - E_NOTICE);
require_once './config.php';
if (!$STORAGE_LOCATION) http_error(501, 'Developer forgot the $STORAGE_LOCATION');
// import useful functions
require_once './utils.php';
require_once './log.php';
$method = sec($_SERVER['REQUEST_METHOD']);
if ($method !== 'GET' && $method !== 'POST' && $method !== 'DELETE') {
http_error(400, "Incorrect request type, expected GET, POST or DELETE, not $method");
}
if ($method === 'POST') {
/**
* post is creation
* You need a token
* You need a path
* You need to be able to overwrite
*/
// add tokens
require_once './tokens.php';
if (!$db_tokens)
http_error(501, 'Developer is dumb and forgot to create tokens');
if (!$authorized_file_extension)
http_error(501, 'Developer is dumb and forgot to create $authorized_file_extension');
$token = p('token');
// verifying token
if ($token === false) http_error(400, 'No token provided');
if (!in_array($token, $db_tokens)) http_error(403, 'Invalid token');
$path = trim(p('path'));
if ($path === false) http_error(400, 'No path provided');
// check path lower than me
$relativePath = remove_dots($path);
$absolutePath = remove_dots($STORAGE_LOCATION . $relativePath);
$myPath = remove_dots($STORAGE_LOCATION);
// avoid hacks to write script or files unauthorized
if (strpos($absolutePath, $myPath) !== 0) http_error(403, 'Path not authorized');
// no php script allowed
if (str_ends_with($absolutePath, '.php') === 0) http_error(403, 'Cannot write PHP scripts');
$extensionFound = false;
$i = 0;
while ($i < count($authorized_file_extension) && !$extensionFound) {
$extensionFound = str_ends_with($absolutePath, $authorized_file_extension[$i]);
$i = $i + 1;
}
if (!$extensionFound) {
http_error(403, 'Extension not allowed');
}
if (!check($_FILES) || !check($_FILES['file'])) {
http_error(400, 'No file provided or the provided file did not contain an original name');
}
// overwrite parameter
$overwrite = !!p('overwrite');
if (!$overwrite && file_exists($absolutePath)) http_error(403, 'File already exists');
$uploadDir = dirname($absolutePath);
// Make sure you can write to this folder.
// php default user is www-data
// you can give rights to a folder with the following command
// sudo chown -R www-data "/path/to/folder/"
// mkdir(path, rw-r--r--, recursive=true)
if (!is_dir($uploadDir) && !mkdir($uploadDir, 0766, true)) {
http_error(500, "PHP script can't create folder " . $uploadDir . ". Check permission, group and owner.");
}
if (!check($_FILES) || !check($_FILES['file'])) http_error(400, 'No actual file was given');
$tmpName = $_FILES['file']['tmp_name'];
// eventually write the file
if (move_uploaded_file($tmpName, $absolutePath)) {
http_success("Written file successfully to $relativePath");
} else {
http_error(500, "PHP script can't write to file. Check permission, group and owner.");
}
die();
} else if ($method === 'GET') {
$path = trim(g('path'));
if ($path === false) http_error(400, 'No path provided');
// check path lower than me
$absolutePath = remove_dots($STORAGE_LOCATION . $path);
$myPath = remove_dots($STORAGE_LOCATION);
// avoid hacks to write script or files unauthorized
if (strpos($absolutePath, $myPath) !== 0) http_error(403, 'Path not authorized');
// no php script allowed
if (substr_compare($absolutePath, ".php", -strlen(".php"), null, true) === 0) http_error(403, 'Cannot read PHP scripts');
if (!file_exists($absolutePath)) http_error(404, 'File not found');
try {
// try to read the image
$imgInfo = getimagesize($absolutePath);
header("Content-type: {$imgInfo['mime']}");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($absolutePath));
ob_clean();
flush();
readfile($absolutePath);
die();
} catch (Throwable $th) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($absolutePath));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($absolutePath));
ob_clean();
flush();
readfile($absolutePath);
die();
}
} else if ($method === 'DELETE') {
/**
* delete is deletion
* You need a token
* You need a path
*/
// add tokens
require_once './tokens.php';
if (!$db_tokens)
http_error(501, 'Developer is dumb and forgot to create tokens');
$data = json_decode(file_get_contents('php://input'), true);
if ($data === false) http_error(400, 'Could not parse input data');
try {
$token = $data['token'];
} catch(Exception $e) {
http_error(400, 'Failed parsing input data');
}
// verifying token
if ($token === false) http_error(400, 'No token provided');
if (!in_array($token, $db_tokens)) http_error(403, 'Invalid token');
$path = trim($data['path']);
if ($path === false) http_error(400, 'No path provided');
// check path lower than me
$relativePath = remove_dots($path);
$absolutePath = remove_dots($STORAGE_LOCATION . $relativePath);
$myPath = remove_dots($STORAGE_LOCATION);
// avoid hacks to write script or files unauthorized
if (strpos($absolutePath, $myPath) !== 0) http_error(403, 'Path not authorized');
if (!file_exists($absolutePath)) http_error(404, 'File not found');
$is_deleted = unlink($absolutePath);
if ($is_deleted) http_success('File successfully deleted');
http_error(500, 'Deletion failed');
}
http_error(501, 'Request unhandled by developer');
function p($var) {
try {
if (!check($_POST[$var])) return false;
} catch (Throwable $th) {
return false;
}
return sec($_POST[$var]);
}
function g($var) {
try {
if (!check($_GET[$var])) return false;
} catch (Throwable $th) {
return false;
}
return sec($_GET[$var]);
}