jcifs.ntlmssp.Type1Message Java Examples

The following examples show how to use jcifs.ntlmssp.Type1Message. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NtlmTest.java    From jcifs-ng with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testParsingType1 () throws IOException {
    int flags = 0x80000000;
    String suppliedDomain = "TESTDOM";
    String suppliedWorkstation = "TESTWS";
    Type1Message t1 = new Type1Message(this.context, flags, suppliedDomain, suppliedWorkstation);

    int origFlags = t1.getFlags();

    Type1Message parsed = new Type1Message(t1.toByteArray());

    assertEquals(origFlags, parsed.getFlags());

    if ( parsed.getFlag(NtlmFlags.NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED) ) {
        assertEquals(suppliedDomain, parsed.getSuppliedDomain());
    }

    if ( parsed.getFlag(NtlmFlags.NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED) ) {
        assertEquals(suppliedWorkstation, parsed.getSuppliedWorkstation());
    }
}
 
Example #2
Source File: NTLMSchemeFactory.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public String generateType1Msg(String domain, String workstation) throws NTLMEngineException {
    Type1Message type1Message = new Type1Message(Type1Message.getDefaultFlags(), domain, workstation);
    return Base64.encode(type1Message.toByteArray());
}
 
Example #3
Source File: NTLMSchemeFactory.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public String generateType1Msg(String domain, String workstation) throws NTLMEngineException {
    Type1Message type1Message = new Type1Message(Type1Message.getDefaultFlags(), domain, workstation);
    return Base64.encode(type1Message.toByteArray());
}
 
Example #4
Source File: JCIFSEngine.java    From davmail with GNU General Public License v2.0 4 votes vote down vote up
public String generateType1Msg(final String domain, final String workstation) {
    final Type1Message type1Message = new Type1Message(TYPE_1_FLAGS, domain, workstation);
    return Base64.encode(type1Message.toByteArray());
}
 
Example #5
Source File: NtlmAuthenticationHandler.java    From springboot-shiro-cas-mybatis with MIT License 4 votes vote down vote up
@Override
protected final HandlerResult doAuthentication(
        final Credential credential) throws GeneralSecurityException, PreventedException {

    final SpnegoCredential ntlmCredential = (SpnegoCredential) credential;
    final byte[] src = ntlmCredential.getInitToken();

    UniAddress dc = null;

    boolean success = false;
    try {
        if (this.loadBalance) {
            // find the first dc that matches the includepattern
            if (this.includePattern != null) {
                final NbtAddress[] dcs= NbtAddress.getAllByName(this.domainController, NBT_ADDRESS_TYPE, null, null);
                for (final NbtAddress dc2 : dcs) {
                    if(dc2.getHostAddress().matches(this.includePattern)){
                        dc = new UniAddress(dc2);
                        break;
                    }
                }
            } else {
                dc = new UniAddress(NbtAddress.getByName(this.domainController, NBT_ADDRESS_TYPE, null));
            }
        } else {
            dc = UniAddress.getByName(this.domainController, true);
        }
        final byte[] challenge = SmbSession.getChallenge(dc);

        switch (src[NTLM_TOKEN_TYPE_FIELD_INDEX]) {
            case NTLM_TOKEN_TYPE_ONE:
                logger.debug("Type 1 received");
                final Type1Message type1 = new Type1Message(src);
                final Type2Message type2 = new Type2Message(type1,
                        challenge, null);
                logger.debug("Type 2 returned. Setting next token.");
                ntlmCredential.setNextToken(type2.toByteArray());
                break;
            case NTLM_TOKEN_TYPE_THREE:
                logger.debug("Type 3 received");
                final Type3Message type3 = new Type3Message(src);
                final byte[] lmResponse = type3.getLMResponse() == null ? new byte[0] : type3.getLMResponse();
                final byte[] ntResponse = type3.getNTResponse() == null ? new byte[0] : type3.getNTResponse();
                final NtlmPasswordAuthentication ntlm = new NtlmPasswordAuthentication(
                        type3.getDomain(), type3.getUser(), challenge,
                        lmResponse, ntResponse);
                logger.debug("Trying to authenticate {} with domain controller", type3.getUser());
                try {
                    SmbSession.logon(dc, ntlm);
                    ntlmCredential.setPrincipal(this.principalFactory.createPrincipal(type3.getUser()));
                    success = true;
                } catch (final SmbAuthException sae) {
                    throw new FailedLoginException(sae.getMessage());
                }
                break;
            default:
                logger.debug("Unknown type: {}", src[NTLM_TOKEN_TYPE_FIELD_INDEX]);
        }
    } catch (final Exception e) {
        throw new FailedLoginException(e.getMessage());
    }

    if (!success) {
        throw new FailedLoginException();
    }
    return new DefaultHandlerResult(this, new BasicCredentialMetaData(ntlmCredential), ntlmCredential.getPrincipal());
}
 
