aboutsummaryrefslogtreecommitdiff
path: root/02/src/cracking.c
blob: 25afcb5d1468522efddb42ad499260b91b4ea53d (plain)
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
#include <pty.h>
#include <termios.h>
#include <sys/wait.h>

#include "cracking.h"

void
password_bump (STRING_BUILDER sb)
{
  if ('A' <= sb->string[0] && sb->string[0] < 'Z' ||
      'a' <= sb->string[0] && sb->string[0] < 'z') {
    sb->string[0] += 1;
    return;
  }

  if (sb->string[0] == 'Z') {
    sb->string[0] = 'a';
    return;
  }

  sb->string[0] = 'A';

  size_t index = 1;

again:
  if (index == sb->length) {
    STRING_BUILDER_APPEND_CHAR (*sb, '!');
    return;
  }

  if ('0' <= sb->string[index] && sb->string[index] < '9' ||
      'A' <= sb->string[index] && sb->string[index] < 'Z' ||
      'a' <= sb->string[index] && sb->string[index] < 'z') {
    sb->string[index] += 1;
    return;
  }

  switch (sb->string[index]) {
  case '!': sb->string[index] = '@'; return;
  case '@': sb->string[index] = '#'; return;
  case '#': sb->string[index] = '$'; return;
  case '$': sb->string[index] = '%'; return;
  case '%': sb->string[index] = '^'; return;
  case '^': sb->string[index] = '&'; return;
  case '&': sb->string[index] = '*'; return;
  case '*': sb->string[index] = '('; return;
  case '(': sb->string[index] = ')'; return;
  case ')': sb->string[index] = '0'; return;

  case '9': sb->string[index] = 'A'; return;
  case 'Z': sb->string[index] = 'a'; return;
  }

  sb->string[index] = '!';

  index += 1;
  goto again;
}

bool
test_credentials (struct shell shell,
                  struct string_view login,
                  struct string_view password)
{
  fprintf (shell.out, "%.*s\n%.*s\n", SVFMT (login), SVFMT (password));
  for (;;) {
    int c = getc (shell.in);
    if (c == '\n')
      break;
  }
  int c = getc (shell.in);
  if (c == 'H')
    return true;

  ungetc (c, shell.in);
  return false;
}

struct shell
spawn_shell (void)
{
  struct shell shell;

  int fd_master;
  int fd_slave;

  if (openpty (&fd_master, &fd_slave, NULL, NULL, NULL) == -1) {
    perror ("openpty");
    exit (EXIT_FAILURE);
  }

  shell.pid = fork ();
  if (shell.pid == -1) {
    perror ("fork");
    exit (EXIT_FAILURE);
  }

  if (shell.pid == 0) {
    close (fd_master);

    if (dup2 (fd_slave, STDIN_FILENO) == -1) {
      perror ("dup2 stdin");
      _exit (1);
    }

    if (dup2 (fd_slave, STDOUT_FILENO) == -1) {
      perror ("dup2 stdout");
      _exit (1);
    }

    close (fd_slave);
    close (STDERR_FILENO);

    struct termios term;
    tcgetattr (STDIN_FILENO, &term);
    term.c_lflag &= ~ECHO;
    tcsetattr (STDIN_FILENO, 0, &term);

    execlp ("shell", "shell", NULL);
    perror ("execlp");
    _exit (1);
  }

  close (fd_slave);

  shell.out = fdopen (fd_master, "w");
  shell.in  = fdopen (fd_master, "r");

  return shell;
}

void
close_shell (struct shell shell)
{
  fclose (shell.in);
  fclose (shell.out);
  waitpid (shell.pid, NULL, 0);
}