forked from hashcat/hashcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.c
341 lines (249 loc) · 8.59 KB
/
monitor.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
* Author......: See docs/credits.txt
* License.....: MIT
*/
#include "common.h"
#include "types.h"
#include "event.h"
#include "hwmon.h"
#include "timer.h"
#include "hashes.h"
#include "thread.h"
#include "restore.h"
#include "status.h"
#include "monitor.h"
int get_runtime_left (const hashcat_ctx_t *hashcat_ctx)
{
const status_ctx_t *status_ctx = hashcat_ctx->status_ctx;
const user_options_t *user_options = hashcat_ctx->user_options;
double msec_paused = status_ctx->msec_paused;
if (status_ctx->devices_status == STATUS_PAUSED)
{
double msec_paused_tmp = hc_timer_get (status_ctx->timer_paused);
msec_paused += msec_paused_tmp;
}
time_t runtime_cur;
time (&runtime_cur);
const int runtime_left = (int) (status_ctx->runtime_start
+ user_options->runtime
+ (msec_paused / 1000)
- runtime_cur);
return runtime_left;
}
static int monitor (hashcat_ctx_t *hashcat_ctx)
{
hashes_t *hashes = hashcat_ctx->hashes;
hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx;
backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx;
restore_ctx_t *restore_ctx = hashcat_ctx->restore_ctx;
status_ctx_t *status_ctx = hashcat_ctx->status_ctx;
user_options_t *user_options = hashcat_ctx->user_options;
bool runtime_check = false;
bool remove_check = false;
bool status_check = false;
bool restore_check = false;
bool hwmon_check = false;
bool performance_check = false;
const int sleep_time = 1;
const double exec_low = 50.0; // in ms
const double util_low = 90.0; // in percent
if (user_options->runtime)
{
runtime_check = true;
}
if (restore_ctx->enabled == true)
{
restore_check = true;
}
if ((user_options->remove == true) && ((hashes->hashlist_mode == HL_MODE_FILE_PLAIN) || (hashes->hashlist_mode == HL_MODE_FILE_BINARY)))
{
remove_check = true;
}
if (user_options->status == true)
{
status_check = true;
}
if (hwmon_ctx->enabled == true)
{
hwmon_check = true;
}
if (hwmon_ctx->enabled == true)
{
performance_check = true; // this check simply requires hwmon to work
}
if ((runtime_check == false) && (remove_check == false) && (status_check == false) && (restore_check == false) && (hwmon_check == false) && (performance_check == false))
{
return 0;
}
// timer
u32 slowdown_warnings = 0;
u32 performance_warnings = 0;
u32 restore_left = user_options->restore_timer;
u32 remove_left = user_options->remove_timer;
u32 status_left = user_options->status_timer;
while (status_ctx->shutdown_inner == false)
{
sleep (sleep_time);
if (status_ctx->devices_status == STATUS_INIT) continue;
if (hwmon_ctx->enabled == true)
{
hc_thread_mutex_lock (status_ctx->mux_hwmon);
for (int backend_devices_idx = 0; backend_devices_idx < backend_ctx->backend_devices_cnt; backend_devices_idx++)
{
hc_device_param_t *device_param = &backend_ctx->devices_param[backend_devices_idx];
if (device_param->skipped == true) continue;
if ((backend_ctx->devices_param[backend_devices_idx].opencl_device_type & CL_DEVICE_TYPE_GPU) == 0) continue;
const int temperature = hm_get_temperature_with_devices_idx (hashcat_ctx, backend_devices_idx);
if (temperature > (int) user_options->hwmon_temp_abort)
{
EVENT_DATA (EVENT_MONITOR_TEMP_ABORT, &backend_devices_idx, sizeof (int));
myabort (hashcat_ctx);
}
}
for (int backend_devices_idx = 0; backend_devices_idx < backend_ctx->backend_devices_cnt; backend_devices_idx++)
{
hc_device_param_t *device_param = &backend_ctx->devices_param[backend_devices_idx];
if (device_param->skipped == true) continue;
if (device_param->skipped_warning == true) continue;
const int rc_throttle = hm_get_throttle_with_devices_idx (hashcat_ctx, backend_devices_idx);
if (rc_throttle == -1) continue;
if (rc_throttle > 0)
{
slowdown_warnings++;
if (slowdown_warnings == 1) EVENT_DATA (EVENT_MONITOR_THROTTLE1, &backend_devices_idx, sizeof (int));
if (slowdown_warnings == 2) EVENT_DATA (EVENT_MONITOR_THROTTLE2, &backend_devices_idx, sizeof (int));
if (slowdown_warnings == 3) EVENT_DATA (EVENT_MONITOR_THROTTLE3, &backend_devices_idx, sizeof (int));
}
else
{
if (slowdown_warnings > 0) slowdown_warnings--;
}
}
hc_thread_mutex_unlock (status_ctx->mux_hwmon);
}
if (restore_check == true)
{
restore_left--;
if (restore_left == 0)
{
const int rc = cycle_restore (hashcat_ctx);
if (rc == -1) return -1;
restore_left = user_options->restore_timer;
}
}
if ((runtime_check == true) && (status_ctx->runtime_start > 0))
{
const int runtime_left = get_runtime_left (hashcat_ctx);
if (runtime_left <= 0)
{
EVENT_DATA (EVENT_MONITOR_RUNTIME_LIMIT, NULL, 0);
myabort_runtime (hashcat_ctx);
}
}
if (remove_check == true)
{
remove_left--;
if (remove_left == 0)
{
if (hashes->digests_saved != hashes->digests_done)
{
hashes->digests_saved = hashes->digests_done;
const int rc = save_hash (hashcat_ctx);
if (rc == -1) return -1;
}
remove_left = user_options->remove_timer;
}
}
if (status_check == true)
{
status_left--;
if (status_left == 0)
{
hc_thread_mutex_lock (status_ctx->mux_display);
EVENT_DATA (EVENT_MONITOR_STATUS_REFRESH, NULL, 0);
hc_thread_mutex_unlock (status_ctx->mux_display);
status_left = user_options->status_timer;
}
}
if (performance_check == true)
{
int exec_cnt = 0;
int util_cnt = 0;
double exec_total = 0;
double util_total = 0;
hc_thread_mutex_lock (status_ctx->mux_hwmon);
for (int backend_devices_idx = 0; backend_devices_idx < backend_ctx->backend_devices_cnt; backend_devices_idx++)
{
hc_device_param_t *device_param = &backend_ctx->devices_param[backend_devices_idx];
if (device_param->skipped == true) continue;
if (device_param->skipped_warning == true) continue;
exec_cnt++;
const double exec = status_get_exec_msec_dev (hashcat_ctx, backend_devices_idx);
exec_total += exec;
const int util = hm_get_utilization_with_devices_idx (hashcat_ctx, backend_devices_idx);
if (util == -1) continue;
util_total += (double) util;
util_cnt++;
}
hc_thread_mutex_unlock (status_ctx->mux_hwmon);
double exec_avg = 0;
double util_avg = 0;
if (exec_cnt > 0) exec_avg = exec_total / exec_cnt;
if (util_cnt > 0) util_avg = util_total / util_cnt;
if ((exec_avg > 0) && (exec_avg < exec_low))
{
performance_warnings++;
if (performance_warnings == 10) EVENT_DATA (EVENT_MONITOR_PERFORMANCE_HINT, NULL, 0);
}
if ((util_avg > 0) && (util_avg < util_low))
{
performance_warnings++;
if (performance_warnings == 10) EVENT_DATA (EVENT_MONITOR_PERFORMANCE_HINT, NULL, 0);
}
}
// stdin read timeout check
// note: we skip the stdin timeout check if it was disabled with stdin_timeout_abort set to 0
if (user_options->stdin_timeout_abort != 0)
{
if (status_get_progress_done (hashcat_ctx) == 0)
{
if (status_ctx->stdin_read_timeout_cnt > 0)
{
if (status_ctx->stdin_read_timeout_cnt >= user_options->stdin_timeout_abort)
{
EVENT_DATA (EVENT_MONITOR_NOINPUT_ABORT, NULL, 0);
myabort (hashcat_ctx);
status_ctx->shutdown_inner = true;
break;
}
if ((status_ctx->stdin_read_timeout_cnt % STDIN_TIMEOUT_WARN) == 0)
{
EVENT_DATA (EVENT_MONITOR_NOINPUT_HINT, NULL, 0);
}
}
}
}
}
// final round of save_hash
if (remove_check == true)
{
if (hashes->digests_saved != hashes->digests_done)
{
const int rc = save_hash (hashcat_ctx);
if (rc == -1) return -1;
}
}
// final round of cycle_restore
if (restore_check == true)
{
const int rc = cycle_restore (hashcat_ctx);
if (rc == -1) return -1;
}
return 0;
}
HC_API_CALL void *thread_monitor (void *p)
{
hashcat_ctx_t *hashcat_ctx = (hashcat_ctx_t *) p;
monitor (hashcat_ctx); // we should give back some useful returncode
return NULL;
}