-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathPretaskUtils.class.php
More file actions
364 lines (339 loc) · 11.1 KB
/
Copy pathPretaskUtils.class.php
File metadata and controls
364 lines (339 loc) · 11.1 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
<?php
use DBA\Pretask;
use DBA\FilePretask;
use DBA\TaskWrapper;
use DBA\Task;
use DBA\OrderFilter;
use DBA\QueryFilter;
use DBA\SupertaskPretask;
use DBA\Factory;
class PretaskUtils {
/**
* @param int $pretaskId
* @param string $attackCmd
* @throws HTException
*/
public static function changeAttack($pretaskId, $attackCmd) {
$pretask = PretaskUtils::getPretask($pretaskId);
if (strpos($attackCmd, SConfig::getInstance()->getVal(DConfig::HASHLIST_ALIAS)) === false) {
throw new HTException("The attack command does not contain the hashlist alias!");
}
else if (Util::containsBlacklistedChars($attackCmd)) {
throw new HTException("The command must contain no blacklisted characters!");
}
Factory::getPretaskFactory()->set($pretask, Pretask::ATTACK_CMD, $attackCmd);
}
/**
* @param Task $copy
* @return Pretask
*/
public static function getFromTask($copy) {
return new Pretask(
null,
$copy->getTaskName(),
$copy->getAttackCmd(),
$copy->getChunkTime(),
$copy->getStatusTimer(),
$copy->getColor(),
$copy->getIsSmall(),
$copy->getIsCpuTask(),
$copy->getUseNewBench(),
0,
$copy->getMaxAgents(),
0,
$copy->getCrackerBinaryTypeId()
);
}
/**
* @return Pretask
*/
public static function getDefault() {
return new Pretask(
null,
'',
SConfig::getInstance()->getVal(DConfig::HASHLIST_ALIAS) . " ",
SConfig::getInstance()->getVal(DConfig::CHUNK_DURATION),
SConfig::getInstance()->getVal(DConfig::STATUS_TIMER),
'',
0,
0,
SConfig::getInstance()->getVal(DConfig::DEFAULT_BENCH),
0,
0,
0,
0
);
}
/**
* @param int $pretaskId
* @param int $isCpuOnly
* @throws HTException
*/
public static function setCpuOnlyTask($pretaskId, $isCpuOnly) {
$pretask = PretaskUtils::getPretask($pretaskId);
if (is_bool($isCpuOnly)) {
$isCpuOnly = ($isCpuOnly) ? 1 : 0;
}
if (!is_numeric($isCpuOnly) || $isCpuOnly < 0 || $isCpuOnly > 1) {
throw new HTException("Invalid boolean value!");
}
Factory::getPretaskFactory()->set($pretask, Pretask::IS_CPU_TASK, $isCpuOnly);
}
/**
* @param int $pretaskId
* @param int $isSmall
* @throws HTException
*/
public static function setSmallTask($pretaskId, $isSmall) {
$pretask = PretaskUtils::getPretask($pretaskId);
if (is_bool($isSmall)) {
$isSmall = ($isSmall) ? 1 : 0;
}
if (!is_numeric($isSmall) || $isSmall < 0 || $isSmall > 1) {
throw new HTException("Invalid boolean value!");
}
Factory::getPretaskFactory()->set($pretask, Pretask::IS_SMALL, $isSmall);
}
/**
* @param int $pretaskId
* @param int $priority
* @throws HTException
*/
public static function setPriority($pretaskId, $priority) {
$pretask = PretaskUtils::getPretask($pretaskId);
if (!is_numeric($priority)) {
throw new HTException("Priority needs to be a number!");
}
Factory::getPretaskFactory()->set($pretask, Pretask::PRIORITY, intval($priority));
}
/**
* @param int $pretaskId
* @param int $maxAgents
* @throws HTException
*/
public static function setMaxAgents($pretaskId, $maxAgents) {
$pretask = PretaskUtils::getPretask($pretaskId);
if (!is_numeric($maxAgents)) {
throw new HTException("Max agents needs to be a number!");
}
$maxAgents = intval($maxAgents);
Factory::getPretaskFactory()->set($pretask, Pretask::MAX_AGENTS, $maxAgents);
}
/**
* @param int $pretaskId
* @param string $color
* @throws HTException
*/
public static function setColor($pretaskId, $color) {
$pretask = PretaskUtils::getPretask($pretaskId);
if (strlen($color) > 0 && preg_match("/[0-9A-Fa-f]{6}/", $color) == 0) {
throw new HTException("Invalid color!");
}
Factory::getPretaskFactory()->set($pretask, Pretask::COLOR, $color);
}
/**
* @param int $pretaskId
* @param int $chunkTime
* @throws HTException
*/
public static function setChunkTime($pretaskId, $chunkTime) {
$pretask = PretaskUtils::getPretask($pretaskId);
$chunkTime = intval($chunkTime);
if ($chunkTime <= 0) {
throw new HTException("Invalid chunk time!");
}
Factory::getPretaskFactory()->set($pretask, Pretask::CHUNK_TIME, $chunkTime);
}
/**
* @param int $pretaskId
* @param string $newName
* @throws HTException
*/
public static function renamePretask($pretaskId, $newName) {
$pretask = PretaskUtils::getPretask($pretaskId);
if (strlen($newName) == 0) {
throw new HTException("Name cannot be empty!");
}
Factory::getPretaskFactory()->set($pretask, Pretask::TASK_NAME, htmlentities($newName, ENT_QUOTES, "UTF-8"));
}
/**
* @param int $pretaskId
* @throws HTException
*/
public static function deletePretask($pretaskId) {
$pretask = PretaskUtils::getPretask($pretaskId);
// delete connections to supertasks
$qF = new QueryFilter(SupertaskPretask::PRETASK_ID, $pretask->getId(), "=");
Factory::getSupertaskPretaskFactory()->massDeletion([Factory::FILTER => $qF]);
// delete connections to files
$qF = new QueryFilter(FilePretask::PRETASK_ID, $pretask->getId(), "=");
Factory::getFilePretaskFactory()->massDeletion([Factory::FILTER => $qF]);
Factory::getPretaskFactory()->delete($pretask);
}
/**
* @param boolean $includeMaskImports
* @return Pretask[]
*/
public static function getPretasks($includeMaskImports = false) {
$oF = new OrderFilter(Pretask::PRIORITY, "DESC");
if ($includeMaskImports) {
$pretasks = Factory::getPretaskFactory()->filter([Factory::ORDER => $oF]);
}
else {
$qF = new QueryFilter(Pretask::IS_MASK_IMPORT, 0, "=");
$pretasks = Factory::getPretaskFactory()->filter([Factory::ORDER => $oF, Factory::FILTER => $qF]);
}
return $pretasks;
}
/**
* @param int $pretaskId
* @return Pretask
* @throws HTException
*/
public static function getPretask($pretaskId) {
$pretask = Factory::getPretaskFactory()->get($pretaskId);
if ($pretask == null) {
throw new HTException("Invalid preconfigured task!");
}
return $pretask;
}
/**
* @param int $pretaskId
* @param int $hashlistId
* @param string $name
* @param int $crackerBinaryId
* @throws HTException
*/
public static function runPretask($pretaskId, $hashlistId, $name, $crackerBinaryId, $user = null) {
$pretask = Factory::getPretaskFactory()->get($pretaskId);
if ($pretask == null) {
throw new HTException("Invalid preconfigured task ID!");
}
$hashlist = Factory::getHashlistFactory()->get($hashlistId);
if ($hashlist == null) {
throw new HTException("Invalid hashlist ID!");
}
$name = htmlentities($name, ENT_QUOTES, "UTF-8");
if (strlen($name) == 0) {
$name = "Task_" . $hashlist->getId() . "_" . date("Ymd_Hi");
}
$cracker = Factory::getCrackerBinaryFactory()->get($crackerBinaryId);
if ($cracker == null) {
throw new HTException("Invalid cracker ID!");
}
else if ($pretask->getCrackerBinaryTypeId() != $cracker->getCrackerBinaryTypeId()) {
throw new HTException("Provided cracker does not match the type of the pretask!");
}
Factory::getAgentFactory()->getDB()->beginTransaction();
$taskWrapper = new TaskWrapper(null, $pretask->getPriority(), $pretask->getMaxAgents(), DTaskTypes::NORMAL, $hashlist->getId(), $hashlist->getAccessGroupId(), "", 0, 0, ($user != null) ? $user->getId() : null);
$taskWrapper = Factory::getTaskWrapperFactory()->save($taskWrapper);
$task = new Task(
null,
$name,
$pretask->getAttackCmd(),
$pretask->getChunkTime(),
$pretask->getStatusTimer(),
0,
0,
$pretask->getPriority(),
$pretask->getMaxAgents(),
$pretask->getColor(),
$pretask->getIsSmall(),
$pretask->getIsCpuTask(),
$pretask->getUseNewBench(),
0,
$cracker->getId(),
$cracker->getCrackerBinaryTypeId(),
$taskWrapper->getId(),
0,
'',
0,
0,
0,
0,
''
);
$task = Factory::getTaskFactory()->save($task);
TaskUtils::copyPretaskFiles($pretask, $task);
Factory::getAgentFactory()->getDB()->commit();
$payload = new DataSet(array(DPayloadKeys::TASK => $task));
NotificationHandler::checkNotifications(DNotificationType::NEW_TASK, $payload);
}
/**
* @param string $name
* @param string $cmdLine
* @param int $chunkTime
* @param int $statusTimer
* @param string $color
* @param int $cpuOnly
* @param int $isSmall
* @param int $benchmarkType
* @param array $files
* @param int $crackerBinaryTypeId
* @param int $maxAgents
* @param int $priority
* @throws HTException
*/
public static function createPretask($name, $cmdLine, $chunkTime, $statusTimer, $color, $cpuOnly, $isSmall, $benchmarkType, $files, $crackerBinaryTypeId, $maxAgents, $priority = 0) {
$crackerBinaryType = Factory::getCrackerBinaryTypeFactory()->get($crackerBinaryTypeId);
if (strlen($name) == 0) {
throw new HTException("Name cannot be empty!");
}
else if (strpos($cmdLine, SConfig::getInstance()->getVal(DConfig::HASHLIST_ALIAS)) === false) {
throw new HTException("The attack command does not contain the hashlist alias!");
}
else if (strlen($cmdLine) > 65535) {
throw new HTException("Attack command is too long (max 65535 characters)!");
}
else if (Util::containsBlacklistedChars($cmdLine)) {
throw new HTException("The command must contain no blacklisted characters!");
}
else if ($crackerBinaryType == null) {
throw new HTException("Invalid cracker binary type!");
}
$chunkTime = intval($chunkTime);
$statusTimer = intval($statusTimer);
$maxAgents = intval($maxAgents);
if (strlen($color) > 0 && preg_match("/[0-9A-Fa-f]{6}/", $color) == 0) {
$color = "";
}
else if ($cpuOnly < 0 || $cpuOnly > 1) {
throw new HTException("Invalid cpuOnly value!");
}
else if ($isSmall < 0 || $isSmall > 1) {
throw new HTException("Invalid isSmall value!");
}
else if ($benchmarkType < 0 || $benchmarkType > 1) {
throw new HTException("Invalid benchmark type!");
}
else if ($chunkTime <= 0) {
$chunkTime = SConfig::getInstance()->getVal(DConfig::CHUNK_DURATION);
}
else if ($statusTimer <= 0) {
$statusTimer = SConfig::getInstance()->getVal(DConfig::STATUS_TIMER);
}
$pretask = new Pretask(null,
htmlentities($name, ENT_QUOTES, "UTF-8"),
$cmdLine,
$chunkTime,
$statusTimer,
$color,
$isSmall,
$cpuOnly,
$benchmarkType,
$priority,
$maxAgents,
0,
$crackerBinaryType->getId()
);
$pretask = Factory::getPretaskFactory()->save($pretask);
// handle files
foreach ($files as $fileId) {
$file = Factory::getFileFactory()->get($fileId);
if ($file !== null) {
$filePretask = new FilePretask(null, $file->getId(), $pretask->getId());
Factory::getFilePretaskFactory()->save($filePretask);
}
}
}
}