Class: PWN::Plugins::REPL::PwnAIInput

Inherits:
Object
  • Object
show all
Defined in:
lib/pwn/plugins/repl.rb

Overview

Custom input handler for pwn-ai to support multi-line submissions:

  • Use only SHIFT+ENTER to insert a newline (continue editing the prompt to the AI).

  • Plain ENTER submits the full (possibly multi-line) prompt to the AI.

  • Multi-line pastes are supported (Reline handles n in buffer; submit with ENTER).

Strict SHIFT+ENTER only — no Ctrl+J, Alt-Enter, or other fallbacks (per requirements).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePwnAIInput

Returns a new instance of PwnAIInput.



24
25
26
# File 'lib/pwn/plugins/repl.rb', line 24

def initialize
  @line_buffer = ''
end

Instance Attribute Details

#line_bufferObject (readonly)

Returns the value of attribute line_buffer.



22
23
24
# File 'lib/pwn/plugins/repl.rb', line 22

def line_buffer
  @line_buffer
end

Instance Method Details

#readline(prompt) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pwn/plugins/repl.rb', line 28

def readline(prompt)
  # SHIFT+ENTER escape sequences (byte arrays). These are terminal-dependent.
  # Listed common ones for xterm, VTE (terminator), kitty, wezterm, etc.
  # (with modifyOtherKeys / extended-keys enabled).
  #
  # For tmux + terminator (or similar):
  #   In ~/.tmux.conf (then `tmux kill-server` + new session):
  #     set -g extended-keys on
  #     set -g xterm-keys on
  #   Use TERM=xterm-256color (or equivalent that supports the CSI) in your terminal profile.
  #
  # The bindings make matching sequences produce :key_newline (insert \n without submit).
  #
  # If after typing text + SHIFT+ENTER it still submits instead of newline:
  #   1. Apply the tmux.conf + TERM changes above and fully restart tmux.
  #   2. In your *real* terminal (the one running `pwn`), run a capture script from /tmp ONLY:
  #        ruby /tmp/capture_keys.rb
  #      (Debugging scripts must live in /tmp per user rule; never commit them to /opt/pwn.)
  #   3. Paste the exact bytes array for the SHIFT+ENTER press here so it can be added to the list.
  shift_enter_seqs = [
    [27, 91, 49, 51, 59, 50, 126],             # \e[13;2~
    [27, 91, 50, 55, 59, 50, 59, 49, 51, 126], # \e[27;2;13~
    [27, 91, 49, 51, 59, 50, 117],             # \e[13;2u (CSI u)
    [27, 91, 50, 55, 59, 50, 59, 49, 51, 117], # \e[27;2;13u
    [27, 91, 49, 59, 50, 126],                 # \e[1;2~
    [27, 13],                                  # \e\r (ESC+CR variant)
    [27, 10],                                  # \e\n (ESC+LF variant)
    [27, 91, 13, 59, 50, 126],                 # \e[13;2~ alt numeric
    [27, 91, 49, 59, 50, 117],                 # \e[1;2u
    [27, 91, 50, 55, 59, 50, 13, 126]          # \e[27;2;13~ variant
  ]

  shift_enter_seqs.each do |seq|
    # Pass the byte array *directly* (required pattern; no .bytes, no string forms)
    Reline.config.add_oneshot_key_binding(seq, :key_newline)
  end

  begin
    # readmultiline with confirm block that *always* returns true:
    #   => default (plain) ENTER triggers finish/submit of the (multi-line) buffer
    # SHIFT+ENTER (matched seq) triggers :key_newline (insert \n, stay in edit mode)
    # Reline handles multi-line pastes by splitting on \n in the buffer.
    @line_buffer = Reline.readmultiline(prompt, true) { |_buffer| true } || ''
  ensure
    Reline.config.reset_oneshot_key_bindings
  end
  @line_buffer
end

#tty?Boolean

Compatibility with Pry input expectations

Returns:

  • (Boolean)


78
79
80
# File 'lib/pwn/plugins/repl.rb', line 78

def tty?
  true
end

#winsizeObject



82
83
84
# File 'lib/pwn/plugins/repl.rb', line 82

def winsize
  [TTY::Screen.rows || 24, TTY::Screen.columns || 80]
end