1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.proxy;
21
22 import static org.apache.mina.proxy.utils.ByteUtilities.asHex;
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.PrintWriter;
26 import java.io.UnsupportedEncodingException;
27 import java.security.MessageDigest;
28 import java.security.NoSuchAlgorithmException;
29 import java.security.Security;
30
31 import org.apache.mina.proxy.handlers.http.ntlm.NTLMResponses;
32 import org.apache.mina.proxy.handlers.http.ntlm.NTLMUtilities;
33 import org.apache.mina.proxy.utils.ByteUtilities;
34 import org.apache.mina.proxy.utils.MD4Provider;
35 import org.junit.Test;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40
41
42
43
44
45 public class NTLMTest {
46 private final static Logger logger = LoggerFactory
47 .getLogger(NTLMTest.class);
48
49 static {
50 if (Security.getProvider("MINA") == null) {
51 Security.addProvider(new MD4Provider());
52 }
53 }
54
55
56
57
58 @Test
59 public void testEncoding() throws UnsupportedEncodingException {
60 assertEquals("d204", asHex(ByteUtilities.writeShort((short) 1234)));
61 assertEquals("d2040000", asHex(ByteUtilities.writeInt(1234)));
62 assertEquals("01000000", asHex(ByteUtilities.writeInt((short) 1)));
63 assertEquals("4e544c4d53535000", asHex(NTLMUtilities.NTLM_SIGNATURE));
64
65 assertEquals("680065006c006c006f00", asHex(ByteUtilities
66 .getUTFStringAsByteArray("hello")));
67 assertEquals("48454c4c4f", asHex(ByteUtilities
68 .getOEMStringAsByteArray("HELLO")));
69 }
70
71
72
73
74 @Test
75 public void testMethods() {
76 byte[] buf = new byte[4];
77 ByteUtilities.intToNetworkByteOrder(1234, buf, 0, 4);
78 assertEquals("000004d2", asHex(buf));
79 assertEquals(1234, ByteUtilities.networkByteOrderToInt(buf, 0, 4));
80 }
81
82
83
84
85 @Test
86 public void testSecurityBuffer() {
87 byte[] secBuf = new byte[8];
88 NTLMUtilities.writeSecurityBuffer((short) 1234, (short) 1234, 4321,
89 secBuf, 0);
90 assertEquals("d204d204e1100000", asHex(secBuf));
91 }
92
93
94
95
96 @Test
97 public void testType1Message() {
98 int customFlags = NTLMUtilities.FLAG_NEGOTIATE_UNICODE
99 | NTLMUtilities.FLAG_NEGOTIATE_OEM
100 | NTLMUtilities.FLAG_NEGOTIATE_NTLM
101 | NTLMUtilities.FLAG_REQUEST_SERVER_AUTH_REALM
102 | NTLMUtilities.FLAG_NEGOTIATE_DOMAIN_SUPPLIED
103 | NTLMUtilities.FLAG_NEGOTIATE_WORKSTATION_SUPPLIED;
104
105 byte[] osVer = new byte[8];
106 NTLMUtilities
107 .writeOSVersion((byte) 5, (byte) 0, (short) 2195, osVer, 0);
108
109 String msgType1 = asHex(NTLMUtilities.createType1Message("WORKSTATION",
110 "DOMAIN", customFlags, osVer));
111 assertEquals(
112 "4e544c4d53535000010000000732000006000600330000000b000b0028000000"
113 + "050093080000000f574f524b53544154494f4e444f4d41494e",
114 msgType1);
115
116 assertEquals("050093080000000f", asHex(osVer));
117
118
119 String os = System.getProperty("os.name");
120 if (os != null && os.toUpperCase().contains("WINDOWS") &&
121 "5.1".equals(System.getProperty("os.version"))) {
122 String hex = asHex(NTLMUtilities.getOsVersion());
123 assertEquals("0501", hex.substring(0, 4));
124 assertEquals(16, hex.length());
125 }
126 }
127
128
129
130
131
132 @Test
133 public void testType3Message() throws Exception {
134 try {
135 MessageDigest.getInstance("MD4");
136 } catch (NoSuchAlgorithmException ex) {
137 logger.warn("No MD4 digest provider found !");
138 return;
139 }
140
141 int flags = 0x00000001 | 0x00000200 | 0x00010000 | 0x00800000;
142 String msg = "4e544c4d53535000020000000c000c003000000001028100"
143 + "0123456789abcdef0000000000000000620062003c000000"
144 + "44004f004d00410049004e0002000c0044004f004d004100"
145 + "49004e0001000c0053004500520056004500520004001400"
146 + "64006f006d00610069006e002e0063006f006d0003002200"
147 + "7300650072007600650072002e0064006f006d0061006900"
148 + "6e002e0063006f006d0000000000";
149
150 byte[] challengePacket = ByteUtilities.asByteArray(msg);
151 int serverFlags = NTLMUtilities
152 .extractFlagsFromType2Message(challengePacket);
153 assertEquals(flags, serverFlags);
154
155 NTLMUtilities
156 .printTargetInformationBlockFromType2Message(challengePacket,
157 serverFlags, new PrintWriter(System.out, true));
158
159 byte[] osVer = new byte[8];
160 NTLMUtilities
161 .writeOSVersion((byte) 5, (byte) 0, (short) 2195, osVer, 0);
162
163 byte[] challenge = NTLMUtilities
164 .extractChallengeFromType2Message(challengePacket);
165 assertEquals("0123456789abcdef", asHex(challenge));
166
167 String expectedTargetInfoBlock = "02000c0044004f004d00410049004e00"
168 + "01000c00530045005200560045005200"
169 + "0400140064006f006d00610069006e00"
170 + "2e0063006f006d000300220073006500"
171 + "72007600650072002e0064006f006d00"
172 + "610069006e002e0063006f006d000000" + "0000";
173
174 byte[] targetInfo = NTLMUtilities.extractTargetInfoFromType2Message(
175 challengePacket, null);
176 assertEquals(expectedTargetInfoBlock, asHex(targetInfo));
177
178 assertEquals("DOMAIN", NTLMUtilities.extractTargetNameFromType2Message(
179 challengePacket, new Integer(serverFlags)));
180
181 serverFlags = 0x00000001 | 0x00000200;
182 String msgType3 = asHex(NTLMUtilities.createType3Message("user",
183 "SecREt01", challenge, "DOMAIN", "WORKSTATION", serverFlags,
184 null));
185
186 String expected = "4e544c4d5353500003000000180018006a00000018001800"
187 + "820000000c000c0040000000080008004c00000016001600"
188 + "54000000000000009a0000000102000044004f004d004100"
189 + "49004e00750073006500720057004f0052004b0053005400"
190 + "4100540049004f004e00c337cd5cbd44fc9782a667af6d42"
191 + "7c6de67c20c2d3e77c5625a98c1c31e81847466b29b2df46"
192 + "80f39958fb8c213a9cc6";
193 assertEquals(expected, msgType3);
194 }
195
196
197
198
199 @Test
200 public void testFlags() {
201 int flags = NTLMUtilities.FLAG_NEGOTIATE_UNICODE
202 | NTLMUtilities.FLAG_REQUEST_SERVER_AUTH_REALM
203 | NTLMUtilities.FLAG_NEGOTIATE_NTLM
204 | NTLMUtilities.FLAG_NEGOTIATE_ALWAYS_SIGN;
205
206 int flags2 = NTLMUtilities.FLAG_NEGOTIATE_UNICODE
207 | NTLMUtilities.FLAG_REQUEST_SERVER_AUTH_REALM
208 | NTLMUtilities.FLAG_NEGOTIATE_NTLM;
209
210 assertEquals(flags2, flags
211 & (~NTLMUtilities.FLAG_NEGOTIATE_ALWAYS_SIGN));
212 assertEquals(flags2, flags2
213 & (~NTLMUtilities.FLAG_NEGOTIATE_ALWAYS_SIGN));
214 assertEquals("05820000", asHex(ByteUtilities.writeInt(flags)));
215
216 byte[] testFlags = ByteUtilities.asByteArray("7F808182");
217 assertEquals("7f808182", asHex(testFlags));
218 ByteUtilities.changeByteEndianess(testFlags, 0, 4);
219 assertEquals("807f8281", asHex(testFlags));
220 ByteUtilities.changeByteEndianess(testFlags, 0, 4);
221 ByteUtilities.changeWordEndianess(testFlags, 0, 4);
222 assertEquals("8281807f", asHex(testFlags));
223 }
224
225
226
227
228
229 @Test
230 public void testResponses() throws Exception {
231 try {
232 MessageDigest.getInstance("MD4");
233 } catch (NoSuchAlgorithmException ex) {
234 logger.warn("No MD4 digest provider found !");
235 return;
236 }
237
238 String LMResponse = "c337cd5cbd44fc9782a667af6d427c6de67c20c2d3e77c56";
239
240 assertEquals(LMResponse, asHex(NTLMResponses.getLMResponse("SecREt01",
241 ByteUtilities.asByteArray("0123456789abcdef"))));
242
243 String NTLMResponse = "25a98c1c31e81847466b29b2df4680f39958fb8c213a9cc6";
244
245 assertEquals(NTLMResponse, asHex(NTLMResponses.getNTLMResponse(
246 "SecREt01", ByteUtilities.asByteArray("0123456789abcdef"))));
247
248 String LMv2Response = "d6e6152ea25d03b7c6ba6629c2d6aaf0ffffff0011223344";
249
250 assertEquals(LMv2Response, asHex(NTLMResponses.getLMv2Response(
251 "DOMAIN", "user", "SecREt01", ByteUtilities
252 .asByteArray("0123456789abcdef"), ByteUtilities
253 .asByteArray("ffffff0011223344"))));
254
255 String NTLM2Response = "10d550832d12b2ccb79d5ad1f4eed3df82aca4c3681dd455";
256
257 assertEquals(NTLM2Response, asHex(NTLMResponses
258 .getNTLM2SessionResponse("SecREt01", ByteUtilities
259 .asByteArray("0123456789abcdef"), ByteUtilities
260 .asByteArray("ffffff0011223344"))));
261
262 String NTLMv2Response = "cbabbca713eb795d04c97abc01ee4983"
263 + "01010000000000000090d336b734c301"
264 + "ffffff00112233440000000002000c00"
265 + "44004f004d00410049004e0001000c00"
266 + "53004500520056004500520004001400"
267 + "64006f006d00610069006e002e006300"
268 + "6f006d00030022007300650072007600"
269 + "650072002e0064006f006d0061006900"
270 + "6e002e0063006f006d00000000000000" + "0000";
271
272 String targetInformation = "02000c0044004f004d00410049004e00"
273 + "01000c00530045005200560045005200"
274 + "0400140064006f006d00610069006e00"
275 + "2e0063006f006d000300220073006500"
276 + "72007600650072002e0064006f006d00"
277 + "610069006e002e0063006f006d000000" + "0000";
278
279 assertEquals(NTLMv2Response, asHex(NTLMResponses.getNTLMv2Response(
280 "DOMAIN", "user", "SecREt01", ByteUtilities
281 .asByteArray(targetInformation), ByteUtilities
282 .asByteArray("0123456789abcdef"), ByteUtilities
283 .asByteArray("ffffff0011223344"), 1055844000000L)));
284 }
285 }