1 /**
2  * Author......: See docs/credits.txt
3  * License.....: MIT
4  */
5 
6 #include "common.h"
7 #include "types.h"
8 #include "memory.h"
9 #include "event.h"
10 #include "user_options.h"
11 #include "shared.h"
12 #include "pidfile.h"
13 #include "folder.h"
14 #include "restore.h"
15 
init_restore(hashcat_ctx_t * hashcat_ctx)16 static int init_restore (hashcat_ctx_t *hashcat_ctx)
17 {
18   restore_ctx_t *restore_ctx = hashcat_ctx->restore_ctx;
19 
20   restore_data_t *rd = (restore_data_t *) hcmalloc (sizeof (restore_data_t));
21 
22   restore_ctx->rd = rd;
23 
24   rd->version = RESTORE_VERSION_CUR;
25 
26   rd->argc = restore_ctx->argc;
27   rd->argv = restore_ctx->argv;
28 
29   if (getcwd (rd->cwd, 255) == NULL)
30   {
31     event_log_error (hashcat_ctx, "getcwd(): %s", strerror (errno));
32 
33     return -1;
34   }
35 
36   return 0;
37 }
38 
read_restore(hashcat_ctx_t * hashcat_ctx)39 static int read_restore (hashcat_ctx_t *hashcat_ctx)
40 {
41   restore_ctx_t   *restore_ctx   = hashcat_ctx->restore_ctx;
42   folder_config_t *folder_config = hashcat_ctx->folder_config;
43 
44   if (restore_ctx->enabled == false) return 0;
45 
46   char *eff_restore_file = restore_ctx->eff_restore_file;
47 
48   HCFILE fp;
49 
50   if (hc_fopen (&fp, eff_restore_file, "rb") == false)
51   {
52     event_log_error (hashcat_ctx, "Restore file '%s': %s", eff_restore_file, strerror (errno));
53 
54     return -1;
55   }
56 
57   restore_data_t *rd = restore_ctx->rd;
58 
59   if (hc_fread (rd, sizeof (restore_data_t), 1, &fp) != 1)
60   {
61     event_log_error (hashcat_ctx, "Cannot read %s", eff_restore_file);
62 
63     hc_fclose (&fp);
64 
65     return -1;
66   }
67 
68   // we only use these 2 checks to avoid "tainted string" warnings
69 
70   if (rd->argc < 1)
71   {
72     event_log_error (hashcat_ctx, "Unusually low number of arguments (argc) within restore file %s", eff_restore_file);
73 
74     hc_fclose (&fp);
75 
76     return -1;
77   }
78 
79   if (rd->argc > 250) // some upper bound check is always good (with some dirs/dicts it could be a large string)
80   {
81     event_log_error (hashcat_ctx, "Unusually high number of arguments (argc) within restore file %s", eff_restore_file);
82 
83     hc_fclose (&fp);
84 
85     return -1;
86   }
87 
88   rd->argv = (char **) hccalloc (rd->argc, sizeof (char *));
89 
90   char *buf = (char *) hcmalloc (HCBUFSIZ_LARGE);
91 
92   for (u32 i = 0; i < rd->argc; i++)
93   {
94     if (hc_fgets (buf, HCBUFSIZ_LARGE - 1, &fp) == NULL)
95     {
96       event_log_error (hashcat_ctx, "Cannot read %s", eff_restore_file);
97 
98       hc_fclose (&fp);
99 
100       hcfree (buf);
101 
102       return -1;
103     }
104 
105     size_t len = strlen (buf);
106 
107     if (len) buf[len - 1] = 0;
108 
109     rd->argv[i] = hcstrdup (buf);
110   }
111 
112   hcfree (buf);
113 
114   hc_fclose (&fp);
115 
116   if (hc_path_exist (rd->cwd) == false)
117   {
118     event_log_error (hashcat_ctx, "%s: %s", rd->cwd, strerror (errno));
119 
120     return -1;
121   }
122 
123   if (hc_path_is_directory (rd->cwd) == false)
124   {
125     event_log_error (hashcat_ctx, "%s: %s", rd->cwd, strerror (errno));
126 
127     return -1;
128   }
129 
130   if (strncmp (rd->cwd, folder_config->cwd, sizeof (rd->cwd)) != 0) // check if we need to change the current working directory
131   {
132     event_log_warning (hashcat_ctx, "Changing current working directory to '%s'", rd->cwd);
133     event_log_warning (hashcat_ctx, NULL);
134 
135     if (chdir (rd->cwd))
136     {
137       event_log_error (hashcat_ctx, "Directory '%s' needed to restore the session was not found.", rd->cwd);
138 
139       event_log_warning (hashcat_ctx, "Either create the directory, or update the directory within the .restore file.");
140       event_log_warning (hashcat_ctx, "Restore files can be analyzed and modified with analyze_hc_restore.pl:");
141       event_log_warning (hashcat_ctx, "    https://github.com/philsmd/analyze_hc_restore");
142       event_log_warning (hashcat_ctx, "Directory must contain all files and folders from the original command line.");
143       event_log_warning (hashcat_ctx, NULL);
144 
145       return -1;
146     }
147 
148     // if we are here, we also need to update the folder_config and .pid file:
149 
150     /**
151      * updated folders
152      */
153 
154     // copy the paths of INSTALL_FOLDER and SHARED_FOLDER from the folder config:
155 
156     char *install_folder = hcstrdup (folder_config->install_dir);
157     char *shared_folder  = hcstrdup (folder_config->shared_dir);
158 
159     folder_config_destroy (hashcat_ctx);
160 
161     const int rc_folder_config_init = folder_config_init (hashcat_ctx, install_folder, shared_folder);
162 
163     hcfree (install_folder);
164     hcfree (shared_folder);
165 
166     if (rc_folder_config_init == -1) return -1;
167 
168     /**
169      * updated pidfile
170      */
171 
172     pidfile_ctx_destroy (hashcat_ctx);
173 
174     if (pidfile_ctx_init (hashcat_ctx) == -1) return -1;
175   }
176 
177   return 0;
178 }
179 
write_restore(hashcat_ctx_t * hashcat_ctx)180 static int write_restore (hashcat_ctx_t *hashcat_ctx)
181 {
182   const mask_ctx_t     *mask_ctx     = hashcat_ctx->mask_ctx;
183   const restore_ctx_t  *restore_ctx  = hashcat_ctx->restore_ctx;
184   const status_ctx_t   *status_ctx   = hashcat_ctx->status_ctx;
185   const straight_ctx_t *straight_ctx = hashcat_ctx->straight_ctx;
186 
187   if (restore_ctx->enabled == false) return 0;
188 
189   restore_data_t *rd = restore_ctx->rd;
190 
191   rd->masks_pos = mask_ctx->masks_pos;
192   rd->dicts_pos = straight_ctx->dicts_pos;
193   rd->words_cur = status_ctx->words_cur;
194 
195   char *new_restore_file = restore_ctx->new_restore_file;
196 
197   HCFILE fp;
198 
199   if (hc_fopen (&fp, new_restore_file, "wb") == false)
200   {
201     event_log_error (hashcat_ctx, "%s: %s", new_restore_file, strerror (errno));
202 
203     return -1;
204   }
205 
206   if (setvbuf (fp.pfp, NULL, _IONBF, 0))
207   {
208     event_log_error (hashcat_ctx, "setvbuf file '%s': %s", new_restore_file, strerror (errno));
209 
210     hc_fclose (&fp);
211 
212     return -1;
213   }
214 
215   hc_fwrite (rd, sizeof (restore_data_t), 1, &fp);
216 
217   for (u32 i = 0; i < rd->argc; i++)
218   {
219     hc_fprintf (&fp, "%s", rd->argv[i]);
220 
221     hc_fputc ('\n', &fp);
222   }
223 
224   hc_fflush (&fp);
225 
226   hc_fsync (&fp);
227 
228   hc_fclose (&fp);
229 
230   rd->masks_pos = 0;
231   rd->dicts_pos = 0;
232   rd->words_cur = 0;
233 
234   return 0;
235 }
236 
cycle_restore(hashcat_ctx_t * hashcat_ctx)237 int cycle_restore (hashcat_ctx_t *hashcat_ctx)
238 {
239   restore_ctx_t *restore_ctx = hashcat_ctx->restore_ctx;
240 
241   if (restore_ctx->enabled == false) return 0;
242 
243   const char *eff_restore_file = restore_ctx->eff_restore_file;
244   const char *new_restore_file = restore_ctx->new_restore_file;
245 
246   if (write_restore (hashcat_ctx) == -1) return -1;
247 
248   if (hc_path_exist (eff_restore_file) == true)
249   {
250     if (unlink (eff_restore_file) == -1)
251     {
252       event_log_warning (hashcat_ctx, "Unlink file '%s': %s", eff_restore_file, strerror (errno));
253     }
254   }
255 
256   if (rename (new_restore_file, eff_restore_file) == -1)
257   {
258     event_log_warning (hashcat_ctx, "Rename file '%s' to '%s': %s", new_restore_file, eff_restore_file, strerror (errno));
259   }
260 
261   return 0;
262 }
263 
unlink_restore(hashcat_ctx_t * hashcat_ctx)264 void unlink_restore (hashcat_ctx_t *hashcat_ctx)
265 {
266   restore_ctx_t *restore_ctx = hashcat_ctx->restore_ctx;
267   status_ctx_t  *status_ctx  = hashcat_ctx->status_ctx;
268 
269   if (restore_ctx->enabled == false) return;
270 
271   if ((status_ctx->devices_status == STATUS_EXHAUSTED) && (status_ctx->run_thread_level1 == true)) // this is to check for [c]heckpoint
272   {
273     unlink (restore_ctx->eff_restore_file);
274     unlink (restore_ctx->new_restore_file);
275   }
276 
277   if (status_ctx->devices_status == STATUS_CRACKED)
278   {
279     unlink (restore_ctx->eff_restore_file);
280     unlink (restore_ctx->new_restore_file);
281   }
282 }
283 
restore_ctx_init(hashcat_ctx_t * hashcat_ctx,int argc,char ** argv)284 int restore_ctx_init (hashcat_ctx_t *hashcat_ctx, int argc, char **argv)
285 {
286   folder_config_t *folder_config = hashcat_ctx->folder_config;
287   restore_ctx_t   *restore_ctx   = hashcat_ctx->restore_ctx;
288   user_options_t  *user_options  = hashcat_ctx->user_options;
289 
290   restore_ctx->enabled = false;
291 
292   if (user_options->benchmark       == true) return 0;
293   if (user_options->hash_info       == true) return 0;
294   if (user_options->keyspace        == true) return 0;
295   if (user_options->left            == true) return 0;
296   if (user_options->backend_info    == true) return 0;
297   if (user_options->show            == true) return 0;
298   if (user_options->stdout_flag     == true) return 0;
299   if (user_options->speed_only      == true) return 0;
300   if (user_options->progress_only   == true) return 0;
301   if (user_options->usage           == true) return 0;
302   if (user_options->version         == true) return 0;
303   if (user_options->identify        == true) return 0;
304   if (user_options->restore_disable == true) return 0;
305 
306   if (argc ==    0) return 0;
307   if (argv == NULL) return 0;
308 
309   if (user_options->restore_file_path == NULL)
310   {
311     hc_asprintf (&restore_ctx->eff_restore_file, "%s/%s.restore",     folder_config->session_dir, user_options->session);
312     hc_asprintf (&restore_ctx->new_restore_file, "%s/%s.restore.new", folder_config->session_dir, user_options->session);
313   }
314   else
315   {
316     restore_ctx->eff_restore_file = hcstrdup (user_options->restore_file_path);
317     hc_asprintf (&restore_ctx->new_restore_file, "%s.new", user_options->restore_file_path);
318   }
319 
320   restore_ctx->argc = argc;
321   restore_ctx->argv = argv;
322 
323   if (init_restore (hashcat_ctx) == -1) return -1;
324 
325   restore_ctx->enabled = true;
326 
327   restore_ctx->restore_execute = false;
328 
329   if (user_options->restore == true)
330   {
331     if (read_restore (hashcat_ctx) == -1) return -1;
332 
333     restore_data_t *rd = restore_ctx->rd;
334 
335     if (rd->version < RESTORE_VERSION_MIN)
336     {
337       event_log_error (hashcat_ctx, "Incompatible restore-file version.");
338 
339       return -1;
340     }
341 
342     user_options_init (hashcat_ctx);
343 
344     if (user_options_getopt (hashcat_ctx, rd->argc, rd->argv) == -1) return -1;
345 
346     restore_ctx->restore_execute = true;
347   }
348 
349   return 0;
350 }
351 
restore_ctx_destroy(hashcat_ctx_t * hashcat_ctx)352 void restore_ctx_destroy (hashcat_ctx_t *hashcat_ctx)
353 {
354   restore_ctx_t *restore_ctx = hashcat_ctx->restore_ctx;
355 
356   if (restore_ctx->enabled == false) return;
357 
358   hcfree (restore_ctx->eff_restore_file);
359   hcfree (restore_ctx->new_restore_file);
360   hcfree (restore_ctx->rd);
361 
362   memset (restore_ctx, 0, sizeof (restore_ctx_t));
363 }
364