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