-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrutforceFile.c
244 lines (189 loc) · 6.62 KB
/
brutforceFile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// Created by kbod on 19/06/23.
//
#include "brutforceFile.h"
#include "basicTreatment.h"
void askDicBrutforce (const char *fileToCrack){
// ask for path to dic
char *pathToDic = malloc(sizeof(char) * 256);
int numberOfThreads;
printf("Saisir le chemin vers le dictionnaire à utiliser : ");
scanf("%s", pathToDic);
// ask for number of threads
printf("Saisir le nombre de thread(s) a utiliser : ");
scanf("%d", &numberOfThreads);
threadsDicController(pathToDic, numberOfThreads, fileToCrack);
}
void askIterativeBrutforce (char *pathToZip){
int numberOfThreads;
int lengthPass;
// ask for length of password
printf("Saisir la longueur du mot de passe : ");
scanf("%d", &lengthPass);
// ask for number of threads
int cpt = 0;
do {
if (cpt > 0) {
printf("Le nombre de thread(s) doit être inférieur ou égal à la longueur du mot de passe\n");
}
printf("Saisir le nombre de thread(s) a utiliser : ");
scanf("%d", &numberOfThreads);
cpt++;
} while (numberOfThreads > lengthPass);
// switch case for choice of chars to use number, letter, special char
char *numbers = "0123456789";
char *letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *lettersAndNumbers = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
char *all = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+";
int choice;
printf("Choisir le type de caractères à utiliser : \n");
printf("1. Chiffres seulement [%s]\n", numbers);
printf("2. Lettres seulement [%s]\n", letters);
printf("3. Lettres et chiffres [%s]\n", lettersAndNumbers);
printf("4. Lettres, chiffres et caractères spéciaux [%s]\n", all);
scanf("%d", &choice);
switch (choice) {
case 1:
threadsIterController(pathToZip, numberOfThreads, lengthPass, numbers);
break;
case 2:
threadsIterController(pathToZip, numberOfThreads, lengthPass, letters);
break;
case 3:
threadsIterController(pathToZip, numberOfThreads, lengthPass, lettersAndNumbers);
break;
case 4:
threadsIterController(pathToZip, numberOfThreads, lengthPass, all);
break;
default:
printf("Choix invalide\n");
break;
}
}
void threadsDicController (char *pathToDic, int numberOfThreads, const char *fileToCrack){
int totalline = getLineNumber(pathToDic);
int startLine = 0;
struct arg_struct_dic args[numberOfThreads];
// create args/struct for threads
for(int i=0; i<numberOfThreads; i++){
args[i].pathToDic = pathToDic;
args[i].startLine = startLine;
args[i].fileToCrack = fileToCrack;
startLine = startLine + totalline/numberOfThreads;
if(i == numberOfThreads-1){
args[i].endLine = totalline;
} else {
args[i].endLine = startLine - 1;
}
}
// create array of threads
pthread_t *t=(pthread_t *)malloc(numberOfThreads * sizeof(pthread_t ));
// create threads
for(int i=0;i<numberOfThreads;i++)
pthread_create(&t[i],NULL,&dicBrutforce,(void *)&args[i]);
// wait for all threads to finish
for(int i=0;i<numberOfThreads;i++)
pthread_join(t[i],NULL);
}
void threadsIterController (char *pathToDic, int numberOfThreads, int lengthMax, char *chars){
struct arg_struct_iter args[numberOfThreads];
// create args/struct for threads
int maxPerThreads = lengthMax/numberOfThreads;
int min = 1;
for(int i=0; i<numberOfThreads; i++){
args[i].pathToDic = pathToDic;
args[i].chars = chars;
args[i].min = min;
args[i].max = min + maxPerThreads - 1;
min = min + maxPerThreads;
}
// create array of threads
pthread_t *t=(pthread_t *)malloc(numberOfThreads * sizeof(pthread_t ));
// create threads
for(int i=0;i<numberOfThreads;i++)
pthread_create(&t[i],NULL,&iterativeBrutforce,(void *)&args[i]);
// wait for all threads to finish
for(int i=0;i<numberOfThreads;i++)
pthread_join(t[i],NULL);
// delete array of threads
free(t);
}
int getLineNumber (char *pathToDic){
//get the number of line of file
FILE *file = fopen(pathToDic, "r");
if (file == NULL) {
printf("Impossible d'ouvrir le fichier");
exit(1);
}
int count = 0;
char c;
for (c = getc(file); c != EOF; c = getc(file))
if (c == '\n') // Increment count if this character is newline
count = count + 1;
fclose(file);
return count;
}
char* removeTrailingNull(char* str) {
size_t len = strlen(str);
if (len > 0 && str[len - 1] == '\n') {
str[len - 1] = '\0';
}
return str;
}
void *dicBrutforce (void *arguments){
//get the args
struct arg_struct_dic *args = arguments;
char *path = args->pathToDic;
int startLine = args->startLine;
int endLine = args->endLine;
const char *fileToCrack = args->fileToCrack;
// open file
FILE *file = fopen(path, "r");
if (file == NULL){
printf("cannot open dictionary file");
exit(EXIT_SUCCESS);
}
// read file
char line[256];
int i = 0;
while (fgets(line, sizeof(line), file)) {
if (i >= startLine && i <= endLine){
removeTrailingNull(line);
if (isZipPasswordEncrypted(fileToCrack, line, 1) == 0){
printf("\nLe mot de passe est : %s", line);
exit(EXIT_SUCCESS);
}
}
i++;
}
fclose(file);
return NULL;
}
void *iterativeBrutforce(void *parameters) {
// printf("thread created\n"); MESSAGE PRINTED WHEN THREAD IS CREATED (DEBUG)
struct arg_struct_iter *args = parameters;
int min = args->min;
int max = args->max;
char *chars = args->chars;
char *pathToDic = args->pathToDic;
int l;
for (l = min; l <= max; ++l)
workerBrutforce("", l, chars, pathToDic);
}
void workerBrutforce(char *current, int len, char *chars, char *fileToCrack) {
if (strlen(current) == len){
printf("%s\n", current);
if (isZipPasswordEncrypted(fileToCrack, current, 1) == 0){
printf("\nLe mot de passe est : %s", current);
exit(0);
}
}
if (strlen(current) < len) {
for (int i = 0; i < strlen(chars); ++i) {
char newCurrent[100]; // Adjust the size as per your requirement
strcpy(newCurrent, current);
strncat(newCurrent, &chars[i], 1);
workerBrutforce(newCurrent, len, chars, fileToCrack);
}
}
}