pwnlib.ui — Functions for user interaction

pwnlib.ui.more(text)[source]

Shows text like the command line tool more.

It not in term_mode, just prints the data to the screen.

Parameters

text (str) – The text to show.

Returns

None

Tests:

 >>> more("text")
 text
 >>> p = testpwnproc("more('text\\n' * (term.height + 2))")
 >>> p.send(b"x")
 >>> data = p.recvall()
 >>> b"text" in data or data
 True
pwnlib.ui.options(prompt, opts, default=None)[source]

Presents the user with a prompt (typically in the form of a question) and a number of options.

Parameters
  • prompt (str) – The prompt to show

  • opts (list) – The options to show to the user

  • default – The default option to choose

Returns

The users choice in the form of an integer.

Examples:

 >>> options("Select a color", ("red", "green", "blue"), "green")
 Traceback (most recent call last):
 ...
 ValueError: options(): default must be a number or None

Tests:

 >>> p = testpwnproc("print(options('select a color', ('red', 'green', 'blue')))")
 >>> p.sendline(b"\33[C\33[A\33[A\33[B\33[1;5A\33[1;5B 0310")
 >>> _ = p.recvall()
 >>> saved_stdin = sys.stdin
 >>> try:
 ...     sys.stdin = io.TextIOWrapper(io.BytesIO(b"\n4\n\n3\n"))
 ...     with context.local(log_level="INFO"):
 ...         options("select a color A", ("red", "green", "blue"), 0)
 ...         options("select a color B", ("red", "green", "blue"))
 ... finally:
 ...     sys.stdin = saved_stdin
  [?] select a color A
        1) red
        2) green
        3) blue
      Choice [1] 0
  [?] select a color B
        1) red
        2) green
        3) blue
      Choice  [?] select a color B
        1) red
        2) green
        3) blue
      Choice  [?] select a color B
        1) red
        2) green
        3) blue
      Choice 2
pwnlib.ui.pause(n=None)[source]

Waits for either user input or a specific number of seconds.

Examples:

 >>> with context.local(log_level="INFO"):
 ...     pause(1)
 [x] Waiting
 [x] Waiting: 1...
 [+] Waiting: Done
 >>> pause("whatever")
 Traceback (most recent call last):
 ...
 ValueError: pause(): n must be a number or None

Tests:

 >>> saved_stdin = sys.stdin
 >>> try:
 ...     sys.stdin = io.TextIOWrapper(io.BytesIO(b"\n"))
 ...     with context.local(log_level="INFO"):
 ...         pause()
 ... finally:
 ...     sys.stdin = saved_stdin
 [*] Paused (press enter to continue)
 >>> p = testpwnproc("pause()")
 >>> b"Paused" in p.recvuntil(b"press any")
 True
 >>> p.send(b"x")
 >>> _ = p.recvall()
pwnlib.ui.yesno(prompt, default=None)[source]

Presents the user with prompt (typically in the form of question) which the user must answer yes or no.

Parameters
  • prompt (str) – The prompt to show

  • default – The default option; True means “yes”

Returns

True if the answer was “yes”, False if “no”

Examples:

 >>> yesno("A number:", 20)
 Traceback (most recent call last):
 ...
 ValueError: yesno(): default must be a boolean or None
 >>> saved_stdin = sys.stdin
 >>> try:
 ...     sys.stdin = io.TextIOWrapper(io.BytesIO(b"x\nyes\nno\n\n"))
 ...     yesno("is it good 1")
 ...     yesno("is it good 2", True)
 ...     yesno("is it good 3", False)
 ... finally:
 ...     sys.stdin = saved_stdin
  [?] is it good 1 [yes/no] Please answer yes or no
  [?] is it good 1 [yes/no] True
  [?] is it good 2 [Yes/no] False
  [?] is it good 3 [yes/No] False

Tests:

 >>> p = testpwnproc("print(yesno('is it ok??'))")
 >>> b"is it ok" in p.recvuntil(b"??")
 True
 >>> p.sendline(b"x\nny")
 >>> b"True" in p.recvall()
 True