def dataReceived(self, data): """ Input received from user """ self.bytesReceived += len(data) if self.bytesReceivedLimit \ and self.bytesReceived > self.bytesReceivedLimit: log.msg(format='Data upload limit reached') #self.loseConnection() self.eofReceived() return if self.stdinlogOpen: with open(self.stdinlogFile, 'ab') as f: f.write(data) elif self.ttylogEnabled and self.ttylogOpen: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_INPUT, time.time(), data) # TODO: this may need to happen inside the shell rather than here to preserve bytes for redirection #if isinstance(data, bytes): # data = data.decode("utf-8") # prevent crash if something like this was passed: # echo cmd ; exit; \n\n if self.terminalProtocol: insults.ServerProtocol.dataReceived(self, data)
def write(self, data: bytes) -> None: if self.ttylogEnabled and self.ttylogOpen: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_OUTPUT, time.time(), data) self.ttylogSize += len(data) insults.ServerProtocol.write(self, data)
def connectionMade(self): transportId, channelId = self.getSessionId() self.startTime = time.time() if self.ttylogEnabled: self.ttylogFile = '%s/%s-%s-%s%s.log' % \ (self.ttylogPath, time.strftime('%Y%m%d-%H%M%S'), transportId, channelId, self.type) ttylog.ttylog_open(self.ttylogFile, self.startTime) self.ttylogOpen = True self.ttylogSize = 0 self.stdinlogFile = '%s/%s-%s-%s-stdin.log' % \ (self.downloadPath, time.strftime('%Y%m%d-%H%M%S'), transportId, channelId) if self.type == 'e': self.stdinlogOpen = True else: self.stdinlogOpen = False insults.ServerProtocol.connectionMade(self) if self.type == 'e': cmd = self.terminalProtocol.execcmd.encode('utf8') ttylog.ttylog_write(self.ttylogFile, len(cmd), ttylog.TYPE_INTERACT, time.time(), cmd)
def sendBackend(self, data): self.backend_buffer.append(data) if not self.client: return for packet in self.backend_buffer: self.client.transport.write(packet) # log raw packets if user sets so if CowrieConfig.getboolean("proxy", "log_raw", fallback=False): log.msg(b"to_backend - " + data) if self.ttylogEnabled and self.authStarted: cleanData = data.replace( b"\x00", b"\n" ) # some frontends send 0xFF instead of newline ttylog.ttylog_write( self.ttylogFile, len(cleanData), ttylog.TYPE_INPUT, time.time(), cleanData, ) self.ttylogSize += len(cleanData) self.backend_buffer = self.backend_buffer[1:]
def write(self, bytes, noLog = False): transport = self.transport.session.conn.transport for i in transport.interactors: i.sessionWrite(bytes) if transport.ttylog_open and not noLog: ttylog.ttylog_write(transport.ttylog_file, len(bytes), ttylog.TYPE_OUTPUT, time.time(), bytes) insults.ServerProtocol.write(self, bytes)
def write(self, bytes, noLog=False): transport = self.transport.session.conn.transport for i in transport.interactors: i.sessionWrite(bytes) if transport.ttylog_open and not noLog: ttylog.ttylog_write(transport.ttylog_file, len(bytes), ttylog.TYPE_OUTPUT, time.time(), bytes) insults.ServerProtocol.write(self, bytes)
def sendFrontend(self, data): self.server.transport.write(data) # log raw packets if user sets so if CowrieConfig().getboolean('proxy', 'log_raw', fallback=False): log.msg(b'to_frontend - ' + data) if self.ttylogEnabled and self.authStarted: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_OUTPUT, time.time(), data)
def sendFrontend(self, data: bytes) -> None: self.server.transport.write(data) # log raw packets if user sets so if CowrieConfig.getboolean("proxy", "log_raw", fallback=False): log.msg("to_frontend - " + data.decode("unicode-escape")) if self.ttylogEnabled and self.authStarted: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_OUTPUT, time.time(), data)
def write(self, bytes): """ Output sent back to user """ if self.ttylogEnabled and self.ttylogOpen: ttylog.ttylog_write(self.ttylogFile, len(bytes), ttylog.TYPE_OUTPUT, time.time(), bytes) self.ttylogSize += len(bytes) insults.ServerProtocol.write(self, bytes)
def write(self, bytes): """ Output sent back to user """ if self.ttylogOpen: ttylog.ttylog_write(self.ttylogFile, len(bytes), ttylog.TYPE_OUTPUT, time.time(), bytes) self.ttylogSize += len(bytes) insults.ServerProtocol.write(self, bytes)
def write(self, bytes): """ """ for i in self.interactors: i.sessionWrite(bytes) if self.ttylog_open: ttylog.ttylog_write(self.ttylog_file, len(bytes), ttylog.TYPE_OUTPUT, time.time(), bytes) insults.ServerProtocol.write(self, bytes)
def dataReceived(self, data, noLog = False): transport = self.transport.session.conn.transport if transport.ttylog_open and not noLog: ttylog.ttylog_write(transport.ttylog_file, len(data), ttylog.TYPE_INPUT, time.time(), data) if transport.stdinlog_open and not noLog: f = file( transport.stdinlog_file, 'ab' ) f.write(data) f.close insults.ServerProtocol.dataReceived(self, data)
def dataReceived(self, data): """ """ if self.stdinlog_open: with file(self.stdinlog_file, 'ab') as f: f.write(data) elif self.ttylog_open: transport = self.transport.session.conn.transport ttylog.ttylog_write(transport.ttylog_file, len(data), ttylog.TYPE_INPUT, time.time(), data) insults.ServerProtocol.dataReceived(self, data)
def write(self, bytes): """ Output sent back to user """ for i in self.interactors: i.sessionWrite(bytes) if self.ttylog_open: ttylog.ttylog_write(self.ttylogFile, len(bytes), ttylog.TYPE_OUTPUT, time.time(), bytes) self.ttylogSize += len(bytes) insults.ServerProtocol.write(self, bytes)
def write(self, data): """ Called when we send data to the user @type data: L{bytes} @param data: Data sent to the client from the server """ if self.ttylogEnabled: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_OUTPUT, time.time(), data) self.bytesWritten += len(data) channel.SSHChannel.write(self, data)
def write(self, data): """ Output sent back to user """ if not isinstance(data, bytes): data = data.encode("utf-8") if self.ttylogEnabled and self.ttylogOpen: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_OUTPUT, time.time(), data) self.ttylogSize += len(data) insults.ServerProtocol.write(self, data)
def sendBackend(self, data): self.client.transport.write(data) # log raw packets if user sets so if CowrieConfig().getboolean('proxy', 'log_raw', fallback=False): log.msg(b'to_backend - ' + data) if self.ttylogEnabled and self.authStarted: cleanData = data.replace( b'\x00', b'\n') # some frontends send 0xFF instead of newline ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_INPUT, time.time(), cleanData) self.ttylogSize += len(data)
def write(self, data): """ IMPORTANT This is where internal data in string representation is converted to bytes to be sent back to the user """ if not isinstance(data, bytes): data = data.encode("utf-8") if self.ttylogEnabled and self.ttylogOpen: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_OUTPUT, time.time(), data) self.ttylogSize += len(data) insults.ServerProtocol.write(self, data)
def dataReceived(self, data, noLog = False): """ """ self.dataAlreadyReceived += len(data) if self.dataReceivedLimit and self.dataAlreadyReceived > self.dataReceivedLimit: self.transport.loseConnection() transport = self.transport.session.conn.transport if transport.ttylog_open and not noLog: ttylog.ttylog_write(transport.ttylog_file, len(data), ttylog.TYPE_INPUT, time.time(), data) if transport.stdinlog_open and not noLog: f = file( transport.stdinlog_file, 'ab' ) f.write(data) f.close insults.ServerProtocol.dataReceived(self, data)
def dataReceived(self, data, noLog=False): """ """ self.dataAlreadyReceived += len(data) if self.dataReceivedLimit and self.dataAlreadyReceived > self.dataReceivedLimit: self.transport.loseConnection() transport = self.transport.session.conn.transport if transport.ttylog_open and not noLog: ttylog.ttylog_write(transport.ttylog_file, len(data), ttylog.TYPE_INPUT, time.time(), data) if transport.stdinlog_open and not noLog: f = file(transport.stdinlog_file, 'ab') f.write(data) f.close insults.ServerProtocol.dataReceived(self, data)
def dataReceived(self, data): """ """ self.bytesReceived += len(data) if self.bytesReceivedLimit and self.bytesReceived > self.bytesReceivedLimit: log.msg(eventid="KIPP0015", format="Data upload limit reached") # self.loseConnection() self.eofReceived() return if self.stdinlog_open: with file(self.stdinlog_file, "ab") as f: f.write(data) elif self.ttylog_open: transport = self.transport.session.conn.transport ttylog.ttylog_write(transport.ttylog_file, len(data), ttylog.TYPE_INPUT, time.time(), data) insults.ServerProtocol.dataReceived(self, data)
def dataReceived(self, data): """ Called when we receive data from the user @type data: L{bytes} @param data: Data sent to the server from the client """ self.bytesReceived += len(data) if self.bytesReceivedLimit and self.bytesReceived > self.bytesReceivedLimit: log.msg(f"Data upload limit reached for channel {self.id}") self.eofReceived() return if self.ttylogEnabled: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_INPUT, time.time(), data) channel.SSHChannel.dataReceived(self, data)
def dataReceived(self, data): """ """ self.bytesReceived += len(data) if self.bytesReceivedLimit and self.bytesReceived > self.bytesReceivedLimit: log.msg(eventid='COW0015', format='Data upload limit reached') #self.loseConnection() self.eofReceived() return if self.stdinlog_open: with file(self.stdinlog_file, 'ab') as f: f.write(data) elif self.ttylog_open: ttylog.ttylog_write(self.ttylog_file, len(data), ttylog.TYPE_INPUT, time.time(), data) insults.ServerProtocol.dataReceived(self, data)
def dataReceived(self, data): """ Input received from user """ self.bytesReceived += len(data) if self.bytesReceivedLimit \ and self.bytesReceived > self.bytesReceivedLimit: log.msg(eventid='COW0015', format='Data upload limit reached') self.eofReceived() return if self.stdinlog_open: with file(self.stdinlog_file, 'ab') as f: f.write(data) elif self.ttylog_open: ttylog.ttylog_write(self.ttylog_file, len(data), ttylog.TYPE_INPUT, time.time(), data) insults.ServerProtocol.dataReceived(self, data)
def dataReceived(self, data): """ Input received from user """ self.bytesReceived += len(data) if self.bytesReceivedLimit and self.bytesReceived > self.bytesReceivedLimit: log.msg(format='Data upload limit reached') self.eofReceived() return if self.stdinlogOpen: with open(self.stdinlogFile, 'ab') as f: f.write(data) elif self.ttylogEnabled and self.ttylogOpen: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_INPUT, time.time(), data) # prevent crash if something like this was passed: # echo cmd ; exit; \n\n if self.terminalProtocol: insults.ServerProtocol.dataReceived(self, data)
def dataReceived(self, data): """ Input received from user """ self.bytesReceived += len(data) if self.bytesReceivedLimit \ and self.bytesReceived > self.bytesReceivedLimit: log.msg(format='Data upload limit reached') #self.loseConnection() self.eofReceived() return if self.stdinlogOpen: with open(self.stdinlogFile, 'ab') as f: f.write(data) elif self.ttylogOpen: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_INPUT, time.time(), data) insults.ServerProtocol.dataReceived(self, data)
def dataReceived(self, data): """ Input received from user """ self.bytesReceived += len(data) if self.bytesReceivedLimit \ and self.bytesReceived > self.bytesReceivedLimit: log.msg(format='Data upload limit reached') #self.loseConnection() self.eofReceived() return if self.stdinlogOpen: with open(self.stdinlogFile, 'ab') as f: f.write(data) elif self.ttylogEnabled and self.ttylogOpen: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_INPUT, time.time(), data) insults.ServerProtocol.dataReceived(self, data)
def applicationDataReceived(self, bytes): """ """ # In command mode, we want to echo characters and buffer the input if not self.interacting: self.transport.write(bytes) if bytes in ('\r', '\n'): self.transport.write('\n') pieces = self.cmdbuf.split(' ', 1) self.cmdbuf = '' cmd, args = pieces[0], '' if len(pieces) > 1: args = pieces[1] try: func = getattr(self, 'cmd_' + cmd) except AttributeError: self.transport.write('** Unknown command.\r\n') return func(args) else: self.cmdbuf += bytes # In non-command mode we are passing input to the session we are # watching else: for c in bytes: if ord(c) == 27: # escape self.interacting.delInteractor(self) self.interacting = None self.transport.write( '\r\n** Interactive session closed.\r\n') return if not self.readonly: if type(bytes) == type(''): ttylog.ttylog_write( self.interacting. terminal.transport.session.conn.transport.ttylog_file, len(bytes), ttylog.TYPE_INTERACT, time.time(), bytes) for c in bytes: recvline.HistoricRecvLine.keystrokeReceived( self.interacting, c, None)
def dataReceived(self, data): """ Called when we receive data from the user @type data: L{bytes} @param data: Data sent to the server from the client """ self.bytesReceived += len(data) if self.bytesReceivedLimit and self.bytesReceived > self.bytesReceivedLimit: log.msg("Data upload limit reached for channel {}".format(self.id)) self.eofReceived() return if self.ttylogEnabled: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_INPUT, time.time(), data) channel.SSHChannel.dataReceived(self, data)
def applicationDataReceived(self, bytes): """ """ # In command mode, we want to echo characters and buffer the input if not self.interacting: self.transport.write(bytes) if bytes in ('\r', '\n'): self.transport.write('\n') pieces = self.cmdbuf.split(' ', 1) self.cmdbuf = '' cmd, args = pieces[0], '' if len(pieces) > 1: args = pieces[1] try: func = getattr(self, 'cmd_' + cmd) except AttributeError: self.transport.write('** Unknown command.\r\n') return func(args) else: self.cmdbuf += bytes # In non-command mode we are passing input to the session we are # watching else: for c in bytes: if ord(c) == 27: # escape self.interacting.terminal.delInteractor(self) self.interacting = None self.transport.write( '\r\n** Interactive session closed.\r\n') return if not self.readonly: if type(bytes) == type(''): ttylog.ttylog_write( self.interacting.terminal.ttylogFile, len(bytes), ttylog.TYPE_INTERACT, time.time(), bytes) for c in bytes: recvline.HistoricRecvLine.keystrokeReceived( self.interacting, c, None)
def connectionMade(self) -> None: transportId, channelId = self.getSessionId() self.startTime = time.time() if self.ttylogEnabled: self.ttylogFile = "{}/{}-{}-{}{}.log".format( self.ttylogPath, time.strftime("%Y%m%d-%H%M%S"), transportId, channelId, self.type, ) ttylog.ttylog_open(self.ttylogFile, self.startTime) self.ttylogOpen = True self.ttylogSize = 0 self.stdinlogFile = "{}/{}-{}-{}-stdin.log".format( self.downloadPath, time.strftime("%Y%m%d-%H%M%S"), transportId, channelId, ) if self.type == "e": self.stdinlogOpen = True # log the command into ttylog if self.ttylogEnabled: (sess, cmd) = self.protocolArgs ttylog.ttylog_write( self.ttylogFile, len(cmd), ttylog.TYPE_INTERACT, time.time(), cmd ) else: self.stdinlogOpen = False insults.ServerProtocol.connectionMade(self) if self.type == "e": self.terminalProtocol.execcmd.encode("utf8")
def parse_packet(self, parent, payload): if self.ttylogEnabled: ttylog.ttylog_write(self.ttylogFile, len(payload), ttylog.TYPE_OUTPUT, time.time(), payload) self.ttylogSize += len(payload)
def parse_packet(self, parent, payload): self.data = payload if parent == "[SERVER]": while len(self.data) > 0: # If Tab Pressed if self.data[:1] == b"\x09": self.tabPress = True self.data = self.data[1:] # If Backspace Pressed elif self.data[:1] == b"\x7f" or self.data[:1] == b"\x08": if self.pointer > 0: self.command = (self.command[:self.pointer - 1] + self.command[self.pointer:]) self.pointer -= 1 self.data = self.data[1:] # If enter or ctrl+c or newline elif (self.data[:1] == b"\x0d" or self.data[:1] == b"\x03" or self.data[:1] == b"\x0a"): if self.data[:1] == b"\x03": self.command += b"^C" self.data = self.data[1:] if self.command != b"": log.msg( eventid="cowrie.command.input", input=self.command.decode("ascii"), format="CMD: %(input)s", ) self.command = b"" self.pointer = 0 # If Home Pressed elif self.data[:3] == b"\x1b\x4f\x48": self.pointer = 0 self.data = self.data[3:] # If End Pressed elif self.data[:3] == b"\x1b\x4f\x46": self.pointer = len(self.command) self.data = self.data[3:] # If Right Pressed elif self.data[:3] == b"\x1b\x5b\x43": if self.pointer != len(self.command): self.pointer += 1 self.data = self.data[3:] # If Left Pressed elif self.data[:3] == b"\x1b\x5b\x44": if self.pointer != 0: self.pointer -= 1 self.data = self.data[3:] # If up or down arrow elif (self.data[:3] == b"\x1b\x5b\x41" or self.data[:3] == b"\x1b\x5b\x42"): self.upArrow = True self.data = self.data[3:] else: self.command = (self.command[:self.pointer] + self.data[:1] + self.command[self.pointer:]) self.pointer += 1 self.data = self.data[1:] if self.ttylogEnabled: self.ttylogSize += len(payload) ttylog.ttylog_write( self.ttylogFile, len(payload), ttylog.TYPE_OUTPUT, time.time(), payload, ) elif parent == "[CLIENT]": if self.tabPress: if not self.data.startswith(b"\x0d"): if self.data != b"\x07": self.command = self.command + self.data self.tabPress = False if self.upArrow: while len(self.data) != 0: # Backspace if self.data[:1] == b"\x08": self.command = self.command[:-1] self.pointer -= 1 self.data = self.data[1:] # ESC[K - Clear Line elif self.data[:3] == b"\x1b\x5b\x4b": self.command = self.command[:self.pointer] self.data = self.data[3:] elif self.data[:1] == b"\x0d": self.pointer = 0 self.data = self.data[1:] # Right Arrow elif self.data[:3] == b"\x1b\x5b\x43": self.pointer += 1 self.data = self.data[3:] elif self.data[:2] == b"\x1b\x5b" and self.data[ 3] == b"\x50": self.data = self.data[4:] # Needed?! elif self.data[:1] != b"\x07" and self.data[:1] != b"\x0d": self.command = (self.command[:self.pointer] + self.data[:1] + self.command[self.pointer:]) self.pointer += 1 self.data = self.data[1:] else: self.pointer += 1 self.data = self.data[1:] self.upArrow = False if self.ttylogEnabled: self.ttylogSize += len(payload) ttylog.ttylog_write( self.ttylogFile, len(payload), ttylog.TYPE_INPUT, time.time(), payload, )
def parse_packet(self, parent, payload): self.data = payload if parent == '[SERVER]': while len(self.data) > 0: # If Tab Pressed if self.data[:1] == b'\x09': self.tabPress = True self.data = self.data[1:] # If Backspace Pressed elif self.data[:1] == b'\x7f' or self.data[:1] == b'\x08': if self.pointer > 0: self.command = self.command[:self.pointer - 1] + self.command[self. pointer:] self.pointer -= 1 self.data = self.data[1:] # If enter or ctrl+c or newline elif self.data[: 1] == b'\x0d' or self.data[: 1] == b'\x03' or self.data[: 1] == b'\x0a': if self.data[:1] == b'\x03': self.command += b'^C' self.data = self.data[1:] if self.command != b'': log.msg(eventid='cowrie.command.input', input=self.command.decode('ascii'), format='CMD: %(input)s') self.command = b'' self.pointer = 0 # If Home Pressed elif self.data[:3] == b'\x1b\x4f\x48': self.pointer = 0 self.data = self.data[3:] # If End Pressed elif self.data[:3] == b'\x1b\x4f\x46': self.pointer = len(self.command) self.data = self.data[3:] # If Right Pressed elif self.data[:3] == b'\x1b\x5b\x43': if self.pointer != len(self.command): self.pointer += 1 self.data = self.data[3:] # If Left Pressed elif self.data[:3] == b'\x1b\x5b\x44': if self.pointer != 0: self.pointer -= 1 self.data = self.data[3:] # If up or down arrow elif self.data[: 3] == b'\x1b\x5b\x41' or self.data[: 3] == b'\x1b\x5b\x42': self.upArrow = True self.data = self.data[3:] else: self.command = self.command[:self. pointer] + self.data[:1] + self.command[ self.pointer:] self.pointer += 1 self.data = self.data[1:] if self.ttylogEnabled: self.ttylogSize += len(payload) ttylog.ttylog_write(self.ttylogFile, len(payload), ttylog.TYPE_OUTPUT, time.time(), payload) elif parent == '[CLIENT]': if self.tabPress: if not self.data.startswith(b'\x0d'): if self.data != b'\x07': self.command = self.command + self.data self.tabPress = False if self.upArrow: while len(self.data) != 0: # Backspace if self.data[:1] == b'\x08': self.command = self.command[:-1] self.pointer -= 1 self.data = self.data[1:] # ESC[K - Clear Line elif self.data[:3] == b'\x1b\x5b\x4b': self.command = self.command[:self.pointer] self.data = self.data[3:] elif self.data[:1] == b'\x0d': self.pointer = 0 self.data = self.data[1:] # Right Arrow elif self.data[:3] == b'\x1b\x5b\x43': self.pointer += 1 self.data = self.data[3:] elif self.data[:2] == b'\x1b\x5b' and self.data[ 3] == b'\x50': self.data = self.data[4:] # Needed?! elif self.data[:1] != b'\x07' and self.data[:1] != b'\x0d': self.command = self.command[:self. pointer] + self.data[:1] + self.command[ self.pointer:] self.pointer += 1 self.data = self.data[1:] else: self.pointer += 1 self.data = self.data[1:] self.upArrow = False if self.ttylogEnabled: self.ttylogSize += len(payload) ttylog.ttylog_write(self.ttylogFile, len(payload), ttylog.TYPE_INPUT, time.time(), payload)