-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjson-net-raw
286 lines (248 loc) · 7.63 KB
/
json-net-raw
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# -*- tab-width: 4 -*- ;; Emacs
# vi: set filetype=sh tabstop=8 shiftwidth=8 noexpandtab :: Vi/ViM
############################################################ IDENT(1)
#
# $Title: dwatch(8) JSON module for network activity $
# $Copyright: 2014-2022 Devin Teske. All rights reserved. $
# $FrauBSD: dwatch-json/json-net-raw 2022-08-19 10:18:35 -0700 freebsdfrau $
# $Version: 0.9 $
#
############################################################ DESCRIPTION
#
# Produce JSON custom log format for network activity
#
############################################################ PROBE
: ${PROBE:=$( echo \
tcp:::debug-user, \
tcp:::state-change, \
udp:::send, \
syscall::read:return, \
syscall::recvfrom:return, \
syscall::recvmsg:return )}
############################################################ EVENT ACTION
EVENT_TEST='this->inet && this->event != ""'
############################################################ ACTIONS
exec 9<<EOF
this uint8_t inet;
this string event;
this string family;
this string local;
this string remote;
this u_char local6;
this u_char remote6;
this uint16_t lport;
this uint16_t rport;
this uint32_t length;
typedef struct sainfo {
sa_family_t sa_family;
uint16_t port;
string addr;
} sainfo_t;
#pragma D binding "1.13" sa_data_size
inline int sa_data_size = 30; /* 14 if only handling IPv4 */
#pragma D binding "1.13" sa_dummy_data
inline char *sa_dummy_data =
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
#pragma D binding "1.13" sa_data_addr
inline string sa_data_addr[sa_family_t af, char data[sa_data_size]] =
af == AF_INET ? inet_ntoa((in_addr_t *)&data[2]) :
af == AF_INET6 ? inet_ntoa6((struct in6_addr *)&data[6]) :
"<unknown>";
#pragma D binding "1.13" sa_data_port
inline uint16_t sa_data_port[sa_family_t af, char data[sa_data_size]] =
af == AF_INET ? ((data[0] & 0xFF) << 8) + (data[1] & 0xFF) :
af == AF_INET6 ? ((data[0] & 0xFF) << 8) + (data[1] & 0xFF) :
0;
#pragma D binding "1.13" translator
translator sainfo_t < struct sockaddr *SA > {
sa_family = SA == NULL ? 0 : SA->sa_family;
addr = SA == NULL ?
sa_data_addr[0, sa_dummy_data] :
sa_data_addr[SA->sa_family, SA->sa_data];
port = SA == NULL ?
sa_data_port[0, sa_dummy_data] :
sa_data_port[SA->sa_family, SA->sa_data];
};
/****************************** TCP ******************************/
tcp:::send,
tcp:::receive /* probe ID $ID */
{${TRACE:+
printf("<$ID>");}
this->length = (uint32_t)args[2]->ip_plength -
(uint8_t)args[4]->tcp_offset;
}
tcp:::debug-user /* probe ID $(( $ID + 1 )) */
{${TRACE:+
printf("<$(( $ID + 1 ))>");
}
/*
* tcpsinfo_t *
*/
this->local = args[0]->tcps_laddr;
this->lport = args[0]->tcps_lport;
this->remote = args[0]->tcps_raddr;
this->rport = args[0]->tcps_rport;
/*
* IPv6 support
*/
this->local6 = strstr(this->local, ":") != NULL ? 1 : 0;
this->remote6 = strstr(this->remote, ":") != NULL ? 1 : 0;
this->local = strjoin(strjoin(this->local6 ? "[" : "",
this->local), this->local6 ? "]" : "");
this->remote = strjoin(strjoin(this->remote6 ? "[" : "",
this->remote), this->remote6 ? "]" : "");
this->family = "tcp";
this->event = prureq_string[arg1];
this->inet = 1;
}
tcp:::state-change /* probe ID $(( $ID + 2 )) */
{${TRACE:+
printf("<$(( $ID + 2 ))>");
}
/*
* tcpsinfo_t *
*/
this->local = args[3]->tcps_laddr;
this->lport = (uint16_t)args[3]->tcps_lport;
this->remote = args[3]->tcps_raddr;
this->rport = (uint16_t)args[3]->tcps_rport;
this->to_state = (int32_t)args[3]->tcps_state;
/*
* tcplsinfo_t *
*/
this->from_state = (int32_t)args[5]->tcps_state;
/*
* IPv6 support
*/
this->local6 = strstr(this->local, ":") != NULL ? 1 : 0;
this->remote6 = strstr(this->remote, ":") != NULL ? 1 : 0;
this->local = strjoin(strjoin(this->local6 ? "[" : "",
this->local), this->local6 ? "]" : "");
this->remote = strjoin(strjoin(this->remote6 ? "[" : "",
this->remote), this->remote6 ? "]" : "");
this->family = "tcp";
this->event = this->to_state == TCPS_CLOSED ? "CLOSE" : "";
this->length = 0;
this->inet = 1;
}
/****************************** UDP ******************************/
/*
* UDP send(2) [syscall] stacks:
* sys_write -> dofilewrite -> soo_write -> sosend -> sosend_dgram -> udp:send
* sys_sendto -> sendit -> kern_sendit -> sosend -> sosend_dgram -> udp:send
* sys_sendmsg -> sendit -> kern_sendit -> sosend -> sosend_dgram -> udp:send
*/
udp:::send /* probe ID $(( $ID + 3 )) */
{${TRACE:+
printf("<$(( $ID + 3 ))>");
}
/*
* ipinfo_t *
*/
this->local = args[2]->ip_saddr;
this->remote = args[2]->ip_daddr;
/*
* udpinfo_t *
*/
this->length = (uint32_t)args[4]->udp_length - sizeof(struct udphdr);
this->lport = args[4]->udp_sport;
this->rport = args[4]->udp_dport;
/*
* IPv6 support
*/
this->local6 = strstr(this->local, ":") != NULL ? 1 : 0;
this->remote6 = strstr(this->remote, ":") != NULL ? 1 : 0;
this->local = strjoin(strjoin(this->local6 ? "[" : "",
this->local), this->local6 ? "]" : "");
this->remote = strjoin(strjoin(this->remote6 ? "[" : "",
this->remote), this->remote6 ? "]" : "");
this->family = "udp";
this->event = "SEND";
this->inet = 1;
}
/*
* UDP recv(2) [syscall] stacks:
* sys_read -> dofileread -> soreceive -> soreceive_dgram
* sys_recvfrom -> kern_recvit -> soreceive -> soreceive_dgram
* sys_recvmsg -> kern_recvit -> soreceive -> soreceive_dgram
*/
syscall::read:entry,
syscall::recvfrom:entry,
syscall::recvmsg:entry
/* probe ID $(( $ID + 4 )) */
{${TRACE:+
printf("<$(( $ID + 4 ))>");
}
this->inet = 0;
}
fbt:kernel:soreceive_dgram:entry /* probe ID $(( $ID + 5 )) */
{${TRACE:+
printf("<$(( $ID + 5 ))>");
}
this->udps = (struct socket *)args[0];
this->af = this->udps->so_proto->pr_domain->dom_family;
this->psa = (struct sockaddr **)args[1];
this->sa = (struct sockaddr *)NULL;
this->so_pcb = this->udps->so_pcb;
this->udpsinfo = xlate <udpsinfo_t> ((struct inpcb *)this->so_pcb);
/* UDP receive local */
this->local = this->udpsinfo.udps_laddr;
this->local6 = this->af == AF_INET6;
this->local = strjoin(strjoin(this->local6 ? "[" : "",
this->local), this->local6 ? "]" : "");
this->lport = this->udpsinfo.udps_lport;
this->family = "udp";
this->event = "RCVD";
this->inet = (
this->af == AF_INET ? 1 :
this->af == AF_INET6 ? 1 :
0
);
}
fbt:kernel:soreceive_dgram:return /this->inet/ /* probe ID $(( $ID + 6 )) */
{${TRACE:+
printf("<$(( $ID + 6 ))>");
}
this->sa = this->psa == NULL ? NULL : *this->psa;
this->sainfo = xlate <sainfo_t> ((struct sockaddr *)this->sa);
/* UDP receive remote */
this->remote = this->sainfo.addr;
this->remote6 = this->sainfo.sa_family == AF_INET6;
this->remote = strjoin(strjoin(this->remote6 ? "[" : "",
this->remote), this->remote6 ? "]" : "");
this->rport = this->sainfo.port;
}
syscall::read:return,
syscall::recvfrom:return,
syscall::recvmsg:return
/this->inet/ /* probe ID $(( $ID + 7 )) */
{${TRACE:+
printf("<$(( $ID + 7 ))>");
}
this->length = (uint32_t)((int)arg0 < 0 ? 0 : arg0);
this->inet = (this->length > 0);
}
EOF
ACTIONS=$( cat <&9 )
ID=$(( $ID + 8 ))
############################################################ EVENT DETAILS
exec 9<<EOF
/*
* Print event details
*/
printf("{\"report_type\":\"${PROFILE%-raw}\",\"epoch\":%u,\"family\":\"%s\",\"local\":\"%s\",\"lport\":%u,\"event\":\"%s\",\"remote\":\"%s\",\"rport\":%u,\"length\":%u}",
walltimestamp / 1000000000,
this->family,
this->local,
this->lport,
this->event,
this->remote,
this->rport,
this->length);
this->event = "";
this->inet = 0;
EOF
EVENT_DETAILS=$( cat <&9 )
################################################################################
# END
################################################################################