-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_pwd.c
363 lines (289 loc) · 9.36 KB
/
gen_pwd.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
/*
*
* gen_pwd
*
* Compiling:
* TCC: tcc.exe gen_pwd.c argtable3.c -o gen_pwd.exe
* PCC:
* GCC:
* CLang:
* ICC:
* MSVC:
*
*/
#include <stdlib.h> // for rand() and srand(), for random() and srandom(), for EXIT_SUCCESS and EXIT_FAILURE macro
#include <stdio.h> // for printf(), for fprintf(), for NULL const
#include <string.h> // for strlen()
#include <locale.h> // for setlocale() - for correct displaying some characters, from "--characters" otion
// #include <stdbool.h>
// #include <limits.h> // for UINT_MAX macro
#if defined(_WIN32) && !defined(_WIN64)
#define srandom srand
#define random rand
#include <windows.h> // for QueryPerformanceCounter()
#elif defined(_WIN32) && defined(_WIN64)
#define srandom srand
#define random rand
#include <windows.h> // for QueryPerformanceCounter()
#elif defined(__linux__)
#elif defined(__unix__) || defined(__APPLE__) && defined(__MACH__)
#include <sys/param.h>
#if defined(BSD)
#define PLATFORM_NAME "bsd"
#endif
#else
#endif
#include "argtable3.h"
/* global arg_xxx structs */
struct arg_lit *help, *version;
struct arg_int *length, *number;
struct arg_str *dictionary;
struct arg_file *o;
struct arg_end *end;
#define PWD_MIN_LEN 6
#define PWD_DEF_LEN 12
#define PWD_MAX_LEN 4096
#define PWD_MIN_NUM 1
#define PWD_DEF_NUM 1
#define PWD_MAX_NUM 4096
int len = PWD_DEF_LEN;
int num = PWD_DEF_NUM;
char *gen_rnd(char dict[], int len);
int chk_rnd(char str[]);
char *gen_pwd(int len);
int main(int argc, char *argv[]) {
setlocale(LC_ALL, "Russian");
/* the global arg_xxx structs are initialised within the argtable */
void *argtable[] = {
help = arg_litn("h", "help", 0, 1, "display this help and exit"),
version = arg_litn("v", "version", 0, 1, "display version info and exit"),
length = arg_intn(NULL, "length", "<n>", 0, 1, "set password length"),
number = arg_intn(NULL, "number", "<n>", 0, 1, "set number of passwords"),
dictionary = arg_str0(NULL,"dictionary","WORD", "across -x, commas -m, horizontal -x, long -l,"),
arg_rem (NULL, " single-column -1, verbose -l, vertical -C"),
o = arg_filen("o", NULL, "myfile", 0, 1, "output file"),
end = arg_end(20),
};
// int exitcode = 0;
int exitcode = EXIT_SUCCESS;
char progname[] = "gen_pwd.exe";
int nerrors;
nerrors = arg_parse(argc,argv,argtable);
/* If the parser returned any errors then display them and exit */
if (nerrors > 0)
{
/* Display the error details contained in the arg_end struct.*/
arg_print_errors(stdout, end, progname);
printf("Try '%s --help' for more information.\n", progname);
// exitcode = 1;
exitcode = EXIT_FAILURE;
goto exit;
}
/* special case: '--help' takes precedence over error reporting */
if (help->count > 0)
{
printf("Usage: %s", progname);
arg_print_syntax(stdout, argtable, "\n");
// printf("Demonstrate command-line parsing in argtable3.\n\n");
arg_print_glossary(stdout, argtable, " %-25s %s\n");
// exitcode = 0;
exitcode = EXIT_SUCCESS;
goto exit;
}
// version
if (version->count > 0)
{
printf("Program: gen_pwd\n");
printf("Version: 0.0.1\n");
// exitcode = 0;
exitcode = EXIT_SUCCESS;
goto exit;
}
// length
if (length->count > 0)
{
//printf("Length: %d", *length->ival);
len = *length->ival;
//printf("Length: %d", len);
}
// number
if (number->count > 0)
{
//printf("Number: %d", *number->ival);
num = *number->ival;
//printf("Number: %d", num);
}
// dictionary
if (dictionary->count > 0)
{
printf("Dictionary: %s\n", dictionary->sval[0]);
exitcode = EXIT_SUCCESS;
goto exit;
}
// to file
if (o->count > 0)
{
FILE *fp;
fp = fopen(*o->filename, "a+");
while (num > 0) {
num--;
fprintf(fp, "%s\n", gen_pwd(len));
}
fclose(fp);
//printf("Length: %d", *length->ival);
//fn = *o->filename;
// printf("File: %s\n", *o->filename);
//while (0==0){printf("%s\n", gen_pwd(len));}
//printf("%s\n", gen_pwd(len));
// exitcode = 0;
exitcode = EXIT_SUCCESS;
goto exit;
}
while (num > 0) {
num--;
printf("%s\n", gen_pwd(len));
}
exit:
/* deallocate each non-null entry in argtable[] */
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
return exitcode;
}
char *gen_rnd(char dict[], int len) {
LARGE_INTEGER pc_val;
QueryPerformanceCounter(&pc_val);
srandom((unsigned int)pc_val);
char *str = (char *) malloc(sizeof(char) * (len + 1));
for(int i = 0; i < len; i++)
{
//str[i] = dict[random() % (sizeof dict - 1)];
str[i] = dict[random() % (strlen(dict))];
}
str[len] = '\0';
char* rnd = str;
free(str);
str = NULL;
return rnd;
}
int chk_rnd(char str[]) {
int strl = strlen(str);
int lc_chars=0;
int uc_chars=0;
int num_chars=0;
int sym_chars=0;
int sym_ext_chars=0;
// Ïðîâåðÿåì ïåðâûé ñèìâîë - îí ìîæåò áûòü òîëüêî áóêâîé
if (str[0] >= 97 && str[0] <= 122) {
++lc_chars;
} else if (str[0] >= 65 && str[0] <= 90) {
++uc_chars;
} else {
return EXIT_FAILURE;
}
// Ïðîâåðÿåì ñåðåäèíó
for (int i = 1; i <= strl-2; i++) {
if (str[i] >= 97 && str[i] <= 122) {
++lc_chars;
} else if (str[i] >= 65 && str[i] <= 90) {
++uc_chars;
} else if (str[i] >= 48 && str[i] <= 57) {
++num_chars;
} else if (str[i] == 42 || str[i] == 45 || str[i] == 46 || str[i] == 58) {
++sym_chars;
} else if (str[i] == 35 || str[i] == 36 || str[i] == 38 || str[i] == 64) {
++sym_ext_chars;
} else {
return EXIT_FAILURE;
}
}
// Ïðîâåðÿåì ïîñëåäíèé ñèìâîë - îí ìîæåò áûòü òîëüêî áóêâîé ëèáî öèôðîé
if (str[strl-1] >= 97 && str[strl-1] <= 122) {
++lc_chars;
} else if (str[strl-1] >= 65 && str[strl-1] <= 90) {
++uc_chars;
} else if (str[strl-1] >= 48 && str[strl-1] <= 57) {
++num_chars;
} else {
return EXIT_FAILURE;
}
// Òóò ìû ïðîâåðÿåì êîëè÷åñòâî ñèìâîëîâ êàæäîé êàòåãîðèè â ïàðîëå
if (lc_chars >= 2) {
} else {
return EXIT_FAILURE;
}
if (uc_chars >= 2) {
} else {
return EXIT_FAILURE;
}
if (num_chars >= 2) {
} else {
return EXIT_FAILURE;
}
if (sym_chars >= 2) {
} else {
return EXIT_FAILURE;
}
if (sym_ext_chars >= 2) {
} else {
return EXIT_FAILURE;
}
//printf ("lc_chars %d\n", lc_chars);
//printf ("uc_chars %d\n", uc_chars);
//printf ("num_chars %d\n", num_chars);
//printf ("sym_chars %d\n", sym_chars);
//printf ("sym_ext_chars %d\n", sym_ext_chars);
return EXIT_SUCCESS;
}
char *gen_pwd(int len) {
char dict[] = "abcdefhkmnprstuxyzABCDEFHKMNPRSTUXYZ2345689:*-.@#$&";
// char dict[] = "abcdefhkmnprstuxyz";
// char dict[] = "ABCDEFHKMNPRSTUXYZ";
// char dict[] = "2345689";
// char dict[] = ":*-.";
// char dict[] = "@#$&";
// int len = 12;
char* str = gen_rnd(dict, len);
while (chk_rnd(str) != EXIT_SUCCESS) {
str = gen_rnd(dict, len);
}
char* pwd = str;
return pwd;
}
/*
// tcc.exe gen_pwd.c argtable3.c -luser32 -o gen_pwd.exe
// -luser32 for GetCursorPos()
int prng(void) {
//unsigned int seed;
printf("RND_SRC:\n");
LARGE_INTEGER pc_val;
QueryPerformanceCounter(&pc_val);
printf("pc_val: %lld\n", pc_val);
printf("pc_val.LowPart: %lu\n", pc_val.LowPart);
printf("pc_val.HighPart: %ld\n", pc_val.HighPart);
printf("pc_val.QuadPart: %lld\n", pc_val.QuadPart);
DWORD pid = GetCurrentProcessId();
printf("PID: %lu\n", pid);
DWORD tid = GetCurrentThreadId();
printf("TID: %lu\n", tid);
POINT cur_pos;
GetCursorPos(&cur_pos);
printf("CPOSx: %li\n", cur_pos.x);
printf("CPOSy: %li\n", cur_pos.y);
long long int entropy;
entropy = (long long int)pid+(long long int)tid+((long long int)cur_pos.x+1)+((long long int)cur_pos.y+1);
printf("ALL0: %lli\n", entropy);
long long int entropy2;
entropy2 = entropy % UINT_MAX;
printf("ALL1: %lli\n", entropy2);
unsigned int entropy3;
entropy3 = (unsigned int)entropy2;
printf("ALL2: %u\n", entropy3);
printf("UINT_MAX: %u\n", UINT_MAX);
//unsigned int test;
//test = entropy % 2147483647;
//printf("unsigned int entropy: %u\n", test);
//unsigned int test;
//test = (unsigned int)seed.HighPart;
//printf("unsigned int seed: %d\n", test);
//return seed;
}
*/