Example #6
Source File: NtlmAuthenticationHandler.java    From cas4.0.x-server-wechat with Apache License 2.0 4 votes vote down vote up
@Override
protected final HandlerResult doAuthentication(
        final Credential credential) throws GeneralSecurityException, PreventedException {

    final SpnegoCredential ntlmCredential = (SpnegoCredential) credential;
    final byte[] src = ntlmCredential.getInitToken();

    UniAddress dc = null;

    boolean success = false;
    try {
        if (this.loadBalance) {
            // find the first dc that matches the includepattern
            if(this.includePattern != null){
                NbtAddress [] dcs  = NbtAddress.getAllByName(this.domainController, 0x1C, null, null);
                for (NbtAddress dc2 : dcs) {
                    if(dc2.getHostAddress().matches(this.includePattern)){
                        dc = new UniAddress(dc2);
                        break;
                    }
                }
            } else {
                dc = new UniAddress(NbtAddress.getByName(this.domainController,
                        0x1C, null));
            }
        } else {
            dc = UniAddress.getByName(this.domainController, true);
        }
        final byte[] challenge = SmbSession.getChallenge(dc);

        switch (src[8]) {
            case 1:
                logger.debug("Type 1 received");
                final Type1Message type1 = new Type1Message(src);
                final Type2Message type2 = new Type2Message(type1,
                        challenge, null);
                logger.debug("Type 2 returned. Setting next token.");
                ntlmCredential.setNextToken(type2.toByteArray());
            case 3:
                logger.debug("Type 3 received");
                final Type3Message type3 = new Type3Message(src);
                final byte[] lmResponse = type3.getLMResponse() == null ? new byte[0] : type3.getLMResponse();
                byte[] ntResponse = type3.getNTResponse() == null ? new byte[0] : type3.getNTResponse();
                final NtlmPasswordAuthentication ntlm = new NtlmPasswordAuthentication(
                        type3.getDomain(), type3.getUser(), challenge,
                        lmResponse, ntResponse);
                logger.debug("Trying to authenticate {} with domain controller", type3.getUser());
                try {
                    SmbSession.logon(dc, ntlm);
                    ntlmCredential.setPrincipal(new SimplePrincipal(type3.getUser()));
                    success = true;
                } catch (final SmbAuthException sae) {
                    throw new FailedLoginException(sae.getMessage());
                }
            default:
                logger.debug("Unknown type: {}", src[8]);
        }
    } catch (final Exception e) {
        throw new FailedLoginException(e.getMessage());
    }

    if (!success) {
        throw new FailedLoginException();
    }
    return new HandlerResult(this, new BasicCredentialMetaData(ntlmCredential), ntlmCredential.getPrincipal());
}
 
Example #7
Source File: NtlmContext.java    From jcifs with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected byte[] makeNegotiate ( byte[] token ) {
    Type1Message msg1 = new Type1Message(this.transportContext, this.ntlmsspFlags, this.auth.getUserDomain(), this.workstation);
    byte[] out = msg1.toByteArray();
    this.type1Bytes = out;

    if ( log.isTraceEnabled() ) {
        log.trace(msg1.toString());
        log.trace(Hexdump.toHexString(out));
    }

    this.state++;
    return out;
}
 
Example #8
Source File: NtlmSsp.java    From jcifs with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Performs NTLM authentication for the servlet request.
 * 
 * @param tc
 *            context to use
 *
 * @param req
 *            The request being serviced.
 * @param resp
 *            The response.
 * @param challenge
 *            The domain controller challenge.
 * @return credentials passed in the servlet request
 * @throws IOException
 *             If an IO error occurs.
 */
public static NtlmPasswordAuthentication authenticate ( CIFSContext tc, HttpServletRequest req, HttpServletResponse resp, byte[] challenge )
        throws IOException {
    String msg = req.getHeader("Authorization");
    if ( msg != null && msg.startsWith("NTLM ") ) {
        byte[] src = Base64.decode(msg.substring(5));
        if ( src[ 8 ] == 1 ) {
            Type1Message type1 = new Type1Message(src);
            Type2Message type2 = new Type2Message(tc, type1, challenge, null);
            msg = new String(Base64.encode(type2.toByteArray()), "US-ASCII");
            resp.setHeader("WWW-Authenticate", "NTLM " + msg);
        }
        else if ( src[ 8 ] == 3 ) {
            Type3Message type3 = new Type3Message(src);
            byte[] lmResponse = type3.getLMResponse();
            if ( lmResponse == null )
                lmResponse = new byte[0];
            byte[] ntResponse = type3.getNTResponse();
            if ( ntResponse == null )
                ntResponse = new byte[0];
            return new NtlmPasswordAuthentication(type3.getDomain(), type3.getUser(), challenge, lmResponse, ntResponse);
        }
    }
    else {
        resp.setHeader("WWW-Authenticate", "NTLM");
    }
    resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    resp.setContentLength(0);
    resp.flushBuffer();
    return null;
}
 
