Skip to content

Commit

Permalink
Adapterfähigkeit für jcifs
Browse files Browse the repository at this point in the history
  • Loading branch information
M. Noerder-Tuitje committed Dec 21, 2023
1 parent f25d5c5 commit 74dd4d3
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import static com.hierynomus.ntlm.messages.Utils.*;

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

/**
Expand Down Expand Up @@ -52,7 +53,7 @@ public NtlmAuthenticate(
this.domainName = ensureNotNull(domainName);
this.workstation = ensureNotNull(workstation);
this.encryptedRandomSessionKey = ensureNotNull(encryptedRandomSessionKey);
this.negotiateFlags = negotiateFlags;
this.negotiateFlags = EnumSet.copyOf(negotiateFlags);
}

private int getBaseMessageSize() {
Expand Down
100 changes: 99 additions & 1 deletion src/main/java/com/hierynomus/ntlm/messages/NtlmChallenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
import com.hierynomus.protocol.commons.Charsets;
import com.hierynomus.protocol.commons.EnumWithValue;
import com.hierynomus.protocol.commons.buffer.Buffer;
import com.hierynomus.protocol.commons.buffer.Endian;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.EnumSet;

import static com.hierynomus.ntlm.messages.NtlmNegotiateFlag.NTLMSSP_REQUEST_TARGET;

/**
* [MS-NLMP].pdf 2.2.1.2 CHALLENGE_MESSAGE
*/
Expand All @@ -32,14 +36,36 @@ public class NtlmChallenge extends NtlmPacket {

private int targetNameLen;
private int targetNameBufferOffset;
private EnumSet<NtlmNegotiateFlag> negotiateFlags;

private byte[] serverChallenge;
private WindowsVersion version;
private int targetInfoLen;
private int targetInfoBufferOffset;
private String targetName;
private TargetInfo targetInfo;


public NtlmChallenge () throws IOException {
}
public NtlmChallenge ( byte[] material ) throws IOException {
parse(material);
}

public NtlmChallenge(int flags, byte[] challenge, String target) {
setFlags(flags);
this.serverChallenge = challenge;
this.targetName = target;
// FIXME : correct? if (target != null) setTargetInformation(getDefaultTargetInformation());
}

public NtlmChallenge(NtlmNegotiate type1, byte[] challenge, String target) {
this(getDefaultFlags(type1), challenge,
(type1 != null &&
target == null &&
(type1.getFlags(type1.negotiateFlags) & NTLMSSP_REQUEST_TARGET.getValue())>0) ?
"coo" : target);
}

@Override
public void read(Buffer.PlainBuffer buffer) throws Buffer.BufferException {
buffer.readString(Charsets.UTF_8, 8); // Signature (8 bytes) (NTLMSSP\0)
Expand Down Expand Up @@ -126,4 +152,76 @@ public String toString() {
" targetInfo=" + targetInfo + "\n" +
'}';
}


private void parse ( byte[] input ) {
Buffer.PlainBuffer buffer = new Buffer.PlainBuffer(input, Endian.BE);
try {
read(buffer);
} catch (Buffer.BufferException e) {
throw new RuntimeException(e);
}

// int pos = 0;
// for ( int i = 0; i < 8; i++ ) {
// if ( input[ i ] != NTLMSSP_SIGNATURE[ i ] ) {
// throw new IOException("Not an NTLMSSP message.");
// }
// }
// pos += 8;
//
// if ( readULong(input, pos) != NTLMSSP_TYPE2 ) {
// throw new IOException("Not a Type 2 message.");
// }
// pos += 4;
//
// int flags = readULong(input, pos + 8);
// setFlags(flags);
//
// byte[] targetName = readSecurityBuffer(input, pos);
// int targetNameOff = readULong(input, pos + 4);
// if ( targetName.length != 0 ) {
// this.targetName = new String(targetName, ( ( flags & NtlmNegotiateFlag.NTLMSSP_NEGOTIATE_UNICODE.getValue() ) != 0 ) ? UNI_ENCODING : OEM_ENCODING);
// }
// pos += 12; // 8 for target, 4 for flags
//
// if ( !allZeros8(input, pos) ) {
// byte[] challengeBytes = new byte[8];
// System.arraycopy(input, pos, challengeBytes, 0, challengeBytes.length);
// serverChallenge = challengeBytes;
// }
// pos += 8;
//
// if ( targetNameOff < pos + 8 || input.length < pos + 8 ) {
// // no room for Context/Reserved
// return;
// }
//
// if ( !allZeros8(input, pos) ) {
// byte[] contextBytes = new byte[8];
// System.arraycopy(input, pos, contextBytes, 0, contextBytes.length);
// setContext(contextBytes);
// }
// pos += 8;
//
// if ( targetNameOff < pos + 8 || input.length < pos + 8 ) {
// // no room for target info
// return;
// }
//
// byte[] targetInfo = readSecurityBuffer(input, pos);
// if ( targetInfo.length != 0 ) {
// setTargetInformation(targetInfo);
// }
}

private static boolean allZeros8 ( byte[] input, int pos ) {
for ( int i = pos; i < pos + 8; i++ ) {
if ( input[ i ] != 0 ) {
return false;
}
}
return true;
}

}
11 changes: 9 additions & 2 deletions src/main/java/com/hierynomus/ntlm/messages/NtlmMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,23 @@
import static com.hierynomus.ntlm.messages.NtlmNegotiateFlag.*;

class NtlmMessage extends NtlmPacket {
protected static Set<NtlmNegotiateFlag> DEFAULT_FLAGS = EnumSet.of(

protected static EnumSet<NtlmNegotiateFlag> DEFAULT_FLAGS = EnumSet.of(
NTLMSSP_NEGOTIATE_NTLM,
NTLMSSP_NEGOTIATE_UNICODE);

protected Set<NtlmNegotiateFlag> negotiateFlags;

protected WindowsVersion version;

protected NtlmMessage(Set<NtlmNegotiateFlag> negotiateFlags, WindowsVersion version) {
this.negotiateFlags = EnumSet.copyOf(negotiateFlags);
this.negotiateFlags.addAll(DEFAULT_FLAGS);
this.version = version;
}

protected NtlmMessage() {
this(DEFAULT_FLAGS, new WindowsVersion(WindowsVersion.ProductMajorVersion.WINDOWS_MAJOR_VERSION_6, WindowsVersion.ProductMinorVersion.WINDOWS_MINOR_VERSION_0, 4711, WindowsVersion.NtlmRevisionCurrent.NTLMSSP_REVISION_W2K3));
}


}
37 changes: 37 additions & 0 deletions src/main/java/com/hierynomus/ntlm/messages/NtlmNegotiate.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.hierynomus.protocol.commons.Charsets;
import com.hierynomus.protocol.commons.buffer.Buffer;

import java.io.IOException;
import java.util.Set;

import static com.hierynomus.ntlm.messages.NtlmNegotiateFlag.*;
Expand All @@ -42,6 +43,11 @@ public NtlmNegotiate(Set<NtlmNegotiateFlag> flags, String domain, String worksta
this.omitVersion = omitVersion;
}

public NtlmNegotiate ( byte[] material ) throws IOException {
parse(material);
}


public void write(Buffer.PlainBuffer buffer) {
buffer.putString("NTLMSSP\0", Charsets.UTF_8); // Signature (8 bytes)
buffer.putUInt32(0x01); // MessageType (4 bytes)
Expand Down Expand Up @@ -95,4 +101,35 @@ public String toString() {
"}";
}


private void parse ( byte[] material ) throws IOException {
int pos = 0;
for ( int i = 0; i < 8; i++ ) {
if ( material[ i ] != NTLMSSP_SIGNATURE[ i ] ) {
throw new IOException("Not an NTLMSSP message.");
}
}
pos += 8;

if ( readULong(material, pos) != NTLMSSP_TYPE1 ) {
throw new IOException("Not a Type 1 message.");
}
pos += 4;

int flags = readULong(material, pos);
setFlags(flags);
pos += 4;

if ( ( flags & NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED.getValue() ) != 0 ) {
this.domain = readSecurityBuffer(material, pos);
}
pos += 8;

if ( ( flags & NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED.getValue() ) != 0 ) {
this.workstation = readSecurityBuffer(material, pos);
}
pos += 8;
}


}
75 changes: 75 additions & 0 deletions src/main/java/com/hierynomus/ntlm/messages/NtlmPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,26 @@
import com.hierynomus.protocol.Packet;
import com.hierynomus.protocol.commons.buffer.Buffer;

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

import static com.hierynomus.ntlm.messages.NtlmMessage.DEFAULT_FLAGS;
import static com.hierynomus.ntlm.messages.NtlmNegotiateFlag.*;

public class NtlmPacket implements Packet<Buffer.PlainBuffer> {

protected static final int NTLMSSP_TYPE1 = 0x1;
protected static final int NTLMSSP_TYPE2 = 0x2;
protected static final int NTLMSSP_TYPE3 = 0x3;
protected EnumSet<NtlmNegotiateFlag> negotiateFlags;

protected static final String OEM_ENCODING = "cp850";
protected static final String UNI_ENCODING = "UTF-16LE";


protected static final byte[] NTLMSSP_SIGNATURE = new byte[] {
(byte) 'N', (byte) 'T', (byte) 'L', (byte) 'M', (byte) 'S', (byte) 'S', (byte) 'P', (byte) 0
};
@Override
public void write(Buffer.PlainBuffer buffer) {
throw new UnsupportedOperationException("Not implemented by base class");
Expand All @@ -29,4 +47,61 @@ public void write(Buffer.PlainBuffer buffer) {
public void read(Buffer.PlainBuffer buffer) throws Buffer.BufferException {
throw new UnsupportedOperationException("Not implemented by base class");
}


static int readULong ( byte[] src, int index ) {
return ( src[ index ] & 0xff ) | ( ( src[ index + 1 ] & 0xff ) << 8 ) | ( ( src[ index + 2 ] & 0xff ) << 16 )
| ( ( src[ index + 3 ] & 0xff ) << 24 );
}


static int readUShort ( byte[] src, int index ) {
return ( src[ index ] & 0xff ) | ( ( src[ index + 1 ] & 0xff ) << 8 );
}



static byte[] readSecurityBuffer ( byte[] src, int index ) {
int length = readUShort(src, index);
int offset = readULong(src, index + 4);
byte[] buffer = new byte[length];
System.arraycopy(src, offset, buffer, 0, length);
return buffer;
}


protected void setFlags(int flags) {
for (NtlmNegotiateFlag flag : NtlmNegotiateFlag.values()) {
if ((flag.getValue() & flags) != 0) {
negotiateFlags.add(flag);
}
}
}

public static int getFlags(EnumSet<NtlmNegotiateFlag> flags) {
int result = 0;
for (NtlmNegotiateFlag flag : flags) {
result |= flag.getValue();
}
return result;
}


public static int getDefaultFlags(NtlmNegotiate type1) {
if (type1 == null) {
return getFlags(DEFAULT_FLAGS);
}

int flags = (int) NTLMSSP_NEGOTIATE_NTLM.getValue();
int type1Flags = type1.getFlags(type1.negotiateFlags);
flags |= ((type1Flags & NTLMSSP_NEGOTIATE_UNICODE.getValue()) != 0) ?
NTLMSSP_NEGOTIATE_UNICODE.getValue() : NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED.getValue();
if ((type1Flags & NTLMSSP_REQUEST_TARGET.getValue()) != 0) {
String domain = "coo";
if (domain != null) {
flags |= NTLMSSP_REQUEST_TARGET.getValue() | NTLMSSP_TARGET_TYPE_DOMAIN.getValue();
}
}
return flags;
}
}

This file was deleted.

0 comments on commit 74dd4d3

Please sign in to comment.