Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 2 | # |
jeromemarchand | 8b17dc3 | 2018-08-04 07:09:36 +0200 | [diff] [blame] | 3 | # sslsniff Captures data on read/recv or write/send functions of OpenSSL, |
| 4 | # GnuTLS and NSS |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 5 | # For Linux, uses BCC, eBPF. |
| 6 | # |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 7 | # USAGE: sslsniff.py [-h] [-p PID] [-u UID] [-x] [-c COMM] [-o] [-g] [-n] [-d] |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 8 | # [--hexdump] [--max-buffer-size SIZE] [-l] [--handshake] |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 9 | # |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 10 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 11 | # |
| 12 | # 12-Aug-2016 Adrian Lopez Created this. |
| 13 | # 13-Aug-2016 Mark Drayton Fix SSL_Read |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 14 | # 17-Aug-2016 Adrian Lopez Capture GnuTLS and add options |
| 15 | # |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 16 | |
| 17 | from __future__ import print_function |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 18 | from bcc import BPF |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 19 | import argparse |
Matthias Hörmann | 1b7aab1 | 2020-07-03 13:54:24 +0200 | [diff] [blame] | 20 | import binascii |
| 21 | import textwrap |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 22 | |
| 23 | # arguments |
| 24 | examples = """examples: |
| 25 | ./sslsniff # sniff OpenSSL and GnuTLS functions |
| 26 | ./sslsniff -p 181 # sniff PID 181 only |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 27 | ./sslsniff -u 1000 # sniff only UID 1000 |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 28 | ./sslsniff -c curl # sniff curl command only |
| 29 | ./sslsniff --no-openssl # don't show OpenSSL calls |
| 30 | ./sslsniff --no-gnutls # don't show GnuTLS calls |
jeromemarchand | 8b17dc3 | 2018-08-04 07:09:36 +0200 | [diff] [blame] | 31 | ./sslsniff --no-nss # don't show NSS calls |
Matthias Hörmann | d40c3a7 | 2020-07-03 14:31:32 +0200 | [diff] [blame] | 32 | ./sslsniff --hexdump # show data as hex instead of trying to decode it as UTF-8 |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 33 | ./sslsniff -x # show process UID and TID |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 34 | ./sslsniff -l # show function latency |
| 35 | ./sslsniff -l --handshake # show SSL handshake latency |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 36 | """ |
| 37 | parser = argparse.ArgumentParser( |
| 38 | description="Sniff SSL data", |
| 39 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 40 | epilog=examples) |
htbegin | 5ac5d6e | 2017-05-24 22:53:17 +0800 | [diff] [blame] | 41 | parser.add_argument("-p", "--pid", type=int, help="sniff this PID only.") |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 42 | parser.add_argument("-u", "--uid", type=int, default=None, |
| 43 | help="sniff this UID only.") |
| 44 | parser.add_argument("-x", "--extra", action="store_true", |
| 45 | help="show extra fields (UID, TID)") |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 46 | parser.add_argument("-c", "--comm", |
| 47 | help="sniff only commands matching string.") |
| 48 | parser.add_argument("-o", "--no-openssl", action="store_false", dest="openssl", |
| 49 | help="do not show OpenSSL calls.") |
| 50 | parser.add_argument("-g", "--no-gnutls", action="store_false", dest="gnutls", |
| 51 | help="do not show GnuTLS calls.") |
jeromemarchand | 8b17dc3 | 2018-08-04 07:09:36 +0200 | [diff] [blame] | 52 | parser.add_argument("-n", "--no-nss", action="store_false", dest="nss", |
| 53 | help="do not show NSS calls.") |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 54 | parser.add_argument('-d', '--debug', dest='debug', action='count', default=0, |
| 55 | help='debug mode.') |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 56 | parser.add_argument("--ebpf", action="store_true", |
| 57 | help=argparse.SUPPRESS) |
Matthias Hörmann | d91b31a | 2020-07-06 09:38:39 +0200 | [diff] [blame] | 58 | parser.add_argument("--hexdump", action="store_true", dest="hexdump", |
| 59 | help="show data as hexdump instead of trying to decode it as UTF-8") |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 60 | parser.add_argument('--max-buffer-size', type=int, default=8192, |
| 61 | help='Size of captured buffer') |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 62 | parser.add_argument("-l", "--latency", action="store_true", |
| 63 | help="show function latency") |
| 64 | parser.add_argument("--handshake", action="store_true", |
| 65 | help="show SSL handshake latency, enabled only if latency option is on.") |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 66 | args = parser.parse_args() |
| 67 | |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 68 | |
| 69 | prog = """ |
| 70 | #include <linux/ptrace.h> |
| 71 | #include <linux/sched.h> /* For TASK_COMM_LEN */ |
| 72 | |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 73 | #define MAX_BUF_SIZE __MAX_BUF_SIZE__ |
| 74 | |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 75 | struct probe_SSL_data_t { |
| 76 | u64 timestamp_ns; |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 77 | u64 delta_ns; |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 78 | u32 pid; |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 79 | u32 tid; |
| 80 | u32 uid; |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 81 | u32 len; |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 82 | int buf_filled; |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 83 | int rw; |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 84 | char comm[TASK_COMM_LEN]; |
| 85 | u8 buf[MAX_BUF_SIZE]; |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 86 | }; |
| 87 | |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 88 | #define BASE_EVENT_SIZE ((size_t)(&((struct probe_SSL_data_t*)0)->buf)) |
| 89 | #define EVENT_SIZE(X) (BASE_EVENT_SIZE + ((size_t)(X))) |
| 90 | |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 91 | BPF_PERCPU_ARRAY(ssl_data, struct probe_SSL_data_t, 1); |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 92 | BPF_PERF_OUTPUT(perf_SSL_rw); |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 93 | |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 94 | BPF_HASH(start_ns, u32); |
| 95 | BPF_HASH(bufs, u32, u64); |
| 96 | |
| 97 | int probe_SSL_rw_enter(struct pt_regs *ctx, void *ssl, void *buf, int num) { |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 98 | int ret; |
| 99 | u32 zero = 0; |
Hengqi Chen | 151fe19 | 2021-05-16 17:18:27 +0800 | [diff] [blame] | 100 | u64 pid_tgid = bpf_get_current_pid_tgid(); |
| 101 | u32 pid = pid_tgid >> 32; |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 102 | u32 tid = pid_tgid; |
| 103 | u32 uid = bpf_get_current_uid_gid(); |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 104 | u64 ts = bpf_ktime_get_ns(); |
Hengqi Chen | 151fe19 | 2021-05-16 17:18:27 +0800 | [diff] [blame] | 105 | |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 106 | PID_FILTER |
| 107 | UID_FILTER |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 108 | |
Hengqi Chen | 151fe19 | 2021-05-16 17:18:27 +0800 | [diff] [blame] | 109 | bufs.update(&tid, (u64*)&buf); |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 110 | start_ns.update(&tid, &ts); |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 114 | static int SSL_exit(struct pt_regs *ctx, int rw) { |
| 115 | int ret; |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 116 | u32 zero = 0; |
Hengqi Chen | 151fe19 | 2021-05-16 17:18:27 +0800 | [diff] [blame] | 117 | u64 pid_tgid = bpf_get_current_pid_tgid(); |
| 118 | u32 pid = pid_tgid >> 32; |
| 119 | u32 tid = (u32)pid_tgid; |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 120 | u32 uid = bpf_get_current_uid_gid(); |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 121 | u64 ts = bpf_ktime_get_ns(); |
Hengqi Chen | 151fe19 | 2021-05-16 17:18:27 +0800 | [diff] [blame] | 122 | |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 123 | PID_FILTER |
| 124 | UID_FILTER |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 125 | |
Hengqi Chen | 151fe19 | 2021-05-16 17:18:27 +0800 | [diff] [blame] | 126 | u64 *bufp = bufs.lookup(&tid); |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 127 | if (bufp == 0) |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 128 | return 0; |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 129 | |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 130 | u64 *tsp = start_ns.lookup(&tid); |
| 131 | if (tsp == 0) |
| 132 | return 0; |
| 133 | |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 134 | int len = PT_REGS_RC(ctx); |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 135 | if (len <= 0) // no data |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 136 | return 0; |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 137 | |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 138 | struct probe_SSL_data_t *data = ssl_data.lookup(&zero); |
| 139 | if (!data) |
| 140 | return 0; |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 141 | |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 142 | data->timestamp_ns = ts; |
| 143 | data->delta_ns = ts - *tsp; |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 144 | data->pid = pid; |
| 145 | data->tid = tid; |
| 146 | data->uid = uid; |
| 147 | data->len = (u32)len; |
| 148 | data->buf_filled = 0; |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 149 | data->rw = rw; |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 150 | u32 buf_copy_size = min((size_t)MAX_BUF_SIZE, (size_t)len); |
| 151 | |
| 152 | bpf_get_current_comm(&data->comm, sizeof(data->comm)); |
| 153 | |
| 154 | if (bufp != 0) |
| 155 | ret = bpf_probe_read_user(&data->buf, buf_copy_size, (char *)*bufp); |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 156 | |
Hengqi Chen | 151fe19 | 2021-05-16 17:18:27 +0800 | [diff] [blame] | 157 | bufs.delete(&tid); |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 158 | start_ns.delete(&tid); |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 159 | |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 160 | if (!ret) |
| 161 | data->buf_filled = 1; |
| 162 | else |
| 163 | buf_copy_size = 0; |
| 164 | |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 165 | perf_SSL_rw.perf_submit(ctx, data, EVENT_SIZE(buf_copy_size)); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | int probe_SSL_read_exit(struct pt_regs *ctx) { |
| 170 | return (SSL_exit(ctx, 0)); |
| 171 | } |
| 172 | |
| 173 | int probe_SSL_write_exit(struct pt_regs *ctx) { |
| 174 | return (SSL_exit(ctx, 1)); |
| 175 | } |
| 176 | |
| 177 | BPF_PERF_OUTPUT(perf_SSL_do_handshake); |
| 178 | |
| 179 | int probe_SSL_do_handshake_enter(struct pt_regs *ctx, void *ssl) { |
| 180 | u64 pid_tgid = bpf_get_current_pid_tgid(); |
| 181 | u32 pid = pid_tgid >> 32; |
| 182 | u32 tid = (u32)pid_tgid; |
| 183 | u64 ts = bpf_ktime_get_ns(); |
| 184 | |
| 185 | PID_FILTER |
| 186 | UID_FILTER |
| 187 | |
| 188 | start_ns.update(&tid, &ts); |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | int probe_SSL_do_handshake_exit(struct pt_regs *ctx) { |
| 193 | u32 zero = 0; |
| 194 | u64 pid_tgid = bpf_get_current_pid_tgid(); |
| 195 | u32 pid = pid_tgid >> 32; |
| 196 | u32 tid = (u32)pid_tgid; |
| 197 | u32 uid = bpf_get_current_uid_gid(); |
| 198 | u64 ts = bpf_ktime_get_ns(); |
| 199 | int ret; |
| 200 | |
| 201 | PID_FILTER |
| 202 | UID_FILTER |
| 203 | |
| 204 | u64 *tsp = start_ns.lookup(&tid); |
| 205 | if (tsp == 0) |
| 206 | return 0; |
| 207 | |
| 208 | ret = PT_REGS_RC(ctx); |
| 209 | if (ret <= 0) // handshake failed |
| 210 | return 0; |
| 211 | |
| 212 | struct probe_SSL_data_t *data = ssl_data.lookup(&zero); |
| 213 | if (!data) |
| 214 | return 0; |
| 215 | |
| 216 | data->timestamp_ns = ts; |
| 217 | data->delta_ns = ts - *tsp; |
| 218 | data->pid = pid; |
| 219 | data->tid = tid; |
| 220 | data->uid = uid; |
| 221 | data->len = ret; |
| 222 | data->buf_filled = 0; |
| 223 | data->rw = 2; |
| 224 | bpf_get_current_comm(&data->comm, sizeof(data->comm)); |
| 225 | start_ns.delete(&tid); |
| 226 | |
| 227 | perf_SSL_do_handshake.perf_submit(ctx, data, EVENT_SIZE(0)); |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | """ |
| 231 | |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 232 | if args.pid: |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 233 | prog = prog.replace('PID_FILTER', 'if (pid != %d) { return 0; }' % args.pid) |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 234 | else: |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 235 | prog = prog.replace('PID_FILTER', '') |
| 236 | |
| 237 | if args.uid is not None: |
| 238 | prog = prog.replace('UID_FILTER', 'if (uid != %d) { return 0; }' % args.uid) |
| 239 | else: |
| 240 | prog = prog.replace('UID_FILTER', '') |
| 241 | |
| 242 | prog = prog.replace('__MAX_BUF_SIZE__', str(args.max_buffer_size)) |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 243 | |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 244 | if args.debug or args.ebpf: |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 245 | print(prog) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 246 | if args.ebpf: |
| 247 | exit() |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 248 | |
| 249 | |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 250 | b = BPF(text=prog) |
| 251 | |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 252 | # It looks like SSL_read's arguments aren't available in a return probe so you |
| 253 | # need to stash the buffer address in a map on the function entry and read it |
| 254 | # on its exit (Mark Drayton) |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 255 | # |
| 256 | if args.openssl: |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 257 | b.attach_uprobe(name="ssl", sym="SSL_write", |
| 258 | fn_name="probe_SSL_rw_enter", pid=args.pid or -1) |
| 259 | b.attach_uretprobe(name="ssl", sym="SSL_write", |
| 260 | fn_name="probe_SSL_write_exit", pid=args.pid or -1) |
| 261 | b.attach_uprobe(name="ssl", sym="SSL_read", |
| 262 | fn_name="probe_SSL_rw_enter", pid=args.pid or -1) |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 263 | b.attach_uretprobe(name="ssl", sym="SSL_read", |
Paul Chaignon | d73c58f | 2017-01-21 14:25:41 +0100 | [diff] [blame] | 264 | fn_name="probe_SSL_read_exit", pid=args.pid or -1) |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 265 | if args.latency and args.handshake: |
| 266 | b.attach_uprobe(name="ssl", sym="SSL_do_handshake", |
| 267 | fn_name="probe_SSL_do_handshake_enter", pid=args.pid or -1) |
| 268 | b.attach_uretprobe(name="ssl", sym="SSL_do_handshake", |
| 269 | fn_name="probe_SSL_do_handshake_exit", pid=args.pid or -1) |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 270 | |
| 271 | if args.gnutls: |
| 272 | b.attach_uprobe(name="gnutls", sym="gnutls_record_send", |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 273 | fn_name="probe_SSL_rw_enter", pid=args.pid or -1) |
| 274 | b.attach_uretprobe(name="gnutls", sym="gnutls_record_send", |
| 275 | fn_name="probe_SSL_write_exit", pid=args.pid or -1) |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 276 | b.attach_uprobe(name="gnutls", sym="gnutls_record_recv", |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 277 | fn_name="probe_SSL_rw_enter", pid=args.pid or -1) |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 278 | b.attach_uretprobe(name="gnutls", sym="gnutls_record_recv", |
Paul Chaignon | d73c58f | 2017-01-21 14:25:41 +0100 | [diff] [blame] | 279 | fn_name="probe_SSL_read_exit", pid=args.pid or -1) |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 280 | |
jeromemarchand | 8b17dc3 | 2018-08-04 07:09:36 +0200 | [diff] [blame] | 281 | if args.nss: |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 282 | b.attach_uprobe(name="nspr4", sym="PR_Write", |
| 283 | fn_name="probe_SSL_rw_enter", pid=args.pid or -1) |
| 284 | b.attach_uretprobe(name="nspr4", sym="PR_Write", |
| 285 | fn_name="probe_SSL_write_exit", pid=args.pid or -1) |
| 286 | b.attach_uprobe(name="nspr4", sym="PR_Send", |
| 287 | fn_name="probe_SSL_rw_enter", pid=args.pid or -1) |
| 288 | b.attach_uretprobe(name="nspr4", sym="PR_Send", |
| 289 | fn_name="probe_SSL_write_exit", pid=args.pid or -1) |
| 290 | b.attach_uprobe(name="nspr4", sym="PR_Read", |
| 291 | fn_name="probe_SSL_rw_enter", pid=args.pid or -1) |
jeromemarchand | 8b17dc3 | 2018-08-04 07:09:36 +0200 | [diff] [blame] | 292 | b.attach_uretprobe(name="nspr4", sym="PR_Read", |
| 293 | fn_name="probe_SSL_read_exit", pid=args.pid or -1) |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 294 | b.attach_uprobe(name="nspr4", sym="PR_Recv", |
| 295 | fn_name="probe_SSL_rw_enter", pid=args.pid or -1) |
jeromemarchand | 8b17dc3 | 2018-08-04 07:09:36 +0200 | [diff] [blame] | 296 | b.attach_uretprobe(name="nspr4", sym="PR_Recv", |
| 297 | fn_name="probe_SSL_read_exit", pid=args.pid or -1) |
| 298 | |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 299 | # define output data structure in Python |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 300 | |
| 301 | |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 302 | # header |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 303 | header = "%-12s %-18s %-16s %-7s %-6s" % ("FUNC", "TIME(s)", "COMM", "PID", "LEN") |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 304 | |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 305 | if args.extra: |
| 306 | header += " %-7s %-7s" % ("UID", "TID") |
| 307 | |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 308 | if args.latency: |
| 309 | header += " %-7s" % ("LAT(ms)") |
| 310 | |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 311 | print(header) |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 312 | # process event |
| 313 | start = 0 |
| 314 | |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 315 | def print_event_rw(cpu, data, size): |
| 316 | print_event(cpu, data, size, "perf_SSL_rw") |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 317 | |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 318 | def print_event_handshake(cpu, data, size): |
| 319 | print_event(cpu, data, size, "perf_SSL_do_handshake") |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 320 | |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 321 | def print_event(cpu, data, size, evt): |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 322 | global start |
Xiaozhou Liu | 51d62d3 | 2019-02-15 13:03:05 +0800 | [diff] [blame] | 323 | event = b[evt].event(data) |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 324 | if event.len <= args.max_buffer_size: |
| 325 | buf_size = event.len |
| 326 | else: |
| 327 | buf_size = args.max_buffer_size |
| 328 | |
| 329 | if event.buf_filled == 1: |
| 330 | buf = bytearray(event.buf[:buf_size]) |
| 331 | else: |
| 332 | buf_size = 0 |
| 333 | buf = b"" |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 334 | |
| 335 | # Filter events by command |
| 336 | if args.comm: |
keyolk | 49fdec6 | 2020-10-08 05:45:00 +0000 | [diff] [blame] | 337 | if not args.comm == event.comm.decode('utf-8', 'replace'): |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 338 | return |
| 339 | |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 340 | if start == 0: |
| 341 | start = event.timestamp_ns |
| 342 | time_s = (float(event.timestamp_ns - start)) / 1000000000 |
| 343 | |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 344 | lat_str = "%.3f" % (event.delta_ns / 1000000) if event.delta_ns else "N/A" |
| 345 | |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 346 | s_mark = "-" * 5 + " DATA " + "-" * 5 |
| 347 | |
| 348 | e_mark = "-" * 5 + " END DATA " + "-" * 5 |
| 349 | |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 350 | truncated_bytes = event.len - buf_size |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 351 | if truncated_bytes > 0: |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 352 | e_mark = "-" * 5 + " END DATA (TRUNCATED, " + str(truncated_bytes) + \ |
| 353 | " bytes lost) " + "-" * 5 |
| 354 | |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 355 | base_fmt = "%(func)-12s %(time)-18.9f %(comm)-16s %(pid)-7d %(len)-6d" |
| 356 | |
| 357 | if args.extra: |
| 358 | base_fmt += " %(uid)-7d %(tid)-7d" |
| 359 | |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 360 | if args.latency: |
| 361 | base_fmt += " %(lat)-7s" |
| 362 | |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 363 | fmt = ''.join([base_fmt, "\n%(begin)s\n%(data)s\n%(end)s\n\n"]) |
Matthias Hörmann | d91b31a | 2020-07-06 09:38:39 +0200 | [diff] [blame] | 364 | if args.hexdump: |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 365 | unwrapped_data = binascii.hexlify(buf) |
| 366 | data = textwrap.fill(unwrapped_data.decode('utf-8', 'replace'), width=32) |
Matthias Hörmann | d91b31a | 2020-07-06 09:38:39 +0200 | [diff] [blame] | 367 | else: |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 368 | data = buf.decode('utf-8', 'replace') |
| 369 | |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 370 | rw_event = { |
| 371 | 0: "READ/RECV", |
| 372 | 1: "WRITE/SEND", |
| 373 | 2: "HANDSHAKE" |
| 374 | } |
| 375 | |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 376 | fmt_data = { |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 377 | 'func': rw_event[event.rw], |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 378 | 'time': time_s, |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 379 | 'lat': lat_str, |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 380 | 'comm': event.comm.decode('utf-8', 'replace'), |
| 381 | 'pid': event.pid, |
| 382 | 'tid': event.tid, |
| 383 | 'uid': event.uid, |
| 384 | 'len': event.len, |
| 385 | 'begin': s_mark, |
| 386 | 'end': e_mark, |
| 387 | 'data': data |
| 388 | } |
| 389 | |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 390 | # use base_fmt if no buf filled |
| 391 | if buf_size == 0: |
| 392 | print(base_fmt % fmt_data) |
| 393 | else: |
| 394 | print(fmt % fmt_data) |
Slava Bacherikov | 91a7983 | 2021-11-21 15:31:49 +0200 | [diff] [blame] | 395 | |
Tao Xu | ba86086 | 2022-01-28 03:28:41 +0800 | [diff] [blame^] | 396 | b["perf_SSL_rw"].open_perf_buffer(print_event_rw) |
| 397 | b["perf_SSL_do_handshake"].open_perf_buffer(print_event_handshake) |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 398 | while 1: |
Jerome Marchand | 5167127 | 2018-12-19 01:57:24 +0100 | [diff] [blame] | 399 | try: |
| 400 | b.perf_buffer_poll() |
| 401 | except KeyboardInterrupt: |
| 402 | exit() |