-
Notifications
You must be signed in to change notification settings - Fork 246
Implemented searchHashes helper #1647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8e8f0bd
added searchHashes helper
s3inlc 7eac5f7
removed typo
s3inlc a72ca69
Update src/inc/apiv2/helper/searchHashes.routes.php
s3inlc 2111372
Update src/inc/apiv2/helper/searchHashes.routes.php
s3inlc c49ef30
Update src/inc/apiv2/helper/searchHashes.routes.php
s3inlc 6573e1a
Update src/inc/apiv2/helper/searchHashes.routes.php
s3inlc 360f860
fixed user retrieval for the helper
s3inlc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| <?php | ||
|
|
||
| use DBA\ContainFilter; | ||
| use DBA\Factory; | ||
| use DBA\Hash; | ||
| use DBA\Hashlist; | ||
| use DBA\JoinFilter; | ||
| use DBA\LikeFilterInsensitive; | ||
| use DBA\QueryFilter; | ||
|
|
||
| require_once(dirname(__FILE__) . "/../common/AbstractHelperAPI.class.php"); | ||
|
|
||
| class SearchHashesHelperAPI extends AbstractHelperAPI { | ||
| public static function getBaseUri(): string { | ||
| return "/api/v2/helper/searchHashes"; | ||
| } | ||
|
|
||
| public static function getAvailableMethods(): array { | ||
| return ['POST']; | ||
| } | ||
|
|
||
| public function getRequiredPermissions(string $method): array { | ||
| return [Hashlist::PERM_READ, Hash::PERM_READ]; | ||
| } | ||
|
|
||
| public function getFormFields(): array { | ||
| return [ | ||
| "searchData" => ["type" => "str"], # base64 encoded search input | ||
| "separator" => ['type' => 'str'], | ||
| "isSalted" => ['type' => 'bool'], | ||
| ]; | ||
| } | ||
|
|
||
| public static function getResponse(): array { | ||
| return [ | ||
| ["found" => false, | ||
| "query" => "12345678", | ||
| ], | ||
| ["found" => true, | ||
| "query" => "54321", | ||
| "matches" => [[ | ||
| "hashlistId" => 4, | ||
| "hash" => "5432173922", | ||
| "salt" => "", | ||
| "plaintext" => "plain", | ||
| "timeCracked" => 0, | ||
| "chunkId" => null, | ||
| "isCracked" => true, | ||
| "crackPos" => 0, | ||
| ], | ||
| [ | ||
| "hashlistId" => 4, | ||
| "hash" => "12345654321", | ||
| "salt" => "", | ||
| "plaintext" => "", | ||
| "timeCracked" => 0, | ||
| "chunkId" => null, | ||
| "isCracked" => false, | ||
| "crackPos" => 0, | ||
| ] | ||
| ], | ||
| ] | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Endpoint to search for hashes in accessible hashlists. | ||
| * @throws HttpError | ||
| */ | ||
| public function actionPost($data): object|array|null { | ||
| $search = base64_decode($data['searchData']); | ||
| $isSalted = $data['isSalted']; | ||
| $separator = $data['separator']; | ||
|
|
||
| if (strlen($search) == 0) { | ||
| throw new HttpError("Search query cannot be empty!"); | ||
| } | ||
| else if ($search === false) { | ||
| throw new HttpError("Search query is not valid base64!"); | ||
| } | ||
| else if ($isSalted && strlen($separator) == 0) { | ||
| throw new HttpError("Salt separator cannot be empty!"); | ||
| } | ||
|
|
||
| $search = str_replace("\r\n", "\n", $search); | ||
| $search = explode("\n", $search); | ||
| $resultEntries = array(); | ||
| $userHashlists = HashlistUtils::getHashlists(self::getCurrentUser(), false); | ||
| $userHashlists += HashlistUtils::getHashlists(self::getCurrentUser(), true); | ||
| foreach ($search as $searchEntry) { | ||
| if (strlen($searchEntry) == 0) { | ||
| continue; | ||
| } | ||
|
|
||
| // test if hash contains salt | ||
| if ($isSalted) { | ||
| $split = explode($separator, $searchEntry); | ||
| $hash = $split[0]; | ||
| unset($split[0]); | ||
| $salt = implode($separator, $split); | ||
| } | ||
| else { | ||
| $hash = $searchEntry; | ||
| $salt = ""; | ||
| } | ||
|
|
||
| // TODO: add option to select if exact match or like match | ||
|
|
||
| $filters = array(); | ||
| $filters[] = new LikeFilterInsensitive(Hash::HASH, "%" . $hash . "%"); | ||
| $filters[] = new ContainFilter(Hash::HASHLIST_ID, Util::arrayOfIds($userHashlists), Factory::getHashFactory()); | ||
| if (strlen($salt) > 0) { | ||
| $filters[] = new QueryFilter(Hash::SALT, $salt, "="); | ||
| } | ||
| $jF = new JoinFilter(Factory::getHashlistFactory(), Hash::HASHLIST_ID, Hashlist::HASHLIST_ID); | ||
| $joined = Factory::getHashFactory()->filter([Factory::FILTER => $filters, Factory::JOIN => $jF]); | ||
|
|
||
| $qF1 = new LikeFilterInsensitive(Hash::PLAINTEXT, "%" . $searchEntry . "%"); | ||
| $qF2 = new ContainFilter(Hash::HASHLIST_ID, Util::arrayOfIds($userHashlists), Factory::getHashFactory()); | ||
| $joined2 = Factory::getHashFactory()->filter([Factory::FILTER => [$qF1, $qF2], Factory::JOIN => $jF]); | ||
| /** @var $hashes Hash[] */ | ||
| $hashes = $joined2[Factory::getHashFactory()->getModelName()]; | ||
| for ($i = 0; $i < sizeof($hashes); $i++) { | ||
s3inlc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $joined[Factory::getHashFactory()->getModelName()][] = $joined2[Factory::getHashFactory()->getModelName()][$i]; | ||
| $joined[Factory::getHashlistFactory()->getModelName()][] = $joined2[Factory::getHashlistFactory()->getModelName()][$i]; | ||
| } | ||
|
|
||
| $resultEntry = []; | ||
| /** @var $hashes Hash[] */ | ||
| $hashes = $joined[Factory::getHashFactory()->getModelName()]; | ||
| if (empty($hashes)) { | ||
| $resultEntry["found"] = false; | ||
| $resultEntry["query"] = $searchEntry; | ||
| } | ||
| else { | ||
| $resultEntry["found"] = true; | ||
| $resultEntry["query"] = $searchEntry; | ||
| $matches = []; | ||
| for ($i = 0; $i < sizeof($hashes); $i++) { | ||
s3inlc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** @var $hash Hash */ | ||
| $hash = $joined[Factory::getHashFactory()->getModelName()][$i]; | ||
| $matches[] = $hash; | ||
| } | ||
| $resultEntry["matches"] = $matches; | ||
| } | ||
| $resultEntries[] = $resultEntry; | ||
| } | ||
| return $resultEntries; | ||
| } | ||
| } | ||
|
|
||
| SearchHashesHelperAPI::register($app); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.