Skip to content

Commit

Permalink
Added null check and rename field
Browse files Browse the repository at this point in the history
  • Loading branch information
hierynomus committed May 12, 2023
1 parent 5f1b9de commit c930ce2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ public byte[] getNTLMv2ClientChallenge(TargetInfo targetInformation) {
ccBuf.putLong(nowAsFileTime); // Timestamp (8)
ccBuf.putRawBytes(challengeFromClient); // ChallengeFromClient (8)
ccBuf.putUInt32(0); // Reserved3 (4)
targetInformation.writeTo(ccBuf); // AvPairs (variable)
if (targetInformation != null) {
targetInformation.writeTo(ccBuf); // AvPairs (variable)
}
ccBuf.putUInt32(0); // Last AV Pair indicator

return ccBuf.getCompactData();
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/hierynomus/ntlm/messages/NtlmAuthenticate.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ public byte[] getVersion() {
return plainBuffer.getCompactData();
}

private int writeOffsettedByteArrayFields(Buffer.PlainBuffer buffer, byte[] array, int offset) {
byte[] _array = array != null ? array : EMPTY;
buffer.putUInt16(_array.length); // ArrayLen
buffer.putUInt16(_array.length); // ArrayMaxLen
private int writeOffsettedByteArrayFields(Buffer.PlainBuffer buffer, byte[] bytes, int offset) {
byte[] arr = bytes != null ? bytes : EMPTY;
buffer.putUInt16(arr.length); // ArrayLen
buffer.putUInt16(arr.length); // ArrayMaxLen
buffer.putUInt32(offset); // ArrayOffset
return offset + _array.length;
return offset + arr.length;
}

private byte[] ensureNotNull(byte[] possiblyNull) {
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/com/hierynomus/ntlm/messages/NtlmChallenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,6 @@ public TargetInfo getTargetInfo() {
return targetInfo;
}

public Object getAvPairObject(AvId key) {
return this.targetInfo.getAvPair(key).getValue();
}

public String getAvPairString(AvId key) {
return (String) this.targetInfo.getAvPair(key).getValue();
}

public WindowsVersion getVersion() {
return version;
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/hierynomus/smbj/auth/NtlmAuthenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.hierynomus.asn1.types.primitive.ASN1ObjectIdentifier;
import com.hierynomus.ntlm.av.AvId;
import com.hierynomus.ntlm.av.AvPairFlags;
import com.hierynomus.ntlm.functions.NtlmFunctions;
import com.hierynomus.ntlm.messages.*;
import com.hierynomus.protocol.commons.ByteArrayUtils;
Expand Down Expand Up @@ -93,7 +94,9 @@ public AuthenticateResponse authenticate(final AuthenticationContext context, fi
logger.debug("Received NTLM challenge from: {}", challenge.getTargetName());

response.setWindowsVersion(challenge.getVersion());
response.setNetBiosName(challenge.getAvPairString(AvId.MsvAvNbComputerName));
if (challenge.getTargetInfo() != null && challenge.getTargetInfo().hasAvPair(AvId.MsvAvNbComputerName)) {
response.setNetBiosName((String) challenge.getTargetInfo().getAvPair(AvId.MsvAvNbComputerName).getValue());
}

byte[] serverChallenge = challenge.getServerChallenge();
byte[] responseKeyNT = ntlmFunctions.NTOWFv2(String.valueOf(context.getPassword()), context.getUsername(), context.getDomain());
Expand Down Expand Up @@ -122,8 +125,8 @@ public AuthenticateResponse authenticate(final AuthenticationContext context, fi
// If NTLM v2 is used, KeyExchangeKey MUST be set to the given 128-bit SessionBaseKey value.

// MIC (16 bytes) provided if in AvPairType is key MsvAvFlags with value & 0x00000002 is true
Object msvAvFlags = challenge.getAvPairObject(AvId.MsvAvFlags);
if (msvAvFlags instanceof Long && ((long) msvAvFlags & 0x00000002) > 0) {
AvPairFlags pair = challenge.getTargetInfo() != null ? challenge.getTargetInfo().getAvPair(AvId.MsvAvFlags) : null;
if (pair != null && (pair.getValue() & 0x00000002) > 0) {
// MIC should be calculated
NtlmAuthenticate resp = new NtlmAuthenticate(new byte[0], ntlmv2Response,
context.getUsername(), context.getDomain(), workStationName, sessionkey, EnumWithValue.EnumUtils.toLong(negotiateFlags),
Expand Down

0 comments on commit c930ce2

Please sign in to comment.