1
0
mirror of https://github.com/calebstewart/pwncat.git synced 2024-11-27 19:04:15 +01:00

Even better file close handling

For linux, we used to send one or two EOFs via C-d to the process to
signal exit, however this was inconsistent. Depending on the previous
input from the attacker, sometimes one was needed, sometimes two.
Sometimes, we even observed more than two being needed. Instead, we now
simply loop sending one at a time and checking for the end delimeter.
This will be slightly slower, but avoids hangups or accidentally closing
the shell.
This commit is contained in:
Caleb Stewart 2021-06-13 17:15:22 -04:00
parent 6cc30a6ab5
commit 3861310d71
2 changed files with 16 additions and 13 deletions

View File

@ -458,19 +458,21 @@ class LinuxWriter(BufferedIOBase):
self.detach()
return
# Indicate EOF
self.popen.stdin.write(b"\x04")
if self.since_newline != 0:
self.popen.stdin.write(b"\x04")
try:
# Check for completion
self.popen.wait(timeout=100)
except pwncat.subprocess.TimeoutExpired:
# Nope, force terminate with C-c
# self.popen.terminate()
# Cleanup
self.popen.wait()
# The number of C-d's needed to trigger an EOF in
# the process and exit is inconsistent based on the
# previous input. So, instead of trying to be deterministic,
# we simply send one and check. We do this until we find
# the ending delimeter and then exit. If the `on_close`
# hook was setup properly, this should be fine.
while True:
try:
self.popen.stdin.write(b"\x04")
self.popen.stdin.flush()
# Check for completion
self.popen.wait(timeout=0.1)
break
except pwncat.subprocess.TimeoutExpired:
continue
# Ensure we don't touch stdio again
self.detach()

View File

@ -5,6 +5,7 @@ import base64
import subprocess
import pytest
from pwncat.util import random_string
from pwncat.platform.windows import PowershellError