Skip to content

Commit 21bdb34

Browse files
committed
Added backend handling of overwriting on import pre-cracked hashes
1 parent 35f6fee commit 21bdb34

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

src/inc/apiv2/helper/importCrackedHashes.routes.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function getFormFields(): array {
2828
Hashlist::HASHLIST_ID => ["type" => "int"],
2929
"sourceData" => ['type' => 'str'],
3030
"separator" => ['type' => 'str'],
31+
"overwrite" => ['type' => 'int'],
3132
];
3233
}
3334

@@ -52,7 +53,7 @@ public function actionPost($data): object|array|null {
5253

5354
$importData = base64_decode($data["sourceData"]);
5455

55-
$result = HashlistUtils::processZap($hashlist->getId(), $data["separator"], "paste", ["hashfield" => $importData], [], $this->getCurrentUser());
56+
$result = HashlistUtils::processZap($hashlist->getId(), $data["separator"], "paste", ["hashfield" => $importData], [], $this->getCurrentUser(), (isset($data["overwrite"]) && intval($data["overwrite"]) == 1) ? true : false);
5657

5758
return [
5859
"totalLines" => $result[0],

src/inc/handlers/HashlistHandler.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function handle($action) {
4949
break;
5050
case DHashlistAction::PROCESS_ZAP:
5151
AccessControl::getInstance()->checkPermission(DHashlistAction::PROCESS_ZAP_PERM);
52-
$data = HashlistUtils::processZap($_POST['hashlist'], $_POST['separator'], $_POST['source'], $_POST, $_FILES, AccessControl::getInstance()->getUser());
52+
$data = HashlistUtils::processZap($_POST['hashlist'], $_POST['separator'], $_POST['source'], $_POST, $_FILES, AccessControl::getInstance()->getUser(), (isset($_POST["overwrite"]) && intval($_POST["overwrite"]) == 1) ? true : false);
5353
UI::addMessage(UI::SUCCESS, "Processed pre-cracked hashes: " . $data[0] . " total lines, " . $data[1] . " new cracked hashes, " . $data[2] . " were already cracked, " . $data[3] . " invalid lines, " . $data[4] . " not matching entries (" . $data[5] . "s)!");
5454
if ($data[6] > 0) {
5555
UI::addMessage(UI::WARN, $data[6] . " entries with too long plaintext");

src/inc/user-api/UserAPIHashlist.class.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ private function importCracked($QUERY) {
182182
'paste',
183183
['hashfield' => base64_decode($QUERY[UQueryHashlist::HASHLIST_DATA])],
184184
[],
185-
$this->user
185+
$this->user,
186+
false
186187
);
187188
$response = [
188189
UResponseHashlist::SECTION => $QUERY[UQueryHashlist::SECTION],

src/inc/utils/HashlistUtils.class.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,11 @@ public static function rename($hashlistId, $name, $user) {
303303
* @param array $post
304304
* @param array $files
305305
* @param User $user
306+
* @param boolean $overwritePlaintext
306307
* @return int[]
307308
* @throws HTException
308309
*/
309-
public static function processZap($hashlistId, $separator, $source, $post, $files, $user) {
310+
public static function processZap($hashlistId, $separator, $source, $post, $files, $user, $overwritePlaintext) {
310311
// pre-crack hashes processor
311312
$hashlist = HashlistUtils::getHashlist($hashlistId);
312313
if (!AccessUtils::userCanAccessHashlists($hashlist, $user)) {
@@ -427,16 +428,22 @@ public static function processZap($hashlistId, $separator, $source, $post, $file
427428
}
428429
else if ($hashEntry->getIsCracked() == 1) {
429430
$alreadyCracked++;
430-
continue;
431+
if (!$overwritePlaintext) {
432+
continue;
433+
}
431434
}
432435
$plain = str_replace($hash . $separator . $hashEntry->getSalt() . $separator, "", $data);
433436
if (strlen($plain) > SConfig::getInstance()->getVal(DConfig::PLAINTEXT_MAX_LENGTH)) {
434437
$tooLong++;
435438
continue;
436439
}
437440
$hashFactory->mset($hashEntry, [Hash::PLAINTEXT => $plain, Hash::IS_CRACKED => 1, Hash::TIME_CRACKED => time()]);
438-
$newCracked++;
439-
$crackedIn[$hashEntry->getHashlistId()]++;
441+
442+
if ($hashEntry->getIsCracked() != 1) {
443+
$newCracked++;
444+
$crackedIn[$hashEntry->getHashlistId()]++;
445+
}
446+
440447
if ($hashlist->getFormat() == DHashlistFormat::PLAIN) {
441448
$zaps[] = new Zap(null, $hashEntry->getHash(), time(), null, $hashlist->getId());
442449
}
@@ -469,19 +476,28 @@ public static function processZap($hashlistId, $separator, $source, $post, $file
469476
foreach ($hashEntries as $hashEntry) {
470477
if ($hashEntry->getIsCracked() == 1) {
471478
$alreadyCracked++;
472-
continue;
479+
if (!$overwritePlaintext) {
480+
continue;
481+
}
473482
}
483+
474484
$plain = str_replace($hash . $separator, "", $data);
485+
475486
if (strlen($plain) > SConfig::getInstance()->getVal(DConfig::PLAINTEXT_MAX_LENGTH)) {
476487
$tooLong++;
477488
continue;
478489
}
490+
479491
$hashFactory->mset($hashEntry, [Hash::PLAINTEXT => $plain, Hash::IS_CRACKED => 1, Hash::TIME_CRACKED => time()]);
480-
$crackedIn[$hashEntry->getHashlistId()]++;
492+
493+
if ($hashEntry->getIsCracked() != 1) {
494+
$newCracked++;
495+
$crackedIn[$hashEntry->getHashlistId()]++;
496+
}
497+
481498
if ($hashlist->getFormat() == DHashlistFormat::PLAIN) {
482499
$zaps[] = new Zap(null, $hashEntry->getHash(), time(), null, $hashlist->getId());
483500
}
484-
$newCracked++;
485501
}
486502
}
487503
$bufferCount++;

0 commit comments

Comments
 (0)