-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfonctions.c
147 lines (131 loc) · 5.96 KB
/
fonctions.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
#include "fonctions.h"
#define MAX_PASSWORD_LENGTH 10
#define MAX_CHARSET_LENGTH 100
/**
* The function `try_password` attempts to open a zip file with a given password and returns 1 if the
* password is correct, otherwise it returns 0.
*
* @param fichier_zip The parameter "fichier_zip" is a string that represents the name or path of the
* zip file that you want to open and try the password on.
* @param password The `password` parameter is a string that represents the password to be used for
* decrypting the files in the ZIP archive.
*
* @return The function `try_password` returns an integer value. It returns 1 if the correct password
* is found for any file in the zip archive, and it returns 0 if the password is incorrect or if there
* is an error opening the zip file.
*/
void print_help() {
printf("Usage: ./zip [OPTION]... [FILE]...\n");
printf("Options:\n");
printf(" -h, --help Show this help\n");
printf(" -o, --open Open a zip file for browsing\n");
printf(" -b, --bruteforce Try to bruteforce the password\n");
printf(" -d, --dictionary=FILE Try to bruteforce the password with a dictionary\n");
printf(" -p, --password=PASSWORD Use this password\n");
printf(" -e, --extract=FILE Extract this file\n");
printf(" -i, --include=FILE Include this file\n");
}
int try_password(char* fichier_zip, char* password) {
struct zip *archive;
struct zip_file *file;
struct zip_stat file_info;
int err;
// Ouvrir l'archive avec le mot de passe
archive = zip_open(fichier_zip, 0, &err);
if (archive == NULL) {
fprintf(stderr, "Erreur lors de l'ouverture du fichier zip. Vérifier le nom\n");
return 0;
}
// Parcourir tous les fichiers de l'archive et essayer le mot de passe
int num_files = zip_get_num_entries(archive, 0);
for (int i = 0; i < num_files; i++) {
if (zip_stat_index(archive, i, 0, &file_info) == 0) {
// Ouvrir le fichier avec le mot de passe
file = zip_fopen_index_encrypted(archive, i, ZIP_FL_ENC_GUESS, password); // l'option de base est 0
if (file != NULL) {
// Le mot de passe est correct, faire quelque chose ici si nécessaire
printf("Mot de passe trouvé pour le fichier %s : %s\n", file_info.name, password);
zip_fclose(file);
zip_close(archive);
return 1;
}
}
}
printf("Mot de passe incorrect.\n");
zip_close(archive);
return 0;
}
/**
* Essayer de trouver le mot de passe d'une archive ZIP en utilisant la méthode brute force
* @param fichier_zip le nom du fichier ZIP
*/
void brute_force(char* fichier_zip) {
int choice;
printf("Choisissez le type de mot de passe :\n");
printf("1. Lettres minuscules\n");
printf("2. Lettres majuscules\n");
printf("3. Chiffres\n");
printf("4. Lettres minuscules + Majuscules\n");
printf("5. Lettres minuscules + Majuscules + Chiffres\n");
printf("6. Lettres minuscules + Majuscules + Chiffres + Caractères spéciaux\n");
printf("Entrez votre choix : ");
scanf("%d", &choice);
getchar(); // Efface le caractère de nouvelle ligne du tampon d'entrée
char charset[MAX_CHARSET_LENGTH + 1];
switch (choice) {
case 1:
strncpy(charset, "abcdefghijklmnopqrstuvwxyz", MAX_CHARSET_LENGTH);
charset[MAX_CHARSET_LENGTH] = '\0';
break;
case 2:
strncpy(charset, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", MAX_CHARSET_LENGTH);
charset[MAX_CHARSET_LENGTH] = '\0';
break;
case 3:
strncpy(charset, "0123456789", MAX_CHARSET_LENGTH);
charset[MAX_CHARSET_LENGTH] = '\0';
break;
case 4:
strncpy(charset, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", MAX_CHARSET_LENGTH);
charset[MAX_CHARSET_LENGTH] = '\0';
break;
case 5:
strncpy(charset, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", MAX_CHARSET_LENGTH);
charset[MAX_CHARSET_LENGTH] = '\0';
break;
case 6:
strncpy(charset, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$^&*", MAX_CHARSET_LENGTH);
charset[MAX_CHARSET_LENGTH] = '\0';
break;
default:
printf("Choix invalide.\n");
return;
}
// Générer tous les mots de passe possibles
int charset_length = strlen(charset);
char password[MAX_PASSWORD_LENGTH + 1];
for (int length = 1; length <= MAX_PASSWORD_LENGTH; length++) {
// Générer tous les mots de passe de la longueur "length"
int num_combinations = 1;
for (int i = 0; i < length; i++) {
num_combinations *= charset_length; // Nombre de combinaisons possibles
}
for (int i = 0; i < num_combinations; i++) {
// Convertir le nombre en base "charset_length" en une chaîne de caractères
int remainder = i;
for (int j = 0; j < length; j++) { // Pour chaque caractère du mot de passe (de gauche à droite)
password[j] = charset[remainder % charset_length]; // Ajouter le caractère correspondant au mot de passe
remainder /= charset_length; // Diviser le nombre par la base pour obtenir le caractère suivant
}
password[length] = '\0'; // Terminer la chaîne de caractères
printf("Essai du mot de passe : %s\n", password);
if (try_password(fichier_zip, password) == 1) {
printf("Mot de passe trouvé : %s\n", password);
return; // Mot de passe trouvé, arrêter la fonction
}
// Ajouter un court délai entre les tentatives pour éviter les bugs
usleep(5000);
}
}
printf("Mot de passe non trouvé.\n");
}