diff options
| author | 2026-11-11 05:31:06 +0000 | |
|---|---|---|
| committer | 2026-11-11 06:14:42 +0000 | |
| commit | 9e1d304cac5367c3dbb1ca14700736680bd3a804 (patch) | |
| tree | 31f328747acb934d365d423adfda144818ff5ad4 /02/src/cracking.c | |
| download | lab-01-05-master.tar.gz | |
Diffstat (limited to '02/src/cracking.c')
| -rw-r--r-- | 02/src/cracking.c | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/02/src/cracking.c b/02/src/cracking.c new file mode 100644 index 0000000..25afcb5 --- /dev/null +++ b/02/src/cracking.c | |||
| @@ -0,0 +1,138 @@ | |||
| 1 | #include <pty.h> | ||
| 2 | #include <termios.h> | ||
| 3 | #include <sys/wait.h> | ||
| 4 | |||
| 5 | #include "cracking.h" | ||
| 6 | |||
| 7 | void | ||
| 8 | password_bump (STRING_BUILDER sb) | ||
| 9 | { | ||
| 10 | if ('A' <= sb->string[0] && sb->string[0] < 'Z' || | ||
| 11 | 'a' <= sb->string[0] && sb->string[0] < 'z') { | ||
| 12 | sb->string[0] += 1; | ||
| 13 | return; | ||
| 14 | } | ||
| 15 | |||
| 16 | if (sb->string[0] == 'Z') { | ||
| 17 | sb->string[0] = 'a'; | ||
| 18 | return; | ||
| 19 | } | ||
| 20 | |||
| 21 | sb->string[0] = 'A'; | ||
| 22 | |||
| 23 | size_t index = 1; | ||
| 24 | |||
| 25 | again: | ||
| 26 | if (index == sb->length) { | ||
| 27 | STRING_BUILDER_APPEND_CHAR (*sb, '!'); | ||
| 28 | return; | ||
| 29 | } | ||
| 30 | |||
| 31 | if ('0' <= sb->string[index] && sb->string[index] < '9' || | ||
| 32 | 'A' <= sb->string[index] && sb->string[index] < 'Z' || | ||
| 33 | 'a' <= sb->string[index] && sb->string[index] < 'z') { | ||
| 34 | sb->string[index] += 1; | ||
| 35 | return; | ||
| 36 | } | ||
| 37 | |||
| 38 | switch (sb->string[index]) { | ||
| 39 | case '!': sb->string[index] = '@'; return; | ||
| 40 | case '@': sb->string[index] = '#'; return; | ||
| 41 | case '#': sb->string[index] = '$'; return; | ||
| 42 | case '$': sb->string[index] = '%'; return; | ||
| 43 | case '%': sb->string[index] = '^'; return; | ||
| 44 | case '^': sb->string[index] = '&'; return; | ||
| 45 | case '&': sb->string[index] = '*'; return; | ||
| 46 | case '*': sb->string[index] = '('; return; | ||
| 47 | case '(': sb->string[index] = ')'; return; | ||
| 48 | case ')': sb->string[index] = '0'; return; | ||
| 49 | |||
| 50 | case '9': sb->string[index] = 'A'; return; | ||
| 51 | case 'Z': sb->string[index] = 'a'; return; | ||
| 52 | } | ||
| 53 | |||
| 54 | sb->string[index] = '!'; | ||
| 55 | |||
| 56 | index += 1; | ||
| 57 | goto again; | ||
| 58 | } | ||
| 59 | |||
| 60 | bool | ||
| 61 | test_credentials (struct shell shell, | ||
| 62 | struct string_view login, | ||
| 63 | struct string_view password) | ||
| 64 | { | ||
| 65 | fprintf (shell.out, "%.*s\n%.*s\n", SVFMT (login), SVFMT (password)); | ||
| 66 | for (;;) { | ||
| 67 | int c = getc (shell.in); | ||
| 68 | if (c == '\n') | ||
| 69 | break; | ||
| 70 | } | ||
| 71 | int c = getc (shell.in); | ||
| 72 | if (c == 'H') | ||
| 73 | return true; | ||
| 74 | |||
| 75 | ungetc (c, shell.in); | ||
| 76 | return false; | ||
| 77 | } | ||
| 78 | |||
| 79 | struct shell | ||
| 80 | spawn_shell (void) | ||
| 81 | { | ||
| 82 | struct shell shell; | ||
| 83 | |||
| 84 | int fd_master; | ||
| 85 | int fd_slave; | ||
| 86 | |||
| 87 | if (openpty (&fd_master, &fd_slave, NULL, NULL, NULL) == -1) { | ||
| 88 | perror ("openpty"); | ||
| 89 | exit (EXIT_FAILURE); | ||
| 90 | } | ||
| 91 | |||
| 92 | shell.pid = fork (); | ||
| 93 | if (shell.pid == -1) { | ||
| 94 | perror ("fork"); | ||
| 95 | exit (EXIT_FAILURE); | ||
| 96 | } | ||
| 97 | |||
| 98 | if (shell.pid == 0) { | ||
| 99 | close (fd_master); | ||
| 100 | |||
| 101 | if (dup2 (fd_slave, STDIN_FILENO) == -1) { | ||
| 102 | perror ("dup2 stdin"); | ||
| 103 | _exit (1); | ||
| 104 | } | ||
| 105 | |||
| 106 | if (dup2 (fd_slave, STDOUT_FILENO) == -1) { | ||
| 107 | perror ("dup2 stdout"); | ||
| 108 | _exit (1); | ||
| 109 | } | ||
| 110 | |||
| 111 | close (fd_slave); | ||
| 112 | close (STDERR_FILENO); | ||
| 113 | |||
| 114 | struct termios term; | ||
| 115 | tcgetattr (STDIN_FILENO, &term); | ||
| 116 | term.c_lflag &= ~ECHO; | ||
| 117 | tcsetattr (STDIN_FILENO, 0, &term); | ||
| 118 | |||
| 119 | execlp ("shell", "shell", NULL); | ||
| 120 | perror ("execlp"); | ||
| 121 | _exit (1); | ||
| 122 | } | ||
| 123 | |||
| 124 | close (fd_slave); | ||
| 125 | |||
| 126 | shell.out = fdopen (fd_master, "w"); | ||
| 127 | shell.in = fdopen (fd_master, "r"); | ||
| 128 | |||
| 129 | return shell; | ||
| 130 | } | ||
| 131 | |||
| 132 | void | ||
| 133 | close_shell (struct shell shell) | ||
| 134 | { | ||
| 135 | fclose (shell.in); | ||
| 136 | fclose (shell.out); | ||
| 137 | waitpid (shell.pid, NULL, 0); | ||
| 138 | } | ||
