-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathtest_hashcat_runtime.py
More file actions
144 lines (119 loc) · 4.55 KB
/
test_hashcat_runtime.py
File metadata and controls
144 lines (119 loc) · 4.55 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
import pytest
from unittest import mock
import unittest
from unittest.mock import MagicMock
import os
import subprocess
import shutil
import requests
import json
from pathlib import Path
from argparse import Namespace
import sys
import datetime
from io import BytesIO
from htpclient.hashcat_cracker import HashcatCracker
from htpclient.binarydownload import BinaryDownload
from htpclient.session import Session
from htpclient.config import Config
from htpclient.initialize import Initialize
from htpclient.chunk import Chunk
from htpclient.hashlist import Hashlist
from htpclient.task import Task
from htpclient.dicts import copy_and_set_token
from htpclient.dicts import dict_sendBenchmark
from htpclient.jsonRequest import JsonRequest
from htpclient.files import Files
from tests.hashtopolis import Hashlist as Hashlist_v2
from tests.hashtopolis import Task as Task_v2
from tests.hashtopolis import FileImport as FileImport_v2
from tests.hashtopolis import File as File_v2
class HashcatRuntime(unittest.TestCase):
@mock.patch('subprocess.Popen', side_effect=subprocess.Popen)
@mock.patch('subprocess.check_output', side_effect=subprocess.check_output)
def test_runtime_windows(self, mock_check_output, moch_popen):
if sys.platform != 'win32':
return
# Setup session object
session = Session(requests.Session()).s
session.headers.update({'User-Agent': Initialize.get_version()})
# Create hashlist
p = Path(__file__).parent.joinpath('create_hashlist_001.json')
payload = json.loads(p.read_text('UTF-8'))
hashlist_v2 = Hashlist_v2(**payload)
hashlist_v2.save()
# Create Task
for p in sorted(Path(__file__).parent.glob('create_task_002.json')):
payload = json.loads(p.read_text('UTF-8'))
payload['hashlistId'] = int(hashlist_v2._id)
obj = Task_v2(**payload)
obj.save()
# Cmd parameters setup
test_args = Namespace( cert=None, cpu_only=False, crackers_path=None, de_register=False, debug=True, disable_update=False, files_path=None, hashlists_path=None, number_only=False, preprocessors_path=None, url='http://hashtopolis/api/server.php', version=False, voucher='devvoucher', zaps_path=None, max_log_size=5_000_000, max_log_backups=5)
# Try to download cracker 1
cracker_id = 1
config = Config()
crackers_path = config.get_value('crackers-path')
binaryDownload = BinaryDownload(test_args)
binaryDownload.check_version(cracker_id)
# --version
cracker = HashcatCracker(1, binaryDownload)
# --keyspace
chunk = Chunk()
task = Task()
task.load_task()
hashlist = Hashlist()
hashlist.load_hashlist(task.get_task()['hashlistId'])
hashlist_id = task.get_task()['hashlistId']
hashlists_path = config.get_value('hashlists-path')
cracker.measure_keyspace(task, chunk)
full_cmd = f'"hashcat.exe" --keyspace --quiet -a3 ?l?l?l?l --hash-type=0 '
mock_check_output.assert_called_with(
full_cmd,
shell=True,
cwd=Path(crackers_path, str(cracker_id)),
stderr=-2
)
# benchmark
hashlist_path = Path(hashlists_path, str(hashlist_id))
hashlist_out_path = Path(hashlists_path, f'{hashlist_id}.out')
result = cracker.run_benchmark(task.get_task())
assert result != 0
full_cmd = [
'"hashcat.exe"',
'--machine-readable',
'--quiet',
'--runtime=30',
'--restore-disable',
'--potfile-disable',
'--session=hashtopolis',
'-p',
'"\t"',
f' "{hashlist_path}"',
'-a3 ?l?l?l?l',
' --hash-type=0 ',
'-o',
f'"{hashlist_out_path}"'
]
full_cmd = ' '.join(full_cmd)
moch_popen.assert_called_with(
full_cmd,
shell=True,
cwd=Path(crackers_path, str(cracker_id)),
stdout=-1,
stderr=-1
)
task_id = task.get_task()['taskId']
# Sending benchmark to server
query = copy_and_set_token(dict_sendBenchmark, config.get_value('token'))
query['taskId'] = task_id
query['result'] = result
query['type'] = task.get_task()['benchType']
req = JsonRequest(query)
req.execute()
assert chunk.get_chunk(task_id) == 1
# Cleanup
obj.delete()
hashlist_v2.delete()
if __name__ == '__main__':
unittest.main()