-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathSupertaskUtils.class.php
More file actions
514 lines (472 loc) · 17.6 KB
/
SupertaskUtils.class.php
File metadata and controls
514 lines (472 loc) · 17.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php /** @noinspection PhpMissingBreakStatementInspection */
use DBA\CrackerBinaryType;
use DBA\Supertask;
use DBA\SupertaskPretask;
use DBA\QueryFilter;
use DBA\Pretask;
use DBA\JoinFilter;
use DBA\TaskWrapper;
use DBA\Task;
use DBA\OrderFilter;
use DBA\User;
use DBA\Factory;
use DBA\File;
use DBA\FilePretask;
class SupertaskUtils {
/**
* @param string $name
* @param string $command
* @param bool $isCpuOnly
* @param bool $isSmall
* @param int $crackerBinaryTypeId
* @param string $benchtype
* @param string[] $basefiles
* @param string[] $iterfiles
* @param User $user
* @throws HTException
*/
public static function bulkSupertask($name, $command, $isCpuOnly, $maxAgents, $isSmall, $crackerBinaryTypeId, $benchtype, $basefiles, $iterfiles, $user) {
$name = htmlentities($name, ENT_QUOTES, "UTF-8");
$isCpuOnly = ($isCpuOnly) ? 1 : 0;
$isSmall = ($isSmall) ? 1 : 0;
$benchtype = ($benchtype == 'speed') ? 1 : 0;
$crackerBinaryType = Factory::getCrackerBinaryTypeFactory()->get($crackerBinaryTypeId);
if ($crackerBinaryType == null) {
throw new HTException("Invalid cracker type ID!");
}
else if (strpos($command, SConfig::getInstance()->getVal(DConfig::HASHLIST_ALIAS)) === false) {
throw new HTException("Command line must contain hashlist alias (" . SConfig::getInstance()->getVal(DConfig::HASHLIST_ALIAS) . ")!");
}
else if (Util::containsBlacklistedChars($command)) {
throw new HTException("The command must contain no blacklisted characters!");
}
else if (!is_array($iterfiles) || sizeof($iterfiles) == 0) {
throw new HTException("At least one file needs to be selected to iterate over!");
}
else if (strpos($command, "FILE") === false) {
throw new HTException("No placeholder (FILE) for the iteration!");
}
if (!is_array($basefiles)) {
$basefiles = [];
}
$basefilesChecked = [];
foreach ($basefiles as $basefile) {
$file = Factory::getFileFactory()->get($basefile);
if ($file == null) {
throw new HTException("Invalid file selected!");
}
else if (!AccessUtils::userCanAccessFile($file, $user)) {
throw new HTException("For at least one file you don't have enough access rights!");
}
$basefilesChecked[] = $file;
}
$iterfilesChecked = [];
foreach ($iterfiles as $iterfile) {
$file = Factory::getFileFactory()->get($iterfile);
if ($file == null) {
throw new HTException("Invalid file selected!");
}
else if (!AccessUtils::userCanAccessFile($file, $user)) {
throw new HTException("For at least one file you don't have enough access rights!");
}
$iterfilesChecked[] = $file;
}
Factory::getAgentFactory()->getDB()->beginTransaction();
$pretasks = SupertaskUtils::createIterationPretasks($command, $name, $basefilesChecked, $iterfilesChecked, $isSmall, $maxAgents, $isCpuOnly, $crackerBinaryType, $benchtype);
$supertask = new Supertask(null, $name);
$supertask = Factory::getSupertaskFactory()->save($supertask);
foreach ($pretasks as $preTask) {
$relation = new SupertaskPretask(null, $supertask->getId(), $preTask->getId());
Factory::getSupertaskPretaskFactory()->save($relation);
}
Factory::getAgentFactory()->getDB()->commit();
}
/**
* @param string $command
* @param string $name
* @param File[] $basefiles
* @param File[] $iterfiles
* @param int $isSmall
* @param int $isCpuOnly
* @param CrackerBinaryType $crackerBinaryType
* @param int $benchtype
* @return Pretask[]
*/
public static function createIterationPretasks($command, $name, $basefiles, $iterfiles, $isSmall, $maxAgents, $isCpuOnly, $crackerBinaryType, $benchtype) {
// create the preconf tasks
$preTasks = array();
$priority = sizeof($iterfiles) + 1;
foreach ($iterfiles as $iterfile) {
$cmd = str_replace('FILE', $iterfile->getFilename(), $command);
$preTaskName = $name . " + " . $iterfile->getFilename();
$pretask = new Pretask(
null,
$preTaskName,
$cmd,
SConfig::getInstance()->getVal(DConfig::CHUNK_DURATION),
SConfig::getInstance()->getVal(DConfig::STATUS_TIMER),
"",
$isSmall,
$isCpuOnly,
$benchtype,
$priority,
$maxAgents,
0,
$crackerBinaryType->getId()
);
$pretask = Factory::getPretaskFactory()->save($pretask);
// save files
$pretaskFiles = [];
foreach ($basefiles as $basefile) {
$pretaskFiles[] = new FilePretask(null, $basefile->getId(), $pretask->getId());
}
$pretaskFiles[] = new FilePretask(null, $iterfile->getId(), $pretask->getId());
Factory::getFilePretaskFactory()->massSave($pretaskFiles);
$preTasks[] = $pretask;
$priority--;
}
return $preTasks;
}
/**
* @param int $supertaskId
* @param string $newName
* @throws HTException
*/
public static function renameSupertask($supertaskId, $newName) {
$supertask = SupertaskUtils::getSupertask($supertaskId);
Factory::getSupertaskFactory()->set($supertask, Supertask::SUPERTASK_NAME, htmlentities($newName, ENT_QUOTES, "UTF-8"));
}
/**
* @param int $supertaskId
* @throws HTException
*/
public static function deleteSupertask($supertaskId) {
$supertask = SupertaskUtils::getSupertask($supertaskId);
Factory::getAgentFactory()->getDB()->beginTransaction();
$qF = new QueryFilter(SupertaskPretask::SUPERTASK_ID, $supertask->getId(), "=", Factory::getSupertaskPretaskFactory());
$jF = new JoinFilter(Factory::getSupertaskPretaskFactory(), Pretask::PRETASK_ID, SupertaskPretask::PRETASK_ID);
$joinedTasks = Factory::getPretaskFactory()->filter([Factory::FILTER => $qF, Factory::JOIN => $jF]);
Factory::getSupertaskPretaskFactory()->massDeletion([Factory::FILTER => $qF]);
/** @var $pretasks Pretask[] */
$pretasks = $joinedTasks[Factory::getPretaskFactory()->getModelName()];
foreach ($pretasks as $pretask) {
if ($pretask->getIsMaskImport() == 1) {
Factory::getPretaskFactory()->delete($pretask);
}
}
Factory::getSupertaskFactory()->delete($supertask);
Factory::getAgentFactory()->getDB()->commit();
}
/**
* @param int $taskWrapperId
* @return Task[]
*/
public static function getRunningSubtasks($taskWrapperId) {
$qF = new QueryFilter(Task::TASK_WRAPPER_ID, $taskWrapperId, "=");
$oF = new OrderFilter(Task::PRIORITY, "DESC");
return Factory::getTaskFactory()->filter([Factory::FILTER => $qF, Factory::ORDER => $oF]);
}
/**
* @param int $taskWrapperId
* @param User $user
* @return TaskWrapper
* @throws HTException
*/
public static function getRunningSupertask($taskWrapperId, $user) {
$supertask = Factory::getTaskWrapperFactory()->get($taskWrapperId);
if ($supertask == null) {
throw new HTException("Invalid taskwrapper ID!");
}
else if (!AccessUtils::userCanAccessTask($supertask, $user)) {
throw new HTException("No access to this task!");
}
return $supertask;
}
/**
* @return Supertask[]
*/
public static function getAllSupertasks() {
$oF = new OrderFilter(Supertask::SUPERTASK_ID, "ASC");
return Factory::getSupertaskFactory()->filter([Factory::ORDER => $oF]);
}
/**
* @param int $supertaskId
* @return Pretask[]
*/
public static function getPretasksOfSupertask($supertaskId) {
$oF = new OrderFilter(Pretask::PRIORITY, "DESC", Factory::getPretaskFactory());
$qF = new QueryFilter(SupertaskPretask::SUPERTASK_ID, $supertaskId, "=");
$jF = new JoinFilter(Factory::getSupertaskPretaskFactory(), Pretask::PRETASK_ID, SupertaskPretask::PRETASK_ID);
$joined = Factory::getPretaskFactory()->filter([Factory::ORDER => $oF, Factory::JOIN => $jF, Factory::FILTER => $qF]);
return $joined[Factory::getPretaskFactory()->getModelName()];
}
/**
* @param int $supertaskId
* @return Supertask
* @throws HTException
*/
public static function getSupertask($supertaskId) {
$supertask = Factory::getSupertaskFactory()->get($supertaskId);
if ($supertask == null) {
throw new HTException("Invalid supertask ID!");
}
return $supertask;
}
/**
* @param int $supertaskId
* @param int $hashlistId
* @param int $crackerId
* @throws HTException
*/
public static function runSupertask($supertaskId, $hashlistId, $crackerId) {
$supertask = Factory::getSupertaskFactory()->get($supertaskId);
if ($supertask == null) {
throw new HTException("Invalid supertask ID!");
}
$hashlist = Factory::getHashlistFactory()->get($hashlistId);
if ($hashlist == null) {
throw new HTException("Invalid hashlist ID!");
}
else if ($hashlist->getIsArchived()) {
throw new HTException("Supertask cannot be applied to an archived hashlist!");
}
$cracker = Factory::getCrackerBinaryFactory()->get($crackerId);
if ($cracker == null) {
throw new HTException("Invalid cracker ID!");
}
$qF = new QueryFilter(SupertaskPretask::SUPERTASK_ID, $supertask->getId(), "=", Factory::getSupertaskPretaskFactory());
$jF = new JoinFilter(Factory::getSupertaskPretaskFactory(), Pretask::PRETASK_ID, SupertaskPretask::PRETASK_ID);
$joined = Factory::getPretaskFactory()->filter([Factory::FILTER => $qF, Factory::JOIN => $jF]);
/** @var $pretasks Pretask[] */
$pretasks = $joined[Factory::getPretaskFactory()->getModelName()];
Factory::getAgentFactory()->getDB()->beginTransaction();
$wrapperPriority = 0;
$wrapperMaxAgents = 0;
foreach ($pretasks as $pretask) {
if ($wrapperPriority == 0 || $wrapperPriority > $pretask->getPriority()) {
$wrapperPriority = $pretask->getPriority();
}
}
$taskWrapper = new TaskWrapper(null, $wrapperPriority, $wrapperMaxAgents, DTaskTypes::SUPERTASK, $hashlist->getId(), $hashlist->getAccessGroupId(), $supertask->getSupertaskName(), 0, 0);
$taskWrapper = Factory::getTaskWrapperFactory()->save($taskWrapper);
foreach ($pretasks as $pretask) {
$crackerBinaryId = $cracker->getId();
if ($cracker->getCrackerBinaryTypeId() != $pretask->getCrackerBinaryTypeId()) {
$crackerBinaryId = CrackerBinaryUtils::getNewestVersion($pretask->getCrackerBinaryTypeId())->getId();
}
$task = new Task(
null,
$pretask->getTaskName(),
$pretask->getAttackCmd(),
$pretask->getChunkTime(),
$pretask->getStatusTimer(),
0,
0,
$pretask->getPriority(),
$pretask->getMaxAgents(),
$pretask->getColor(),
$pretask->getIsSmall(),
$pretask->getIsCpuTask(),
$pretask->getUseNewBench(),
0,
$crackerBinaryId,
$cracker->getCrackerBinaryTypeId(),
$taskWrapper->getId(),
0,
'',
0,
0,
0,
0,
''
);
if ($hashlist->getHexSalt() == 1 && strpos($task->getAttackCmd(), "--hex-salt") === false) {
$task->setAttackCmd("--hex-salt " . $task->getAttackCmd());
}
$task = Factory::getTaskFactory()->save($task);
TaskUtils::copyPretaskFiles($pretask, $task);
}
Factory::getAgentFactory()->getDB()->commit();
}
/**
* @param string $name
* @param int[] $pretasks
* @throws HTException
*/
public static function createSupertask($name, $pretasks) {
if (!is_array($pretasks) || sizeof($pretasks) == 0) {
throw new HTException("Cannot create empty supertask!");
}
$name = htmlentities($name, ENT_QUOTES, "UTF-8");
$tasks = [];
foreach ($pretasks as $pretaskId) {
$pretask = Factory::getPretaskFactory()->get($pretaskId);
if ($pretask == null) {
throw new HTException("Invalid preconfigured task ID ($pretaskId)!");
}
$tasks[] = $pretask;
}
Factory::getAgentFactory()->getDB()->beginTransaction();
$supertask = new Supertask(null, $name);
$supertask = Factory::getSupertaskFactory()->save($supertask);
foreach ($tasks as $pretask) {
$supertaskPretask = new SupertaskPretask(null, $supertask->getId(), $pretask->getId());
Factory::getSupertaskPretaskFactory()->save($supertaskPretask);
}
Factory::getAgentFactory()->getDB()->commit();
}
/**
* @param string $name
* @param boolean $isCpuOnly
* @param int $maxAgents
* @param boolean $isSmall
* @param boolean $useOptimized
* @param int $crackerBinaryTypeId
* @param array $masks
* @param string $benchtype
* @throws HTException
*/
public static function importSupertask($name, $isCpuOnly, $maxAgents, $isSmall, $useOptimized, $crackerBinaryTypeId, $masks, $benchtype) {
$name = htmlentities($name, ENT_QUOTES, "UTF-8");
$isCpuOnly = ($isCpuOnly) ? 1 : 0;
$isSmall = ($isSmall) ? 1 : 0;
$useOptimized = ($useOptimized) ? true : false;
$benchtype = ($benchtype == 'speed') ? 1 : 0;
$crackerBinaryType = Factory::getCrackerBinaryTypeFactory()->get($crackerBinaryTypeId);
if ($crackerBinaryType == null) {
throw new HTException("Invalid cracker type ID!");
}
else if (!is_array($masks)) {
throw new HTException("Masks need to be provided as array!");
}
SupertaskUtils::prepareImportMasks($masks);
if (sizeof($masks) == 0) {
throw new HTException("No valid masks found!");
}
Factory::getAgentFactory()->getDB()->beginTransaction();
$pretasks = SupertaskUtils::createImportPretasks($masks, $isSmall, $maxAgents, $isCpuOnly, $crackerBinaryType, $useOptimized, $benchtype);
$supertask = new Supertask(null, $name);
$supertask = Factory::getSupertaskFactory()->save($supertask);
foreach ($pretasks as $preTask) {
$relation = new SupertaskPretask(null, $supertask->getId(), $preTask->getId());
Factory::getSupertaskPretaskFactory()->save($relation);
}
Factory::getAgentFactory()->getDB()->commit();
}
/**
* @param $masks
* @param $isSmall
* @param $isCpu
* @param $crackerBinaryType CrackerBinaryType
* @param bool $useOptimized
* @param int $newBench
* @return array
*/
private static function createImportPretasks($masks, $isSmall, $maxAgents, $isCpu, $crackerBinaryType, $useOptimized = false, $newBench = 1) {
// create the preconf tasks
$preTasks = array();
$priority = sizeof($masks) + 1;
foreach ($masks as $mask) {
$pattern = $mask[sizeof($mask) - 1];
$cmd = "";
switch (sizeof($mask)) {
case 5:
$cmd = " -4 " . $mask[3] . $cmd;
case 4:
$cmd = " -3 " . $mask[2] . $cmd;
case 3:
$cmd = " -2 " . $mask[1] . $cmd;
case 2:
$cmd = " -1 " . $mask[0] . $cmd;
case 1:
$cmd .= " $pattern";
}
if ($useOptimized) {
$cmd .= " -O ";
}
$cmd = str_replace("COMMA_PLACEHOLDER", "\\,", $cmd);
$cmd = str_replace("HASH_PLACEHOLDER", "\\#", $cmd);
$preTaskName = implode(",", $mask);
$preTaskName = str_replace("COMMA_PLACEHOLDER", "\\,", $preTaskName);
$preTaskName = str_replace("HASH_PLACEHOLDER", "\\#", $preTaskName);
$pretask = new Pretask(
null,
$preTaskName,
SConfig::getInstance()->getVal(DConfig::HASHLIST_ALIAS) . " -a 3 " . $cmd,
SConfig::getInstance()->getVal(DConfig::CHUNK_DURATION),
SConfig::getInstance()->getVal(DConfig::STATUS_TIMER),
"",
$isSmall,
$isCpu,
$newBench,
$priority,
$maxAgents,
1,
$crackerBinaryType->getId()
);
$pretask = Factory::getPretaskFactory()->save($pretask);
$preTasks[] = $pretask;
$priority--;
}
return $preTasks;
}
/**
* @param array $masks
*/
private static function prepareImportMasks(&$masks) {
for ($i = 0; $i < sizeof($masks); $i++) {
if (strlen($masks[$i]) == 0) {
unset($masks[$i]);
continue;
}
$mask = str_replace("\\,", "COMMA_PLACEHOLDER", $masks[$i]);
$mask = str_replace("\\#", "HASH_PLACEHOLDER", $mask);
if (strpos($mask, "#") !== false) {
$mask = substr($mask, 0, strpos($mask, "#"));
}
$mask = explode(",", $mask);
if (sizeof($mask) > 5) {
unset($masks[$i]);
continue;
}
$masks[$i] = $mask;
}
}
/**
* @param $supertaskId
* @param $pretaskId
* @throws HTException
*/
public static function removePretaskFromSupertask($supertaskId, $pretaskId) {
if ($supertaskId == null) {
throw new HTException("Invalid supertask ID!");
}
if ($pretaskId == null) {
throw new HTException("Invalid pretask ID!");
}
$qF1 = new QueryFilter(SupertaskPretask::SUPERTASK_ID, $supertaskId, "=");
$qF2 = new QueryFilter(SupertaskPretask::PRETASK_ID, $pretaskId, "=");
$supertaskPretask = Factory::getSupertaskPretaskFactory()->filter([Factory::FILTER => [$qF1, $qF2]], true);
Factory::getSupertaskPretaskFactory()->delete($supertaskPretask);
// check if the preconfigured task was from an import. in this case also delete it
$pretask = PretaskUtils::getPretask($pretaskId);
if ($pretask->getIsMaskImport() == 1) {
PretaskUtils::deletePretask($pretaskId);
}
}
/**
* @param $supertaskId
* @param $pretaskId
* @throws HTException
*/
public static function addPretaskToSupertask($supertaskId, $pretaskId) {
if ($supertaskId == null) {
throw new HTException("Invalid supertask ID!");
}
if ($pretaskId == null) {
throw new HTException("Invalid pretask ID!");
}
$supertaskPretask = new SupertaskPretask(null, $supertaskId, $pretaskId);
Factory::getSupertaskPretaskFactory()->save($supertaskPretask);
}
}