2 * Example of LUKS/TrueCrypt password dictionary search
4 * Copyright (C) 2012 Milan Broz <gmazyland@gmail.com>
6 * Run this (for LUKS as root),
7 * e.g. ./crypt_dict test.img /usr/share/john/password.lst 4
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.
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.
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.
28 #include <sys/types.h>
30 #include <sys/prctl.h>
32 #include <sys/resource.h>
33 #include <libcryptsetup.h>
37 static enum { LUKS, TCRYPT } device_type;
39 static void check(struct crypt_device *cd, const char *pwd_file, unsigned my_id, unsigned max_id)
43 unsigned long line = 0;
49 /* open password file, now in separate process */
50 f = fopen(pwd_file, "r");
52 printf("Cannot open %s.\n", pwd_file);
56 while (fgets(pwd, MAX_LEN, f)) {
58 /* every process tries N-th line, skip others */
59 if (line++ % max_id != my_id)
64 /* strip EOL - this is like a input from tty */
65 if (len && pwd[len - 1] == '\n') {
70 /* lines starting "#!comment" are comments */
71 if (len >= 9 && !strncmp(pwd, "#!comment", 9)) {
72 /* printf("skipping %s\n", pwd); */
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,
83 .passphrase_size = len,
85 r = crypt_load(cd, CRYPT_TCRYPT, ¶ms);
88 printf("Found passphrase for slot %d: \"%s\"\n", r, pwd);
95 exit(r >= 0 ? 2 : EXIT_SUCCESS);
98 int main(int argc, char *argv[])
100 int i, status, procs = 4;
101 struct crypt_device *cd;
103 if (argc < 4 || argc > 5) {
104 printf("Use: %s luks|tcrypt <device|file> <password file> [#processes] %d\n", argv[0], argc);
108 if (argc == 5 && (sscanf(argv[4], "%i", &procs) != 1 || procs < 1)) {
109 printf("Wrong number of processes.\n");
113 if (!strcmp(argv[1], "luks"))
115 else if (!strcmp(argv[1], "tcrypt"))
116 device_type = TCRYPT;
118 printf("Wrong device type %s.\n", argv[1]);
122 /* crypt_set_debug_level(CRYPT_DEBUG_ALL); */
125 * Need to create temporary keyslot device-mapper devices and allocate loop if needed,
126 * so root is requried here.
129 printf("You must be root to run this program.\n");
133 /* signal all children if anything happens */
134 prctl(PR_SET_PDEATHSIG, SIGHUP);
135 setpriority(PRIO_PROCESS, 0, -5);
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]);
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);
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)
152 /* kill rest of processes */