T 136 | 000003E0: 68 69 73 20 64 6F 6D 61 69 6E 20 69 73 20 65 73 his domain is es 137 | 000003F0: 74 61 62 6C 69 73 68 65 64 20 74 6F 20 62 65 20 tablished to be 138 | 139 | SSL Session: 1820201001719DF42ECCA1D289C3D32E0AA0454B50E8AF00E8A65B0108F209A8 140 | [SSL_read] 93.184.216.34:443 --> 100.97.20.44:45836 141 | 00000000: 75 73 65 64 20 66 6F 72 20 69 6C 6C 75 73 74 72 used for illustr 142 | 00000010: 61 74 69 76 65 20 65 78 61 6D 70 6C 65 73 20 69 ative examples i 143 | 00000020: 6E 20 64 6F 63 75 6D 65 6E 74 73 2E 20 59 6F 75 n documents. You 144 | 00000030: 20 6D 61 79 20 75 73 65 20 74 68 69 73 0A 20 20 may use this. 145 | 00000040: 20 20 64 6F 6D 61 69 6E 20 69 6E 20 65 78 61 6D domain in exam 146 | 00000050: 70 6C 65 73 20 77 69 74 68 6F 75 74 20 70 72 69 ples without pri 147 | 00000060: 6F 72 20 63 6F 6F 72 64 69 6E 61 74 69 6F 6E 20 or coordination 148 | 00000070: 6F 72 20 61 73 6B 69 6E 67 20 66 6F 72 20 70 65 or asking for pe 149 | 00000080: 72 6D 69 73 73 69 6F 6E 2E 3C 2F 70 3E 0A 20 20 rmission.
. 150 | 00000090: 20 20 3C 70 3E 3C 61 20 68 72 65 66 3D 22 68 74More informat 154 | 000000D0: 69 6F 6E 2E 2E 2E 3C 2F 61 3E 3C 2F 70 3E 0A 3C ion...
.< 155 | 000000E0: 2F 64 69 76 3E 0A 3C 2F 62 6F 64 79 3E 0A 3C 2F /div>.. 156 | 000000F0: 68 74 6D 6C 3E 0A html>. 157 | ``` 158 | 159 | ## Dependencies 160 | This program uses the [frida](https://www.frida.re/) framework to perform code injection. 161 | 162 | Frida can be installed as follows: `sudo pip install frida` 163 | 164 | ## TODO 165 | 166 | - Add support for processes that communicate via SSL without using [libssl](https://wiki.openssl.org/index.php/Libssl_API). 167 | - Allow user to run *ssl_logger* before starting the process to be logged. 168 | 169 | 170 | ## Disclaimer 171 | 172 | This is not an official Google product. -------------------------------------------------------------------------------- /ssl_logger.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 Google Inc. All Rights Reserved. 2 | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """Decrypts and logs a process's SSL traffic. 16 | 17 | Hooks the functions SSL_read() and SSL_write() in a given process and logs the 18 | decrypted data to the console and/or to a pcap file. 19 | 20 | Typical usage example: 21 | 22 | ssl_log("wget", "log.pcap", True) 23 | 24 | Dependencies: 25 | frida (https://www.frida.re/): 26 | sudo pip install frida 27 | hexdump (https://bitbucket.org/techtonik/hexdump/) if using verbose output: 28 | sudo pip install hexdump 29 | """ 30 | 31 | __author__ = "geffner@google.com (Jason Geffner)" 32 | __version__ = "1.0" 33 | 34 | 35 | import argparse 36 | import os 37 | import platform 38 | import pprint 39 | import random 40 | import signal 41 | import socket 42 | import struct 43 | import time 44 | 45 | import frida 46 | 47 | try: 48 | import hexdump # pylint: disable=g-import-not-at-top 49 | except ImportError: 50 | pass 51 | 52 | 53 | _FRIDA_SCRIPT = """ 54 | /** 55 | * Initializes 'addresses' dictionary and NativeFunctions. 56 | */ 57 | function initializeGlobals() 58 | { 59 | global.addresses = {}; 60 | 61 | var resolver = new ApiResolver("module"); 62 | 63 | var exps = [ 64 | ["*libssl*", 65 | ["SSL_read", "SSL_write", "SSL_get_fd", "SSL_get_session", 66 | "SSL_SESSION_get_id"]], 67 | [Process.platform == "darwin" ? "*libsystem*" : "*libc*", 68 | ["getpeername", "getsockname"]] 69 | ]; 70 | for (var i = 0; i < exps.length; i++) 71 | { 72 | var lib = exps[i][0]; 73 | var names = exps[i][1]; 74 | 75 | for (var j = 0; j < names.length; j++) 76 | { 77 | var name = names[j]; 78 | var matches = resolver.enumerateMatchesSync("exports:" + lib + "!" + 79 | name); 80 | if (matches.length == 0) 81 | { 82 | throw "Could not find " + lib + "!" + name; 83 | } 84 | else if (matches.length != 1) 85 | { 86 | // Sometimes Frida returns duplicates. 87 | var address = 0; 88 | var s = ""; 89 | var duplicates_only = true; 90 | for (var k = 0; k < matches.length; k++) 91 | { 92 | if (s.length != 0) 93 | { 94 | s += ", "; 95 | } 96 | s += matches[k].name + "@" + matches[k].address; 97 | if (address == 0) 98 | { 99 | address = matches[k].address; 100 | } 101 | else if (!address.equals(matches[k].address)) 102 | { 103 | duplicates_only = false; 104 | } 105 | } 106 | if (!duplicates_only) 107 | { 108 | throw "More than one match found for " + lib + "!" + name + ": " + 109 | s; 110 | } 111 | } 112 | addresses[name] = matches[0].address; 113 | } 114 | } 115 | 116 | global.SSL_get_fd = new NativeFunction(addresses["SSL_get_fd"], "int", 117 | ["pointer"]); 118 | global.SSL_get_session = new NativeFunction(addresses["SSL_get_session"], 119 | "pointer", ["pointer"]); 120 | global.SSL_SESSION_get_id = new NativeFunction(addresses["SSL_SESSION_get_id"], 121 | "pointer", ["pointer", "pointer"]); 122 | global.getpeername = new NativeFunction(addresses["getpeername"], "int", ["int", 123 | "pointer", "pointer"]); 124 | global.getsockname = new NativeFunction(addresses["getsockname"], "int", ["int", 125 | "pointer", "pointer"]); 126 | } 127 | initializeGlobals(); 128 | 129 | /** 130 | * Returns a dictionary of a sockfd's "src_addr", "src_port", "dst_addr", and 131 | * "dst_port". 132 | * @param {int} sockfd The file descriptor of the socket to inspect. 133 | * @param {boolean} isRead If true, the context is an SSL_read call. If 134 | * false, the context is an SSL_write call. 135 | * @return {dict} Dictionary of sockfd's "src_addr", "src_port", "dst_addr", 136 | * and "dst_port". 137 | */ 138 | function getPortsAndAddresses(sockfd, isRead) 139 | { 140 | var message = {}; 141 | 142 | var addrlen = Memory.alloc(4); 143 | var addr = Memory.alloc(16); 144 | 145 | var src_dst = ["src", "dst"]; 146 | for (var i = 0; i < src_dst.length; i++) 147 | { 148 | Memory.writeU32(addrlen, 16); 149 | if ((src_dst[i] == "src") ^ isRead) 150 | { 151 | getsockname(sockfd, addr, addrlen); 152 | } 153 | else 154 | { 155 | getpeername(sockfd, addr, addrlen); 156 | } 157 | message[src_dst[i] + "_port"] = Memory.readU16(addr.add(2)); 158 | message[src_dst[i] + "_addr"] = Memory.readU32(addr.add(4)); 159 | } 160 | 161 | return message; 162 | } 163 | 164 | /** 165 | * Get the session_id of SSL object and return it as a hex string. 166 | * @param {!NativePointer} ssl A pointer to an SSL object. 167 | * @return {dict} A string representing the session_id of the SSL object's 168 | * SSL_SESSION. For example, 169 | * "59FD71B7B90202F359D89E66AE4E61247954E28431F6C6AC46625D472FF76336". 170 | */ 171 | function getSslSessionId(ssl) 172 | { 173 | var session = SSL_get_session(ssl); 174 | if (session == 0) 175 | { 176 | return 0; 177 | } 178 | var len = Memory.alloc(4); 179 | var p = SSL_SESSION_get_id(session, len); 180 | len = Memory.readU32(len); 181 | 182 | var session_id = ""; 183 | for (var i = 0; i < len; i++) 184 | { 185 | // Read a byte, convert it to a hex string (0xAB ==> "AB"), and append 186 | // it to session_id. 187 | session_id += 188 | ("0" + Memory.readU8(p.add(i)).toString(16).toUpperCase()).substr(-2); 189 | } 190 | 191 | return session_id; 192 | } 193 | 194 | Interceptor.attach(addresses["SSL_read"], 195 | { 196 | onEnter: function (args) 197 | { 198 | var message = getPortsAndAddresses(SSL_get_fd(args[0]), true); 199 | message["ssl_session_id"] = getSslSessionId(args[0]); 200 | message["function"] = "SSL_read"; 201 | this.message = message; 202 | this.buf = args[1]; 203 | }, 204 | onLeave: function (retval) 205 | { 206 | retval |= 0; // Cast retval to 32-bit integer. 207 | if (retval <= 0) 208 | { 209 | return; 210 | } 211 | send(this.message, Memory.readByteArray(this.buf, retval)); 212 | } 213 | }); 214 | 215 | Interceptor.attach(addresses["SSL_write"], 216 | { 217 | onEnter: function (args) 218 | { 219 | var message = getPortsAndAddresses(SSL_get_fd(args[0]), false); 220 | message["ssl_session_id"] = getSslSessionId(args[0]); 221 | message["function"] = "SSL_write"; 222 | send(message, Memory.readByteArray(args[1], parseInt(args[2]))); 223 | }, 224 | 225 | onLeave: function (retval) 226 | { 227 | } 228 | }); 229 | """ 230 | 231 | 232 | # ssl_session[