Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions htpclient/hashcat_cracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,48 @@ def select_cache_arg(cache_path, version_string, isdir=os.path.isdir):
return [f'{CACHE_PATH_FLAG}="{cache_path}"']


# What one line from a producer is worth in the chunk's keyspace.
STREAM_DIRECT = 'direct' # no producer, hashcat reads the attack itself
STREAM_KEYSPACE = 'keyspace' # one keyspace unit per line
STREAM_CANDIDATE = 'candidate' # a finished candidate, rules already expanded


def stream_unit(task, use_pipe):
"""Which producer feeds the hashcat that reports progress, in run_chunk's order of preference.

A preprocessor is given the chunk's window and emits one line per keyspace unit, so hashcat's
restore point is an offset into the chunk. A self pipe is "hashcat --stdout" instead, which
expands rules and mask positions, so one line is a finished candidate and says nothing about
how far into the chunk the run has got.
"""
if ('usePrince' in task and task['usePrince']) or ('usePreprocessor' in task and task['usePreprocessor']):
return STREAM_KEYSPACE
if use_pipe:
return STREAM_CANDIDATE
return STREAM_DIRECT


def keyspace_progress(curku, skip, length, stream):
"""The chunk checkpoint to report to the server, in task keyspace units.

hashcat's CURKU is its restore point. A direct run was given the chunk's --skip, so that point
is already a position in the task's keyspace. A run behind a producer was not: the producer was
given the window instead, so hashcat counts the stream it is fed from zero and the chunk's own
offset has to be added back. hashcat before 8.0 had no restore point for a pipe at all and
reported 0, which leaves the chunk start, as it did before.

A stream of candidates passes the chunk's length long before the chunk is done, so its restore
point cannot be placed in the chunk at all. Staying at the chunk start makes the server redo an
interrupted chunk rather than skip the part hashcat never reached.
"""
if stream == STREAM_DIRECT:
return curku
if stream == STREAM_CANDIDATE:
return skip
# The clamp guards against a producer that emits more than its window.
return skip + min(curku, length)


class HashcatCracker:
def __init__(self, cracker_id, binary_download):
self.config = Config()
Expand Down Expand Up @@ -364,6 +406,7 @@ def run_loop(self, proc, chunk, task):
# read is assumed to be a recent custom build, as get_outfile_format() does.
version = parse_version(self.version_string)
progress_includes_skip = version is not None and version < (7, 1, 2, 546)
stream = stream_unit(task, self.usePipe)
while True:
try:
# Block for 1 second.
Expand Down Expand Up @@ -448,9 +491,7 @@ def run_loop(self, proc, chunk, task):
self.cracks = new_cracks
query = copy_and_set_token(dict_sendProgress, self.config.get_value('token'))
query['chunkId'] = chunk['chunkId']
query['keyspaceProgress'] = status.get_curku()
if (self.usePipe or 'usePrince' in task and task['usePrince'] or 'usePreprocessor' in task and task['usePreprocessor']) and status.get_curku() == 0:
query['keyspaceProgress'] = chunk['skip']
query['keyspaceProgress'] = keyspace_progress(status.get_curku(), chunk['skip'], chunk['length'], stream)
query['relativeProgress'] = relative_progress
query['speed'] = speed
query['state'] = status.get_state()
Expand Down
63 changes: 63 additions & 0 deletions tests/test_keyspace_progress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import unittest
from htpclient.hashcat_cracker import (
STREAM_CANDIDATE,
STREAM_DIRECT,
STREAM_KEYSPACE,
keyspace_progress,
stream_unit,
)

SKIP = 5000000
LENGTH = 100000


class TestStreamUnit(unittest.TestCase):
def test_a_plain_task_runs_without_a_producer(self):
self.assertEqual(stream_unit({}, False), STREAM_DIRECT)

def test_a_preprocessor_emits_keyspace_units(self):
self.assertEqual(stream_unit({'usePreprocessor': 1}, False), STREAM_KEYSPACE)
self.assertEqual(stream_unit({'usePrince': True}, False), STREAM_KEYSPACE)

def test_a_self_pipe_emits_candidates(self):
self.assertEqual(stream_unit({}, True), STREAM_CANDIDATE)

def test_a_preprocessor_wins_over_the_pipe_flag(self):
# run_chunk prefers the preprocessor command, so the producer is the preprocessor
self.assertEqual(stream_unit({'usePreprocessor': 1}, True), STREAM_KEYSPACE)
self.assertEqual(stream_unit({'usePrince': True}, True), STREAM_KEYSPACE)


class TestKeyspaceProgress(unittest.TestCase):
def test_direct_run_reports_curku_as_it_stands(self):
# hashcat was given the chunk's --skip, so its restore point is already a task position
self.assertEqual(keyspace_progress(SKIP, SKIP, LENGTH, STREAM_DIRECT), SKIP)
self.assertEqual(keyspace_progress(SKIP + 2500, SKIP, LENGTH, STREAM_DIRECT), SKIP + 2500)
self.assertEqual(keyspace_progress(0, SKIP, LENGTH, STREAM_DIRECT), 0)

def test_keyspace_stream_on_hashcat_without_a_restore_point(self):
# hashcat before 8.0 reported CURKU 0 for the whole of a piped run
self.assertEqual(keyspace_progress(0, SKIP, LENGTH, STREAM_KEYSPACE), SKIP)

def test_keyspace_stream_adds_the_chunk_offset(self):
# the producer was given the window, so CURKU counts the stream from its start
self.assertEqual(keyspace_progress(1200, SKIP, LENGTH, STREAM_KEYSPACE), SKIP + 1200)
self.assertEqual(keyspace_progress(LENGTH, SKIP, LENGTH, STREAM_KEYSPACE), SKIP + LENGTH)

def test_keyspace_stream_past_the_end_of_the_chunk_is_clamped(self):
self.assertEqual(keyspace_progress(LENGTH * 7, SKIP, LENGTH, STREAM_KEYSPACE), SKIP + LENGTH)

def test_candidate_stream_stays_at_the_chunk_start(self):
# with two rules CURKU reaches the chunk's length half way through the chunk, so it cannot
# be placed in the chunk; reporting the end would let the server skip the untried half
for curku in (0, 1200, LENGTH, LENGTH * 2):
with self.subTest(curku=curku):
self.assertEqual(keyspace_progress(curku, SKIP, LENGTH, STREAM_CANDIDATE), SKIP)

def test_first_chunk_of_a_task(self):
self.assertEqual(keyspace_progress(0, 0, LENGTH, STREAM_KEYSPACE), 0)
self.assertEqual(keyspace_progress(900, 0, LENGTH, STREAM_KEYSPACE), 900)


if __name__ == '__main__':
unittest.main()
Loading