def command(self) -> Generator[str, None, None]: """ Yield list of commands to transfer the file """ remote_path = shlex.quote(self.remote_path) blocksz = 1024 * 1024 binary = self.pty.which("dd", quote=True) if binary is None: binary = self.pty.which("cat", quote=True) if "dd" in binary: pipe = self.pty.subprocess( f"{binary} if={remote_path} bs={blocksz} 2>/dev/null" ) else: pipe = self.pty.subprocess(f"{binary} {remote_path}") try: with open(self.local_path, "wb") as filp: util.copyfileobj(pipe, filp, self.on_progress) finally: self.on_progress(0, -1) pipe.close() return False
def handle(self): self.request.settimeout(1) with open(local_path, "rb") as filp: util.copyfileobj(filp, SocketWrapper(self.request), on_progress) self.request.close() pty.client.send(util.CTRL_C)
def run(self, args): # Create a progress bar for the download progress = Progress( TextColumn("[bold cyan]{task.fields[filename]}", justify="right"), BarColumn(bar_width=None), "[progress.percentage]{task.percentage:>3.1f}%", "•", DownloadColumn(), "•", TransferSpeedColumn(), "•", TimeRemainingColumn(), ) if not args.destination: args.destination = f"./{os.path.basename(args.source)}" else: access = pwncat.victim.access(args.destination) if Access.DIRECTORY in access: args.destination = os.path.join(args.destination, os.path.basename(args.source)) elif Access.PARENT_EXIST not in access: console.log( f"[cyan]{args.destination}[/cyan]: no such file or directory" ) return try: length = os.path.getsize(args.source) started = time.time() with progress: task_id = progress.add_task("upload", filename=args.destination, total=length, start=False) with open(args.source, "rb") as source: with pwncat.victim.open(args.destination, "wb", length=length) as destination: progress.start_task(task_id) copyfileobj( source, destination, lambda count: progress.update(task_id, advance=count), ) elapsed = time.time() - started console.log(f"uploaded [cyan]{human_readable_size(length)}[/cyan] " f"in [green]{human_readable_delta(elapsed)}[/green]") except (FileNotFoundError, PermissionError, IsADirectoryError) as exc: self.parser.error(str(exc))
def run(self, manager: "pwncat.manager.Manager", args): # Create a progress bar for the download progress = Progress( TextColumn("[bold cyan]{task.fields[filename]}", justify="right"), BarColumn(bar_width=None), "[progress.percentage]{task.percentage:>3.1f}%", "•", DownloadColumn(), "•", TransferSpeedColumn(), "•", TimeRemainingColumn(), ) if not args.destination: args.destination = f"./{os.path.basename(args.source)}" try: length = os.path.getsize(args.source) started = time.time() with progress: task_id = progress.add_task("upload", filename=args.destination, total=length, start=False) with open(args.source, "rb") as source: with manager.target.platform.open(args.destination, "wb") as destination: progress.start_task(task_id) copyfileobj( source, destination, lambda count: progress.update(task_id, advance=count), ) progress.update(task_id, filename="draining buffers...") progress.stop_task(task_id) progress.start_task(task_id) progress.update(task_id, filename=args.destination) elapsed = time.time() - started console.log(f"uploaded [cyan]{human_readable_size(length)}[/cyan] " f"in [green]{human_readable_delta(elapsed)}[/green]") except (FileNotFoundError, PermissionError, IsADirectoryError) as exc: self.parser.error(str(exc))
def do_GET(self): """ handle http POST request """ if self.path != "/": self.send_error(404) return length = os.path.getsize(self.uploader.local_path) self.send_response(200) self.send_header("Content-Length", str(length)) self.send_header("Content-Type", "application/octet-stream") self.end_headers() with open(self.uploader.local_path, "rb") as filp: util.copyfileobj(filp, self.wfile, self.on_progress)
def handle(self): self.request.settimeout(1) with open(local_path, "wb") as fp: util.copyfileobj(SocketWrapper(self.request), fp, on_progress) self.request.close()
def command(self): with self.pty.open(self.remote_path, "wb", length=self.length) as remote: with open(self.local_path, "rb") as local: util.copyfileobj(local, remote, self.on_progress)