summaryrefslogtreecommitdiffstats
path: root/cddl/contrib/dtracetoolkit/Apps/shellsnoop
blob: 69ff379a65194e0c3774157ca6734b5161825f91 (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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/bin/sh
#
# shellsnoop - A program to print read/write details from shells,
#	       such as keystrokes and command outputs.
#	       Written using DTrace (Solaris 10 3/05).
#
# This program sounds somewhat dangerous (snooping keystrokes), but is
# no more so than /usr/bin/truss, and both need root or dtrace privileges to
# run. In fact, less dangerous, as we only print visible text (not password
# text, for example). Having said that, it goes without saying that this
# program shouldn't be used for breeching privacy of other users.
#
# This was written as a tool to demonstrate the capabilities of DTrace.
#
# $Id: shellsnoop 19 2007-09-12 07:47:59Z brendan $
#
# USAGE:	shellsnoop [-hqsv] [-p PID] [-u UID]
#
#		-q		# quiet, only print data
#		-s		# include start time, us
#		-v		# include start time, string
#		-p PID		# process ID to snoop
#		-u UID		# user ID to snoop
#  eg,
#		shellsnoop		# default output
#		shellsnoop -v		# human readable timestamps
#		shellsnoop -p 1892	# snoop this PID only
#		shellsnoop -qp 1892	# watch this PID data only
# 	
# FIELDS:
#		UID		User ID
#		PID		process ID
#		PPID		parent process ID
#		COMM		command name
#		DIR		direction (R read, W write)
#		TEXT		text contained in the read/write
#		TIME		timestamp for the command, us
#		STRTIME		timestamp for the command, string
#
# SEE ALSO: ttywatcher
#
# COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
#
# CDDL HEADER START
#
#  The contents of this file are subject to the terms of the
#  Common Development and Distribution License, Version 1.0 only
#  (the "License").  You may not use this file except in compliance
#  with the License.
#
#  You can obtain a copy of the license at Docs/cddl1.txt
#  or http://www.opensolaris.org/os/licensing.
#  See the License for the specific language governing permissions
#  and limitations under the License.
#
# CDDL HEADER END
#
# Author: Brendan Gregg  [Sydney, Australia]
#
# 28-Mar-2004	Brendan Gregg	Created this.
# 21-Jan-2005	   "	  "	Wrapped in sh to provide options.
# 30-Nov-2005	   "	  "	Fixed trailing buffer text bug.
# 30-Nov-2005	   "	  "	Fixed sh no keystroke text in quiet bug.
# 30-Nov-2005	   "	  "	Last update.
# 


##############################
# --- Process Arguments ---
#
opt_pid=0; opt_uid=0; opt_time=0; opt_timestr=0; opt_quiet=0; opt_debug=0
filter=0; pid=0; uid=0

while getopts dhp:qsu:v name
do
	case $name in
	d)	opt_debug=1 ;;
	p)	opt_pid=1; pid=$OPTARG ;;
	q)	opt_quiet=1 ;;
	s)	opt_time=1 ;;
	u)	opt_uid=1; uid=$OPTARG ;;
	v)	opt_timestr=1 ;;
	h|?)	cat <<-END >&2
		USAGE: shellsnoop [-hqsv] [-p PID] [-u UID]
		       shellsnoop		# default output
		                -q		# quiet, only print data
		                -s		# include start time, us
		                -v		# include start time, string
		                -p PID		# process ID to snoop
		                -u UID		# user ID to snoop
		END
		exit 1
	esac
done

if [ $opt_quiet -eq 1 ]; then
	opt_time=0; opt_timestr=0
fi
if [ $opt_pid -eq 1 -o $opt_uid -eq 1 ]; then
	filter=1
fi


#################################
# --- Main Program, DTrace ---
#
dtrace -n '
 /*
  * Command line arguments
  */
 inline int OPT_debug 	= '$opt_debug';
 inline int OPT_quiet 	= '$opt_quiet';
 inline int OPT_pid 	= '$opt_pid';
 inline int OPT_uid 	= '$opt_uid';
 inline int OPT_time 	= '$opt_time';
 inline int OPT_timestr	= '$opt_timestr';
 inline int FILTER 	= '$filter';
 inline int PID 	= '$pid';
 inline int UID 	= '$uid';
 
 #pragma D option quiet
 #pragma D option switchrate=20hz
 
 /*
  * Print header
  */
 dtrace:::BEGIN /OPT_time == 1/
 { 
 	printf("%-14s ","TIME");
 }
 dtrace:::BEGIN /OPT_timestr == 1/
 { 
 	printf("%-20s ","STRTIME");
 }
 dtrace:::BEGIN /OPT_quiet == 0/
 {
	printf("%5s %5s %8s %3s  %s\n", "PID", "PPID", "CMD", "DIR", "TEXT");
 }

 /*
  * Remember this PID is a shell child
  */
 syscall::execve:entry
 /execname == "sh"   || execname == "ksh"  || execname == "csh"  || 
  execname == "tcsh" || execname == "zsh"  || execname == "bash"/
 {
	child[pid] = 1;
 
 }
 syscall::execve:entry
 /(OPT_pid == 1 && PID != ppid) || (OPT_uid == 1 && UID != uid)/
 {
	/* forget if filtered */
	child[pid] = 0;
 }

 /*
  * Print shell keystrokes
  */
 syscall::write:entry, syscall::read:entry
 /(execname == "sh"   || execname == "ksh"  || execname == "csh"  ||
  execname == "tcsh" || execname == "zsh"  || execname == "bash")
  && (arg0 >= 0 && arg0 <= 2)/
 {
	self->buf = arg1;
 }
 syscall::write:entry, syscall::read:entry
 /(OPT_pid == 1 && PID != pid) || (OPT_uid == 1 && UID != uid)/
 {
	self->buf = 0;
 }
 syscall::write:return, syscall::read:return
 /self->buf && child[pid] == 0 && OPT_time == 1/
 {
 	printf("%-14d ", timestamp/1000);
 }
 syscall::write:return, syscall::read:return
 /self->buf && child[pid] == 0 && OPT_timestr == 1/
 {
	printf("%-20Y ", walltimestamp);
 }
 syscall::write:return, syscall::read:return
 /self->buf && child[pid] == 0 && OPT_quiet == 0/
 {
	this->text = (char *)copyin(self->buf, arg0);
	this->text[arg0] = '\'\\0\'';
 
	printf("%5d %5d %8s %3s  %s\n", pid, curpsinfo->pr_ppid, execname, 
	    probefunc == "read" ? "R" : "W", stringof(this->text));
 }
 syscall::write:return
 /self->buf && child[pid] == 0 && OPT_quiet == 1/
 {
	this->text = (char *)copyin(self->buf, arg0);
	this->text[arg0] = '\'\\0\'';
	printf("%s", stringof(this->text));
 }
 syscall::read:return
 /self->buf && execname == "sh" && child[pid] == 0 && OPT_quiet == 1/
 {
	this->text = (char *)copyin(self->buf, arg0);
	this->text[arg0] = '\'\\0\'';
	printf("%s", stringof(this->text));
 }
 syscall::write:return, syscall::read:return
 /self->buf && child[pid] == 0/
 {
	self->buf = 0;
 }

 /*
  * Print command output
  */
 syscall::write:entry, syscall::read:entry
 /child[pid] == 1 && (arg0 == 1 || arg0 == 2)/
 {
	self->buf = arg1;
 }
 syscall::write:return, syscall::read:return
 /self->buf && OPT_time == 1/
 {
 	printf("%-14d ", timestamp/1000);
 }
 syscall::write:return, syscall::read:return
 /self->buf && OPT_timestr == 1/
 {
	printf("%-20Y ", walltimestamp);
 }
 syscall::write:return, syscall::read:return
 /self->buf && OPT_quiet == 0/
 {
	this->text = (char *)copyin(self->buf, arg0);
	this->text[arg0] = '\'\\0\'';
 
	printf("%5d %5d %8s %3s  %s", pid, curpsinfo->pr_ppid, execname,
	    probefunc == "read" ? "R" : "W", stringof(this->text));
 
	/* here we check if a newline is needed */
	this->length = strlen(this->text);
	printf("%s", this->text[this->length - 1] == '\'\\n\'' ? "" : "\n");
	self->buf = 0;
 }
 syscall::write:return, syscall::read:return
 /self->buf && OPT_quiet == 1/
 {
	this->text = (char *)copyin(self->buf, arg0);
	this->text[arg0] = '\'\\0\'';
	printf("%s", stringof(this->text));
	self->buf = 0;
 }

 /*
  *  Cleanup
  */
 syscall::exit:entry
 {
	child[pid] = 0;

	/* debug */
	this->parent = (char *)curthread->td_proc->p_pptr->p_comm;
	OPT_debug == 1 ? printf("PID %d CMD %s exited. (%s)\n",
	 pid, execname, stringof(this->parent)) : 1;
 }
'
OpenPOWER on IntegriCloud