From 7e3387516d32c746cd1c6588be71a9ed0d70a02d Mon Sep 17 00:00:00 2001 From: dusoleil Date: Sun, 26 Feb 2023 07:09:29 -0500 Subject: Add special cases for read(size <= 0) A read of 0 isn't particularly useful, but it is weird that it will cause a BrokenPipeError. Instead, it makes more sense to just return an empty string. A read of <0 would normally read until EOF, but we already have that feature in readall() and it wouldn't be particularly useful here. A similar functionality of reading the entire current contents of the buffer is useful, though. This is already implemented in readall_nonblock() and this would be a nice user-facing way of calling that. Signed-off-by: dusoleil --- sploit/comm.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/sploit/comm.py b/sploit/comm.py index e9ea53d..8078c9f 100644 --- a/sploit/comm.py +++ b/sploit/comm.py @@ -24,10 +24,15 @@ class Comm: except BrokenPipeError: pass - def read(self, size): - data = self.back.stdin.read(size) - if(data == b''): - raise BrokenPipeError('Tried to read on broken pipe') + def read(self, size=-1): + if size < 0: + return self.readall_nonblock() + elif size == 0: + data = b'' + else: + data = self.back.stdin.read(size) + if(data == b''): + raise BrokenPipeError('Tried to read on broken pipe') if self.logonread : ilog(data, file=sys.stdout, color=NORMAL) self.last = data return data -- cgit v1.2.3