forked from hashcat/hashcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinduct.c
153 lines (117 loc) · 3.9 KB
/
induct.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
/**
* Author......: See docs/credits.txt
* License.....: MIT
*/
#include "common.h"
#include "types.h"
#include "memory.h"
#include "event.h"
#include "folder.h"
#include "shared.h"
#include "induct.h"
static int sort_by_mtime (const void *p1, const void *p2)
{
const char* const *f1 = (const char* const *) p1;
const char* const *f2 = (const char* const *) p2;
struct stat s1;
struct stat s2;
const int rc1 = stat (*f1, &s1);
const int rc2 = stat (*f2, &s2);
if (rc1 < rc2) return 1;
if (rc1 > rc2) return -1;
if (s1.st_mtime < s2.st_mtime) return 1;
if (s1.st_mtime > s2.st_mtime) return -1;
return 0;
}
int induct_ctx_init (hashcat_ctx_t *hashcat_ctx)
{
folder_config_t *folder_config = hashcat_ctx->folder_config;
induct_ctx_t *induct_ctx = hashcat_ctx->induct_ctx;
user_options_t *user_options = hashcat_ctx->user_options;
induct_ctx->enabled = false;
if (user_options->usage > 0) return 0;
if (user_options->backend_info > 0) return 0;
if (user_options->benchmark == true) return 0;
if (user_options->hash_info == true) return 0;
if (user_options->keyspace == true) return 0;
if (user_options->left == true) return 0;
if (user_options->show == true) return 0;
if (user_options->stdout_flag == true) return 0;
if (user_options->speed_only == true) return 0;
if (user_options->progress_only == true) return 0;
if (user_options->version == true) return 0;
if (user_options->identify == true) return 0;
if ((user_options->attack_mode != ATTACK_MODE_STRAIGHT)
&& (user_options->attack_mode != ATTACK_MODE_ASSOCIATION)) return 0;
induct_ctx->enabled = true;
if (user_options->induction_dir == NULL)
{
char *root_directory;
hc_asprintf (&root_directory, "%s/%s.%s", folder_config->session_dir, user_options->session, INDUCT_DIR);
if (rmdir (root_directory) == -1)
{
if (errno == ENOENT)
{
// good, we can ignore
}
else if (errno == ENOTEMPTY)
{
char *root_directory_mv;
hc_asprintf (&root_directory_mv, "%s/%s.induct.%d", folder_config->session_dir, user_options->session, (int) time (NULL));
if (rename (root_directory, root_directory_mv) != 0)
{
event_log_error (hashcat_ctx, "Rename directory %s to %s: %s", root_directory, root_directory_mv, strerror (errno));
return -1;
}
hcfree (root_directory_mv);
}
else
{
event_log_error (hashcat_ctx, "%s: %s", root_directory, strerror (errno));
return -1;
}
}
if (hc_mkdir (root_directory, 0700) == -1)
{
event_log_error (hashcat_ctx, "%s: %s", root_directory, strerror (errno));
return -1;
}
induct_ctx->root_directory = root_directory;
}
else
{
induct_ctx->root_directory = hcstrdup (user_options->induction_dir);
}
return 0;
}
void induct_ctx_scan (hashcat_ctx_t *hashcat_ctx)
{
induct_ctx_t *induct_ctx = hashcat_ctx->induct_ctx;
if (induct_ctx->enabled == false) return;
induct_ctx->induction_dictionaries = scan_directory (induct_ctx->root_directory);
induct_ctx->induction_dictionaries_cnt = count_dictionaries (induct_ctx->induction_dictionaries);
qsort (induct_ctx->induction_dictionaries, (size_t) induct_ctx->induction_dictionaries_cnt, sizeof (char *), sort_by_mtime);
}
void induct_ctx_destroy (hashcat_ctx_t *hashcat_ctx)
{
induct_ctx_t *induct_ctx = hashcat_ctx->induct_ctx;
if (induct_ctx->enabled == false) return;
if (rmdir (induct_ctx->root_directory) == -1)
{
if (errno == ENOENT)
{
// good, we can ignore
}
else if (errno == ENOTEMPTY)
{
// good, we can ignore
}
else
{
event_log_error (hashcat_ctx, "%s: %s", induct_ctx->root_directory, strerror (errno));
//return -1;
}
}
hcfree (induct_ctx->root_directory);
memset (induct_ctx, 0, sizeof (induct_ctx_t));
}