"Fossies" - the Fresh Open Source Software Archive 
Member "cryptsetup-2.4.3/misc/dict_search/crypt_dict.c" (24 Jul 2021, 4192 Bytes) of package /linux/misc/cryptsetup-2.4.3.tar.xz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "crypt_dict.c" see the
Fossies "Dox" file reference documentation.
1 /*
2 * Example of LUKS/TrueCrypt password dictionary search
3 *
4 * Copyright (C) 2012 Milan Broz <gmazyland@gmail.com>
5 *
6 * Run this (for LUKS as root),
7 * e.g. ./crypt_dict test.img /usr/share/john/password.lst 4
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <sys/prctl.h>
31 #include <sys/time.h>
32 #include <sys/resource.h>
33 #include <libcryptsetup.h>
34
35 #define MAX_LEN 512
36
37 static enum { LUKS, TCRYPT } device_type;
38
39 static void check(struct crypt_device *cd, const char *pwd_file, unsigned my_id, unsigned max_id)
40 {
41 FILE *f;
42 int len, r = -1;
43 unsigned long line = 0;
44 char pwd[MAX_LEN];
45
46 if (fork())
47 return;
48
49 /* open password file, now in separate process */
50 f = fopen(pwd_file, "r");
51 if (!f) {
52 printf("Cannot open %s.\n", pwd_file);
53 exit(EXIT_FAILURE);
54 }
55
56 while (fgets(pwd, MAX_LEN, f)) {
57
58 /* every process tries N-th line, skip others */
59 if (line++ % max_id != my_id)
60 continue;
61
62 len = strlen(pwd);
63
64 /* strip EOL - this is like a input from tty */
65 if (len && pwd[len - 1] == '\n') {
66 pwd[len - 1] = '\0';
67 len--;
68 }
69
70 /* lines starting "#!comment" are comments */
71 if (len >= 9 && !strncmp(pwd, "#!comment", 9)) {
72 /* printf("skipping %s\n", pwd); */
73 continue;
74 }
75
76 /* printf("%d: checking %s\n", my_id, pwd); */
77 if (device_type == LUKS)
78 r = crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, pwd, len, 0);
79 else if (device_type == TCRYPT) {
80 struct crypt_params_tcrypt params = {
81 .flags = CRYPT_TCRYPT_LEGACY_MODES,
82 .passphrase = pwd,
83 .passphrase_size = len,
84 };
85 r = crypt_load(cd, CRYPT_TCRYPT, ¶ms);
86 }
87 if (r >= 0) {
88 printf("Found passphrase for slot %d: \"%s\"\n", r, pwd);
89 break;
90 }
91 }
92
93 fclose(f);
94 crypt_free(cd);
95 exit(r >= 0 ? 2 : EXIT_SUCCESS);
96 }
97
98 int main(int argc, char *argv[])
99 {
100 int i, status, procs = 4;
101 struct crypt_device *cd;
102
103 if (argc < 4 || argc > 5) {
104 printf("Use: %s luks|tcrypt <device|file> <password file> [#processes] %d\n", argv[0], argc);
105 exit(EXIT_FAILURE);
106 }
107
108 if (argc == 5 && (sscanf(argv[4], "%i", &procs) != 1 || procs < 1)) {
109 printf("Wrong number of processes.\n");
110 exit(EXIT_FAILURE);
111 }
112
113 if (!strcmp(argv[1], "luks"))
114 device_type = LUKS;
115 else if (!strcmp(argv[1], "tcrypt"))
116 device_type = TCRYPT;
117 else {
118 printf("Wrong device type %s.\n", argv[1]);
119 exit(EXIT_FAILURE);
120 }
121
122 /* crypt_set_debug_level(CRYPT_DEBUG_ALL); */
123
124 /*
125 * Need to create temporary keyslot device-mapper devices and allocate loop if needed,
126 * so root is required here.
127 */
128 if (getuid() != 0) {
129 printf("You must be root to run this program.\n");
130 exit(EXIT_FAILURE);
131 }
132
133 /* signal all children if anything happens */
134 prctl(PR_SET_PDEATHSIG, SIGHUP);
135 setpriority(PRIO_PROCESS, 0, -5);
136
137 /* we are not going to modify anything, so common init is ok */
138 if (crypt_init(&cd, argv[2]) ||
139 (device_type == LUKS && crypt_load(cd, CRYPT_LUKS1, NULL))) {
140 printf("Cannot open %s.\n", argv[2]);
141 exit(EXIT_FAILURE);
142 }
143
144 /* run scan in separate processes, it is up to scheduler to assign CPUs inteligently */
145 for (i = 0; i < procs; i++)
146 check(cd, argv[3], i, procs);
147
148 /* wait until at least one finishes with error or status 2 (key found) */
149 while (wait(&status) != -1 && WIFEXITED(status)) {
150 if (WEXITSTATUS(status) == EXIT_SUCCESS)
151 continue;
152 /* kill rest of processes */
153 kill(0, SIGHUP);
154 /* not reached */
155 break;
156 }
157 exit(0);
158 }