Example #9
Source File: NtlmTest.java    From jcifs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void testParsingType1 () throws IOException {
    int flags = 0x80000000;
    String suppliedDomain = "TESTDOM";
    String suppliedWorkstation = "TESTWS";
    Type1Message t1 = new Type1Message(this.context, flags, suppliedDomain, suppliedWorkstation);

    int origFlags = t1.getFlags();

    Type1Message parsed = new Type1Message(t1.toByteArray());

    assertEquals(origFlags, parsed.getFlags());

    if ( parsed.getFlag(NtlmFlags.NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED) ) {
        assertEquals(suppliedDomain, parsed.getSuppliedDomain());
    }

    if ( parsed.getFlag(NtlmFlags.NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED) ) {
        assertEquals(suppliedWorkstation, parsed.getSuppliedWorkstation());
    }
}
 
Example #10
Source File: NtlmContext.java    From jcifs-ng with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected byte[] makeNegotiate ( byte[] token ) {
    Type1Message msg1 = new Type1Message(this.transportContext, this.ntlmsspFlags, this.auth.getUserDomain(), this.workstation);
    byte[] out = msg1.toByteArray();
    this.type1Bytes = out;

    if ( log.isTraceEnabled() ) {
        log.trace(msg1.toString());
        log.trace(Hexdump.toHexString(out));
    }

    this.state++;
    return out;
}
 
Example #11
Source File: NtlmSsp.java    From jcifs-ng with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Performs NTLM authentication for the servlet request.
 * 
 * @param tc
 *            context to use
 *
 * @param req
 *            The request being serviced.
 * @param resp
 *            The response.
 * @param challenge
 *            The domain controller challenge.
 * @return credentials passed in the servlet request
 * @throws IOException
 *             If an IO error occurs.
 */
public static NtlmPasswordAuthentication authenticate ( CIFSContext tc, HttpServletRequest req, HttpServletResponse resp, byte[] challenge )
        throws IOException {
    String msg = req.getHeader("Authorization");
    if ( msg != null && msg.startsWith("NTLM ") ) {
        byte[] src = Base64.decode(msg.substring(5));
        if ( src[ 8 ] == 1 ) {
            Type1Message type1 = new Type1Message(src);
            Type2Message type2 = new Type2Message(tc, type1, challenge, null);
            msg = new String(Base64.encode(type2.toByteArray()), "US-ASCII");
            resp.setHeader("WWW-Authenticate", "NTLM " + msg);
        }
        else if ( src[ 8 ] == 3 ) {
            Type3Message type3 = new Type3Message(src);
            byte[] lmResponse = type3.getLMResponse();
            if ( lmResponse == null )
                lmResponse = new byte[0];
            byte[] ntResponse = type3.getNTResponse();
            if ( ntResponse == null )
                ntResponse = new byte[0];
            return new NtlmPasswordAuthentication(type3.getDomain(), type3.getUser(), challenge, lmResponse, ntResponse);
        }
    }
    else {
        resp.setHeader("WWW-Authenticate", "NTLM");
    }
    resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    resp.setContentLength(0);
    resp.flushBuffer();
    return null;
}
 
Example #12
Source File: NTLMSchemeFactory.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public String generateType1Msg(String domain, String workstation) throws NTLMEngineException {
    Type1Message type1Message = new Type1Message(Type1Message.getDefaultFlags(), domain, workstation);
    return Base64.encode(type1Message.toByteArray());
}
 
Example #13
Source File: NTLMSchemeFactory.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public String generateType1Msg(String domain, String workstation) throws NTLMEngineException {
    Type1Message type1Message = new Type1Message(Type1Message.getDefaultFlags(), domain, workstation);
    return Base64.encode(type1Message.toByteArray());
}
 
Example #14
Source File: JCIFSEngine.java    From cs-actions with Apache License 2.0 4 votes vote down vote up
public String generateType1Msg(final String domain, final String workstation)
        throws NTLMEngineException {
    final Type1Message type1Message = new Type1Message(TYPE_1_FLAGS, domain, workstation);
    return Base64.encode(type1Message.toByteArray());
}
 
Example #15
Source File: WebServiceNtlmSender.java    From iaf with Apache License 2.0 4 votes vote down vote up
@Override
public String generateType1Msg(final String domain, final String workstation) throws NTLMEngineException {
	final Type1Message type1Message = new Type1Message(TYPE_1_FLAGS, domain, workstation);
	return Base64.encode(type1Message.toByteArray());
}