def register_session(server, username=None, password=None, port=445, encrypt=None, connection_timeout=60, connection_cache=None, auth_protocol='negotiate'): """ Creates an active connection and session to the server specified. This can be manually called to register the credentials of a specific server instead of defining it on the first function connecting to the server. The opened connection is registered in a pool and re-used if a connection is made to the same server with the same credentials. :param server: The server name to register. :param username: Optional username to connect with. Required if no session has been registered for the server and Kerberos auth is not being used. :param password: Optional password to connect with. :param port: The port to connect with. :param encrypt: Whether to force encryption or not, once this has been set to True the session cannot be changed back to False. :param connection_timeout: Override the timeout used for the initial connection. :param connection_cache: Connection cache to be used with :param auth_protocol: The protocol to use for authentication. Possible values are 'negotiate', 'ntlm' or 'kerberos'. Defaults to 'negotiate'. :return: The Session that was registered or already existed in the pool. """ connection_key = "%s:%s" % (server.lower(), port) if connection_cache is None: connection_cache = _SMB_CONNECTIONS connection = connection_cache.get(connection_key, None) # Make sure we ignore any connections that may have had a closed connection if not connection or not connection.transport.connected: connection = Connection(ClientConfig().client_guid, server, port) connection.connect(timeout=connection_timeout) connection_cache[connection_key] = connection # Find the first session in the connection session list that match the username specified, if not username then # just use the first session found or fall back to creating a new one with implicit auth/kerberos. session = next((s for s in connection.session_table.values() if username is None or s.username == username), None) if not session: session = Session(connection, username=username, password=password, require_encryption=(encrypt is True), auth_protocol=auth_protocol) session.connect() elif encrypt is not None: # We cannot go from encryption to no encryption on an existing session but we can do the opposite. if session.encrypt_data and not encrypt: raise ValueError( "Cannot disable encryption on an already negotiated session.") elif not session.encrypt_data and encrypt: session.encrypt = True return session
def bule_screen(IP, username=None, password=None, port=445, encode=None, connectionTimeout=10): _SMB_CONNECTIONS = {} connection_key = "%s:%s" %(IP, port) connection = _SMB_CONNECTIONS.get(connection_key, None) if not connection: connection = Connection(uuid.uuid4(), IP, port) connection.connect(timeout=connectionTimeout) _SMB_CONNECTIONS[connection_key] = connection session = next((s for s in connection.session_table.values() if username is None or s.username == username), None) if not session: session = Session(connection, username=username, password=password, require_encryption=(encode is True)) session.connect() elif encode is not None: if session.encrypt_data and not encode: print("[\033[33m-\033[0m]Cannot disable encryption on an already negotiated session.") elif not session.encrypt_data and encode: session.encrypt = True return session
def register_session(server, username=None, password=None, port=445, encrypt=None, connection_timeout=60): """ Creates an active connection and session to the server specified. This can be manually called to register the credentials of a specific server instead of defining it on the first function connecting to the server. The opened connection is registered in a pool and re-used if a connection is made to the same server with the same credentials. :param server: The server name to register. :param username: Optional username to connect with. Required if no session has been registered for the server and Kerberos auth is not being used. :param password: Optional password to connect with. :param port: The port to connect with. :param encrypt: Whether to force encryption or not, once this has been set to True the session cannot be changed back to False. :param connection_timeout: Override the timeout used for the initial connection. :return: The Session that was registered or already existed in the pool. """ connection_key = "%s:%s" % (server, port) global _SMB_CONNECTIONS connection = _SMB_CONNECTIONS.get(connection_key, None) if not connection: connection = Connection(_CLIENT_GUID, server, port) connection.connect(timeout=connection_timeout) _SMB_CONNECTIONS[connection_key] = connection # Find the first session in the connection session list that match the username specified, if not username then # just use the first session found or fall back to creating a new one with implicit auth/kerberos. session = next((s for s in connection.session_table.values() if username is None or s.username == username), None) if not session: session = Session(connection, username=username, password=password, require_encryption=(encrypt is True)) session.connect() elif encrypt is not None: # We cannot go from encryption to no encryption on an existing session but we can do the opposite. if session.encrypt_data and not encrypt: raise ValueError("Cannot disable encryption on an already negotiated session.") elif not session.encrypt_data and encrypt: session.encrypt = True return session