blob: f7bc11767da6892ddd37005055a76d0e1570298e [file] [log] [blame]
Alexey Ivanov777e8022019-01-03 13:46:38 -08001#!/usr/bin/env python
Adrian Lopezd496d5c2016-08-16 17:49:49 +02002#
jeromemarchand8b17dc32018-08-04 07:09:36 +02003# sslsniff Captures data on read/recv or write/send functions of OpenSSL,
4# GnuTLS and NSS
Adrian Lopezd496d5c2016-08-16 17:49:49 +02005# For Linux, uses BCC, eBPF.
6#
Adrian Lopezd9cc3de2016-08-17 14:08:08 +02007# USAGE: sslsniff.py [-h] [-p PID] [-c COMM] [-o] [-g] [-d]
8#
Adrian Lopezd496d5c2016-08-16 17:49:49 +02009# Licensed under the Apache License, Version 2.0 (the "License")
10#
11# 12-Aug-2016 Adrian Lopez Created this.
12# 13-Aug-2016 Mark Drayton Fix SSL_Read
Adrian Lopezd9cc3de2016-08-17 14:08:08 +020013# 17-Aug-2016 Adrian Lopez Capture GnuTLS and add options
14#
Adrian Lopezd496d5c2016-08-16 17:49:49 +020015
16from __future__ import print_function
17import ctypes as ct
18from bcc import BPF
Adrian Lopezd9cc3de2016-08-17 14:08:08 +020019import argparse
20
21# arguments
22examples = """examples:
23 ./sslsniff # sniff OpenSSL and GnuTLS functions
24 ./sslsniff -p 181 # sniff PID 181 only
25 ./sslsniff -c curl # sniff curl command only
26 ./sslsniff --no-openssl # don't show OpenSSL calls
27 ./sslsniff --no-gnutls # don't show GnuTLS calls
jeromemarchand8b17dc32018-08-04 07:09:36 +020028 ./sslsniff --no-nss # don't show NSS calls
Adrian Lopezd9cc3de2016-08-17 14:08:08 +020029"""
30parser = argparse.ArgumentParser(
31 description="Sniff SSL data",
32 formatter_class=argparse.RawDescriptionHelpFormatter,
33 epilog=examples)
htbegin5ac5d6e2017-05-24 22:53:17 +080034parser.add_argument("-p", "--pid", type=int, help="sniff this PID only.")
Adrian Lopezd9cc3de2016-08-17 14:08:08 +020035parser.add_argument("-c", "--comm",
36 help="sniff only commands matching string.")
37parser.add_argument("-o", "--no-openssl", action="store_false", dest="openssl",
38 help="do not show OpenSSL calls.")
39parser.add_argument("-g", "--no-gnutls", action="store_false", dest="gnutls",
40 help="do not show GnuTLS calls.")
jeromemarchand8b17dc32018-08-04 07:09:36 +020041parser.add_argument("-n", "--no-nss", action="store_false", dest="nss",
42 help="do not show NSS calls.")
Adrian Lopezd9cc3de2016-08-17 14:08:08 +020043parser.add_argument('-d', '--debug', dest='debug', action='count', default=0,
44 help='debug mode.')
Nathan Scottcf0792f2018-02-02 16:56:50 +110045parser.add_argument("--ebpf", action="store_true",
46 help=argparse.SUPPRESS)
Adrian Lopezd9cc3de2016-08-17 14:08:08 +020047args = parser.parse_args()
48
Adrian Lopezd496d5c2016-08-16 17:49:49 +020049
50prog = """
51#include <linux/ptrace.h>
52#include <linux/sched.h> /* For TASK_COMM_LEN */
53
54struct probe_SSL_data_t {
55 u64 timestamp_ns;
56 u32 pid;
57 char comm[TASK_COMM_LEN];
Brenden Blanco71eb1772017-04-20 09:47:28 -070058 char v0[464];
Adrian Lopezd496d5c2016-08-16 17:49:49 +020059 u32 len;
60};
61
62BPF_PERF_OUTPUT(perf_SSL_write);
63
64int probe_SSL_write(struct pt_regs *ctx, void *ssl, void *buf, int num) {
Adrian Lopezd9cc3de2016-08-17 14:08:08 +020065 u32 pid = bpf_get_current_pid_tgid();
66 FILTER
67
Adrian Lopezd496d5c2016-08-16 17:49:49 +020068 struct probe_SSL_data_t __data = {0};
69 __data.timestamp_ns = bpf_ktime_get_ns();
Adrian Lopezd9cc3de2016-08-17 14:08:08 +020070 __data.pid = pid;
Adrian Lopezd496d5c2016-08-16 17:49:49 +020071 __data.len = num;
72
73 bpf_get_current_comm(&__data.comm, sizeof(__data.comm));
74
75 if ( buf != 0) {
76 bpf_probe_read(&__data.v0, sizeof(__data.v0), buf);
77 }
78
79 perf_SSL_write.perf_submit(ctx, &__data, sizeof(__data));
80 return 0;
81}
82
83BPF_PERF_OUTPUT(perf_SSL_read);
84
85BPF_HASH(bufs, u32, u64);
86
87int probe_SSL_read_enter(struct pt_regs *ctx, void *ssl, void *buf, int num) {
88 u32 pid = bpf_get_current_pid_tgid();
Adrian Lopezd9cc3de2016-08-17 14:08:08 +020089 FILTER
90
Adrian Lopezd496d5c2016-08-16 17:49:49 +020091 bufs.update(&pid, (u64*)&buf);
92 return 0;
93}
94
95int probe_SSL_read_exit(struct pt_regs *ctx, void *ssl, void *buf, int num) {
96 u32 pid = bpf_get_current_pid_tgid();
Adrian Lopezd9cc3de2016-08-17 14:08:08 +020097 FILTER
98
Adrian Lopezd496d5c2016-08-16 17:49:49 +020099 u64 *bufp = bufs.lookup(&pid);
100 if (bufp == 0) {
101 return 0;
102 }
103
104 struct probe_SSL_data_t __data = {0};
105 __data.timestamp_ns = bpf_ktime_get_ns();
106 __data.pid = pid;
107 __data.len = PT_REGS_RC(ctx);
108
109 bpf_get_current_comm(&__data.comm, sizeof(__data.comm));
110
111 if (bufp != 0) {
112 bpf_probe_read(&__data.v0, sizeof(__data.v0), (char *)*bufp);
113 }
114
115 bufs.delete(&pid);
116
117 perf_SSL_read.perf_submit(ctx, &__data, sizeof(__data));
118 return 0;
119}
120"""
121
Adrian Lopezd9cc3de2016-08-17 14:08:08 +0200122if args.pid:
htbegin5ac5d6e2017-05-24 22:53:17 +0800123 prog = prog.replace('FILTER', 'if (pid != %d) { return 0; }' % args.pid)
Adrian Lopezd9cc3de2016-08-17 14:08:08 +0200124else:
125 prog = prog.replace('FILTER', '')
126
Nathan Scottcf0792f2018-02-02 16:56:50 +1100127if args.debug or args.ebpf:
Adrian Lopezd9cc3de2016-08-17 14:08:08 +0200128 print(prog)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100129 if args.ebpf:
130 exit()
Adrian Lopezd9cc3de2016-08-17 14:08:08 +0200131
132
Adrian Lopezd496d5c2016-08-16 17:49:49 +0200133b = BPF(text=prog)
134
Adrian Lopezd496d5c2016-08-16 17:49:49 +0200135# It looks like SSL_read's arguments aren't available in a return probe so you
136# need to stash the buffer address in a map on the function entry and read it
137# on its exit (Mark Drayton)
Adrian Lopezd9cc3de2016-08-17 14:08:08 +0200138#
139if args.openssl:
Paul Chaignond73c58f2017-01-21 14:25:41 +0100140 b.attach_uprobe(name="ssl", sym="SSL_write", fn_name="probe_SSL_write",
141 pid=args.pid or -1)
142 b.attach_uprobe(name="ssl", sym="SSL_read", fn_name="probe_SSL_read_enter",
143 pid=args.pid or -1)
Adrian Lopezd9cc3de2016-08-17 14:08:08 +0200144 b.attach_uretprobe(name="ssl", sym="SSL_read",
Paul Chaignond73c58f2017-01-21 14:25:41 +0100145 fn_name="probe_SSL_read_exit", pid=args.pid or -1)
Adrian Lopezd9cc3de2016-08-17 14:08:08 +0200146
147if args.gnutls:
148 b.attach_uprobe(name="gnutls", sym="gnutls_record_send",
Paul Chaignond73c58f2017-01-21 14:25:41 +0100149 fn_name="probe_SSL_write", pid=args.pid or -1)
Adrian Lopezd9cc3de2016-08-17 14:08:08 +0200150 b.attach_uprobe(name="gnutls", sym="gnutls_record_recv",
Paul Chaignond73c58f2017-01-21 14:25:41 +0100151 fn_name="probe_SSL_read_enter", pid=args.pid or -1)
Adrian Lopezd9cc3de2016-08-17 14:08:08 +0200152 b.attach_uretprobe(name="gnutls", sym="gnutls_record_recv",
Paul Chaignond73c58f2017-01-21 14:25:41 +0100153 fn_name="probe_SSL_read_exit", pid=args.pid or -1)
Adrian Lopezd496d5c2016-08-16 17:49:49 +0200154
jeromemarchand8b17dc32018-08-04 07:09:36 +0200155if args.nss:
156 b.attach_uprobe(name="nspr4", sym="PR_Write", fn_name="probe_SSL_write",
157 pid=args.pid or -1)
158 b.attach_uprobe(name="nspr4", sym="PR_Send", fn_name="probe_SSL_write",
159 pid=args.pid or -1)
160 b.attach_uprobe(name="nspr4", sym="PR_Read", fn_name="probe_SSL_read_enter",
161 pid=args.pid or -1)
162 b.attach_uretprobe(name="nspr4", sym="PR_Read",
163 fn_name="probe_SSL_read_exit", pid=args.pid or -1)
164 b.attach_uprobe(name="nspr4", sym="PR_Recv", fn_name="probe_SSL_read_enter",
165 pid=args.pid or -1)
166 b.attach_uretprobe(name="nspr4", sym="PR_Recv",
167 fn_name="probe_SSL_read_exit", pid=args.pid or -1)
168
Adrian Lopezd496d5c2016-08-16 17:49:49 +0200169# define output data structure in Python
170TASK_COMM_LEN = 16 # linux/sched.h
Brenden Blanco71eb1772017-04-20 09:47:28 -0700171MAX_BUF_SIZE = 464 # Limited by the BPF stack
Adrian Lopezd496d5c2016-08-16 17:49:49 +0200172
173
174# Max size of the whole struct: 512 bytes
175class Data(ct.Structure):
176 _fields_ = [
177 ("timestamp_ns", ct.c_ulonglong),
178 ("pid", ct.c_uint),
179 ("comm", ct.c_char * TASK_COMM_LEN),
180 ("v0", ct.c_char * MAX_BUF_SIZE),
181 ("len", ct.c_uint)
182 ]
183
184
185# header
186print("%-12s %-18s %-16s %-6s %-6s" % ("FUNC", "TIME(s)", "COMM", "PID",
187 "LEN"))
188
189# process event
190start = 0
191
192
193def print_event_write(cpu, data, size):
Adrian Lopezd9cc3de2016-08-17 14:08:08 +0200194 print_event(cpu, data, size, "WRITE/SEND")
Adrian Lopezd496d5c2016-08-16 17:49:49 +0200195
196
197def print_event_read(cpu, data, size):
Adrian Lopezd9cc3de2016-08-17 14:08:08 +0200198 print_event(cpu, data, size, "READ/RECV")
Adrian Lopezd496d5c2016-08-16 17:49:49 +0200199
200
201def print_event(cpu, data, size, rw):
202 global start
203 event = ct.cast(data, ct.POINTER(Data)).contents
Adrian Lopezd9cc3de2016-08-17 14:08:08 +0200204
205 # Filter events by command
206 if args.comm:
207 if not args.comm == event.comm:
208 return
209
Adrian Lopezd496d5c2016-08-16 17:49:49 +0200210 if start == 0:
211 start = event.timestamp_ns
212 time_s = (float(event.timestamp_ns - start)) / 1000000000
213
214 s_mark = "-" * 5 + " DATA " + "-" * 5
215
216 e_mark = "-" * 5 + " END DATA " + "-" * 5
217
218 truncated_bytes = event.len - MAX_BUF_SIZE
Adrian Lopezd9cc3de2016-08-17 14:08:08 +0200219 if truncated_bytes > 0:
Adrian Lopezd496d5c2016-08-16 17:49:49 +0200220 e_mark = "-" * 5 + " END DATA (TRUNCATED, " + str(truncated_bytes) + \
221 " bytes lost) " + "-" * 5
222
Paul Chaignon7b911b52017-10-07 11:52:17 +0200223 fmt = "%-12s %-18.9f %-16s %-6d %-6d\n%s\n%s\n%s\n\n"
jeromemarchandb96ebcd2018-10-10 01:58:15 +0200224 print(fmt % (rw, time_s, event.comm.decode('utf-8', 'replace'),
225 event.pid, event.len, s_mark,
226 event.v0.decode('utf-8', 'replace'), e_mark))
Adrian Lopezd496d5c2016-08-16 17:49:49 +0200227
228b["perf_SSL_write"].open_perf_buffer(print_event_write)
229b["perf_SSL_read"].open_perf_buffer(print_event_read)
230while 1:
Jerome Marchand51671272018-12-19 01:57:24 +0100231 try:
232 b.perf_buffer_poll()
233 except KeyboardInterrupt:
234 exit()