Skip to content

Null pointer when authenticating as anonymous #872

@L-JINBIN

Description

@L-JINBIN

Similar as #792

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'byte[] javax.crypto.SecretKey.getEncoded()' on a null object reference
	at com.hierynomus.smbj.connection.SMBSessionBuilder.deriveKey(SMBSessionBuilder.java:326)
	at com.hierynomus.smbj.connection.SMBSessionBuilder.deriveKeys(SMBSessionBuilder.java:292)
	at com.hierynomus.smbj.connection.SMBSessionBuilder.setupSession(SMBSessionBuilder.java:175)
	at com.hierynomus.smbj.connection.SMBSessionBuilder.setupSession(SMBSessionBuilder.java:153)
	at com.hierynomus.smbj.connection.SMBSessionBuilder.establish(SMBSessionBuilder.java:120)
	at com.hierynomus.smbj.connection.Connection.authenticate(Connection.java:206)

The problem still exists in version 0.14.0. The reason for this problem is that SessionContext.sessionKey is null. I traced its assignment upwards and found that it comes from BuilderContext.sessionKey, which comes from AuthenticateResponse.sessionKey.

AuthenticateResponse.sessionKey is assigned in the NtlmAuthenticator.doAuthenticate() method

private AuthenticateResponse doAuthenticate(...) throws SpnegoException {

    ......

    // [MS-NLMP] 3.2.2 -- Special case for anonymous authentication
    if (context.isAnonymous()) {
        NtlmAuthenticate msg = new NtlmAuthenticate(null, null, context.getUsername(), context.getDomain(),
            config.getWorkstationName(), null, negotiateFlags, config.getWindowsVersion());
        response.setNegToken(negTokenTarg(msg));
        return response;
    }

    ......

    response.setSessionKey(exportedSessionKey);
    logger.trace("Sending NTLM authenticate message: {}", msg);
    response.setNegToken(negTokenTarg(msg));
    response.setNegotiateFlags(negotiateFlags);
    return response;
}

The problem is that when the logged-in user is Anonymous, response.setSessionKey() is not called, which eventually causes SessionContext.sessionKey to be null

I tried commenting out the isAnonymous judgment, and everything worked fine, but I'm not sure if it will cause new problems

private AuthenticateResponse doAuthenticate(...) throws SpnegoException {

    ......

//    // [MS-NLMP] 3.2.2 -- Special case for anonymous authentication
//    if (context.isAnonymous()) {
//        NtlmAuthenticate msg = new NtlmAuthenticate(null, null, context.getUsername(), context.getDomain(),
//            config.getWorkstationName(), null, negotiateFlags, config.getWindowsVersion());
//        response.setNegToken(negTokenTarg(msg));
//        return response;
//    }

    ......

}

@hierynomus

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions