Skip to content

Instantly share code, notes, and snippets.

@benmmurphy
Created February 6, 2017 12:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benmmurphy/d2918d3aaea46372501b851814f4ce8a to your computer and use it in GitHub Desktop.
Save benmmurphy/d2918d3aaea46372501b851814f4ce8a to your computer and use it in GitHub Desktop.
Dump secret key in wireshark format so tls connections can be decrypted. This uses the erlang:trace functionality which may destroy the performance of your node.
DumpMS = fun() ->
FindMs = fun(Socket) ->
Pid = element(3, Socket),
Connection = sys:get_state(Pid),
State = element(2, Connection),
Session = element(18, State),
SessionId = element(2, Session),
MasterSecret = element(7, Session),
{SessionId, MasterSecret}
end,
Hex = fun(Id) -> << <<Y>> ||<<X:4>> <= Id, Y <- integer_to_list(X,16)>> end,
{ok, File} = file:open("/tmp/tls.log", [write, append]),
DebugHandler = fun DebugHandler() ->
receive
{trace, _Pid, return_from, _MFA, {ok, Socket}} ->
try
{SessionId, MasterSecret} = FindMs(Socket),
Bytes = io_lib:format("RSA Session-ID:~s Master-Key:~s~n", [Hex(SessionId), Hex(MasterSecret)]),
file:write(File, Bytes),
ok
catch C:E ->
ok
end,
DebugHandler();
quit ->
file:close(File),
ok;
_ ->
DebugHandler()
end,
ok
end,
Pid = spawn(DebugHandler),
register(ms_tracer, Pid),
erlang:trace(processes, true, [{tracer, Pid}, call]),
erlang:trace_pattern({ssl, connect, 2}, [{['_', '_'], [], [{return_trace}]}]),
erlang:trace_pattern({ssl, connect, 3}, [{['_', '_', '_'], [], [{return_trace}]}]),
erlang:trace_pattern({ssl, connect, 4}, [{['_', '_', '_', '_'], [], [{return_trace}]}]),
Pid
end.
QuitMS = fun() ->
erlang:trace(all, false, [{tracer, whereis(ms_tracer)}, call]),
ms_tracer ! quit
end.
@benmmurphy
Copy link
Author

tested in R19. won't work in other versions of erlang because of changed record format.

@Ryanauger95
Copy link

@BenMurphy, Where does this code go? I am currently using TLS-PSK and trying to pull out the psk-identity for authentication purposes. Where is this code that the Socket is a very large tuple?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment