-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathTaskUtils.php
More file actions
1471 lines (1372 loc) · 53.4 KB
/
TaskUtils.php
File metadata and controls
1471 lines (1372 loc) · 53.4 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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Hashtopolis\inc\utils;
use Hashtopolis\inc\DataSet;
use Hashtopolis\dba\models\AccessGroup;
use Hashtopolis\dba\models\AccessGroupAgent;
use Hashtopolis\dba\models\Agent;
use Hashtopolis\dba\models\Assignment;
use Hashtopolis\dba\models\Chunk;
use Hashtopolis\dba\ContainFilter;
use Hashtopolis\dba\models\CrackerBinary;
use Hashtopolis\dba\models\File;
use Hashtopolis\dba\models\FileTask;
use Hashtopolis\dba\JoinFilter;
use Hashtopolis\dba\OrderFilter;
use Hashtopolis\dba\QueryFilter;
use Hashtopolis\dba\models\Task;
use Hashtopolis\dba\models\TaskWrapper;
use Hashtopolis\dba\models\NotificationSetting;
use Hashtopolis\dba\models\AgentError;
use Hashtopolis\dba\models\Hash;
use Hashtopolis\dba\models\FilePretask;
use Hashtopolis\dba\models\Pretask;
use Hashtopolis\dba\models\User;
use Hashtopolis\dba\models\Hashlist;
use Hashtopolis\dba\models\AccessGroupUser;
use Hashtopolis\dba\models\TaskDebugOutput;
use Hashtopolis\dba\UpdateSet;
use Hashtopolis\dba\Factory;
use Hashtopolis\dba\models\Speed;
use Hashtopolis\dba\Aggregation;
use Hashtopolis\inc\apiv2\error\HttpError;
use Hashtopolis\inc\apiv2\error\HttpForbidden;
use Hashtopolis\inc\defines\DAccessControl;
use Hashtopolis\inc\defines\DConfig;
use Hashtopolis\inc\defines\DDirectories;
use Hashtopolis\inc\defines\DFileType;
use Hashtopolis\inc\defines\DHashcatStatus;
use Hashtopolis\inc\defines\DNotificationObjectType;
use Hashtopolis\inc\defines\DNotificationType;
use Hashtopolis\inc\defines\DPayloadKeys;
use Hashtopolis\inc\defines\DPrince;
use Hashtopolis\inc\defines\DTaskStaticChunking;
use Hashtopolis\inc\defines\DTaskTypes;
use Hashtopolis\inc\handlers\NotificationHandler;
use Hashtopolis\inc\HTException;
use Hashtopolis\inc\SConfig;
use Hashtopolis\inc\Util;
class TaskUtils {
/**
* @param Pretask $copy
* @return Task
*/
public static function getFromPretask($copy) {
return new Task(
null,
$copy->getTaskName(),
$copy->getAttackCmd(),
$copy->getChunkTime(),
$copy->getStatusTimer(),
0,
0,
$copy->getPriority(),
$copy->getMaxAgents(),
$copy->getColor(),
$copy->getIsSmall(),
$copy->getIsCpuTask(),
$copy->getUseNewBench(),
0,
0,
$copy->getCrackerBinaryTypeId(),
0,
0,
'',
0,
0,
0,
0,
''
);
}
/**
* @return Task
*/
public static function getDefault() {
return new Task(
null,
"",
"",
SConfig::getInstance()->getVal(DConfig::CHUNK_DURATION),
SConfig::getInstance()->getVal(DConfig::STATUS_TIMER),
0,
0,
0,
0,
"",
0,
0,
SConfig::getInstance()->getVal(DConfig::DEFAULT_BENCH),
0,
0,
0,
0,
0,
'',
0,
0,
0,
0,
''
);
}
/**
* @param int $taskId
* @param string $notes
* @param User $user
* @throws HTException
*/
public static function editNotes($taskId, $notes, $user) {
$task = TaskUtils::getTask($taskId, $user);
Factory::getTaskFactory()->set($task, Task::NOTES, $notes);
}
/**
* @param User $user
*/
public static function deleteArchived($user) {
$accessGroups = AccessUtils::getAccessGroupsOfUser($user);
$qF1 = new QueryFilter(TaskWrapper::IS_ARCHIVED, 1, "=");
$qF2 = new ContainFilter(TaskWrapper::ACCESS_GROUP_ID, Util::arrayOfIds($accessGroups));
$taskWrappers = Factory::getTaskWrapperFactory()->filter([Factory::FILTER => [$qF1, $qF2]]);
foreach ($taskWrappers as $taskWrapper) {
Factory::getAgentFactory()->getDB()->beginTransaction();
$tasks = TaskUtils::getTasksOfWrapper($taskWrapper->getId());
foreach ($tasks as $task) {
TaskUtils::deleteTask($task);
}
Factory::getTaskWrapperFactory()->delete($taskWrapper);
Factory::getAgentFactory()->getDB()->commit();
}
}
/**
* @param int $taskId
* @param string $attackCmd
* @param User $user
* @return void
* @throws HTException
*/
public static function changeAttackCmd($taskId, $attackCmd, $user) {
if (strlen($attackCmd) == 0) {
throw new HTException("Attack command cannot be empty!");
}
else if (strpos($attackCmd, SConfig::getInstance()->getVal(DConfig::HASHLIST_ALIAS)) === false) {
throw new HTException("Attack command must contain the hashlist alias!");
}
else if (Util::containsBlacklistedChars($attackCmd)) {
throw new HTException("The attack command must contain no blacklisted characters!");
}
$task = TaskUtils::getTask($taskId, $user);
if ($task->getAttackCmd() == $attackCmd) {
// no change required, we avoid all the overhead
return;
}
TaskUtils::purgeTask($task->getId(), $user);
$task = TaskUtils::getTask($taskId, $user); // reload task, otherwise we overwrite purge changes
Factory::getTaskFactory()->set($task, Task::ATTACK_CMD, $attackCmd);
}
/**
* @param int $supertaskId
* @param User $user
* @throws HTException
*/
public static function archiveSupertask($supertaskId, $user) {
$taskWrapper = TaskUtils::getTaskWrapper($supertaskId, $user);
$qF = new QueryFilter(Task::TASK_WRAPPER_ID, $taskWrapper->getId(), "=");
$uS = new UpdateSet(Task::IS_ARCHIVED, 1);
Factory::getTaskFactory()->massUpdate([Factory::FILTER => $qF, Factory::UPDATE => $uS]);
Factory::getTaskWrapperFactory()->set($taskWrapper, TaskWrapper::IS_ARCHIVED, 1);
}
/**
* @param int $taskId
* @param User $user
* @throws HTException
*/
public static function archiveTask($taskId, $user) {
$task = TaskUtils::getTask($taskId, $user);
$taskWrapper = TaskUtils::getTaskWrapper($task->getTaskWrapperId(), $user);
if ($taskWrapper->getTaskType() == DTaskTypes::NORMAL) {
Factory::getTaskWrapperFactory()->set($taskWrapper, TaskWrapper::IS_ARCHIVED, 1);
}
Factory::getTaskFactory()->set($task, Task::IS_ARCHIVED, 1);
}
/**
* @param int $taskId
* @param bool $taskState
* @param User $user
* @throws HTException
*/
public static function toggleArchiveTask($taskId, $taskState, $user) {
$task = TaskUtils::getTask($taskId, $user);
$taskWrapper = TaskUtils::getTaskWrapper($task->getTaskWrapperId(), $user);
switch ($taskWrapper->getTaskType()) {
case DTaskTypes::NORMAL:
Factory::getTaskFactory()->set($task, Task::IS_ARCHIVED, $taskState);
break;
case DTaskTypes::SUPERTASK:
$qF = new QueryFilter(Task::TASK_WRAPPER_ID, $taskWrapper->getId(), "=");
$uS = new UpdateSet(Task::IS_ARCHIVED, $taskState);
Factory::getTaskFactory()->massUpdate([Factory::FILTER => $qF, Factory::UPDATE => $uS]);
break;
default:
throw new HTException("Invalid task type for archiving!");
}
Factory::getTaskWrapperFactory()->set($taskWrapper, TaskWrapper::IS_ARCHIVED, $taskState);
}
/**
* @param int $taskWrapperId
* @param string $newName
* @param User $user
* @throws HTException
*/
public static function renameSupertask($taskWrapperId, $newName, $user) {
$taskWrapper = TaskUtils::getTaskWrapper($taskWrapperId, $user);
Factory::getTaskWrapperFactory()->set($taskWrapper, TaskWrapper::TASK_WRAPPER_NAME, $newName);
}
/**
* @param int $taskWrapperId
* @return Task
*/
public static function getTaskOfWrapper($taskWrapperId) {
$qF = new QueryFilter(Task::TASK_WRAPPER_ID, $taskWrapperId, "=");
return Factory::getTaskFactory()->filter([Factory::FILTER => $qF], true);
}
/**
* @param int $taskWrapperId
* @return Task[]
*/
public static function getTasksOfWrapper($taskWrapperId) {
$qF = new QueryFilter(Task::TASK_WRAPPER_ID, $taskWrapperId, "=");
return Factory::getTaskFactory()->filter([Factory::FILTER => $qF]);
}
/**
* @param User $user
* @return TaskWrapper[]
*/
public static function getTaskWrappersForUser($user) {
$accessGroupIds = Util::getAccessGroupIds($user->getId());
$qF = new ContainFilter(TaskWrapper::ACCESS_GROUP_ID, $accessGroupIds);
$oF1 = new OrderFilter(TaskWrapper::PRIORITY, "DESC");
$oF2 = new OrderFilter(TaskWrapper::TASK_WRAPPER_ID, "DESC");
return Factory::getTaskWrapperFactory()->filter([Factory::FILTER => $qF, Factory::ORDER => [$oF1, $oF2]]);
}
/**
* @param int $taskId
* @return Assignment[]
*/
public static function getAssignments($taskId) {
$qF = new QueryFilter(Assignment::TASK_ID, $taskId, "=");
return Factory::getAssignmentFactory()->filter([Factory::FILTER => $qF]);
}
/**
* @param int $taskId
* @return Chunk[]
*/
public static function getChunks($taskId) {
$qF = new QueryFilter(Chunk::TASK_ID, $taskId, "=");
$oF = new OrderFilter(Chunk::DISPATCH_TIME, "DESC");
return Factory::getChunkFactory()->filter([Factory::FILTER => $qF, Factory::ORDER => $oF]);
}
/**
* @param int $taskWrapperId
* @param User $user
* @return TaskWrapper
* @throws HTException
*/
public static function getTaskWrapper($taskWrapperId, $user) {
$taskWrapper = Factory::getTaskWrapperFactory()->get($taskWrapperId);
if ($taskWrapper == null) {
throw new HTException("Invalid taskWrapper ID!");
}
else if (!AccessUtils::userCanAccessTask($taskWrapper, $user)) {
throw new HTException("No access to this task!");
}
return $taskWrapper;
}
/**
* @param int $taskId
* @param User $user
* @return Task
* @throws HTException
*/
public static function getTask($taskId, $user) {
$task = Factory::getTaskFactory()->get($taskId);
if ($task == null) {
throw new HTException("Invalid task ID!");
}
$taskWrapper = Factory::getTaskWrapperFactory()->get($task->getTaskWrapperId());
if (!AccessUtils::userCanAccessTask($taskWrapper, $user)) {
throw new HTException("No access to this task!");
}
return $task;
}
/**
* @param Pretask $pretask
* @param Task $task
*/
public static function copyPretaskFiles($pretask, $task) {
$qF = new QueryFilter(FilePretask::PRETASK_ID, $pretask->getId(), "=");
$pretaskFiles = Factory::getFilePretaskFactory()->filter([Factory::FILTER => $qF]);
$subTasks[] = $task;
foreach ($pretaskFiles as $pretaskFile) {
$fileTask = new FileTask(null, $pretaskFile->getFileId(), $task->getId());
Factory::getFileTaskFactory()->save($fileTask);
FileDownloadUtils::addDownload($fileTask->getFileId());
}
}
/**
* @param int $supertaskId
* @param int $priority
* @param User $user
* @param bool $topPriority
* @throws HTException
*/
public static function setSupertaskPriority($supertaskId, $priority, $user, $topPriority = false) {
// note that supertaskId here corresponds with the taskwrapper Id of the underlying subtasks of the running supertask
$supertaskWrapper = TaskUtils::getTaskWrapper($supertaskId, $user);
if ($supertaskWrapper === null) {
throw new HTException("Invalid supertask!");
}
else if (!AccessUtils::userCanAccessTask($supertaskWrapper, $user)) {
throw new HTException("No access to this task!");
}
$priority = self::getIntegerPriorityValue($priority, $topPriority, $user, $supertaskWrapper);
Factory::getTaskWrapperFactory()->set($supertaskWrapper, TaskWrapper::PRIORITY, $priority);
}
/**
* @param int $priority
* @param bool $topPriority
* @param $user
* @param $taskWrapper
* @return int
*/
public static function getIntegerPriorityValue($priority, $topPriority, $user, $taskWrapper) {
if ($topPriority) {
// determine the current highest priority of all tasks this user has access to
$auxTaskWrappers = TaskUtils::getTaskWrappersForUser($user);
$highestPriority = 0;
foreach ($auxTaskWrappers as $auxTaskWrapper) {
if ($auxTaskWrapper != $taskWrapper) {
if ($auxTaskWrapper->getPriority() > $highestPriority) {
$highestPriority = $auxTaskWrapper->getPriority();
}
}
}
// set task priority to the current highest priority plus one hundred
$priority = $highestPriority + 100;
}
else {
$priority = intval($priority);
$priority = ($priority < 0) ? 0 : $priority;
}
return $priority;
}
/**
* @param int $taskId
* @param int $isCpuOnly
* @param User $user
* @throws HTException
*/
public static function setCpuTask($taskId, $isCpuOnly, $user) {
$task = Factory::getTaskFactory()->get($taskId);
if ($task == null) {
throw new HTException("No such task!");
}
$taskWrapper = Factory::getTaskWrapperFactory()->get($task->getTaskWrapperId());
if (!AccessUtils::userCanAccessTask($taskWrapper, $user)) {
throw new HTException("No access to this task!");
}
$isCpuTask = intval($isCpuOnly);
if ($isCpuTask != 0 && $isCpuTask != 1) {
$isCpuTask = 0;
}
Factory::getTaskFactory()->set($task, Task::IS_CPU_TASK, $isCpuTask);
}
/**
* @param User $user
*/
public static function deleteFinished($user) {
// check every task wrapper (non-archived ones)
$qF = new QueryFilter(TaskWrapper::IS_ARCHIVED, 0, "=");
$taskWrappers = Factory::getTaskWrapperFactory()->filter([Factory::FILTER => $qF]);
foreach ($taskWrappers as $taskWrapper) {
if (!AccessUtils::userCanAccessTask($taskWrapper, $user)) {
continue; // we only delete finished ones where the user has access to
}
$qF = new QueryFilter(Task::TASK_WRAPPER_ID, $taskWrapper->getId(), "=");
$tasks = Factory::getTaskFactory()->filter([Factory::FILTER => $qF]);
$isComplete = true;
foreach ($tasks as $task) {
if ($task->getKeyspace() == 0 || $task->getKeyspace() > $task->getKeyspaceProgress()) {
$isComplete = false;
break;
}
$sumProg = 0;
$qF = new QueryFilter(Chunk::TASK_ID, $task->getId(), "=");
$chunks = Factory::getChunkFactory()->filter([Factory::FILTER => $qF]);
foreach ($chunks as $chunk) {
$sumProg += $chunk->getCheckpoint() - $chunk->getSkip();
}
if ($sumProg < $task->getKeyspace()) {
$isComplete = false;
break;
}
}
if ($isComplete) {
Factory::getAgentFactory()->getDB()->beginTransaction();
foreach ($tasks as $task) {
TaskUtils::deleteTask($task);
}
Factory::getTaskWrapperFactory()->delete($taskWrapper);
Factory::getAgentFactory()->getDB()->commit();
}
}
}
/**
* @param int $taskId
* @param int $chunkTime
* @param User $user
* @throws HTException
*/
public static function changeChunkTime($taskId, $chunkTime, $user) {
// update task chunk time
$task = Factory::getTaskFactory()->get($taskId);
if ($task == null) {
throw new HTException("No such task!");
}
$taskWrapper = Factory::getTaskWrapperFactory()->get($task->getTaskWrapperId());
if (!AccessUtils::userCanAccessTask($taskWrapper, $user)) {
throw new HTException("No access to this task!");
}
$chunktime = intval($chunkTime);
Factory::getAgentFactory()->getDB()->beginTransaction();
$qF = new QueryFilter(Assignment::TASK_ID, $task->getId(), "=", Factory::getTaskFactory());
$jF = new JoinFilter(Factory::getTaskFactory(), Task::TASK_ID, Assignment::TASK_ID);
$join = Factory::getAssignmentFactory()->filter([Factory::FILTER => $qF, Factory::JOIN => $jF]);
/** @var $assignments Assignment[] */
$assignments = $join[Factory::getAssignmentFactory()->getModelName()];
foreach ($assignments as $assignment) {
if ($task->getUseNewBench() == 0) {
Factory::getAssignmentFactory()->set($assignment, Assignment::BENCHMARK, $assignment->getBenchmark() / $task->getChunkTime() * $chunktime);
}
}
$task->setChunkTime($chunktime);
Factory::getTaskFactory()->update($task);
Factory::getAgentFactory()->getDB()->commit();
}
/**
* @param int $chunkId
* @param User $user
* @throws HTException
*/
public static function abortChunk($chunkId, $user) {
// reset chunk state and progress to zero
$chunk = Factory::getChunkFactory()->get($chunkId);
if ($chunk == null) {
throw new HTException("No such chunk!");
}
$task = Factory::getTaskFactory()->get($chunk->getTaskId());
$taskWrapper = Factory::getTaskWrapperFactory()->get($task->getTaskWrapperId());
if (!AccessUtils::userCanAccessTask($taskWrapper, $user)) {
throw new HTException("No access to this task!");
}
Factory::getChunkFactory()->set($chunk, Chunk::STATE, DHashcatStatus::ABORTED);
}
/**
* @param int $chunkId
* @param User $user
* @throws HTException
*/
public static function resetChunk($chunkId, $user) {
// reset chunk state and progress to zero
$chunk = Factory::getChunkFactory()->get($chunkId);
if ($chunk == null) {
throw new HTException("No such chunk!");
}
$task = Factory::getTaskFactory()->get($chunk->getTaskId());
$taskWrapper = Factory::getTaskWrapperFactory()->get($task->getTaskWrapperId());
if (!AccessUtils::userCanAccessTask($taskWrapper, $user)) {
throw new HTException("No access to this task!");
}
$initialProgress = ($task->getUsePreprocessor() || $task->getForcePipe()) ? null : 0;
Factory::getChunkFactory()->mset($chunk, [
Chunk::STATE => DHashcatStatus::INIT,
Chunk::PROGRESS => $initialProgress,
Chunk::CHECKPOINT => $chunk->getSkip(),
Chunk::DISPATCH_TIME => 0,
Chunk::SOLVE_TIME => 0
]
);
}
/**
* @param int $agentId
* @param string $benchmark
* @param User $user
* @throws HTException
*/
public static function setBenchmark($agentId, $benchmark, $user) {
// adjust agent benchmark
$qF = new QueryFilter(Assignment::AGENT_ID, $agentId, "=");
$assignment = Factory::getAssignmentFactory()->filter([Factory::FILTER => $qF], true);
if ($assignment == null) {
throw new HTException("No assignment for this agent!");
}
else if (!AccessUtils::userCanAccessAgent(Factory::getAgentFactory()->get($agentId), $user)) {
throw new HTException("No access to this agent!");
}
// TODO: check benchmark validity
Factory::getAssignmentFactory()->set($assignment, Assignment::BENCHMARK, $benchmark);
}
/**
* @param int $taskId
* @param User $user
* @throws HTException
*/
public static function purgeTask($taskId, $user) {
// delete all task chunks, forget its keyspace value and reset progress to zero
$task = Factory::getTaskFactory()->get($taskId);
if ($task == null) {
throw new HTException("No such task!");
}
$taskWrapper = Factory::getTaskWrapperFactory()->get($task->getTaskWrapperId());
if (!AccessUtils::userCanAccessTask($taskWrapper, $user)) {
throw new HTException("No access to this task!");
}
Factory::getAgentFactory()->getDB()->beginTransaction();
// reset all benchmarks on assignments
$qF = new QueryFilter(Assignment::TASK_ID, $task->getId(), "=");
$uS = new UpdateSet(Assignment::BENCHMARK, 0);
Factory::getAssignmentFactory()->massUpdate([Factory::FILTER => $qF, Factory::UPDATE => $uS]);
// get all chunk ids of this task
$chunks = Factory::getChunkFactory()->filter([Factory::FILTER => $qF]);
$chunkIds = array();
foreach ($chunks as $chunk) {
$chunkIds[] = $chunk->getId();
}
if (sizeof($chunkIds) > 0) {
// remove relation of all hashes which were cracked with the chunks which are going to be deleted
$qF2 = new ContainFilter(Hash::CHUNK_ID, $chunkIds);
$uS = new UpdateSet(Hash::CHUNK_ID, null);
Factory::getHashFactory()->massUpdate([Factory::FILTER => $qF2, Factory::UPDATE => $uS]);
Factory::getHashBinaryFactory()->massUpdate([Factory::FILTER => $qF2, Factory::UPDATE => $uS]);
}
// delete all chunks and set keyspace and progress of task to 0
Factory::getChunkFactory()->massDeletion([Factory::FILTER => $qF]);
Factory::getTaskFactory()->mset($task, [Task::KEYSPACE => 0, Task::KEYSPACE_PROGRESS => 0]);
// set cracked count of taskwrapper to 0
$taskWrapper->setCracked(0);
Factory::getTaskWrapperFactory()->update($taskWrapper);
Factory::getAgentFactory()->getDB()->commit();
}
/**
* @param int $taskId
* @param User $user
* @param boolean $api
* @throws HTException
*/
public static function delete($taskId, $user, $api = false) {
// delete a task
$task = Factory::getTaskFactory()->get($taskId);
if ($task == null) {
throw new HTException("No such task!");
}
$taskWrapper = Factory::getTaskWrapperFactory()->get($task->getTaskWrapperId());
if (!AccessUtils::userCanAccessTask($taskWrapper, $user)) {
throw new HTException("No access to this task!");
}
Factory::getAgentFactory()->getDB()->beginTransaction();
$payload = new DataSet(array(DPayloadKeys::TASK => $task));
NotificationHandler::checkNotifications(DNotificationType::DELETE_TASK, $payload);
TaskUtils::deleteTask($task);
if ($taskWrapper->getTaskType() != DTaskTypes::SUPERTASK) {
Factory::getTaskWrapperFactory()->delete($taskWrapper);
}
Factory::getAgentFactory()->getDB()->commit();
if (!$api) {
header("Location: tasks.php");
die();
}
}
/**
* @param int $taskId
* @param int $isSmall
* @param User $user
* @throws HTException
*/
public static function setSmallTask($taskId, $isSmall, $user) {
$task = TaskUtils::getTask($taskId, $user);
$isSmall = intval($isSmall);
if ($isSmall != 0 && $isSmall != 1) {
$isSmall = 0;
}
Factory::getTaskFactory()->set($task, Task::IS_SMALL, $isSmall);
}
/**
* @param int $taskId
* @param int $maxAgents
* @param User $user
* @throws HTException
*/
public static function setTaskMaxAgents($taskId, $maxAgents, $user) {
$task = TaskUtils::getTask($taskId, $user);
$taskWrapper = TaskUtils::getTaskWrapper($task->getTaskWrapperId(), $user);
$maxAgents = intval($maxAgents);
Factory::getTaskFactory()->set($task, Task::MAX_AGENTS, $maxAgents);
if ($taskWrapper->getTaskType() != DTaskTypes::SUPERTASK) {
Factory::getTaskWrapperFactory()->set($taskWrapper, TaskWrapper::MAX_AGENTS, $maxAgents);
}
}
/**
* @param int $superTaskId
* @param int $maxAgents
* @param User $user
* @throws HTException
*/
public static function setSuperTaskMaxAgents($superTaskId, $maxAgents, $user) {
$taskWrapper = TaskUtils::getTaskWrapper($superTaskId, $user);
$maxAgents = intval($maxAgents);
Factory::getTaskWrapperFactory()->set($taskWrapper, TaskWrapper::MAX_AGENTS, $maxAgents);
}
/**
* @param int $taskId
* @param string $color
* @param User $user
* @throws HTException
*/
public static function updateColor($taskId, $color, $user) {
// change task color
$task = TaskUtils::getTask($taskId, $user);
if (preg_match("/[0-9A-Za-z]{6}/", $color) == 0) {
$color = null;
}
Factory::getTaskFactory()->set($task, Task::COLOR, $color);
}
/**
* @param int $taskId
* @param string $name
* @param User $user
* @throws HTException
*/
public static function rename($taskId, $name, $user) {
// change task name
$task = TaskUtils::getTask($taskId, $user);
Factory::getTaskFactory()->set($task, Task::TASK_NAME, $name);
}
/**
* @param int $taskId
* @param int $statusTimer
* @param User $user
* @throws HTException
*/
public static function updateStatusTimer($taskId, $statusTimer, $user) {
// change the statusTimer value, the interval in seconds clients should report back to the server
$task = TaskUtils::getTask($taskId, $user);
if ($task == null) {
throw new HTException("No such task!");
}
$taskWrapper = Factory::getTaskWrapperFactory()->get($task->getTaskWrapperId());
if (!AccessUtils::userCanAccessTask($taskWrapper, $user)) {
throw new HTException("No access to this task!");
}
$statusTimer = intval($statusTimer);
if ($statusTimer <= 0 || !is_numeric($statusTimer)) {
throw new HTException("Invalid status interval!");
}
if ($statusTimer > $task->getChunkTime()) {
throw new HTException("Chunk time must be higher than status timer!");
}
Factory::getTaskFactory()->set($task, Task::STATUS_TIMER, $statusTimer);
}
/**
* @param int $taskId
* @param int $priority
* @param User $user
* @param bool $top
* @throws HTException
*/
public static function updatePriority($taskId, $priority, $user, $topPriority = false) {
// change task priority
$task = TaskUtils::getTask($taskId, $user);
$taskWrapper = TaskUtils::getTaskWrapper($task->getTaskWrapperId(), $user);
$priority = self::getIntegerPriorityValue($priority, $topPriority, $user, $taskWrapper);
Factory::getTaskFactory()->set($task, Task::PRIORITY, $priority);
if ($taskWrapper->getTaskType() != DTaskTypes::SUPERTASK) {
Factory::getTaskWrapperFactory()->set($taskWrapper, TaskWrapper::PRIORITY, $priority);
}
}
/**
* @param int $taskId
* @param int $maxAgents
* @param User $user
* @throws HTException
*/
public static function updateMaxAgents($taskId, $maxAgents, $user) {
$task = TaskUtils::getTask($taskId, $user);
if ($task == null) {
throw new HTException("No such task!");
}
$taskWrapper = Factory::getTaskWrapperFactory()->get($task->getTaskWrapperId());
if (!AccessUtils::userCanAccessTask($taskWrapper, $user)) {
throw new HTException("No access to this task!");
}
if (!is_numeric($maxAgents)) {
throw new HTException("Invalid number of agents!");
}
$maxAgents = intval($maxAgents);
if ($maxAgents < 0) {
throw new HTException("Invalid number of agents!");
}
if ($taskWrapper->getTaskType() != DTaskTypes::SUPERTASK) {
Factory::getTaskWrapperFactory()->set($taskWrapper, TaskWrapper::MAX_AGENTS, $maxAgents);
}
Factory::getTaskFactory()->set($task, Task::MAX_AGENTS, $maxAgents);
}
/**
* @param int $hashlistId
* @param string $name
* @param string $attackCmd
* @param int $chunkTime
* @param int $status
* @param string $benchtype
* @param string $color
* @param boolean $isCpuOnly
* @param boolean $isSmall
* @param $usePreprocessor
* @param $preprocessorCommand
* @param int $skip
* @param int $priority
* @param int $maxAgents
* @param int[] $files
* @param int $crackerVersionId
* @param User $user
* @param string $notes
* @param int $staticChunking
* @param int $chunkSize
* @return Task
* @throws HttpError
*/
public static function createTask($hashlistId, $name, $attackCmd, $chunkTime, $status, $benchtype, $color, $isCpuOnly, $isSmall, $usePreprocessor, $preprocessorCommand, $skip, $priority, $maxAgents, $files, $crackerVersionId, $user, $notes = "", $staticChunking = DTaskStaticChunking::NORMAL, $chunkSize = 0, $enforcePipe = 0) {
$hashlist = Factory::getHashlistFactory()->get($hashlistId);
if ($hashlist == null) {
throw new HttpError("Invalid hashlist ID!");
}
else if ($hashlist->getIsArchived()) {
throw new HttpError("You cannot create a task for an archived hashlist!");
}
if (strlen($name) == 0) {
$name = "Task_" . $hashlist->getId() . "_" . date("Ymd_Hi");
}
$accessGroup = Factory::getAccessGroupFactory()->get($hashlist->getAccessGroupId());
$qF1 = new QueryFilter(AccessGroupUser::ACCESS_GROUP_ID, $accessGroup->getId(), "=");
$qF2 = new QueryFilter(AccessGroupUser::USER_ID, $user->getId(), "=");
$accessGroupUser = Factory::getAccessGroupUserFactory()->filter([Factory::FILTER => [$qF1, $qF2]], true);
if ($accessGroupUser == null) {
throw new HttpForbidden("You have no access to this hashlist!");
}
$cracker = Factory::getCrackerBinaryFactory()->get($crackerVersionId);
if ($cracker == null) {
throw new HttpError("Invalid cracker ID!");
}
else if (strpos($attackCmd, SConfig::getInstance()->getVal(DConfig::HASHLIST_ALIAS)) === false) {
throw new HttpError("Attack command does not contain hashlist alias!");
}
else if (strlen($attackCmd) > 65535) {
throw new HttpError("Attack command is too long (max 65535 characters)!");
}
else if ($staticChunking < DTaskStaticChunking::NORMAL || $staticChunking > DTaskStaticChunking::NUM_CHUNKS) {
throw new HttpError("Invalid static chunk setting!");
}
else if ($staticChunking > DTaskStaticChunking::NORMAL && $chunkSize <= 0) {
throw new HttpError("Invalid chunk size / number of chunks for static chunking!");
}
else if (Util::containsBlacklistedChars($attackCmd)) {
throw new HttpError("Attack command contains blacklisted characters!");
}
else if (Util::containsBlacklistedChars($preprocessorCommand)) {
throw new HttpError("Preprocessor command contains blacklisted characters!");
}
else if (!is_numeric($chunkTime) || $chunkTime < 1) {
throw new HttpError("Invalid chunk size!");
}
else if (!is_numeric($status) || $status < 1) {
throw new HttpError("Invalid status timer!");
}
else if ($benchtype != 'speed' && $benchtype != 'runtime') {
throw new HttpError("Invalid benchmark type!");
} else if ($enforcePipe < 0 || $enforcePipe > 1) {
throw new HttpError("Invalid enforce pipe value");
}
$benchtype = ($benchtype == 'speed') ? 1 : 0;
if (preg_match("/[0-9A-Za-z]{6}/", $color) != 1) {
$color = null;
}
$isCpuOnly = ($isCpuOnly) ? 1 : 0;
$isSmall = ($isSmall) ? 1 : 0;
if ($usePreprocessor < 0) {
$usePreprocessor = 0;
}
else if ($usePreprocessor > 0) {
$preprocessor = PreprocessorUtils::getPreprocessor($usePreprocessor);
}
if ($skip < 0) {
$skip = 0;
}
if ($priority < 0) {
$priority = 0;
}
if ($usePreprocessor && $benchtype == 'runtime') {
// enforce speed benchmark type when using PRINCE
$benchtype = 'speed';
}
Factory::getAgentFactory()->getDB()->beginTransaction();
$taskWrapper = new TaskWrapper(null, $priority, $maxAgents, DTaskTypes::NORMAL, $hashlist->getId(), $accessGroup->getId(), "", 0, 0);
$taskWrapper = Factory::getTaskWrapperFactory()->save($taskWrapper);
$task = new Task(
null,
$name,
$attackCmd,
$chunkTime,
$status,
0,
0,
$priority,
$maxAgents,
$color,
$isSmall,
$isCpuOnly,
$benchtype,
$skip,
$cracker->getId(),
$cracker->getCrackerBinaryTypeId(),
$taskWrapper->getId(),
0,
$notes,
$staticChunking,
$chunkSize,
$enforcePipe,
($usePreprocessor > 0) ? $preprocessor->getId() : 0,
($usePreprocessor > 0) ? $preprocessorCommand : ''
);
$task = Factory::getTaskFactory()->save($task);
if (is_array($files) && sizeof($files) > 0) {
foreach ($files as $fileId) {
$taskFile = new FileTask(null, $fileId, $task->getId());
Factory::getFileTaskFactory()->save($taskFile);
FileDownloadUtils::addDownload($taskFile->getFileId());
}
}
Factory::getAgentFactory()->getDB()->commit();
$payload = new DataSet(array(DPayloadKeys::TASK => $task));
NotificationHandler::checkNotifications(DNotificationType::NEW_TASK, $payload);
return $task;
}
/**
* Splits a given task into subtasks within a supertask by splitting the rule file
* @param Task $task
* @param TaskWrapper $taskWrapper
* @param File[] $files
* @param File $splitFile
* @param array $split
*/
public static function splitByRules($task, $taskWrapper, $files, $splitFile, $split) {
// calculate how much we need to split
$numSplits = floor($split[1] / 1000 / $task->getChunkTime());
// replace countLines with fileLineCount? Could be a better option: not OS-dependent
$numLines = Util::countLines(Factory::getStoredValueFactory()->get(DDirectories::FILES)->getVal() . "/" . $splitFile->getFilename());
$linesPerFile = floor($numLines / $numSplits) + 1;
// create the temporary rule files
$newFiles = [];
$content = explode("\n", str_replace("\r\n", "\n", file_get_contents(Factory::getStoredValueFactory()->get(DDirectories::FILES)->getVal() . "/" . $splitFile->getFilename())));
$count = 0;
$taskId = $task->getId();
for ($i = 0; $i < $numLines; $i += $linesPerFile, $count++) {
$copy = [];
for ($j = $i; $j < $i + $linesPerFile && $j < sizeof($content); $j++) {
$copy[] = $content[$j];
}
$filename = $splitFile->getFilename() . "_p$taskId-$count";
$path = Factory::getStoredValueFactory()->get(DDirectories::FILES)->getVal() . "/" . $splitFile->getFilename() . "_p$taskId-$count";
file_put_contents($path, implode("\n", $copy) . "\n");
$f = new File(null, $filename, Util::filesize($path), $splitFile->getIsSecret(), DFileType::TEMPORARY, $taskWrapper->getAccessGroupId(), Util::fileLineCount($path));
$f = Factory::getFileFactory()->save($f);
$newFiles[] = $f;
}
// take out the split file from the file list
for ($i = 0; $i < sizeof($files); $i++) {
if ($files[$i]->getId() == $splitFile->getId()) {
unset($files[$i]);
break;
}
}
// create new tasks as supertask
$newWrapper = new TaskWrapper(null, 0, 0, DTaskTypes::SUPERTASK, $taskWrapper->getHashlistId(), $taskWrapper->getAccessGroupId(), $task->getTaskName() . " (From Rule Split)", 0, 0);
$newWrapper = Factory::getTaskWrapperFactory()->save($newWrapper);
$prio = sizeof($newFiles) + 1;
foreach ($newFiles as $newFile) {
$newTask = new Task(null,
"Part " . (sizeof($newFiles) + 2 - $prio),
str_replace($splitFile->getFilename(), $newFile->getFilename(), $task->getAttackCmd()),
$task->getChunkTime(),
$task->getStatusTimer(),
0,
0,
$prio,
$task->getMaxAgents(),
$task->getColor(),
(SConfig::getInstance()->getVal(DConfig::RULE_SPLIT_SMALL_TASKS) == 0) ? 0 : 1,
$task->getIsCpuTask(),
$task->getUseNewBench(),
$task->getSkipKeyspace(),
$task->getCrackerBinaryId(),
$task->getCrackerBinaryTypeId(),
$newWrapper->getId(),
0,
'',
0,
0,
0,
0,
''
);
$newTask = Factory::getTaskFactory()->save($newTask);
$taskFiles = [];
$taskFiles[] = new FileTask(null, $newFile->getId(), $newTask->getId());
foreach ($files as $f) {
$taskFiles[] = new FileTask(null, $f->getId(), $newTask->getId());
FileDownloadUtils::addDownload($f->getId());
}
Factory::getFileTaskFactory()->massSave($taskFiles);
$prio--;
}
$newWrapper->setPriority($taskWrapper->getPriority());
$newWrapper->setMaxAgents($taskWrapper->getMaxAgents());
Factory::getTaskWrapperFactory()->update($newWrapper);