/** * Returns a hexadecimal representation of the given byte array. * * @param bytes the array to output to an hex string * @return the hex representation as a string */ public static String asHex(byte[] bytes) { return asHex(bytes, null); }
/** * Reads the byte array described by the security buffer stored at the * <code>securityBufferOffset</code> offset. * * @param msg the message where to read the security buffer and it's value * @param securityBufferOffset the offset at which to read the security buffer * @return a new byte array holding the data pointed by the security buffer */ public static final byte[] readSecurityBufferTarget(byte[] msg, int securityBufferOffset) { byte[] securityBuffer = new byte[8]; System.arraycopy(msg, securityBufferOffset, securityBuffer, 0, 8); ByteUtilities.changeWordEndianess(securityBuffer, 0, 8); int length = ByteUtilities.makeIntFromByte2(securityBuffer); int offset = ByteUtilities.makeIntFromByte4(securityBuffer, 4); byte[] secBufValue = new byte[length]; System.arraycopy(msg, offset, secBufValue, 0, length); return secBufValue; }
/** * Encodes the string to a byte array using UTF-16LE or the ASCII charset * in function of the <code>useUnicode</code> argument. * * @param s the string to encode * @param useUnicode if true then string is encoded to UTF-16LE * otherwise to ASCII * @return the encoded string as a byte array * @throws UnsupportedEncodingException if encoding fails */ public static final byte[] encodeString(String s, boolean useUnicode) throws UnsupportedEncodingException { if (useUnicode) { return getUTFStringAsByteArray(s); } return getOEMStringAsByteArray(s); }
/** * Extracts the NTLM flags from the type 2 message. * * @param msg the type 2 message byte array * @return the proxy flags as an int */ public static final int extractFlagsFromType2Message(byte[] msg) { byte[] flagsBytes = new byte[4]; System.arraycopy(msg, 20, flagsBytes, 0, 4); ByteUtilities.changeWordEndianess(flagsBytes, 0, 4); return ByteUtilities.makeIntFromByte4(flagsBytes); }
ByteUtilities.changeByteEndianess(len, 0, 2); int length = ByteUtilities.makeIntFromByte2(len, 0); out.println("Length: " + length + " bytes"); out.print("Data: "); if (ByteUtilities.isFlagSet(flags, FLAG_NEGOTIATE_UNICODE)) { out.println(new String(infoBlock, pos + 4, length, "UTF-16LE")); } else {
baos.write(ByteUtilities.writeInt(MESSAGE_TYPE_3)); boolean useUnicode = ByteUtilities.isFlagSet(flags, FLAG_NEGOTIATE_UNICODE); byte[] targetName = ByteUtilities.encodeString(target, useUnicode); byte[] userName = ByteUtilities.encodeString(user, useUnicode); byte[] workstationName = ByteUtilities.encodeString(workstation, useUnicode); baos.write(ByteUtilities.writeInt(flags));
baos.write(ByteUtilities.writeInt(MESSAGE_TYPE_1)); baos.write(ByteUtilities.writeInt(flags)); byte[] domainData = ByteUtilities.getOEMStringAsByteArray(domain); byte[] workStationData = ByteUtilities.getOEMStringAsByteArray(workStation);
LOGGER.debug(" Received Token[{}] = {}", token.length, ByteUtilities.asHex(token)); LOGGER.debug(" Sending Token[{}] = {}", token.length, ByteUtilities.asHex(token)); SocksProxyConstants.GSSAPI_MSG_TYPE }); buf.put(ByteUtilities.intToNetworkByteOrder(token.length, 2)); buf.put(token);
byte[] size = new byte[2]; buf.get(size); int s = ByteUtilities.makeIntFromByte2(size); if (buf.remaining() >= s) { byte[] token = new byte[s]; throw new Exception("Proxy handshake failed - Code: 0x" + ByteUtilities.asHex(new byte[] { status }));
/** * Extracts the target name from the type 2 message. * * @param msg the type 2 message byte array * @param msgFlags the flags if null then flags are extracted from the * type 2 message * @return the target name * @throws UnsupportedEncodingException if unable to use the * needed UTF-16LE or ASCII charsets */ public static final String extractTargetNameFromType2Message(byte[] msg, Integer msgFlags) throws UnsupportedEncodingException { // Read the security buffer to determine where the target name // is stored and what it's length is byte[] targetName = readSecurityBufferTarget(msg, 12); // now we convert it to a string int flags = msgFlags == null ? extractFlagsFromType2Message(msg) : msgFlags; if (ByteUtilities.isFlagSet(flags, FLAG_NEGOTIATE_UNICODE)) { return new String(targetName, "UTF-16LE"); } return new String(targetName, "ASCII"); }
/** * Encodes an integer into up to 4 bytes in network byte order. * * @param num the int to convert to a byte array * @param count the number of reserved bytes for the write operation * @return the resulting byte array */ public static byte[] intToNetworkByteOrder(int num, int count) { byte[] buf = new byte[count]; intToNetworkByteOrder(num, buf, 0, count); return buf; }
/** * Reads an int from 4 bytes of the given array at offset 0. * * @param b the byte array to read * @return the integer value */ public static final int makeIntFromByte4(byte[] b) { return makeIntFromByte4(b, 0); }
/** * Reads an int from 2 bytes of the given array at offset 0. * * @param b the byte array to read * @return the int value */ public static final int makeIntFromByte2(byte[] b) { return makeIntFromByte2(b, 0); }
ByteUtilities.changeByteEndianess(len, 0, 2); int length = ByteUtilities.makeIntFromByte2(len, 0); out.println("Length: " + length + " bytes"); out.print("Data: "); if (ByteUtilities.isFlagSet(flags, FLAG_NEGOTIATE_UNICODE)) { out.println(new String(infoBlock, pos + 4, length, "UTF-16LE")); } else {
baos.write(ByteUtilities.writeInt(MESSAGE_TYPE_3)); boolean useUnicode = ByteUtilities.isFlagSet(flags, FLAG_NEGOTIATE_UNICODE); byte[] targetName = ByteUtilities.encodeString(target, useUnicode); byte[] userName = ByteUtilities.encodeString(user, useUnicode); byte[] workstationName = ByteUtilities.encodeString(workstation, useUnicode); baos.write(ByteUtilities.writeInt(flags));
/** * Extracts the NTLM flags from the type 2 message. * * @param msg the type 2 message byte array * @return the proxy flags as an int */ public static int extractFlagsFromType2Message(byte[] msg) { byte[] flagsBytes = new byte[4]; System.arraycopy(msg, 20, flagsBytes, 0, 4); ByteUtilities.changeWordEndianess(flagsBytes, 0, 4); return ByteUtilities.makeIntFromByte4(flagsBytes); }
baos.write(ByteUtilities.writeInt(MESSAGE_TYPE_1)); baos.write(ByteUtilities.writeInt(flags)); byte[] domainData = ByteUtilities.getOEMStringAsByteArray(domain); byte[] workStationData = ByteUtilities.getOEMStringAsByteArray(workStation);
LOGGER.debug(" Received Token[{}] = {}", token.length, ByteUtilities.asHex(token)); LOGGER.debug(" Sending Token[{}] = {}", token.length, ByteUtilities.asHex(token)); SocksProxyConstants.GSSAPI_MSG_TYPE }); buf.put(ByteUtilities.intToNetworkByteOrder(token.length, 2)); buf.put(token);
byte[] size = new byte[2]; buf.get(size); int s = ByteUtilities.makeIntFromByte2(size); if (buf.remaining() >= s) { byte[] token = new byte[s]; throw new Exception("Proxy handshake failed - Code: 0x" + ByteUtilities.asHex(new byte[] { status }));
/** * Extracts the target information block from the type 2 message. * * @param msg the type 2 message byte array * @param msgFlags the flags if null then flags are extracted from the * type 2 message * @return the target info */ public static final byte[] extractTargetInfoFromType2Message(byte[] msg, Integer msgFlags) { int flags = msgFlags == null ? extractFlagsFromType2Message(msg) : msgFlags; if (!ByteUtilities.isFlagSet(flags, FLAG_NEGOTIATE_TARGET_INFO)) { return null; } int pos = 40; return readSecurityBufferTarget(msg, pos); }