Skip to content

Commit

Permalink
Added trace logging to log Ntlm message flow
Browse files Browse the repository at this point in the history
Signed-off-by: Jeroen van Erp <[email protected]>
  • Loading branch information
hierynomus committed Mar 29, 2023
1 parent 5d281dc commit 449d5d7
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/it/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
<appender-ref ref="STDOUT"/>
</root>

<logger name="com.hierynomus.smbj" level="debug"/>
<logger name="com.hierynomus.smbj" level="trace"/>

</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public static byte[] unicode(String string) {
return string == null ? new byte[0] : string.getBytes(UNICODE);
}

public static String unicode(byte[] bytes) {
return new String(bytes, UNICODE);
}

/**
* [MS-NLMP].pdf 6 Appendix A: Cryptographic Operations Reference
* (MD4(M)).
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/hierynomus/ntlm/messages/NtlmAuthenticate.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.hierynomus.ntlm.messages;

import com.hierynomus.ntlm.functions.NtlmFunctions;
import com.hierynomus.protocol.commons.ByteArrayUtils;
import com.hierynomus.protocol.commons.Charsets;
import com.hierynomus.protocol.commons.EnumWithValue;
import com.hierynomus.protocol.commons.buffer.Buffer;
Expand Down Expand Up @@ -118,4 +120,17 @@ public byte[] getVersion() {
plainBuffer.putByte((byte) 0x0F); // NTLM Revision Current
return plainBuffer.getCompactData();
}

@Override
public String toString() {
return "NtlmAuthenticate{\n" +
" mic=" + ByteArrayUtils.printHex(mic) + ",\n" +
" lmResponse=" + ByteArrayUtils.printHex(lmResponse) + ",\n" +
" ntResponse=" + ByteArrayUtils.printHex(ntResponse) + ",\n" +
" domainName='" + NtlmFunctions.unicode(domainName) + "',\n" +
" userName='" + NtlmFunctions.unicode(userName) + "',\n" +
" workstation='" + NtlmFunctions.unicode(workstation) + "',\n" +
" encryptedRandomSessionKey=[<secret>],\n" +
'}';
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/hierynomus/ntlm/messages/NtlmChallenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.hierynomus.ntlm.messages;

import com.hierynomus.protocol.commons.ByteArrayUtils;
import com.hierynomus.protocol.commons.Charsets;
import com.hierynomus.protocol.commons.EnumWithValue;
import com.hierynomus.protocol.commons.buffer.Buffer;
Expand Down Expand Up @@ -122,4 +123,15 @@ public byte[] getRawTargetInfo() {
public WindowsVersion getVersion() {
return version;
}

@Override
public String toString() {
return "NtlmChallenge{\n" +
" targetName='" + targetName + "',\n" +
" negotiateFlags=" + negotiateFlags + ",\n" +
" serverChallenge=" + ByteArrayUtils.printHex(serverChallenge) + ",\n" +
" version=" + version + ",\n" +
" targetInfo=" + targetInfo + "\n" +
'}';
}
}
23 changes: 17 additions & 6 deletions src/main/java/com/hierynomus/ntlm/messages/NtlmNegotiate.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
*/
package com.hierynomus.ntlm.messages;

import com.hierynomus.protocol.commons.Charsets;
import com.hierynomus.protocol.commons.buffer.Buffer;
import static com.hierynomus.ntlm.messages.NtlmNegotiateFlag.NTLMSSP_NEGOTIATE_VERSION;
import static com.hierynomus.ntlm.messages.Utils.EMPTY;
import static com.hierynomus.ntlm.messages.Utils.oem;
import static com.hierynomus.ntlm.messages.Utils.writeOffsettedByteArrayFields;

import java.util.EnumSet;
import java.util.Set;

import static com.hierynomus.ntlm.messages.NtlmNegotiateFlag.*;
import static com.hierynomus.ntlm.functions.NtlmFunctions.unicode;
import static com.hierynomus.ntlm.messages.Utils.*;
import com.hierynomus.protocol.commons.Charsets;
import com.hierynomus.protocol.commons.EnumWithValue.EnumUtils;
import com.hierynomus.protocol.commons.buffer.Buffer;

/**
* [MS-NLMP].pdf 2.2.1.1 NEGOTIATE_MESSAGE
Expand Down Expand Up @@ -62,4 +63,14 @@ public void write(Buffer.PlainBuffer buffer) {
buffer.putRawBytes(domain); // DomainName (variable)
buffer.putRawBytes(workstation); // Workstation (variable)
}

@Override
public String toString() {
return "NtlmNegotiate{\n" +
" domain='" + new String(domain) + "'',\n" +
" workstation='" + new String(workstation) + "',\n" +
" negotiateFlags=" + negotiateFlags + ",\n" +
" version=" + version + "\n" +
"}";
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/hierynomus/ntlm/messages/TargetInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,10 @@ public String getAvPairString(AvId key) {
return String.valueOf(obj);
}

@Override
public String toString() {
return "TargetInfo{" +
"targetInfo=" + targetInfo +
'}';
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/hierynomus/smbj/auth/NtlmAuthenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public AuthenticateResponse authenticate(final AuthenticationContext context, fi
} catch (Buffer.BufferException e) {
throw new IOException(e);
}
logger.trace("Received NTLM challenge: {}", serverNtlmChallenge);
logger.debug("Received NTLM challenge from: {}", serverNtlmChallenge.getTargetName());

// Only keep the negotiate flags that the server indicated it supports
Expand Down Expand Up @@ -157,6 +158,7 @@ private AuthenticateResponse doNegotiate(AuthenticationContext context, byte[] g
}

this.negotiateMessage = new NtlmNegotiate(negotiateFlags, workStationName, context.getDomain(), windowsVersion);
logger.trace("Sending NTLM negotiate message: {}", this.negotiateMessage);
response.setNegToken(negTokenInit(negotiateMessage));
return response;
}
Expand Down Expand Up @@ -216,6 +218,7 @@ private AuthenticateResponse doAuthenticate(AuthenticationContext context, NtlmC
msg.setMic(mic);
}
response.setSessionKey(exportedSessionKey);
logger.trace("Sending NTLM authenticate message: {}", msg);
response.setNegToken(negTokenTarg(msg));

return response;
Expand Down

0 comments on commit 449d5d7

Please sign in to comment.