Skip to content

Commit a4998bb

Browse files
author
Jethro Beekman
committed
Finished dynamic accessors. GnuTLS and NSS should now be supported out of the box if you have the appropriate debugging symbols installed.
* Added dependency on libdw (from Fedora elfutils)
1 parent 7a8312f commit a4998bb

File tree

8 files changed

+504
-35
lines changed

8 files changed

+504
-35
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
SOURCES=ssltrace.cpp openssl.cpp nss.cpp gnutls.cpp
2-
HEADERS=ssltrace.h nssimpl.h nsstypes.h gnutlstypes.h
1+
SOURCES=ssltrace.cpp openssl.cpp nss.cpp gnutls.cpp symbols.cpp
2+
HEADERS=ssltrace.h nssimpl.h nsstypes.h gnutlstypes.h symbols.h
33
OBJECTS=$(SOURCES:.cpp=.o)
44
OUTPUT=ssltrace.so
55

66
all: $(SOURCES) $(HEADERS) $(OUTPUT) Makefile
77

88
$(OUTPUT): $(OBJECTS)
9-
g++ -g -shared -Wall $(OBJECTS) -o $@ -ldl
9+
g++ -g -shared -Wall $(OBJECTS) -o $@ -ldl -ldw
1010

1111
.cpp.o:
12-
g++ -g -fPIC -std=gnu++11 -Wall -I/usr/include/nspr -c $< -o $@
12+
g++ -g -fPIC -std=gnu++11 -Wall -D_GNU_SOURCE -I/usr/include/nspr -I/usr/include/elfutils -c $< -o $@
1313

1414
clean:
1515
rm -f $(OBJECTS) $(OUTPUT)

gnutls.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
*/
1919

2020
#include "ssltrace.h"
21+
#include "symbols.h"
2122

2223
#include <gnutls/gnutls.h>
24+
#include <dlfcn.h>
2325

2426
namespace gnutlstypes
2527
{
@@ -34,17 +36,26 @@ namespace gnutlstypes
3436
};
3537
using namespace gnutlstypes::__accessor;
3638

37-
static __attribute__((constructor)) void init_offsets()
39+
static void load_offsets(void* fn)
3840
{
39-
//libgnutls26:amd64=2.12.23-1ubuntu4.2
40-
__set_offset("security_parameters_st.master_secret", 0x16,48);
41-
__set_offset("security_parameters_st.client_random", 0x46,32);
42-
__set_offset("gnutls_session_int.security_parameters", 0,0/*TODO*/);
41+
static bool load=false;
42+
if (!load)
43+
{
44+
Dl_info dli={0};
45+
if (dladdr(fn,&dli)==0)
46+
{
47+
ssltrace_die("Unable to get libgnutls.so filename");
48+
}
49+
else
50+
{
51+
load=symbols_load_all(dli.dli_fname,__get_parameter_names(),ssltrace_debug,__set_offset,__set_offset);
52+
}
53+
}
4354
}
4455

4556
WRAP(int,gnutls_handshake,(::gnutls_session_t session))
4657
{
47-
WRAPINIT(gnutls_handshake);
58+
WRAPINIT_FN(gnutls_handshake,load_offsets);
4859

4960
int ret=_gnutls_handshake(session);
5061

magic3.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@
172172
}
173173
}
174174

175-
operator T&()
175+
T* operator&()
176176
{
177-
if (bitfieldTable[parameter]) __accessor_exit("Attempted to obtain a reference/pointer to a bitfield");
178-
return *(T*)this;
177+
if (bitfieldTable[parameter]) __accessor_exit("Attempted to obtain a pointer to a bitfield");
178+
return (T*)this;
179179
}
180180

181181
BitfieldAccessor<T,parameter>& operator=(const T v)

nss.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
*/
1919

2020
#include "ssltrace.h"
21+
#include "symbols.h"
2122

2223
#include <nss/ssl.h>
2324
#include <nss/pk11pub.h>
2425
#include <nss/nssrwlk.h>
26+
#include <dlfcn.h>
2527
#include <stdlib.h>
28+
#include <string.h>
2629

2730
namespace nsstypes
2831
{
@@ -39,25 +42,21 @@ using namespace nsstypes::__accessor;
3942

4043
#include "nssimpl.h"
4144

42-
static __attribute__((constructor)) void init_offsets()
45+
static void load_offsets(void* fn)
4346
{
44-
//libnss3:amd64=2:3.15.4-0ubuntu0.13.10.2
45-
__set_offset("ssl3CipherSpec.master_secret", 0x80,0/*TODO*/);
46-
__set_offset("SSL3Random.rand", 0,32);
47-
__set_offset("SSL3HandshakeState.client_random", 0x20,0/*TODO*/);
48-
__set_offset("ssl3State.cwSpec", 0x10,0/*TODO*/);
49-
__set_offset("ssl3State.hs", 0x60,0/*TODO*/);
50-
__set_offset("sslOptions.noLocks", 0x18,16,0);
51-
__set_offset("sslSocket.fd", 0,0/*TODO*/);
52-
__set_offset("sslSocket.opt", 0x10,0/*TODO*/);
53-
__set_offset("sslSocket.handshakeCallback", 0x308,0/*TODO*/);
54-
__set_offset("sslSocket.handshakeCallbackData", 0x310,0/*TODO*/);
55-
__set_offset("sslSocket.recvBufLock", 0x360,0/*TODO*/);
56-
__set_offset("sslSocket.xmitBufLock", 0x368,0/*TODO*/);
57-
__set_offset("sslSocket.firstHandshakeLock", 0x370,0/*TODO*/);
58-
__set_offset("sslSocket.ssl3HandshakeLock", 0x378,0/*TODO*/);
59-
__set_offset("sslSocket.specLock", 0x380,0/*TODO*/);
60-
__set_offset("sslSocket.ssl3", 0x608,0/*TODO*/);
47+
static bool load=false;
48+
if (!load)
49+
{
50+
Dl_info dli={0};
51+
if (dladdr(fn,&dli)==0)
52+
{
53+
ssltrace_die("Unable to get libssl3.so filename");
54+
}
55+
else
56+
{
57+
load=symbols_load_all(dli.dli_fname,__get_parameter_names(),ssltrace_debug,__set_offset,__set_offset);
58+
}
59+
}
6160
}
6261

6362
static int strsame(const char* a,const char* b)
@@ -141,7 +140,7 @@ void nss_SSLHandshakeCallback(PRFileDesc *fd,void *client_data)
141140

142141
WRAP(SECStatus,SSL_HandshakeCallback,(PRFileDesc *fd,SSLHandshakeCallback cb,void *client_data))
143142
{
144-
WRAPINIT(SSL_HandshakeCallback);
143+
WRAPINIT_FN(SSL_HandshakeCallback,load_offsets);
145144

146145
if (cb)
147146
{
@@ -162,7 +161,7 @@ WRAP(SECStatus,SSL_HandshakeCallback,(PRFileDesc *fd,SSLHandshakeCallback cb,voi
162161

163162
WRAP(PRFileDesc*,SSL_ImportFD,(PRFileDesc *model, PRFileDesc *fd))
164163
{
165-
WRAPINIT(SSL_ImportFD);
164+
WRAPINIT_FN(SSL_ImportFD,load_offsets);
166165

167166
PRFileDesc* ret=_SSL_ImportFD(model,fd);
168167

ssltrace.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919

2020
#include "ssltrace.h"
2121

22-
#define _GNU_SOURCE
23-
2422
#include <dlfcn.h>
2523
#include <link.h>
2624
#include <stdio.h>
2725
#include <stdlib.h>
26+
#include <stdarg.h>
2827
#include <string.h>
2928
#include <errno.h>
3029

@@ -112,6 +111,16 @@ void *ssltrace_dlsym(const char *symbol)
112111
return ret;
113112
}
114113

114+
void ssltrace_debug(const char* fmt, ...)
115+
{
116+
va_list ap;
117+
118+
fputs(SSLTRACE ": ",ssltrace_log_handle());
119+
va_start(ap, fmt);vfprintf(stderr, fmt, ap);va_end(ap);
120+
fputc('\n',ssltrace_log_handle());
121+
fflush(ssltrace_log_handle());
122+
}
123+
115124
void ssltrace_die(const char* message)
116125
{
117126
fprintf(ssltrace_log_handle(),SSLTRACE ": %s\n",message);

ssltrace.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@
3535
ssltrace_die("Unable to resolve symbol " #name); \
3636
}
3737

38+
#define WRAPINIT_FN(name,fn) \
39+
WRAPINIT(name); \
40+
fn((void*)_##name);
41+
3842
void *ssltrace_dlsym(const char *symbol);
3943
void ssltrace_die(const char* message);
44+
void ssltrace_debug(const char* fmt, ...);
4045
void ssltrace_trace_sessionid(unsigned char* sessionid, unsigned int sessionid_length, unsigned char* masterkey, unsigned int masterkey_length);
4146
void ssltrace_trace_clientrandom(unsigned char* clientrandom, unsigned int clientrandom_length, unsigned char* masterkey, unsigned int masterkey_length);
4247

0 commit comments

Comments
 (0)