Skip to content
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

Support starting a kitty debugging window with the 'kitten' command #2522

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ The table below shows which release corresponds to each branch, and what date th
| [2.2.0](#220) | | Jan 5, 2015

## 5.0.0 (`dev`)
- [#2522][2522] Support starting a kitty debugging window with the 'kitten' command

[2522]: https://github.com/Gallopsled/pwntools/pull/2522

## 4.15.0 (`beta`)
- [#2508][2508] Ignore a warning when compiling with asm on nix
Expand Down
21 changes: 13 additions & 8 deletions pwnlib/util/misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import division

import json
import base64
import errno
import os
Expand Down Expand Up @@ -450,13 +451,11 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr
log.debug("Launching a new terminal: %r" % argv)

stdin = stdout = stderr = open(os.devnull, 'r+b')
if terminal == 'tmux' or terminal == 'kitty':
if terminal == 'tmux' or terminal in ('kitty', 'kitten'):
stdout = subprocess.PIPE

p = subprocess.Popen(argv, stdin=stdin, stdout=stdout, stderr=stderr, preexec_fn=preexec_fn)

kittyid = None

if terminal == 'tmux':
out, _ = p.communicate()
try:
Expand All @@ -469,16 +468,24 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr
with subprocess.Popen((qdbus, konsole_dbus_service, '/Sessions/{}'.format(last_konsole_session),
'org.kde.konsole.Session.processId'), stdout=subprocess.PIPE) as proc:
pid = int(proc.communicate()[0].decode())
elif terminal == 'kitty':
pid = p.pid

elif terminal in ('kitty', 'kitten'):
pid = None
out, _ = p.communicate()
try:
kittyid = int(out)
except ValueError:
kittyid = None
if kittyid is None:
log.error("Could not parse kitty window ID from output (%r)", out)
else:
lsout, _ = subprocess.Popen(["kitten", "@", "ls", "--match", "id:%d" % kittyid], stdin=stdin, stdout=stdout, stderr=stderr).communicate()
try:
lsj = json.loads(lsout)
pid = int(lsj[0]["tabs"][0]["windows"][0]["pid"])
except json.JSONDecodeError as e:
pid = None
log.error("Json decode failed while parsing 'kitten @ ls' output (%r) (error: %r)", lsout, e)

elif terminal == 'cmd.exe':
# p.pid is cmd.exe's pid instead of the WSL process we want to start eventually.
# I don't know how to trace the execution through Windows and back into the WSL2 VM.
Expand All @@ -503,8 +510,6 @@ def kill():
try:
if terminal == 'qdbus':
os.kill(pid, signal.SIGHUP)
elif terminal == 'kitty':
subprocess.Popen(["kitten", "@", "close-window", "--match", "id:{}".format(kittyid)], stderr=stderr)
else:
os.kill(pid, signal.SIGTERM)
except OSError:
Expand Down
Loading