|
5 | 5 | import abc
|
6 | 6 | import logging
|
7 | 7 | import re
|
| 8 | +import select |
8 | 9 | import six
|
9 | 10 | import string
|
10 | 11 | import subprocess
|
|
17 | 18 | from pwnlib import atexit
|
18 | 19 | from pwnlib import term
|
19 | 20 | from pwnlib.context import context
|
| 21 | +from pwnlib.exception import PwnlibException |
20 | 22 | from pwnlib.log import Logger
|
21 | 23 | from pwnlib.timeout import Timeout
|
22 | 24 | from pwnlib.tubes.buffer import Buffer
|
@@ -862,21 +864,33 @@ def recv_thread():
|
862 | 864 | t.start()
|
863 | 865 |
|
864 | 866 | try:
|
865 |
| - while not go.isSet(): |
866 |
| - if term.term_mode: |
867 |
| - data = term.readline.readline(prompt = prompt, float = True) |
868 |
| - else: |
869 |
| - stdin = getattr(sys.stdin, 'buffer', sys.stdin) |
870 |
| - data = stdin.read(1) |
| 867 | + while not go.isSet() and self.connected(): |
| 868 | + # Block until there is input on stdin or there's input on the receiving side |
| 869 | + try: |
| 870 | + rfds, _, _ = select.select([sys.stdin, self], [], []) |
| 871 | + except PwnlibException: |
| 872 | + # If the process stops while we're waiting for input, an |
| 873 | + # exception will be thrown |
| 874 | + go.set() |
| 875 | + continue |
871 | 876 |
|
872 |
| - if data: |
873 |
| - try: |
874 |
| - self.send(data) |
875 |
| - except EOFError: |
| 877 | + # If the there's input on stdin, handle it. Otherwise, on the |
| 878 | + # next iteration of the loop we'll check if the receiving side disconnected |
| 879 | + if sys.stdin in rfds: |
| 880 | + if term.term_mode: |
| 881 | + data = term.readline.readline(prompt = prompt, float = True) |
| 882 | + else: |
| 883 | + stdin = getattr(sys.stdin, 'buffer', sys.stdin) |
| 884 | + data = stdin.read(1) |
| 885 | + |
| 886 | + if data: |
| 887 | + try: |
| 888 | + self.send(data) |
| 889 | + except EOFError: |
| 890 | + go.set() |
| 891 | + self.info('Got EOF while sending in interactive') |
| 892 | + else: |
876 | 893 | go.set()
|
877 |
| - self.info('Got EOF while sending in interactive') |
878 |
| - else: |
879 |
| - go.set() |
880 | 894 | except KeyboardInterrupt:
|
881 | 895 | self.info('Interrupted')
|
882 | 896 | go.set()
|
|
0 commit comments