1 /**
2  * Author......: See docs/credits.txt
3  * License.....: MIT
4  */
5 
6 #include "common.h"
7 #include "types.h"
8 #include "event.h"
9 #include "locking.h"
10 #include "emu_inc_rp.h"
11 #include "emu_inc_rp_optimized.h"
12 #include "mpsp.h"
13 #include "backend.h"
14 #include "shared.h"
15 #include "stdout.h"
16 
out_flush(out_t * out)17 static void out_flush (out_t *out)
18 {
19   if (out->len == 0) return;
20 
21   hc_fwrite (out->buf, 1, out->len, &out->fp);
22 
23   out->len = 0;
24 }
25 
out_push(out_t * out,const u8 * pw_buf,const int pw_len)26 static void out_push (out_t *out, const u8 *pw_buf, const int pw_len)
27 {
28   char *ptr = out->buf + out->len;
29 
30   memcpy (ptr, pw_buf, pw_len);
31 
32   #if defined (_WIN)
33 
34   ptr[pw_len + 0] = '\r';
35   ptr[pw_len + 1] = '\n';
36 
37   out->len += pw_len + 2;
38 
39   #else
40 
41   ptr[pw_len] = '\n';
42 
43   out->len += pw_len + 1;
44 
45   #endif
46 
47   if (out->len >= HCBUFSIZ_SMALL - 300)
48   {
49     out_flush (out);
50   }
51 }
52 
process_stdout(hashcat_ctx_t * hashcat_ctx,hc_device_param_t * device_param,const u64 pws_cnt)53 int process_stdout (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, const u64 pws_cnt)
54 {
55   combinator_ctx_t *combinator_ctx = hashcat_ctx->combinator_ctx;
56   hashconfig_t     *hashconfig     = hashcat_ctx->hashconfig;
57   mask_ctx_t       *mask_ctx       = hashcat_ctx->mask_ctx;
58   outfile_ctx_t    *outfile_ctx    = hashcat_ctx->outfile_ctx;
59   straight_ctx_t   *straight_ctx   = hashcat_ctx->straight_ctx;
60   user_options_t   *user_options   = hashcat_ctx->user_options;
61 
62   char *filename = outfile_ctx->filename;
63 
64   out_t out;
65 
66   if (filename)
67   {
68     if (hc_fopen (&out.fp, filename, "ab") == false)
69     {
70       event_log_error (hashcat_ctx, "%s: %s", filename, strerror (errno));
71 
72       return -1;
73     }
74 
75     if (hc_lockfile (&out.fp) == -1)
76     {
77       hc_fclose (&out.fp);
78 
79       event_log_error (hashcat_ctx, "%s: %s", filename, strerror (errno));
80 
81       return -1;
82     }
83   }
84   else
85   {
86     HCFILE *fp = &out.fp;
87 
88     fp->fd       = fileno (stdout);
89     fp->pfp      = stdout;
90     fp->gfp      = NULL;
91     fp->ufp      = NULL;
92     fp->bom_size = 0;
93     fp->path     = NULL;
94     fp->mode     = NULL;
95   }
96 
97   out.len = 0;
98 
99   u32 plain_buf[64] = { 0 };
100 
101   u8 *plain_ptr = (u8 *) plain_buf;
102 
103   u32 plain_len = 0;
104 
105   const u32 il_cnt = device_param->kernel_params_buf32[30]; // ugly, i know
106 
107   if ((user_options->attack_mode == ATTACK_MODE_STRAIGHT) || (user_options->attack_mode == ATTACK_MODE_ASSOCIATION))
108   {
109     pw_t pw;
110 
111     for (u64 gidvid = 0; gidvid < pws_cnt; gidvid++)
112     {
113       const int rc = gidd_to_pw_t (hashcat_ctx, device_param, gidvid, &pw);
114 
115       if (rc == -1)
116       {
117         if (filename) hc_fclose (&out.fp);
118 
119         return -1;
120       }
121 
122       for (u32 il_pos = 0; il_pos < il_cnt; il_pos++)
123       {
124         const u32 off = device_param->innerloop_pos + il_pos;
125 
126         if (hashconfig->opti_type & OPTI_TYPE_OPTIMIZED_KERNEL)
127         {
128           for (int i = 0; i < 8; i++)
129           {
130             plain_buf[i] = pw.i[i];
131           }
132 
133           plain_len = apply_rules_optimized (straight_ctx->kernel_rules_buf[off].cmds, &plain_buf[0], &plain_buf[4], pw.pw_len);
134         }
135         else
136         {
137           for (int i = 0; i < 64; i++)
138           {
139             plain_buf[i] = pw.i[i];
140           }
141 
142           plain_len = apply_rules (straight_ctx->kernel_rules_buf[off].cmds, plain_buf, pw.pw_len);
143         }
144 
145         if (plain_len > hashconfig->pw_max) plain_len = hashconfig->pw_max;
146 
147         out_push (&out, plain_ptr, plain_len);
148       }
149     }
150   }
151   else if (user_options->attack_mode == ATTACK_MODE_COMBI)
152   {
153     pw_t pw;
154 
155     for (u64 gidvid = 0; gidvid < pws_cnt; gidvid++)
156     {
157       const int rc = gidd_to_pw_t (hashcat_ctx, device_param, gidvid, &pw);
158 
159       if (rc == -1)
160       {
161         if (filename) hc_fclose (&out.fp);
162 
163         return -1;
164       }
165 
166       for (u32 il_pos = 0; il_pos < il_cnt; il_pos++)
167       {
168         for (int i = 0; i < 64; i++)
169         {
170           plain_buf[i] = pw.i[i];
171         }
172 
173         plain_len = pw.pw_len;
174 
175         char *comb_buf = (char *) device_param->combs_buf[il_pos].i;
176         u32   comb_len =          device_param->combs_buf[il_pos].pw_len;
177 
178         if (combinator_ctx->combs_mode == COMBINATOR_MODE_BASE_LEFT)
179         {
180           memcpy (plain_ptr + plain_len, comb_buf, comb_len);
181         }
182         else
183         {
184           memmove (plain_ptr + comb_len, plain_ptr, plain_len);
185 
186           memcpy (plain_ptr, comb_buf, comb_len);
187         }
188 
189         plain_len += comb_len;
190 
191         if (plain_len > hashconfig->pw_max) plain_len = hashconfig->pw_max;
192 
193         out_push (&out, plain_ptr, plain_len);
194       }
195     }
196   }
197   else if (user_options->attack_mode == ATTACK_MODE_BF)
198   {
199     for (u64 gidvid = 0; gidvid < pws_cnt; gidvid++)
200     {
201       for (u32 il_pos = 0; il_pos < il_cnt; il_pos++)
202       {
203         u64 l_off = device_param->kernel_params_mp_l_buf64[3] + gidvid;
204         u64 r_off = device_param->kernel_params_mp_r_buf64[3] + il_pos;
205 
206         u32 l_start = device_param->kernel_params_mp_l_buf32[5];
207         u32 r_start = device_param->kernel_params_mp_r_buf32[5];
208 
209         u32 l_stop = device_param->kernel_params_mp_l_buf32[4];
210         u32 r_stop = device_param->kernel_params_mp_r_buf32[4];
211 
212         sp_exec (l_off, (char *) plain_ptr + l_start, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, l_start, l_start + l_stop);
213         sp_exec (r_off, (char *) plain_ptr + r_start, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, r_start, r_start + r_stop);
214 
215         plain_len = mask_ctx->css_cnt;
216 
217         out_push (&out, plain_ptr, plain_len);
218       }
219     }
220   }
221   else if (user_options->attack_mode == ATTACK_MODE_HYBRID1)
222   {
223     pw_t pw;
224 
225     for (u64 gidvid = 0; gidvid < pws_cnt; gidvid++)
226     {
227       const int rc = gidd_to_pw_t (hashcat_ctx, device_param, gidvid, &pw);
228 
229       if (rc == -1)
230       {
231         if (filename) hc_fclose (&out.fp);
232 
233         return -1;
234       }
235 
236       for (u32 il_pos = 0; il_pos < il_cnt; il_pos++)
237       {
238         for (int i = 0; i < 64; i++)
239         {
240           plain_buf[i] = pw.i[i];
241         }
242 
243         plain_len = pw.pw_len;
244 
245         u64 off = device_param->kernel_params_mp_buf64[3] + il_pos;
246 
247         u32 start = 0;
248         u32 stop  = device_param->kernel_params_mp_buf32[4];
249 
250         sp_exec (off, (char *) plain_ptr + plain_len, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, start, start + stop);
251 
252         plain_len += start + stop;
253 
254         out_push (&out, plain_ptr, plain_len);
255       }
256     }
257   }
258   else if (user_options->attack_mode == ATTACK_MODE_HYBRID2)
259   {
260     pw_t pw;
261 
262     for (u64 gidvid = 0; gidvid < pws_cnt; gidvid++)
263     {
264       const int rc = gidd_to_pw_t (hashcat_ctx, device_param, gidvid, &pw);
265 
266       if (rc == -1)
267       {
268         if (filename) hc_fclose (&out.fp);
269 
270         return -1;
271       }
272 
273       for (u32 il_pos = 0; il_pos < il_cnt; il_pos++)
274       {
275         u64 off = device_param->kernel_params_mp_buf64[3] + gidvid;
276 
277         u32 start = 0;
278         u32 stop  = device_param->kernel_params_mp_buf32[4];
279 
280         sp_exec (off, (char *) plain_ptr, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, start, start + stop);
281 
282         plain_len = stop;
283 
284         char *comb_buf = (char *) device_param->combs_buf[il_pos].i;
285         u32   comb_len =          device_param->combs_buf[il_pos].pw_len;
286 
287         memcpy (plain_ptr + plain_len, comb_buf, comb_len);
288 
289         plain_len += comb_len;
290 
291         if (plain_len > hashconfig->pw_max) plain_len = hashconfig->pw_max;
292 
293         out_push (&out, plain_ptr, plain_len);
294       }
295     }
296   }
297 
298   out_flush (&out);
299 
300   if (filename)
301   {
302     hc_unlockfile (&out.fp);
303 
304     hc_fclose (&out.fp);
305   }
306 
307   return 0;
308 }
309