def check(computer_name: str, connection: SMBConnection) -> (str, dict): """ Return Health check for a Samba connection. :param computer_name: Remote computer name. :param connection: Samba connection. :return: A tuple with a string providing the status (pass, warn, fail) and the checks. Checks are based on https://inadarei.github.io/rfc-healthcheck/ """ try: response = connection.echo(b"") return ( "pass", { f"{computer_name}:echo": { "componentType": connection.remote_name, "observedValue": response.decode(), "status": "pass", "time": datetime.utcnow().isoformat(), } }, ) except Exception as e: return ( "fail", { f"{computer_name}:echo": { "componentType": connection.remote_name, "status": "fail", "time": datetime.utcnow().isoformat(), "output": str(e), } }, )
def get_connection(self): conn = SMBConnection(self.user_id, self.password, self.client_name, self.server_name, use_ntlm_v2=True, is_direct_tcp=True) try: conn.connect(self.server_ip, 445) print(conn.echo('SMB CONNECTED')) self.conn = conn except Exception as e: print(e)
class Connection(komand.Connection): def __init__(self): self.conn = None super(self.__class__, self).__init__(input=ConnectionSchema()) def connect(self, params): username = params["credentials"]["username"] password = params["credentials"]["password"] host = params["host"] port = params["port"] netbios_name = params["netbios_name"] domain = params.get("domain", None) use_ntvlm_v2 = params.get("use_ntlmv2", True) is_direct_tcp = True if domain: self.conn = SMBConnection(username, password, "InsightConnect", netbios_name, domain=domain, use_ntlm_v2=use_ntvlm_v2, is_direct_tcp=is_direct_tcp) else: self.conn = SMBConnection(username, password, "InsightConnect", netbios_name, use_ntlm_v2=use_ntvlm_v2, is_direct_tcp=is_direct_tcp) try: result = self.conn.connect(host, port, timeout=params.get(Input.TIMEOUT)) if not result: error = "Failed to authenticate to SMB endpoint. Validate credentials and connection details." raise Exception(error) except socket.timeout: raise Exception("Timeout reached when connecting to SMB endpoint. Validate network connectivity or " "extend connection timeout") except Exception as e: self.logger.error(f"Error connecting to SMB endpoint: {e}") raise def test(self): from komand.exceptions import ConnectionTestException message = "Hello World" try: echo_response = self.conn.echo(message.encode()) except Exception as e: raise ConnectionTestException(cause="Connectivity test to SMB server failed.", assistance=e) if echo_response.decode() == message: self.logger.info("Connectivity test to SMB server was successful") return else: raise ConnectionTestException(cause="Connectivity test to SMB server failed.", assistance="echo response was not as expected")
raise RuntimeError('Cannot connect to host ' + target + '; looking up NetBIOS IP failed') target_ip = ips[0] if target_nb_name is None: print('Looking up NetBIOS name from target IP: ' + target_ip) nb_names = nb.queryIPForName(target_ip) print('Got NB names: ' + str(nb_names)) if nb_names is None or len(nb_names) < 1: raise RuntimeError('Cannot connect to host ' + target + '; looking up NetBIOS name failed') target_nb_name = nb_names[0] nb.close() client_machine_name = socket.gethostbyaddr(socket.gethostname())[0] # client_machine_name = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(15)) # print('Generated client machine name: ' + client_machine_name + '\n') domain = input('Enter domain [none]: ') username = input('Enter username: '******'Enter password: '******'Could not connect to host ' + target + '; establishing connection failed') if conn.echo('blah') != 'blah': raise RuntimeError('Connection test (echo) failed') conn.close()
if nb_names is None or len(nb_names) < 1: raise RuntimeError('Cannot connect to host ' + target + '; looking up NetBIOS name failed') target_nb_name = nb_names[0] nb.close() client_machine_name = socket.gethostbyaddr(socket.gethostname())[0] # client_machine_name = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(15)) # print('Generated client machine name: ' + client_machine_name + '\n') domain = input('Enter domain [none]: ') username = input('Enter username: '******'Enter password: '******'Could not connect to host ' + target + '; establishing connection failed') if conn.echo('blah') != 'blah': raise RuntimeError('Connection test (echo) failed') conn.close()