-
Notifications
You must be signed in to change notification settings - Fork 89
Add key logging for TLS 1.3 and TLS 1.2 (#523) #542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nickrabbott
wants to merge
2
commits into
tlsfuzzer:master
Choose a base branch
from
nickrabbott:ssl-key-log-file
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import os | ||
| import sys | ||
| import threading | ||
| from .utils.compat import b2a_hex | ||
|
|
||
|
|
||
| def posix_lock_write(file_path, lines): | ||
| import fcntl | ||
| with open(file_path, 'a') as f: | ||
| fcntl.flock(f, fcntl.LOCK_EX) | ||
| try: | ||
| f.writelines(lines) | ||
| f.flush() | ||
| finally: | ||
| fcntl.flock(f, fcntl.LOCK_UN) | ||
|
|
||
|
|
||
| def unsafe_write(file_path, lines): | ||
| with open(file_path, 'a') as f: | ||
| f.writelines(lines) | ||
| f.flush() | ||
|
|
||
|
|
||
| class SSLKeyLogger: | ||
| """ | ||
| Write session secrets to the file pointed to by the SSLKEYLOGFILE | ||
| environment variable. Implemented to be thread-safe at the class level and | ||
| uses OS level file-locking. The file-locking implementation is a function | ||
| assigned to self.lock_write and determined by the value of sys.platform. | ||
|
|
||
| Currently, POSIX is supported for thread and process safety. If enabled for | ||
| other systems, safety is not guaranteed. | ||
|
|
||
| :param logfile_override: specify the filepath for logging | ||
| :type logfile_override: str | ||
| """ | ||
| _lock = threading.Lock() | ||
|
|
||
| def __init__(self, logfile_override=None): | ||
| self.ssl_key_logfile = os.environ.get('SSLKEYLOGFILE') | ||
| if logfile_override: | ||
| self.ssl_key_logfile = logfile_override | ||
| self.platform = sys.platform | ||
| if self.platform in ['darwin', 'linux']: | ||
| self.lock_write = posix_lock_write | ||
| else: | ||
| self.lock_write = unsafe_write | ||
|
|
||
| def log_session_keys(self, keys): | ||
| """ | ||
| Log session keys to the SSL key log file, if configured. | ||
|
|
||
| Write session keys in the `SSLKEYLOGFILE` format, allowing tools like | ||
| Wireshark to decrypt captured traffic. Each entry is written as a line | ||
| with the format: ``<LABEL> <CLIENT_RANDOM> <SECRET>``. | ||
|
|
||
| If neither the `SSLKEYLOGFILE` environment variable nor a | ||
| `logfile_override` is set, this method is a no-op. | ||
|
|
||
| :param keys: List of (label, client_random, secret) tuples. | ||
| :type keys: list[tuple[str, bytes, bytes]] | ||
| """ | ||
| if not self.ssl_key_logfile: | ||
| return | ||
|
|
||
| lines = [ | ||
| "{0} {1} {2}\n".format( | ||
| label, | ||
| b2a_hex(client_random).upper(), | ||
| b2a_hex(secret).upper() | ||
| ) | ||
| for label, client_random, secret in keys | ||
| ] | ||
|
|
||
| with self._lock: | ||
| self.lock_write(self.ssl_key_logfile, lines) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm on a mac and ".db" was the file extension