Skip to content

Instantly share code, notes, and snippets.

@phemmer
Last active December 6, 2023 00:53
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save phemmer/31e89d1a3823230de1e1c47386ff508b to your computer and use it in GitHub Desktop.
Save phemmer/31e89d1a3823230de1e1c47386ff508b to your computer and use it in GitHub Desktop.
haproxy log SSL master key
global
lua-load haproxy.lua
frontend X
tcp-request session set-var(sess.ssl_session_id) ssl_fc_session_id,hex if { ssl_fc }
tcp-request content lua.ssl-log-key if { ssl_fc }
core.register_action("ssl-log-key", { "tcp-req", "http-req" }, function(txn)
local dolog = false
local ssl_session_id = txn.sc:hex(txn.sf:ssl_fc_session_id())
local ssl_session_id_var = txn:get_var("sess.ssl_session_id")
if ssl_session_id then
if not ssl_session_id_var or ssl_session_id ~= ssl_session_id_var then
dolog = true
txn:set_var("sess.ssl_session_id", ssl_session_id)
end
elseif ssl_session_id_var then
ssl_session_id = ssl_session_id_var
end
local ssl_session_key = txn.sc:hex(txn.sf:ssl_fc_session_key())
local ssl_session_key_var = txn:get_var("sess.ssl_session_key")
if ssl_session_key then
if not ssl_session_key_var or ssl_session_key ~= ssl_session_key_var then
dolog = true
txn:set_var("sess.ssl_session_key", ssl_session_key)
end
elseif ssl_session_key_var then
ssl_session_id = ssl_session_key_var
end
if dolog then
local src = txn.sf:src() .. ":" .. txn.sf:src_port()
local dst = txn.sf:dst() .. ":" .. txn.sf:dst_port()
-- The formats supported by wireshark can be found here:
-- https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob;f=epan/dissectors/packet-tls-utils.c;h=28a51fb1fb029eae5cea52d37ff5b67d9b11950f;hb=HEAD#l5209
txn:log(core.debug, "SSL " .. src .. "/" .. dst .. " RSA Session-ID:" .. ssl_session_id .. " Master-Key:" .. ssl_session_key)
end
end)
@Tiduster
Copy link

Tiduster commented Sep 8, 2020

Works perfectly !
Thanks.

@exabrial
Copy link

Where does txn:log kick out it's log entires?

@exabrial
Copy link

I found the answer, it'll go out the global log. https://www.haproxy.com/blog/introduction-to-haproxy-logging

@ElijahLynn
Copy link

ElijahLynn commented Dec 6, 2023

Here is the whole link in the comment: https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob;f=epan/dissectors/packet-tls-utils.c;h=28a51fb1fb029eae5cea52d37ff5b67d9b11950f;hb=HEAD#l5209

edit: that hostname DNS is dead now though, here is the code on GitLab https://gitlab.com/wireshark/wireshark/-/blob/master/epan/dissectors/packet-tls-utils.c, but that commit doesn't exist and the line number doesn't line up anymore. If anyone can find the right line number please post back, thanks!

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