extract-handshakes: add BTF approach and fix compatibility
cover extract-handshakes: add BTF approach and fix compatibility
While trying to decrypt WireGuard traffic in Wireshark, I noticed the extract-handshakes utility broke since upstreaming in Linux v5.6 (2019).
Attached is a patch to fix the main issue, and another one to remove a dependency on the WireGuard kernel module sources by relying on BTF.
Tested on Arch Linux (7.0.10-arch1-1) and Ubuntu 24.04 (6.8.0-41-generic), both have BTF support.
Peter Wu (2):
extract-handshakes: fix compatibility with upstream WireGuard module
extract-handshakes: add a more user-friendly BTF approach
contrib/extract-handshakes/README | 7 +- .../extract-handshakes/extract-handshakes.sh | 25 ++++--- contrib/extract-handshakes/make-offsets.sh | 70 +++++++++++++++++++ 3 files changed, 93 insertions(+), 9 deletions(-) create mode 100755 contrib/extract-handshakes/make-offsets.sh
Signature · 1 line
2.54.0
PATCH 1/2 extract-handshakes: fix compatibility with upstream WireGuard module
Fix probe target, it got prefixed with wg_ in WireGuard 0.0.20181006 to prepare for upstream Linux inclusion. Fix the trace directory, since Linux v6.17 the old location has been disabled by default.
--- .../extract-handshakes/extract-handshakes.sh | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
contrib/extract-handshakes/extract-handshakes.sh +11 -6
@@ -34,17 +34,22 @@ for key in "${!OFFSETS[@]}"; do done done +# Use the new tracefs dir, since Linux v4.1. The old debugfs tracing directory +# is gone since Linux v6.17 without CONFIG_TRACEFS_AUTOMOUNT_DEPRECATED=y. +tracefs=/sys/kernel/tracing +[ -e "$tracefs" ] || tracefs=/sys/kernel/debug/tracing + turn_off() { set +e - [[ -f /sys/kernel/debug/tracing/events/wireguard/idxadd/enable ]] || exit - echo 0 > /sys/kernel/debug/tracing/events/wireguard/idxadd/enable - echo "-:wireguard/idxadd" >> /sys/kernel/debug/tracing/kprobe_events + [[ -f "$tracefs/events/wireguard/idxadd/enable" ]] || exit + echo 0 > "$tracefs/events/wireguard/idxadd/enable" + echo "-:wireguard/idxadd" >> "$tracefs/kprobe_events" exit } trap turn_off INT TERM EXIT -echo "p:wireguard/idxadd index_hashtable_insert ${ARGS[*]}" >> /sys/kernel/debug/tracing/kprobe_events -echo 1 > /sys/kernel/debug/tracing/events/wireguard/idxadd/enable +echo "p:wireguard/idxadd wg_index_hashtable_insert ${ARGS[*]}" >> "$tracefs/kprobe_events" +echo 1 > "$tracefs/events/wireguard/idxadd/enable" unpack_u64() { local i expanded="$1"
@@ -77,4 +82,4 @@ while read -r line; do done echo " $key = $(printf "$bytes" | base64)" done -done < /sys/kernel/debug/tracing/trace_pipe +done < "$tracefs/trace_pipe"
Signature · 1 line
2.54.0
PATCH 2/2 extract-handshakes: add a more user-friendly BTF approach
In reply to Peter WuThe previous implementation requires a copy of WireGuard kernel module module sources matching the current kernel and a writeable directory. Add an alternative implementation that reads the module offsets at runtime using BTF (BPF Type Format).
The previous module-based implementation is maintained in case BTF is unavailable, like hardened systems with structure layout randomization.
A fprobetrace-based approach was also considered as that would avoid the need to manually extract BTF offsets. It is currently not feasible however since the wg_index_hashtable_insert entry parameter cannot be typecast to the larger structure it was embedded in.
--- contrib/extract-handshakes/README | 7 +- .../extract-handshakes/extract-handshakes.sh | 8 ++- contrib/extract-handshakes/make-offsets.sh | 70 +++++++++++++++++++ 3 files changed, 82 insertions(+), 3 deletions(-) create mode 100755 contrib/extract-handshakes/make-offsets.sh
contrib/extract-handshakes/README +6 -1
@@ -6,7 +6,12 @@ to them being sent, via kprobes. It exports the bare minimum to be able to then decrypt all packets in the handshake and in the subsequent transport data session. -Build: +These probes depend on knowledge of kernel structure offsets. Ideally +from BTF (BPF Type Format), available with CONFIG_DEBUG_INFO_BTF=y +and Linux 5.2+. On systems where this is not available, an alternative +approach is possible when WireGuard module sources are available. + +Build (only if BTF is unavailable): $ make
contrib/extract-handshakes/extract-handshakes.sh +6 -2
@@ -2,13 +2,17 @@ # SPDX-License-Identifier: GPL-2.0 # # Copyright (C) 2015-2026 Jason A. Donenfeld <[email protected]>. All Rights Reserved. -# Copyright (C) 2017-2018 Peter Wu <[email protected]>. All Rights Reserved. +# Copyright (C) 2017-2026 Peter Wu <[email protected]>. All Rights Reserved. set -e ME_DIR="${BASH_SOURCE[0]}" ME_DIR="${ME_DIR%/*}" -source "$ME_DIR/offsets.include" || { echo "Did you forget to run make?" >&2; exit 1; } +if [ -e "$ME_DIR/offsets.include" ]; then + source "$ME_DIR/offsets.include" +else + source "$ME_DIR/make-offsets.sh" +fi case "$(uname -m)" in x86_64) ARGUMENT_REGISTER="%si" ;;
contrib/extract-handshakes/make-offsets.sh new file +70 -0
70 lines not on this page · raw message
Signature · 1 line
2.54.0