From 535c9a00a322efdd4d43f62cf6c9999eb3146d31 Mon Sep 17 00:00:00 2001 From: TomasGl Date: Tue, 1 Feb 2022 13:01:31 +0300 Subject: [PATCH] Use a more reliable way to split the KDE konsole (#2026) * Use a more reliable way to split the KDE konsole * Changed xml parsing to pure parsing * Update CHANGELOG.md --- CHANGELOG.md | 2 ++ pwnlib/util/misc.py | 26 ++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42a5012bcd..bbc2a60599 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,9 +65,11 @@ The table below shows which release corresponds to each branch, and what date th ## 4.9.0 (`dev`) - [#2011][2011] Fix tube's debug output of same byte compression +- [#2023][2023] Support KDE Konsole in run_in_new_terminal function - [#2027][2027] Fix ELF.libc_start_main_return with glibc 2.34 [2011]: https://github.com/Gallopsled/pwntools/pull/2011 +[2023]: https://github.com/Gallopsled/pwntools/pull/2023 [2027]: https://github.com/Gallopsled/pwntools/pull/2027 ## 4.8.0 (`beta`) diff --git a/pwnlib/util/misc.py b/pwnlib/util/misc.py index 255b7899ef..646e89284e 100644 --- a/pwnlib/util/misc.py +++ b/pwnlib/util/misc.py @@ -300,13 +300,31 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr terminal = 'x-terminal-emulator' args = ['-e'] elif 'KONSOLE_VERSION' in os.environ and which('qdbus'): - konsole_window = os.environ['KONSOLE_DBUS_WINDOW'].split('/')[-1] - konsole_dbus_service = os.environ['KONSOLE_DBUS_SERVICE'] qdbus = which('qdbus') - # SPLIT - subprocess.run((qdbus, konsole_dbus_service, '/konsole/MainWindow_{}'.format(konsole_window), + window_id = os.environ['WINDOWID'] + konsole_dbus_service = os.environ['KONSOLE_DBUS_SERVICE'] + + with subprocess.Popen((qdbus, konsole_dbus_service), stdout=subprocess.PIPE) as proc: + lines = proc.communicate()[0].decode().split('\n') + + # Iterate over all MainWindows + for line in lines: + parts = line.split('/') + if len(parts) == 3 and parts[2].startswith('MainWindow_'): + name = parts[2] + with subprocess.Popen((qdbus, konsole_dbus_service, '/konsole/' + name, + 'org.kde.KMainWindow.winId'), stdout=subprocess.PIPE) as proc: + target_window_id = proc.communicate()[0].decode().strip() + if target_window_id == window_id: + break + else: + log.error('MainWindow not found') + + # Split + subprocess.run((qdbus, konsole_dbus_service, '/konsole/' + name, 'org.kde.KMainWindow.activateAction', 'split-view-left-right'), stdout=subprocess.DEVNULL) + # Find new session with subprocess.Popen((qdbus, konsole_dbus_service, os.environ['KONSOLE_DBUS_WINDOW'], 'org.kde.konsole.Window.sessionList'), stdout=subprocess.PIPE) as proc: session_list = map(int, proc.communicate()[0].decode().split())