/** * Unpack a string from the byte area * * @param uni boolean * @return String */ public final String unpackString(boolean uni) { // Check for Unicode or ASCII String ret = null; if (uni) { // Word align the current buffer position m_pos = DataPacker.wordAlign(m_pos); ret = DataPacker.getUnicodeString(m_smbbuf, m_pos, m_smbbuf.length - m_pos); if (ret != null) m_pos += (ret.length() * 2) + 2; } else { // Unpack the ASCII string ret = DataPacker.getString(m_smbbuf, m_pos, m_smbbuf.length - m_pos); if (ret != null) m_pos += ret.length() + 1; } // Return the string return ret; }
/** * Send the SMB response packet. * * @param out Output stream associated with the session socket. * @param proto Protocol type, either PROTOCOL_NETBIOS or PROTOCOL_TCPIP * @param len Packet length * @exception java.io.IOException If an I/O error occurs. */ public final void SendResponseSMB(DataOutputStream out, int proto, int len) throws java.io.IOException { // Make sure the response flag is set int flg = getFlags(); if ((flg & FLG_RESPONSE) == 0) setFlags(flg + FLG_RESPONSE); // NetBIOS SMB protocol if (proto == PROTOCOL_NETBIOS) { // Fill in the NetBIOS message header, this is already allocated as // part of the users buffer. m_smbbuf[0] = (byte) RFCNetBIOSProtocol.SESSION_MESSAGE; m_smbbuf[1] = (byte) 0; DataPacker.putShort((short) len, m_smbbuf, 2); } else { // TCP/IP native SMB DataPacker.putInt(len, m_smbbuf, 0); } // Output the data packet len += RFCNetBIOSProtocol.HEADER_LEN; out.write(m_smbbuf, 0, len); }
/** * Initialize the DCE/RPC request. Set the SMB transaction parameter count so that the data offset * can be calculated. * * @param handle int * @param typ byte * @param flags int * @param callId int */ public final void initializeDCERequest(int handle, byte typ, int flags, int callId) { // Initialize the transaction InitializeTransact(16, null, 0, null, 0); // Set the parameter byte count/offset for this packet int bytPos = DCEDataPacker.longwordAlign(getByteOffset()); setParameter(3, 0); setParameter(4, bytPos - RFCNetBIOSProtocol.HEADER_LEN); // Set the parameter displacement setParameter(5, 0); // Set the data byte count/offset for this packet setParameter(6, 0); setParameter(7, bytPos - RFCNetBIOSProtocol.HEADER_LEN); // Set the data displacement setParameter(8, 0); // Set up word count setParameter(9, 0); // Set the setup words setSetupParameter(0, PacketType.TransactNmPipe); setSetupParameter(1, handle); // Reset the DCE offset for a DCE reply m_offset = bytPos; // Build the DCE/RPC header byte[] buf = getBuffer(); DataPacker.putZeros(buf, m_offset, 24); buf[m_offset + VERSIONMAJOR] = HDR_VERSIONMAJOR; buf[m_offset + VERSIONMINOR] = HDR_VERSIONMINOR; buf[m_offset + PDUTYPE] = typ; buf[m_offset + HEADERFLAGS] = (byte) (flags & 0xFF); DataPacker.putIntelInt(HDR_PACKEDDATAREP, buf, m_offset + PACKEDDATAREP); DataPacker.putIntelInt(0, buf, m_offset + AUTHLEN); DataPacker.putIntelInt(callId, buf, m_offset + CALLID); }
/** * Get the data byte count for the SMB packet * * @return Data byte count */ public final int getByteCount() { // Calculate the offset of the byte count int pos = PARAMWORDS + (2 * getParameterCount()); return (int) DataPacker.getIntelShort(m_smbbuf, pos); }
/** * Set the data byte area in the SMB packet * * @param byts Byte array containing the data to be copied to the SMB packet. */ public final void setBytes(byte[] byts) { int offset = getByteOffset() - 2; DataPacker.putIntelShort(byts.length, m_smbbuf, offset); offset += 2; for (int idx = 0; idx < byts.length; m_smbbuf[offset + idx] = byts[idx++]) ; }
/** * Pack a string using either ASCII or Unicode into the byte area * * @param str String * @param uni boolean */ public final void packString(String str, boolean uni) { // Check for Unicode or ASCII if (uni) { // Word align the buffer position, pack the Unicode string m_pos = DataPacker.wordAlign(m_pos); DataPacker.putUnicodeString(str, m_smbbuf, m_pos, true); m_pos += (str.length() * 2) + 2; } else { // Pack the ASCII string DataPacker.putString(str, m_smbbuf, m_pos, true); m_pos += str.length() + 1; } }
/** * Get a parameter word from the SMB packet. * * @param idx Parameter index (zero based). * @return Parameter word value. * @exception java.lang.IndexOutOfBoundsException If the parameter index is out of range. */ public final int getParameter(int idx) throws java.lang.IndexOutOfBoundsException { // Range check the parameter index if (idx > getParameterCount()) throw new java.lang.IndexOutOfBoundsException(); // Calculate the parameter word offset int pos = WORDCNT + (2 * idx) + 1; return (int) (DataPacker.getIntelShort(m_smbbuf, pos) & 0xFFFF); }
/** * Get an AndX parameter integer from the SMB packet. * * @param off Offset to the AndX command. * @param idx Parameter index (zero based). * @return Parameter integer value. * @exception java.lang.IndexOutOfBoundsException If the parameter index is out of range. */ public final int getAndXParameterLong(int off, int idx) throws java.lang.IndexOutOfBoundsException { // Range check the parameter index if (idx > getAndXParameterCount(off)) throw new java.lang.IndexOutOfBoundsException(); // Calculate the parameter word offset int pos = off + (2 * idx) + 1; return DataPacker.getIntelInt(m_smbbuf, pos); }
/** * Set the fragment length * * @param len int */ public final void setFragmentLength(int len) { // Set the DCE header fragment length DataPacker.putIntelShort(len, getBuffer(), m_offset + FRAGMENTLEN); }
/** * Return the fragment length * * @return int */ public final int getFragmentLength() { return DataPacker.getIntelShort(getBuffer(), m_offset + FRAGMENTLEN); }
/** * Return the packed data representation * * @return int */ public final int getPackedDataRepresentation() { return DataPacker.getIntelInt(getBuffer(), m_offset + PACKEDDATAREP); }
/** * Get the SMB flags2 value. * * @return SMB flags2 value. */ public final int getFlags2() { return (int) DataPacker.getIntelShort(m_smbbuf, FLAGS2); }
/** * Get the request operation id * * @return int */ public final int getOperationId() { return DataPacker.getIntelShort(getBuffer(), m_offset + OPERATIONID); }
/** * Get the request presentation identifier * * @return int */ public final int getPresentationIdentifier() { return DataPacker.getIntelShort(getBuffer(), m_offset + PRESENTIDENT); }
/** * Get the request allocation hint * * @return int */ public final int getAllocationHint() { return DataPacker.getIntelInt(getBuffer(), m_offset + ALLOCATIONHINT); }
/** * Get the specified parameter words, as an int value. * * @param idx Parameter index (zero based). * @return int */ public final int getParameterLong(int idx) { int pos = WORDCNT + (2 * idx) + 1; return DataPacker.getIntelInt(m_smbbuf, pos); }
/** * Get the multiplex identifier. * * @return Multiplex identifier. */ public final int getMultiplexId() { return DataPacker.getIntelShort(m_smbbuf, MID); }
/** * Get the long SMB error code * * @return Long SMB error code. */ public final int getLongErrorCode() { return DataPacker.getIntelInt(m_smbbuf, ERRORCODE); }
/** * Return the available buffer space for data bytes for the specified buffer length * * @param len int * @return int */ public final int getAvailableLength(int len) { return len - DataPacker.longwordAlign(getByteOffset()); }
/** * Return the available buffer space for data bytes * * @return int */ public final int getAvailableLength() { return m_smbbuf.length - DataPacker.longwordAlign(getByteOffset()); }
/** * Return the NetBIOS header data length value. * * @return int */ public final int getHeaderLength() { return DataPacker.getIntelShort(m_smbbuf, 2) & 0xFFFF; }
/** * Return the authentication length * * @return int */ public final int getAuthenticationLength() { return DataPacker.getIntelShort(getBuffer(), m_offset + AUTHLEN); }
/** * Return the call id * * @return int */ public final int getCallId() { return DataPacker.getIntelInt(getBuffer(), m_offset + CALLID); }
/** Clear the data byte count */ public final void clearBytes() { int offset = getByteOffset() - 2; DataPacker.putIntelShort((short) 0, m_smbbuf, offset); }
/** * Set the allocation hint * * @param alloc int */ public final void setAllocationHint(int alloc) { DataPacker.putIntelInt(alloc, getBuffer(), m_offset + ALLOCATIONHINT); }
/** * Get the session identifier (SID) * * @return Session identifier (SID) */ public final int getSID() { return DataPacker.getIntelShort(m_smbbuf, SID); }
/** * Set the presentation identifier * * @param ident int */ public final void setPresentationIdentifier(int ident) { DataPacker.putIntelShort(ident, getBuffer(), m_offset + PRESENTIDENT); }
/** * Get the tree identifier (TID) * * @return Tree identifier (TID) */ public final int getTreeId() { return DataPacker.getIntelShort(m_smbbuf, TID); }
/** * Get the process identifier (PID) high bytes, or zero if not used * * @return Process identifier value. */ public final int getProcessIdHigh() { return DataPacker.getIntelShort(m_smbbuf, PIDHIGH); }
/** * Get the user identifier (UID) * * @return User identifier (UID) */ public final int getUserId() { return DataPacker.getIntelShort(m_smbbuf, UID); }