-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
433 lines (362 loc) · 10.2 KB
/
main.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define VERSION "v0.1"
#define BUFFER_SIZE 512
#define BLUE "\e[0;34m"
#define RED "\x1b[31m"
#define RESET "\x1b[0m"
volatile sig_atomic_t running;
static inline void
help(char *bin)
{
printf("Lycan - Leak Analyzer %s\n\n"
"Usage: %s [FILENAME]\n", VERSION, bin);
exit(EXIT_FAILURE);
}
static void
die(char *str, int exit_code)
{
if (exit_code == 0)
printf("%s\n", str);
else
fprintf(stderr, str);
exit(exit_code);
}
void handler(int signum){
printf("Ctrl+c pressed\n");
running = 0;
}
void
update_progress_bar(double current_perc, int width)
{
printf("\rProgress [");
int comp = current_perc * width / 100;
int incomp = width - comp;
printf(RED);
for (int i = 0; i < comp-1; i++) {
printf("=");
}
if (incomp == 0)
printf("=");
else
printf(">");
if (incomp != 0) {
for (int i = 0; i < incomp; i++) {
printf(" ");
}
}
printf(RESET);
fflush(stdout);
printf("] %.2f%c", current_perc, '%');
}
static void
statprint(char *name, double stat, int num)
{
printf("%3s: %7.3f% " BLUE " (%d) " RESET "\n", name, stat, num);
}
static int
get_lines(FILE *f)
{
int ch;
int count=0;
do {
ch = fgetc(f);
if(ch == '\n') count++;
} while( ch != EOF );
return count;
}
//static void
//top_passwords(char *buffer, char *passwords[], int passwords_count[], int length)
//{
// int flag = 0;
//
// for (int i=0; i<length; i++) {
// if (strcmp(passwords[i],buffer) == 0) {
// passwords_count[i]++;
// flag = 1;
// }
// }
//
// if (flag == 0) {
// // expand arrays //
// strcpy(pass, passwords[length]);
// passwords_count[length]++;
// length++;
// }
//}
static void
length_stat(char *buffer, int length[])
{
int len = strlen(buffer);
if (len < 30)
length[len]++;
//length[len-1]++;
else
length[30]++;
}
static int
issymbol(char c)
{
if (c == '!' || c == '@' || c == '#'
|| c == '$' || c == '%' || c == '^'
|| c == '&' || c == '*' || c == '('
|| c == ')' || c == '-' || c == '{'
|| c == '}' || c == '[' || c == ']'
|| c == ':' || c == ';' || c == '"'
|| c == '\''|| c == '<' || c == '>'
|| c == '.' || c == '/' || c == '?'
|| c == '~' || c == '`' || c == ' ')
return 1;
else
return 0;
}
static void
charset_stat(char *buffer, int chars[])
{
int lower=0, upper=0, num=0, symb=0, other=0;
for (int i = 0; i < strlen(buffer); i++) {
if (islower(buffer[i]) != 0)
chars[0]++;
else if (isupper(buffer[i]) != 0)
chars[1]++;
else if (isdigit(buffer[i]) != 0)
chars[2]++;
else if (issymbol(buffer[i]) != 0)
chars[3]++;
else
chars[4]++;
}
}
static void
symbols_stat(char *buffer, int symbols[], char symbols_name[])
{
for (int i = 0; i < strlen(buffer); i++) {
for (int j = 0; j < 89; j++) {
if (buffer[i] == symbols_name[j])
symbols[j]++;
}
}
}
static void
symbol_bsort(int min, int max, int symbols[], char symbols_name[])
{
for (int i = min; i < max - 1; i++) {
//for (int j = min; j < max - j - 1; j++) {
for (int j = min; j < i; j++) {
if (symbols[j] <= symbols[j+1]) {
int swap = symbols[j];
symbols[j] = symbols[j+1];
symbols[j+1] = swap;
char swapc = symbols_name[j];
symbols_name[j] = symbols_name[j+1];
symbols_name[j+1] = swapc;
}
}
}
}
void
main(int argc, char **argv)
{
if (argc < 2)
help(argv[0]);
FILE *f = fopen(argv[1], "r");
if (!f)
die("File could not be opened", EXIT_FAILURE);
char buffer[BUFFER_SIZE];
int count = 0;
int total = get_lines(f);
int length[31] = {0};
/* al, au, n, s, al_au, al_n, al_s, au_n, au_s, n_s; */
//int charset[10] = {1};
/* al, au, n, s */
int symbols[89] = {0};
char symbols_name[89] = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-',
'{', '}', '[', ']', ':', ';', '"', '\'','<', '>', '.',
'/', '?', '~', '`', ' '
};
int chars[5] = {0};
char *chars_name[5] = {
"Lower-case",
"Upper-case",
"Numbers",
"Symbols",
"Other",
};
FILE *f1 = fopen(argv[1], "r");
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
printf("\e[?25l");
running = 1;
signal(SIGINT, handler);
while ((fgets(buffer, sizeof(buffer), f1) != NULL) && running == 1) {
if (count % 10000 == 0 || count == total-1) {
double current_perc = (double)count / (double)total * 100.0;
update_progress_bar(current_perc, w.ws_col/2);
}
strtok(buffer, "\n");
length_stat(buffer, length);
charset_stat(buffer, chars);
symbols_stat(buffer, symbols, symbols_name);
count++;
}
fclose(f1);
printf("\n\n");
printf("\e[?25h");
struct stat st = {0};
if (stat("data/", &st) == -1) {
mkdir("data/", 0700);
if (stat("data/logs", &st) == -1) {
mkdir("data/logs/", 0700);
}
if (stat("data/charts", &st) == -1) {
mkdir("data/charts/", 0700);
}
}
printf("Length:\n------------------------\n");
FILE *f2 = fopen("data/logs/length.txt", "w");
for (int i=0; i<30; i++) {
char snum[13];
snprintf(snum, 13, "%d", i);
double perc = (double)length[i] * 100.0 / (double)count;
//double perc = (double)length[i+1] * 100.0 / (double)count;
statprint(snum, perc, length[i]);
//statprint(snum, perc, length[i+1]);
if (i <= 30)
fprintf(f2, "%d %d %.2f%\n", i, i, perc);
}
fclose(f2);
statprint("30+", (double)length[30]*100.0/(double)length[30]*100.0/(double)count, length[30]);
system("cat data/logs/length.txt| gnuplot -p -e "
"\"set terminal png;"
"set boxwidth 0.7;"
"set style fill solid;"
"set title \'Length\';"
"unset key;"
"set border 1+2 back;"
"plot '-' using 1:3:xtic(2) with boxes lt rgb \'red\'\""
" > data/charts/length.png");
printf("\n\n");
printf("Basic character sets:\n----------\n");
FILE *f3 = fopen("data/logs/chars.txt", "w");
int chars_total = chars[0] + chars[1] + chars[2] + chars[3] + chars[4];
for (int i=0; i<5; i++) {
double perc = (double)chars[i] * 100.0 / (double)chars_total;
printf("%-10s: %6.3f% "BLUE" (%d)"RESET"\n", chars_name[i], perc, chars[i]);
//if (i <= 30)
fprintf(f3, "%d \"%s\" %f\n", i, chars_name[i], perc);
}
fclose(f3);
system("cat data/logs/chars.txt| gnuplot -p -e "
"\"set terminal png;"
"set boxwidth 0.7;"
"set style fill solid;"
"set title \'Basic character sets\';"
"unset key;"
"set border 1+2 back;"
"plot '-' using 1:3:xtic(2) with boxes lt rgb \'blue\'\""
"> data/charts/chars.png");
printf("\n\n");
printf("Symbols:\n-----------------------\n");
symbol_bsort(0, 52, symbols, symbols_name);
symbol_bsort(52, 62, symbols, symbols_name);
symbol_bsort(52, 62, symbols, symbols_name);
symbol_bsort(52, 62, symbols, symbols_name);
symbol_bsort(52, 62, symbols, symbols_name);
symbol_bsort(52, 62, symbols, symbols_name);
symbol_bsort(52, 62, symbols, symbols_name);
symbol_bsort(52, 62, symbols, symbols_name);
symbol_bsort(52, 62, symbols, symbols_name);
symbol_bsort(52, 62, symbols, symbols_name);
symbol_bsort(52, 62, symbols, symbols_name);
symbol_bsort(52, 62, symbols, symbols_name);
symbol_bsort(62, 89, symbols, symbols_name);
symbol_bsort(62, 89, symbols, symbols_name);
symbol_bsort(62, 89, symbols, symbols_name);
symbol_bsort(62, 89, symbols, symbols_name);
symbol_bsort(62, 89, symbols, symbols_name);
symbol_bsort(62, 89, symbols, symbols_name);
symbol_bsort(62, 89, symbols, symbols_name);
/*for (int i = 52; i < 62 - 1; i++) {
//for (int j = min; j < max - j - 1; j++) {
for (int j = 52; j < 62; j++) {
if (symbols[j] < symbols[j+1]) {
int swap = symbols[j];
symbols[j] = symbols[j+1];
symbols[j+1] = swap;
char swapc = symbols_name[j];
symbols_name[j] = symbols_name[j+1];
symbols_name[j+1] = swapc;
}
}
}*/
printf("Letters:\n---------\n");
FILE *f4 = fopen("data/logs/symb_letters.txt", "w");
for (int i=0; i<20; i++) {
double perc = (double)symbols[i] * 100.0 / (double)chars_total;
printf("%c: %6.3f% "BLUE" (%d)"RESET"\n", symbols_name[i], perc, symbols[i]);
fprintf(f4, "%d '%c' %f\n", i, symbols_name[i], perc);
}
fclose(f4);
system("cat data/logs/symb_letters.txt| gnuplot -p -e "
"\"set terminal png;"
"set boxwidth 0.7;"
"set style fill solid;"
"set title \'Most popular characters (letters)\';"
"unset key;"
"set border 1+2 back;"
"plot '-' using 1:3:xtic(2) with boxes lt rgb \'blue\'\""
"> data/charts/symb_letters.png");
printf("\n\n");
printf("Numbers:\n---------\n");
FILE *f5 = fopen("data/logs/symb_numbers.txt", "w");
for (int i=52; i<62; i++) {
double perc = (double)symbols[i] * 100.0 / (double)chars_total;
printf("%c: %6.3f% "BLUE" (%d)"RESET"\n", symbols_name[i], perc, symbols[i]);
fprintf(f4, "%d '%c' %f\n", i-52, symbols_name[i], perc);
}
fclose(f5);
system("cat data/logs/symb_numbers.txt| gnuplot -p -e "
"\"set terminal png;"
"set boxwidth 0.7;"
"set style fill solid;"
"set title \'Most popular characters (numbers)\';"
"unset key;"
"set border 1+2 back;"
"plot '-' using 1:3:xtic(2) with boxes lt rgb \'blue\'\""
"> data/charts/symb_numbers.png");
printf("\n\n");
printf("Special:\n---------\n");
FILE *f6 = fopen("data/logs/symb_special.txt", "w");
for (int i=62; i<89; i++) {
double perc = (double)symbols[i] * 100.0 / (double)chars_total;
printf("%c: %6.3f% "BLUE" (%d)"RESET"\n", symbols_name[i], perc, symbols[i]);
fprintf(f4, "%d '%c' %f\n", i-62, symbols_name[i], perc);
}
fclose(f6);
system("cat data/logs/symb_special.txt| gnuplot -p -e "
"\"set terminal png;"
"set boxwidth 0.7;"
"set style fill solid;"
"set title \'Most popular characters (numbers)\';"
"unset key;"
"set border 1+2 back;"
"plot '-' using 1:3:xtic(2) with boxes lt rgb \'blue\'\""
"> data/charts/symb_special.png");
printf("\n\n");
exit(EXIT_SUCCESS);
}