📄 ntlmresponses.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * */package org.apache.mina.proxy.handlers.http.ntlm;import java.io.UnsupportedEncodingException;import java.security.Key;import java.security.MessageDigest;import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;/** * NTLMResponses.java - Calculates the various Type 3 responses. Needs an MD4, MD5 and DES * crypto provider (Please note that default provider doesn't provide MD4). * * Copyright (c) 2003 Eric Glass * Permission to use, copy, modify, and distribute this document for any purpose and without * any fee is hereby granted, provided that the above copyright notice and this list of * conditions appear in all copies. * @see http://curl.haxx.se/rfc/ntlm.html * * @author The Apache MINA Project (dev@mina.apache.org) * @version $Rev: 756987 $, $Date: 2009-03-21 19:47:52 +0100 (Sat, 21 Mar 2009) $ * @since MINA 2.0.0-M3 */public class NTLMResponses { // LAN Manager magic constant used in LM Response calculation public static byte[] LM_HASH_MAGIC_CONSTANT = null; static { try { LM_HASH_MAGIC_CONSTANT = "KGS!@#$%".getBytes("US-ASCII"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } /** * Calculates the LM Response for the given challenge, using the specified * password. * * @param password The user's password. * @param challenge The Type 2 challenge from the server. * * @return The LM Response. */ public static byte[] getLMResponse(String password, byte[] challenge) throws Exception { byte[] lmHash = lmHash(password); return lmResponse(lmHash, challenge); } /** * Calculates the NTLM Response for the given challenge, using the * specified password. * * @param password The user's password. * @param challenge The Type 2 challenge from the server. * * @return The NTLM Response. */ public static byte[] getNTLMResponse(String password, byte[] challenge) throws Exception { byte[] ntlmHash = ntlmHash(password); return lmResponse(ntlmHash, challenge); } /** * Calculates the NTLMv2 Response for the given challenge, using the * specified authentication target, username, password, target information * block, and client nonce. * * @param target The authentication target (i.e., domain). * @param user The username. * @param password The user's password. * @param targetInformation The target information block from the Type 2 * message. * @param challenge The Type 2 challenge from the server. * @param clientNonce The random 8-byte client nonce. * * @return The NTLMv2 Response. */ public static byte[] getNTLMv2Response(String target, String user, String password, byte[] targetInformation, byte[] challenge, byte[] clientNonce) throws Exception { return getNTLMv2Response(target, user, password, targetInformation, challenge, clientNonce, System.currentTimeMillis()); } /** * Calculates the NTLMv2 Response for the given challenge, using the * specified authentication target, username, password, target information * block, and client nonce. * * @param target The authentication target (i.e., domain). * @param user The username. * @param password The user's password. * @param targetInformation The target information block from the Type 2 * message. * @param challenge The Type 2 challenge from the server. * @param clientNonce The random 8-byte client nonce. * @param time The time stamp. * * @return The NTLMv2 Response. */ public static byte[] getNTLMv2Response(String target, String user, String password, byte[] targetInformation, byte[] challenge, byte[] clientNonce, long time) throws Exception { byte[] ntlmv2Hash = ntlmv2Hash(target, user, password); byte[] blob = createBlob(targetInformation, clientNonce, time); return lmv2Response(ntlmv2Hash, blob, challenge); } /** * Calculates the LMv2 Response for the given challenge, using the * specified authentication target, username, password, and client * challenge. * * @param target The authentication target (i.e., domain). * @param user The username. * @param password The user's password. * @param challenge The Type 2 challenge from the server. * @param clientNonce The random 8-byte client nonce. * * @return The LMv2 Response. */ public static byte[] getLMv2Response(String target, String user, String password, byte[] challenge, byte[] clientNonce) throws Exception { byte[] ntlmv2Hash = ntlmv2Hash(target, user, password); return lmv2Response(ntlmv2Hash, clientNonce, challenge); } /** * Calculates the NTLM2 Session Response for the given challenge, using the * specified password and client nonce. * * @param password The user's password. * @param challenge The Type 2 challenge from the server. * @param clientNonce The random 8-byte client nonce. * * @return The NTLM2 Session Response. This is placed in the NTLM * response field of the Type 3 message; the LM response field contains * the client nonce, null-padded to 24 bytes. */ public static byte[] getNTLM2SessionResponse(String password, byte[] challenge, byte[] clientNonce) throws Exception { byte[] ntlmHash = ntlmHash(password); MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(challenge); md5.update(clientNonce); byte[] sessionHash = new byte[8]; System.arraycopy(md5.digest(), 0, sessionHash, 0, 8); return lmResponse(ntlmHash, sessionHash); } /** * Creates the LM Hash of the user's password. * * @param password The password. * * @return The LM Hash of the given password, used in the calculation * of the LM Response. */ private static byte[] lmHash(String password) throws Exception { byte[] oemPassword = password.toUpperCase().getBytes("US-ASCII"); int length = Math.min(oemPassword.length, 14); byte[] keyBytes = new byte[14]; System.arraycopy(oemPassword, 0, keyBytes, 0, length); Key lowKey = createDESKey(keyBytes, 0); Key highKey = createDESKey(keyBytes, 7); Cipher des = Cipher.getInstance("DES/ECB/NoPadding"); des.init(Cipher.ENCRYPT_MODE, lowKey); byte[] lowHash = des.doFinal(LM_HASH_MAGIC_CONSTANT); des.init(Cipher.ENCRYPT_MODE, highKey); byte[] highHash = des.doFinal(LM_HASH_MAGIC_CONSTANT); byte[] lmHash = new byte[16]; System.arraycopy(lowHash, 0, lmHash, 0, 8); System.arraycopy(highHash, 0, lmHash, 8, 8); return lmHash; } /** * Creates the NTLM Hash of the user's password. * * @param password The password. * * @return The NTLM Hash of the given password, used in the calculation * of the NTLM Response and the NTLMv2 and LMv2 Hashes.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -