PageRenderTime 75ms CodeModel.GetById 46ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 1ms

/ghost-legacy/bnlsprotocol.cpp

http://ghostcb.googlecode.com/
C++ | 157 lines | 93 code | 26 blank | 38 comment | 10 complexity | 78d10f0c238c8d7e11ad38989a094f1b MD5 | raw file
  1/*
  2
  3   Copyright [2008] [Trevor Hogan]
  4
  5   Licensed under the Apache License, Version 2.0 (the "License");
  6   you may not use this file except in compliance with the License.
  7   You may obtain a copy of the License at
  8
  9       http://www.apache.org/licenses/LICENSE-2.0
 10
 11   Unless required by applicable law or agreed to in writing, software
 12   distributed under the License is distributed on an "AS IS" BASIS,
 13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14   See the License for the specific language governing permissions and
 15   limitations under the License.
 16
 17   CODE PORTED FROM THE ORIGINAL GHOST PROJECT: http://ghost.pwner.org/
 18
 19*/
 20
 21#include "ghost.h"
 22#include "util.h"
 23#include "bnlsprotocol.h"
 24
 25CBNLSProtocol :: CBNLSProtocol( )
 26{
 27
 28}
 29
 30CBNLSProtocol :: ~CBNLSProtocol( )
 31{
 32
 33}
 34
 35///////////////////////
 36// RECEIVE FUNCTIONS //
 37///////////////////////
 38
 39BYTEARRAY CBNLSProtocol :: RECEIVE_BNLS_WARDEN( BYTEARRAY data )
 40{
 41	// 2 bytes					-> Length
 42	// 1 byte					-> ID
 43	// (BYTE)					-> Usage
 44	// (DWORD)					-> Cookie
 45	// (BYTE)					-> Result
 46	// (WORD)					-> Length of data
 47	// (VOID)					-> Data
 48
 49	if( ValidateLength( data ) && data.size( ) >= 11 )
 50	{
 51		unsigned char Usage = data[3];
 52		uint32_t Cookie = UTIL_ByteArrayToUInt32( data, false, 4 );
 53		unsigned char Result = data[8];
 54		uint16_t Length = UTIL_ByteArrayToUInt16( data, false, 10 );
 55
 56		if( Result == 0x00 )
 57			return BYTEARRAY( data.begin( ) + 11, data.end( ) );
 58		else
 59			CONSOLE_Print( "[BNLSPROTO] received error code " + UTIL_ToString( data[8] ) );
 60	}
 61
 62	return BYTEARRAY( );
 63}
 64
 65////////////////////
 66// SEND FUNCTIONS //
 67////////////////////
 68
 69BYTEARRAY CBNLSProtocol :: SEND_BNLS_NULL( )
 70{
 71	BYTEARRAY packet;
 72	packet.push_back( 0 );							// packet length will be assigned later
 73	packet.push_back( 0 );							// packet length will be assigned later
 74	packet.push_back( BNLS_NULL );					// BNLS_NULL
 75	AssignLength( packet );
 76	return packet;
 77}
 78
 79BYTEARRAY CBNLSProtocol :: SEND_BNLS_WARDEN_SEED( uint32_t cookie, uint32_t seed )
 80{
 81	unsigned char Client[] = {  80,  88,  51,  87 };	// "W3XP"
 82
 83	BYTEARRAY packet;
 84	packet.push_back( 0 );								// packet length will be assigned later
 85	packet.push_back( 0 );								// packet length will be assigned later
 86	packet.push_back( BNLS_WARDEN );					// BNLS_WARDEN
 87	packet.push_back( 0 );								// BNLS_WARDEN_SEED
 88	UTIL_AppendByteArray( packet, cookie, false );		// cookie
 89	UTIL_AppendByteArray( packet, Client, 4 );			// Client
 90	UTIL_AppendByteArray( packet, (uint16_t)4, false );	// length of seed
 91	UTIL_AppendByteArray( packet, seed, false );		// seed
 92	packet.push_back( 0 );								// username is blank
 93	UTIL_AppendByteArray( packet, (uint16_t)0, false );	// password length
 94														// password
 95	AssignLength( packet );
 96	return packet;
 97}
 98
 99BYTEARRAY CBNLSProtocol :: SEND_BNLS_WARDEN_RAW( uint32_t cookie, BYTEARRAY raw )
100{
101	BYTEARRAY packet;
102	packet.push_back( 0 );											// packet length will be assigned later
103	packet.push_back( 0 );											// packet length will be assigned later
104	packet.push_back( BNLS_WARDEN );								// BNLS_WARDEN
105	packet.push_back( 1 );											// BNLS_WARDEN_RAW
106	UTIL_AppendByteArray( packet, cookie, false );					// cookie
107	UTIL_AppendByteArray( packet, (uint16_t)raw.size( ), false );	// raw length
108	UTIL_AppendByteArray( packet, raw );							// raw
109	AssignLength( packet );
110	return packet;
111}
112
113BYTEARRAY CBNLSProtocol :: SEND_BNLS_WARDEN_RUNMODULE( uint32_t cookie )
114{
115	return BYTEARRAY( );
116}
117
118/////////////////////
119// OTHER FUNCTIONS //
120/////////////////////
121
122bool CBNLSProtocol :: AssignLength( BYTEARRAY &content )
123{
124	// insert the actual length of the content array into bytes 1 and 2 (indices 0 and 1)
125
126	BYTEARRAY LengthBytes;
127
128	if( content.size( ) >= 2 && content.size( ) <= 65535 )
129	{
130		LengthBytes = UTIL_CreateByteArray( (uint16_t)content.size( ), false );
131		content[0] = LengthBytes[0];
132		content[1] = LengthBytes[1];
133		return true;
134	}
135
136	return false;
137}
138
139bool CBNLSProtocol :: ValidateLength( BYTEARRAY &content )
140{
141	// verify that bytes 1 and 2 (indices 0 and 1) of the content array describe the length
142
143	uint16_t Length;
144	BYTEARRAY LengthBytes;
145
146	if( content.size( ) >= 2 && content.size( ) <= 65535 )
147	{
148		LengthBytes.push_back( content[0] );
149		LengthBytes.push_back( content[1] );
150		Length = UTIL_ByteArrayToUInt16( LengthBytes, false );
151
152		if( Length == content.size( ) )
153			return true;
154	}
155
156	return false;
157}