-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathindex.php
More file actions
293 lines (263 loc) · 11.6 KB
/
index.php
File metadata and controls
293 lines (263 loc) · 11.6 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
declare(strict_types=1);
$enabled = getenv('HASHTOPOLIS_APIV2_ENABLE');
if (!$enabled || $enabled == 'false') {
die("APIv2 is not enabled, it can be enabled via environment variable!");
}
date_default_timezone_set("UTC");
error_reporting(E_ALL ^ E_DEPRECATED);
/**
* Treat warnings as error, very useful during unit testing.
* TODO: How-ever during Xdebug debugging under VS Code, this is very
* TODO: slightly annoying since the last call stack is not very interesting.
* TODO: Thus for the time-being do not-enable by default.
*/
// set_error_handler(function ($severity, $message, $file, $line) {
// if (error_reporting() & $severity) {
// throw new \ErrorException($message, 0, $severity, $file, $line);
// }
// });
use Hashtopolis\inc\apiv2\auth\HashtopolisAuthenticator;
use Hashtopolis\inc\apiv2\auth\JWTBeforeHandler;
use Hashtopolis\inc\apiv2\common\ClassMapper;
use Hashtopolis\inc\apiv2\error\ErrorHandler;
use Hashtopolis\inc\apiv2\util\CorsHackMiddleware;
use Hashtopolis\inc\apiv2\util\JsonBodyParserMiddleware;
use Hashtopolis\inc\apiv2\util\TokenAsParameterMiddleware;
use Hashtopolis\inc\apiv2\helper\AbortChunkHelperAPI;
use Hashtopolis\inc\apiv2\helper\AssignAgentHelperAPI;
use Hashtopolis\inc\apiv2\helper\BulkSupertaskBuilderHelperAPI;
use Hashtopolis\inc\apiv2\helper\ChangeOwnPasswordHelperAPI;
use Hashtopolis\inc\apiv2\helper\CreateSuperHashlistHelperAPI;
use Hashtopolis\inc\apiv2\helper\CreateSupertaskHelperAPI;
use Hashtopolis\inc\apiv2\helper\CurrentUserHelperAPI;
use Hashtopolis\inc\apiv2\helper\ExportCrackedHashesHelperAPI;
use Hashtopolis\inc\apiv2\helper\ExportLeftHashesHelperAPI;
use Hashtopolis\inc\apiv2\helper\ExportWordlistHelperAPI;
use Hashtopolis\inc\apiv2\helper\GetAccessGroupsHelperAPI;
use Hashtopolis\inc\apiv2\helper\GetAgentBinaryHelperAPI;
use Hashtopolis\inc\apiv2\helper\GetCracksOfTaskHelper;
use Hashtopolis\inc\apiv2\helper\GetBestTasksAgent;
use Hashtopolis\inc\apiv2\helper\GetFileHelperAPI;
use Hashtopolis\inc\apiv2\helper\GetTaskProgressImageHelperAPI;
use Hashtopolis\inc\apiv2\helper\GetUserPermissionHelperAPI;
use Hashtopolis\inc\apiv2\helper\ImportCrackedHashesHelperAPI;
use Hashtopolis\inc\apiv2\helper\ImportFileHelperAPI;
use Hashtopolis\inc\apiv2\helper\MaskSupertaskBuilderHelperAPI;
use Hashtopolis\inc\apiv2\helper\PurgeTaskHelperAPI;
use Hashtopolis\inc\apiv2\helper\RebuildChunkCacheHelperAPI;
use Hashtopolis\inc\apiv2\helper\RecountFileLinesHelperAPI;
use Hashtopolis\inc\apiv2\helper\RescanGlobalFilesHelperAPI;
use Hashtopolis\inc\apiv2\helper\ResetChunkHelperAPI;
use Hashtopolis\inc\apiv2\helper\ResetUserPasswordHelperAPI;
use Hashtopolis\inc\apiv2\helper\SearchHashesHelperAPI;
use Hashtopolis\inc\apiv2\helper\SetUserPasswordHelperAPI;
use Hashtopolis\inc\apiv2\helper\UnassignAgentHelperAPI;
use Hashtopolis\inc\apiv2\model\AccessGroupAPI;
use Hashtopolis\inc\apiv2\model\AgentAPI;
use Hashtopolis\inc\apiv2\model\AgentAssignmentAPI;
use Hashtopolis\inc\apiv2\model\AgentBinaryAPI;
use Hashtopolis\inc\apiv2\model\AgentErrorAPI;
use Hashtopolis\inc\apiv2\model\AgentStatAPI;
use Hashtopolis\inc\apiv2\model\ApiTokenAPI;
use Hashtopolis\inc\apiv2\model\ChunkAPI;
use Hashtopolis\inc\apiv2\model\ConfigAPI;
use Hashtopolis\inc\apiv2\model\ConfigSectionAPI;
use Hashtopolis\inc\apiv2\model\CrackerBinaryAPI;
use Hashtopolis\inc\apiv2\model\CrackerBinaryTypeAPI;
use Hashtopolis\inc\apiv2\model\FileAPI;
use Hashtopolis\inc\apiv2\model\GlobalPermissionGroupAPI;
use Hashtopolis\inc\apiv2\model\HashAPI;
use Hashtopolis\inc\apiv2\model\HashlistAPI;
use Hashtopolis\inc\apiv2\model\HashTypeAPI;
use Hashtopolis\inc\apiv2\model\HealthCheckAgentAPI;
use Hashtopolis\inc\apiv2\model\HealthCheckAPI;
use Hashtopolis\inc\apiv2\model\LogEntryAPI;
use Hashtopolis\inc\apiv2\model\NotificationSettingAPI;
use Hashtopolis\inc\apiv2\model\PreprocessorAPI;
use Hashtopolis\inc\apiv2\model\PreTaskAPI;
use Hashtopolis\inc\apiv2\model\SpeedAPI;
use Hashtopolis\inc\apiv2\model\SupertaskAPI;
use Hashtopolis\inc\apiv2\model\TaskAPI;
use Hashtopolis\inc\apiv2\model\TaskWrapperAPI;
use Hashtopolis\inc\apiv2\model\UserAPI;
use Hashtopolis\inc\apiv2\model\VoucherAPI;
use DI\Container;
use Hashtopolis\inc\StartupConfig;
use Psr\Container\ContainerInterface;
use Slim\Factory\AppFactory;
use Slim\Middleware\ContentLengthMiddleware;
use Slim\Exception\HttpMethodNotAllowedException;
use Tuupola\Middleware\HttpBasicAuthentication;
use JimTools\JwtAuth\Decoder\FirebaseDecoder;
use JimTools\JwtAuth\Middleware\JwtAuthentication;
use JimTools\JwtAuth\Options;
use JimTools\JwtAuth\Secret;
use JimTools\JwtAuth\Exceptions\AuthorizationException;
use Middlewares\DeflateEncoder;
use Psr\Http\Message\ServerRequestInterface as Request;
use JimTools\JwtAuth\Rules\RequestMethodRule;
use JimTools\JwtAuth\Rules\RequestPathRule;
require_once(__DIR__ . "/../../../vendor/autoload.php");
require_once(__DIR__ . "/../../inc/startup/include.php");
/* Construct container for middleware */
$container = new Container();
AppFactory::setContainer($container);
$container->set("HttpBasicAuthentication", function (ContainerInterface $container) {
return new HttpBasicAuthentication([
"path" => "/api/v2/auth/token",
"secure" => false,
"error" => function ($response, $arguments) {
return ErrorHandler::errorResponse($response, $arguments["message"], 401);
},
"authenticator" => new HashtopolisAuthenticator,
"before" => function ($request, $arguments) {
return $request->withAttribute("user", $arguments["user"]);
}
]);
});
$container->set("classMapper", function () {
return new ClassMapper();
});
/* API token validation */
$container->set("JwtAuthentication", function (ContainerInterface $container) {
$decoder = new FirebaseDecoder(
new Secret(StartupConfig::getInstance()->getPepper(0), 'HS256', hash("sha256", StartupConfig::getInstance()->getPepper(0)))
);
$options = new Options(
isSecure: false,
attribute: null,
before: new JWTBeforeHandler
);
$rules = [
new RequestPathRule(ignore: ["/api/v2/auth/token", "/api/v2/auth/oauth-token", "/api/v2/helper/resetUserPassword", "/api/v2/openapi.json"]),
new RequestMethodRule(ignore: ["OPTIONS"])
];
return new JwtAuthentication($options, $decoder, $rules);
});
/*
* SLIM framework middleware requires specific order to ensure middleware layers are executed in correct order.
* Also see https://www.slimframework.com/docs/v4/concepts/middleware.html for details.
*
* When you run the Slim application, the Request object traverses the middleware structure from the outside in.
* They first enter the outer-most middleware, then the next outer-most middleware, (and so on), until they ultimately
* arrive at the Slim application itself. After the Slim application dispatches the appropriate route, the resultant Response
* object exits the Slim application and traverses the middleware structure from the inside out. Ultimately, a final Response
* object exits the outer-most middleware, is serialized into a raw HTTP response, and is returned to the HTTP client.
*/
$app = AppFactory::create();
$app->addBodyParsingMiddleware();
$app->add(new JsonBodyParserMiddleware());
$app->add("HttpBasicAuthentication");
$app->add("JwtAuthentication");
$app->add(new TokenAsParameterMiddleware());
$app->add((new DeflateEncoder())->contentType(
'/^(image\/svg\\+xml|text\/.*|application\/json|"application\/vnd\.api+json)(;.*)?$/'
)
);
$app->add(new ContentLengthMiddleware()); // NOTE: Add any middleware which may modify the response body before adding the ContentLengthMiddleware
$app->add(new CorsHackMiddleware()); // NOTE: The RoutingMiddleware should be added after our CORS middleware so routing is performed first
// NOTE: The ErrorMiddleware should be added after any middleware which may modify the response body
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
$errorHandler = $errorMiddleware->getDefaultErrorHandler();
$errorHandler->forceContentType('application/json');
$customErrorHandler = function (
Request $request,
Throwable $exception,
bool $displayErrorDetails,
bool $logErrors,
bool $logErrorDetails) use ($app) {
$response = $app->getResponseFactory()->createResponse();
$response = CorsHackMiddleware::addCORSheaders($request, $response);
//Quirk to handle HTExceptions without status code, this can be removed when all HTExceptions have been migrated
error_log($exception->getMessage());
$code = $exception->getCode();
if ($code == 0 || $code == 1 || !is_integer($code)) {
$code = 500;
}
$msg = $exception->getMessage();
if ($exception instanceof AuthorizationException && empty($msg)) {
//the JWT authorization exceptions are wrapped in an outer exception
$previous = $exception->getPrevious();
if ($previous !== null) {
$code = 400;
$msg = $previous->getMessage();
}
}
return ErrorHandler::errorResponse($response, $msg, $code);
};
$errorMiddleware->setDefaultErrorHandler($customErrorHandler);
$app->addRoutingMiddleware(); //Routing middleware has to be added after the default error handler
$errorMiddlewareMethodNotAllowed = $app->addErrorMiddleware(true, true, true);
$errorMiddlewareMethodNotAllowed->setErrorHandler(HttpMethodNotAllowedException::class, function (
Request $request,
Throwable $exception,
bool $displayErrorDetails,
bool $logErrors,
bool $logErrorDetails) use ($app) {
$response = $app->getResponseFactory()->createResponse();
return ErrorHandler::errorResponse($response, $exception->getMessage(), 405);
});
include(__DIR__ . "/../../inc/apiv2/common/openAPISchema.routes.php");
include(__DIR__ . "/../../inc/apiv2/auth/token.routes.php");
// register model APIs
AccessGroupAPI::register($app);
AgentAPI::register($app);
AgentAssignmentAPI::register($app);
AgentBinaryAPI::register($app);
AgentErrorAPI::register($app);
AgentStatAPI::register($app);
ApiTokenAPI::register($app);
ChunkAPI::register($app);
ConfigAPI::register($app);
ConfigSectionAPI::register($app);
CrackerBinaryAPI::register($app);
CrackerBinaryTypeAPI::register($app);
FileAPI::register($app);
GlobalPermissionGroupAPI::register($app);
HashAPI::register($app);
HashlistAPI::register($app);
HashTypeAPI::register($app);
HealthCheckAgentAPI::register($app);
HealthCheckAPI::register($app);
LogEntryAPI::register($app);
NotificationSettingAPI::register($app);
PreprocessorAPI::register($app);
PreTaskAPI::register($app);
SpeedAPI::register($app);
SupertaskAPI::register($app);
TaskAPI::register($app);
TaskWrapperAPI::register($app);
UserAPI::register($app);
VoucherAPI::register($app);
// register helpers
AbortChunkHelperAPI::register($app);
AssignAgentHelperAPI::register($app);
BulkSupertaskBuilderHelperAPI::register($app);
ChangeOwnPasswordHelperAPI::register($app);
CreateSuperHashlistHelperAPI::register($app);
CreateSupertaskHelperAPI::register($app);
CurrentUserHelperAPI::register($app);
ExportCrackedHashesHelperAPI::register($app);
ExportLeftHashesHelperAPI::register($app);
ExportWordlistHelperAPI::register($app);
GetAccessGroupsHelperAPI::register($app);
GetAgentBinaryHelperAPI::register($app);
GetBestTasksAgent::register($app);
GetCracksOfTaskHelper::register($app);
GetFileHelperAPI::register($app);
GetTaskProgressImageHelperAPI::register($app);
GetUserPermissionHelperAPI::register($app);
ImportCrackedHashesHelperAPI::register($app);
ImportFileHelperAPI::register($app);
MaskSupertaskBuilderHelperAPI::register($app);
PurgeTaskHelperAPI::register($app);
RebuildChunkCacheHelperAPI::register($app);
RecountFileLinesHelperAPI::register($app);
RescanGlobalFilesHelperAPI::register($app);
ResetChunkHelperAPI::register($app);
ResetUserPasswordHelperAPI::register($app);
SearchHashesHelperAPI::register($app);
SetUserPasswordHelperAPI::register($app);
UnassignAgentHelperAPI::register($app);
$app->run();