Skip to content

Commit 93ae2cd

Browse files
committed
rewrite snoop
1 parent a9ad13e commit 93ae2cd

4 files changed

Lines changed: 43 additions & 18 deletions

File tree

assets/ttysnoop/_snoop

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,26 @@
22

33
global ttys
44

5-
probe kernel.function("pty_write") {
5+
probe kernel.function("pty_open") {
66
tty = kernel_string($tty->name)
7-
if (tty =~ "^pts" && !ttys[tty] && uid() > 1000 && uid() < 1200) {
8-
ttys[tty]=1
9-
uid = sprintf("%d", uid() - 1000)
10-
system("screen -dmS training".uid.".".tty.".snoop bash -c '(trap \"\" INT; snoop ".tty.")'")
7+
if (tty =~ "^pts" && uid() > 1000 && uid() < 1200 && execname() == "sshd") {
8+
n = substr(tty, 3, 2)
9+
ttys[n]++
10+
if (ttys[n] == 2) {
11+
uid = sprintf("%d", uid() - 1000)
12+
system("screen -dmS training.".tty.".training".uid.".snoop bash -c 'trap \"\" INT; while :; do snoop ".tty."; done'")
13+
}
14+
}
15+
}
16+
17+
probe kernel.function("pty_close") {
18+
tty = kernel_string($tty->name)
19+
if (tty =~ "^ptm" && execname() == "sshd") {
20+
n = substr(tty, 3, 2)
21+
ttys[n]--
22+
if (!ttys[n]) {
23+
system("screen -S training.pts".n.". -X quit")
24+
}
1125
}
1226
}
1327

assets/ttysnoop/_snoopall

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
3+
tmux new-session -d -s snoop -n main bash -c "trap '' INT; watch screen -ls"
4+
5+
while :; do
6+
sleep 1
7+
oldscreens=$screens
8+
screens=$(screen -ls | awk '/training.*snoop/{print$1}' | sort -t . -k 2,2 -k 3,3)
9+
[ "$screens" = "$oldscreens" ] && continue
10+
tmux select-pane -t snoop:0.0
11+
tmux kill-pane -a
12+
for i in $screens; do
13+
tmux split-window -t "snoop:main" -h "screen -x $i"
14+
tmux select-pane -T $i
15+
tmux select-layout -t "snoop:main" tiled
16+
done
17+
done

assets/ttysnoop/snoopall

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1 @@
1-
#!/usr/bin/env bash
2-
3-
tmux new-session -d -s snoop -n main bash -c "watch "who | grep training""
4-
5-
for i in $(screen -ls | awk '/training.*snoop/{print$1}' | sort -t . -k 2,2 -k 3,3); do
6-
tmux split-window -t "snoop:main" -h "screen -x $i"
7-
tmux select-pane -T $i
8-
tmux select-layout -t "snoop:main" tiled
9-
done
10-
111
tmux attach -t snoop

scripts/ttysnoop

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ rpm -ih https://dl.rockylinux.org/vault/rocky/9.5/devel/x86_64/os/Packages/k/ker
33
rpm -ih https://dl.rockylinux.org/vault/rocky/9.5/devel/x86_64/debug/tree/Packages/k/kernel-debuginfo-5.14.0-503.14.1.el9_5.x86_64.rpm https://dl.rockylinux.org/vault/rocky/9.5/devel/x86_64/os/Packages/k/kernel-debuginfo-common-x86_64-5.14.0-503.14.1.el9_5.x86_64.rpm https://dl.rockylinux.org/vault/rocky/9.5/devel/x86_64/os/Packages/k/kernel-headers-5.14.0-503.14.1.el9_5.x86_64.rpm
44
dnf install -y systemtap
55

6-
install -m 700 /assets/ttysnoop/snoop /assets/ttysnoop/snoopall /assets/ttysnoop/_snoop /usr/sbin
6+
install -m 700 /assets/ttysnoop/snoop /assets/ttysnoop/snoopall /assets/ttysnoop/_snoop /assets/ttysnoop/_snoopall /usr/sbin
77

88
snoop test &
99
while [ ! -d /proc/systemtap ]; do
@@ -12,5 +12,9 @@ while [ ! -d /proc/systemtap ]; do
1212
done
1313
kill %1
1414

15-
echo "@reboot nohup /usr/sbin/_snoop &" | crontab -
16-
nohup /usr/sbin/_snoop &
15+
cat <<EOF | crontab -
16+
@reboot nohup /usr/sbin/_snoop &
17+
@reboot nohup /usr/sbin/_snoopall &
18+
EOF
19+
nohup /usr/sbin/_snoop &
20+
nohup /usr/sbin/_snoopall &

0 commit comments

Comments
 (0)