Created
July 16, 2019 17:07
-
-
Save HacKanCuBa/8c0505f3bbeda021fb3d8bdcb1da9c0b to your computer and use it in GitHub Desktop.
Bruteforce a numeric ID from a SHA256 hash using every CPU core available
This file contains 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
#!/usr/bin/env python3 | |
# *************************************************************************** | |
# Bruteforce a numeric ID from a SHA256 hash. | |
# Copyright (C) <2019> <Ivan Ariel Barrera Oro> | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# | |
# *************************************************************************** | |
"""Bruteforce a numeric ID from a SHA256 hash using every CPU core available.""" | |
from hashlib import sha256 | |
from multiprocessing import cpu_count, Process, Value | |
from time import time | |
from typing import List | |
# NIDs with more than 40bits might take a LONG while using CPU solely. | |
NID = 5204238009 # norah jones | |
HASH_TO_CRACK = sha256(str(NID).encode()).hexdigest() | |
INITIAL_BRUTEFORCE_VALUE = 5200000000 # just a demo | |
def _crack_parallel(hash_value: str, nid_initial: int, nid_found: Value, | |
nid_delta: int = 10000000) -> None: | |
candidate = nid_initial | |
nid_end = nid_initial + nid_delta | |
while candidate < nid_end and not nid_found.value: | |
if sha256(str(candidate).encode()).hexdigest() == hash_value: | |
nid_found.value = candidate | |
break | |
candidate += 1 | |
def crack_sha256_nid(hash_value: str, initial: int = 1) -> int: | |
"""Bruteforce a numeric ID hashed with SHA256. | |
A numeric ID is an integer bigger than 0. It sequentially tries to match | |
the hash with the generated NID. | |
:param hash_value: The SHA256 hash to crack. | |
:param initial: [optional] Initial NID value (defaults to 0). | |
:return: Cracked NID. | |
""" | |
def create_process(*args): | |
new_proc = Process(target=_crack_parallel, args=args) | |
print(new_proc.name, '- Current NID:', args[1], '-', | |
f'{time() - time_start:.2f}', 'seconds') | |
return new_proc | |
nid_found = Value('Q', 0) # type: Value | |
nid_candidate = initial # type: int | |
nid_delta = 10000000 # type: int | |
time_start = time() # type: float | |
processes = [] # type: List[Process] | |
for _ in range(cpu_count()): | |
proc = create_process(hash_value, nid_candidate, nid_found, nid_delta) | |
processes.append(proc) | |
nid_candidate += nid_delta | |
proc.start() | |
while not nid_found.value: | |
proc = processes.pop(0) | |
proc.join() | |
proc = create_process(hash_value, nid_candidate, nid_found, nid_delta) | |
processes.append(proc) | |
nid_candidate += nid_delta | |
proc.start() | |
return nid_found.value | |
def main(): | |
"""Crack a NID from a hash.""" | |
print('Hash to crack:', HASH_TO_CRACK) | |
print('Cracking... Press CTRL+C to stop', flush=True) | |
time_start = time() | |
nid_found = crack_sha256_nid(HASH_TO_CRACK, INITIAL_BRUTEFORCE_VALUE) | |
time_end = time() | |
print('Numeric ID found in', f'{time_end - time_start:.2f}', 'seconds!') | |
print('Cracked NID:', nid_found, '- Validation:', NID, '==', nid_found, | |
'is', NID == nid_found) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment