execsnoop revision 1.1 1 1.1 christos #!/bin/sh
2 1.1 christos #
3 1.1 christos # execsnoop - snoop process execution as it occurs.
4 1.1 christos # Written using DTrace (Solaris 10 3/05).
5 1.1 christos #
6 1.1 christos # $Id: execsnoop,v 1.1 2015/09/30 22:01:06 christos Exp $
7 1.1 christos #
8 1.1 christos # USAGE: execsnoop [-a|-A|-ehsvJ] [-c command]
9 1.1 christos #
10 1.1 christos # execsnoop # default output
11 1.1 christos #
12 1.1 christos # -a # print all data
13 1.1 christos # -A # dump all data, space delimited
14 1.1 christos # -e # safe output - parseable
15 1.1 christos # -s # print start time, us
16 1.1 christos # -v # print start time, string
17 1.1 christos # -J # print jail ID
18 1.1 christos # -c command # command name to snoop
19 1.1 christos # eg,
20 1.1 christos # execsnoop -v # human readable timestamps
21 1.1 christos # execsnoop -J # print jail ID
22 1.1 christos # execsnoop -c ls # snoop ls commands only
23 1.1 christos #
24 1.1 christos # The parseable output ensures that the ARGS field doesn't contain
25 1.1 christos # any "\n"s, which normally sometimes can - and would wreck postprocessing.
26 1.1 christos #
27 1.1 christos # FIELDS:
28 1.1 christos # UID User ID
29 1.1 christos # PID Process ID
30 1.1 christos # PPID Parent Process ID
31 1.1 christos # COMM command name for the process
32 1.1 christos # ARGS argument listing for the process
33 1.1 christos # JAIL ID Jail ID
34 1.1 christos # TIME timestamp for the command, us
35 1.1 christos # STRTIME timestamp for the command, string
36 1.1 christos #
37 1.1 christos # SEE ALSO: BSM auditing.
38 1.1 christos #
39 1.1 christos # COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
40 1.1 christos #
41 1.1 christos # CDDL HEADER START
42 1.1 christos #
43 1.1 christos # The contents of this file are subject to the terms of the
44 1.1 christos # Common Development and Distribution License, Version 1.0 only
45 1.1 christos # (the "License"). You may not use this file except in compliance
46 1.1 christos # with the License.
47 1.1 christos #
48 1.1 christos # You can obtain a copy of the license at Docs/cddl1.txt
49 1.1 christos # or http://www.opensolaris.org/os/licensing.
50 1.1 christos # See the License for the specific language governing permissions
51 1.1 christos # and limitations under the License.
52 1.1 christos #
53 1.1 christos # CDDL HEADER END
54 1.1 christos #
55 1.1 christos # Author: Brendan Gregg [Sydney, Australia]
56 1.1 christos #
57 1.1 christos # 27-Mar-2004 Brendan Gregg Created this.
58 1.1 christos # 21-Jan-2005 " " Wrapped in sh to provide options.
59 1.1 christos # 08-May-2005 " " Rewritten for performance.
60 1.1 christos # 14-May-2005 " " Added zonename.
61 1.1 christos # 02-Jul-2005 " " Added projid, safe printing.
62 1.1 christos # 11-Sep-2005 " " Increased switchrate.
63 1.1 christos # 11-Sep-2005 " " Last update.
64 1.1 christos #
65 1.1 christos
66 1.1 christos
67 1.1 christos ##############################
68 1.1 christos # --- Process Arguments ---
69 1.1 christos #
70 1.1 christos
71 1.1 christos ### default variables
72 1.1 christos opt_dump=0; opt_cmd=0; opt_time=0; opt_timestr=0; filter=0; command=.
73 1.1 christos opt_jailid=0; opt_safe=0
74 1.1 christos
75 1.1 christos ### process options
76 1.1 christos while getopts aAc:ehsvJ name
77 1.1 christos do
78 1.1 christos case $name in
79 1.1 christos a) opt_time=1; opt_timestr=1; opt_jailid=1 ;;
80 1.1 christos A) opt_dump=1 ;;
81 1.1 christos c) opt_cmd=1; command=$OPTARG ;;
82 1.1 christos e) opt_safe=1 ;;
83 1.1 christos s) opt_time=1 ;;
84 1.1 christos v) opt_timestr=1 ;;
85 1.1 christos J) opt_jailid=1 ;;
86 1.1 christos h|?) cat <<-END >&2
87 1.1 christos USAGE: execsnoop [-a|-A|-ehjsvJ] [-c command]
88 1.1 christos execsnoop # default output
89 1.1 christos -a # print all data
90 1.1 christos -A # dump all data, space delimited
91 1.1 christos -e # safe output, parseable
92 1.1 christos -s # print start time, us
93 1.1 christos -v # print start time, string
94 1.1 christos -J # print jail ID
95 1.1 christos -c command # command name to snoop
96 1.1 christos eg,
97 1.1 christos execsnoop -v # human readable timestamps
98 1.1 christos execsnoop -J # print jail ID
99 1.1 christos execsnoop -c ls # snoop ls commands only
100 1.1 christos END
101 1.1 christos exit 1
102 1.1 christos esac
103 1.1 christos done
104 1.1 christos
105 1.1 christos ### option logic
106 1.1 christos if [ $opt_dump -eq 1 ]; then
107 1.1 christos opt_time=0; opt_timestr=0; opt_jailid=0
108 1.1 christos fi
109 1.1 christos if [ $opt_cmd -eq 1 ]; then
110 1.1 christos filter=1
111 1.1 christos fi
112 1.1 christos
113 1.1 christos
114 1.1 christos #################################
115 1.1 christos # --- Main Program, DTrace ---
116 1.1 christos #
117 1.1 christos /usr/sbin/dtrace -n '
118 1.1 christos /*
119 1.1 christos * Command line arguments
120 1.1 christos */
121 1.1 christos inline int OPT_dump = '$opt_dump';
122 1.1 christos inline int OPT_cmd = '$opt_cmd';
123 1.1 christos inline int OPT_time = '$opt_time';
124 1.1 christos inline int OPT_timestr = '$opt_timestr';
125 1.1 christos inline int OPT_jailid = '$opt_jailid';
126 1.1 christos inline int OPT_safe = '$opt_safe';
127 1.1 christos inline int FILTER = '$filter';
128 1.1 christos inline string COMMAND = "'$command'";
129 1.1 christos
130 1.1 christos #pragma D option quiet
131 1.1 christos #pragma D option switchrate=10hz
132 1.1 christos
133 1.1 christos /*
134 1.1 christos * Print header
135 1.1 christos */
136 1.1 christos dtrace:::BEGIN
137 1.1 christos {
138 1.1 christos /* print optional headers */
139 1.1 christos OPT_time ? printf("%-14s ", "TIME") : 1;
140 1.1 christos OPT_timestr ? printf("%-20s ", "STRTIME") : 1;
141 1.1 christos OPT_jailid ? printf("%-10s ", "JAIL ID") : 1;
142 1.1 christos
143 1.1 christos /* print main headers */
144 1.1 christos OPT_dump ? printf("%s %s %s %s %s %s %s\n",
145 1.1 christos "TIME", "JAIL ID", "UID", "PID", "PPID", "COMM", "ARGS") :
146 1.1 christos printf("%5s %6s %6s %s\n", "UID", "PID", "PPID", "ARGS");
147 1.1 christos }
148 1.1 christos
149 1.1 christos /*
150 1.1 christos * Print exec event
151 1.1 christos */
152 1.1 christos syscall::execve:return
153 1.1 christos /(FILTER == 0) || (OPT_cmd == 1 && COMMAND == execname)/
154 1.1 christos {
155 1.1 christos /* print optional fields */
156 1.1 christos OPT_time ? printf("%-14d ", timestamp/1000) : 1;
157 1.1 christos OPT_timestr ? printf("%-20Y ", walltimestamp) : 1;
158 1.1 christos OPT_jailid ? printf("%-10d ", curpsinfo->pr_jailid) : 1;
159 1.1 christos
160 1.1 christos /* print main data */
161 1.1 christos OPT_dump ? printf("%d %d %d %d %d %s ", timestamp/1000,
162 1.1 christos curpsinfo->pr_jailid, uid, pid, ppid, execname) :
163 1.1 christos printf("%5d %6d %6d ", uid, pid, ppid);
164 1.1 christos OPT_safe ? printf("%S\n", curpsinfo->pr_psargs) :
165 1.1 christos printf("%s\n", curpsinfo->pr_psargs);
166 1.1 christos }
167 1.1 christos '
168