Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 3 | # sslsniff Captures data on read/recv or write/send functions of OpenSSL and |
| 4 | # GnuTLS |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 5 | # For Linux, uses BCC, eBPF. |
| 6 | # |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 7 | # USAGE: sslsniff.py [-h] [-p PID] [-c COMM] [-o] [-g] [-d] |
| 8 | # |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 9 | # 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 Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 13 | # 17-Aug-2016 Adrian Lopez Capture GnuTLS and add options |
| 14 | # |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 15 | |
| 16 | from __future__ import print_function |
| 17 | import ctypes as ct |
| 18 | from bcc import BPF |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 19 | import argparse |
| 20 | |
| 21 | # arguments |
| 22 | examples = """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 |
| 28 | """ |
| 29 | parser = argparse.ArgumentParser( |
| 30 | description="Sniff SSL data", |
| 31 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 32 | epilog=examples) |
htbegin | 5ac5d6e | 2017-05-24 22:53:17 +0800 | [diff] [blame] | 33 | parser.add_argument("-p", "--pid", type=int, help="sniff this PID only.") |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 34 | parser.add_argument("-c", "--comm", |
| 35 | help="sniff only commands matching string.") |
| 36 | parser.add_argument("-o", "--no-openssl", action="store_false", dest="openssl", |
| 37 | help="do not show OpenSSL calls.") |
| 38 | parser.add_argument("-g", "--no-gnutls", action="store_false", dest="gnutls", |
| 39 | help="do not show GnuTLS calls.") |
| 40 | parser.add_argument('-d', '--debug', dest='debug', action='count', default=0, |
| 41 | help='debug mode.') |
| 42 | args = parser.parse_args() |
| 43 | |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 44 | |
| 45 | prog = """ |
| 46 | #include <linux/ptrace.h> |
| 47 | #include <linux/sched.h> /* For TASK_COMM_LEN */ |
| 48 | |
| 49 | struct probe_SSL_data_t { |
| 50 | u64 timestamp_ns; |
| 51 | u32 pid; |
| 52 | char comm[TASK_COMM_LEN]; |
Brenden Blanco | 71eb177 | 2017-04-20 09:47:28 -0700 | [diff] [blame] | 53 | char v0[464]; |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 54 | u32 len; |
| 55 | }; |
| 56 | |
| 57 | BPF_PERF_OUTPUT(perf_SSL_write); |
| 58 | |
| 59 | int probe_SSL_write(struct pt_regs *ctx, void *ssl, void *buf, int num) { |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 60 | u32 pid = bpf_get_current_pid_tgid(); |
| 61 | FILTER |
| 62 | |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 63 | struct probe_SSL_data_t __data = {0}; |
| 64 | __data.timestamp_ns = bpf_ktime_get_ns(); |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 65 | __data.pid = pid; |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 66 | __data.len = num; |
| 67 | |
| 68 | bpf_get_current_comm(&__data.comm, sizeof(__data.comm)); |
| 69 | |
| 70 | if ( buf != 0) { |
| 71 | bpf_probe_read(&__data.v0, sizeof(__data.v0), buf); |
| 72 | } |
| 73 | |
| 74 | perf_SSL_write.perf_submit(ctx, &__data, sizeof(__data)); |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | BPF_PERF_OUTPUT(perf_SSL_read); |
| 79 | |
| 80 | BPF_HASH(bufs, u32, u64); |
| 81 | |
| 82 | int probe_SSL_read_enter(struct pt_regs *ctx, void *ssl, void *buf, int num) { |
| 83 | u32 pid = bpf_get_current_pid_tgid(); |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 84 | FILTER |
| 85 | |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 86 | bufs.update(&pid, (u64*)&buf); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int probe_SSL_read_exit(struct pt_regs *ctx, void *ssl, void *buf, int num) { |
| 91 | u32 pid = bpf_get_current_pid_tgid(); |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 92 | FILTER |
| 93 | |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 94 | u64 *bufp = bufs.lookup(&pid); |
| 95 | if (bufp == 0) { |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | struct probe_SSL_data_t __data = {0}; |
| 100 | __data.timestamp_ns = bpf_ktime_get_ns(); |
| 101 | __data.pid = pid; |
| 102 | __data.len = PT_REGS_RC(ctx); |
| 103 | |
| 104 | bpf_get_current_comm(&__data.comm, sizeof(__data.comm)); |
| 105 | |
| 106 | if (bufp != 0) { |
| 107 | bpf_probe_read(&__data.v0, sizeof(__data.v0), (char *)*bufp); |
| 108 | } |
| 109 | |
| 110 | bufs.delete(&pid); |
| 111 | |
| 112 | perf_SSL_read.perf_submit(ctx, &__data, sizeof(__data)); |
| 113 | return 0; |
| 114 | } |
| 115 | """ |
| 116 | |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 117 | if args.pid: |
htbegin | 5ac5d6e | 2017-05-24 22:53:17 +0800 | [diff] [blame] | 118 | prog = prog.replace('FILTER', 'if (pid != %d) { return 0; }' % args.pid) |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 119 | else: |
| 120 | prog = prog.replace('FILTER', '') |
| 121 | |
| 122 | if args.debug: |
| 123 | print(prog) |
| 124 | |
| 125 | |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 126 | b = BPF(text=prog) |
| 127 | |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 128 | # It looks like SSL_read's arguments aren't available in a return probe so you |
| 129 | # need to stash the buffer address in a map on the function entry and read it |
| 130 | # on its exit (Mark Drayton) |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 131 | # |
| 132 | if args.openssl: |
Paul Chaignon | d73c58f | 2017-01-21 14:25:41 +0100 | [diff] [blame] | 133 | b.attach_uprobe(name="ssl", sym="SSL_write", fn_name="probe_SSL_write", |
| 134 | pid=args.pid or -1) |
| 135 | b.attach_uprobe(name="ssl", sym="SSL_read", fn_name="probe_SSL_read_enter", |
| 136 | pid=args.pid or -1) |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 137 | b.attach_uretprobe(name="ssl", sym="SSL_read", |
Paul Chaignon | d73c58f | 2017-01-21 14:25:41 +0100 | [diff] [blame] | 138 | fn_name="probe_SSL_read_exit", pid=args.pid or -1) |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 139 | |
| 140 | if args.gnutls: |
| 141 | b.attach_uprobe(name="gnutls", sym="gnutls_record_send", |
Paul Chaignon | d73c58f | 2017-01-21 14:25:41 +0100 | [diff] [blame] | 142 | fn_name="probe_SSL_write", pid=args.pid or -1) |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 143 | b.attach_uprobe(name="gnutls", sym="gnutls_record_recv", |
Paul Chaignon | d73c58f | 2017-01-21 14:25:41 +0100 | [diff] [blame] | 144 | fn_name="probe_SSL_read_enter", pid=args.pid or -1) |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 145 | b.attach_uretprobe(name="gnutls", sym="gnutls_record_recv", |
Paul Chaignon | d73c58f | 2017-01-21 14:25:41 +0100 | [diff] [blame] | 146 | fn_name="probe_SSL_read_exit", pid=args.pid or -1) |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 147 | |
| 148 | # define output data structure in Python |
| 149 | TASK_COMM_LEN = 16 # linux/sched.h |
Brenden Blanco | 71eb177 | 2017-04-20 09:47:28 -0700 | [diff] [blame] | 150 | MAX_BUF_SIZE = 464 # Limited by the BPF stack |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 151 | |
| 152 | |
| 153 | # Max size of the whole struct: 512 bytes |
| 154 | class Data(ct.Structure): |
| 155 | _fields_ = [ |
| 156 | ("timestamp_ns", ct.c_ulonglong), |
| 157 | ("pid", ct.c_uint), |
| 158 | ("comm", ct.c_char * TASK_COMM_LEN), |
| 159 | ("v0", ct.c_char * MAX_BUF_SIZE), |
| 160 | ("len", ct.c_uint) |
| 161 | ] |
| 162 | |
| 163 | |
| 164 | # header |
| 165 | print("%-12s %-18s %-16s %-6s %-6s" % ("FUNC", "TIME(s)", "COMM", "PID", |
| 166 | "LEN")) |
| 167 | |
| 168 | # process event |
| 169 | start = 0 |
| 170 | |
| 171 | |
| 172 | def print_event_write(cpu, data, size): |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 173 | print_event(cpu, data, size, "WRITE/SEND") |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 174 | |
| 175 | |
| 176 | def print_event_read(cpu, data, size): |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 177 | print_event(cpu, data, size, "READ/RECV") |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 178 | |
| 179 | |
| 180 | def print_event(cpu, data, size, rw): |
| 181 | global start |
| 182 | event = ct.cast(data, ct.POINTER(Data)).contents |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 183 | |
| 184 | # Filter events by command |
| 185 | if args.comm: |
| 186 | if not args.comm == event.comm: |
| 187 | return |
| 188 | |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 189 | if start == 0: |
| 190 | start = event.timestamp_ns |
| 191 | time_s = (float(event.timestamp_ns - start)) / 1000000000 |
| 192 | |
| 193 | s_mark = "-" * 5 + " DATA " + "-" * 5 |
| 194 | |
| 195 | e_mark = "-" * 5 + " END DATA " + "-" * 5 |
| 196 | |
| 197 | truncated_bytes = event.len - MAX_BUF_SIZE |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 198 | if truncated_bytes > 0: |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 199 | e_mark = "-" * 5 + " END DATA (TRUNCATED, " + str(truncated_bytes) + \ |
| 200 | " bytes lost) " + "-" * 5 |
| 201 | |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 202 | print("%-12s %-18.9f %-16s %-6d %-6d\n%s\n%s\n%s\n\n" % (rw, time_s, |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 203 | event.comm.decode(), |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 204 | event.pid, |
| 205 | event.len, |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 206 | s_mark, |
| 207 | event.v0.decode(), |
Adrian Lopez | d9cc3de | 2016-08-17 14:08:08 +0200 | [diff] [blame] | 208 | e_mark)) |
Adrian Lopez | d496d5c | 2016-08-16 17:49:49 +0200 | [diff] [blame] | 209 | |
| 210 | b["perf_SSL_write"].open_perf_buffer(print_event_write) |
| 211 | b["perf_SSL_read"].open_perf_buffer(print_event_read) |
| 212 | while 1: |
| 213 | b.kprobe_poll() |