Add example of dictionary search.
[platform/upstream/cryptsetup.git] / misc / dict_search / luks_dict.c
1 /*
2  * Example of LUKS password dictionary search
3  *
4  * Run this as root, e.g. ./luks_check test.img /usr/share/john/password.lst 4
5  *
6  * Copyright (C) 2012 Milan Broz <asi@ucw.cz>
7  *
8  * This copyrighted material is made available to anyone wishing to use,
9  * modify, copy, or redistribute it subject to the terms and conditions
10  * of the GNU General Public License v.2.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <sys/prctl.h>
24 #include <sys/time.h>
25 #include <sys/resource.h>
26 #include <libcryptsetup.h>
27
28 #define MAX_LEN 512
29
30 static void check(struct crypt_device *cd, const char *pwd_file, unsigned my_id, unsigned max_id)
31 {
32         FILE *f;
33         int len, r;
34         unsigned long line = 0;
35         char pwd[MAX_LEN];
36
37         if (fork())
38                 return;
39
40         /* open password file, now in separate process */
41         f = fopen(pwd_file, "r");
42         if (!f) {
43                 printf("Cannot open %s.\n", pwd_file);
44                 exit(EXIT_FAILURE);
45         }
46
47         while (fgets(pwd, MAX_LEN, f)) {
48
49                 /* every process tries N-th line, skip others */
50                 if (line++ % max_id != my_id)
51                         continue;
52
53                 len = strlen(pwd);
54
55                 /* lines starting "#" are comments */
56                 if (pwd[0] == '#')
57                         continue;
58
59                 /* strip EOL - this is like a input from tty */
60                 if (len && pwd[len - 1] == '\n') {
61                         pwd[len - 1] = '\0';
62                         len--;
63                 }
64
65                 //printf("%d: checking %s\n", my_id, pwd);
66                 r = crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, pwd, len, 0);
67                 if (r >= 0) {
68                         printf("Found passphrase for slot %d: %s\n", r, pwd);
69                         break;
70                 }
71         }
72
73         fclose(f);
74         crypt_free(cd);
75         exit(0);
76 }
77
78 int main(int argc, char *argv[])
79 {
80         int i, status, procs = 4;
81         struct crypt_device *cd;
82
83         if (argc < 3 || argc > 4) {
84                 printf("Use: %s <LUKS_device|file> <password file> [#processes] %d\n", argv[0], argc);
85                 exit(EXIT_FAILURE);
86         }
87
88         if (argc == 4 && sscanf(argv[3], "%i", &procs) != 1) {
89                 printf("Wrong number of processes.\n");
90                 exit(EXIT_FAILURE);
91         }
92
93         /* signal all children if anything happens */
94         prctl(PR_SET_PDEATHSIG, SIGHUP);
95         setpriority(PRIO_PROCESS, 0, -5);
96
97         /* we are not going to modify anything, so common init is ok */
98         if (crypt_init(&cd, argv[1]) || crypt_load(cd, CRYPT_LUKS1, NULL)) {
99                 printf("Cannot open %s.\n", argv[1]);
100                 exit(EXIT_FAILURE);
101         }
102
103         /* run scan in separate processes */
104         for (i = 0; i < procs; i++)
105                 check(cd, argv[2], i, procs);
106
107         /* wait until at least one finishes */
108         wait(&status);
109         kill(0, SIGHUP);
110         return 0;
111 }