TELNET.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package de.tudarmstadt.informatik.hostage.protocol;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  5. import de.tudarmstadt.informatik.hostage.wrapper.Packet;
  6. /**
  7. * TELNET protocol. Implementation of RFC document 854.
  8. *
  9. * @author Wulf Pfeiffer
  10. */
  11. public class TELNET implements Protocol {
  12. /**
  13. * Represents the states of the protocol
  14. */
  15. private static enum STATE {
  16. NONE, OPEN, CLOSED, LOGIN, AUTHENTICATE, LOGGED_IN
  17. };
  18. /**
  19. * Denotes in which state the protocol is right now
  20. */
  21. private STATE state = STATE.NONE;
  22. /** user entered by the client */
  23. private static byte[] user;
  24. /** last command sent by the client */
  25. private byte[] command;
  26. /** name of the server */
  27. private static String serverName = HelperUtils.getRandomString(16, false);
  28. /** command line prefix */
  29. private static byte[] sessionToken = null;
  30. /** options requested by the server */
  31. private static final byte[] optionRequest = { (byte) 0xff, (byte) 0xfb,
  32. 0x03, // will suppress go ahead
  33. (byte) 0xff, (byte) 0xfb, 0x01 // will echo
  34. };
  35. // session token prefix, mid and suffix
  36. private static final byte[] sessionPrefix = { 0x1b, 0x5d, 0x30, 0x3b };
  37. private static final byte[] sessionMiddle = { 0x3a, 0x20, 0x7e, 0x07, 0x1b,
  38. 0x5b, 0x30, 0x31, 0x3b, 0x33, 0x32, 0x6d };
  39. private static final byte[] sessionSuffix = { 0x1b, 0x5b, 0x30, 0x30, 0x6d,
  40. 0x20, 0x1b, 0x5b, 0x30, 0x31, 0x3b, 0x33, 0x34, 0x6d, 0x7e, 0x20,
  41. 0x24, 0x1b, 0x5b, 0x30, 0x30, 0x6d, 0x20 };
  42. @Override
  43. public int getPort() {
  44. return 23;
  45. }
  46. @Override
  47. public boolean isClosed() {
  48. return (state == STATE.CLOSED);
  49. }
  50. @Override
  51. public boolean isSecure() {
  52. return false;
  53. }
  54. @Override
  55. public List<Packet> processMessage(Packet requestPacket) {
  56. byte[] request = null;
  57. if (requestPacket != null) {
  58. request = requestPacket.getBytes();
  59. }
  60. List<Packet> responsePackets = new ArrayList<Packet>();
  61. switch (state) {
  62. case NONE:
  63. responsePackets.add(new Packet(optionRequest));
  64. state = STATE.OPEN;
  65. break;
  66. case OPEN:
  67. if (request != null) {
  68. responsePackets.add(new Packet(getOptionResponse(request)));
  69. responsePackets.add(new Packet(serverName + " login: "));
  70. state = STATE.LOGIN;
  71. }
  72. break;
  73. case LOGIN:
  74. if (request == null)
  75. break;
  76. else if (checkForByte(request, (byte) 0x0d)) {
  77. if (request.length > 2) {
  78. byte[] buffer = new byte[request.length - 2];
  79. System.arraycopy(request, 0, buffer, 0, request.length - 2);
  80. user = HelperUtils.concat(user, buffer);
  81. responsePackets.add(new Packet(buffer));
  82. }
  83. responsePackets.add(new Packet("\r\n"));
  84. responsePackets.add(new Packet("Password: "));
  85. state = STATE.AUTHENTICATE;
  86. sessionToken = HelperUtils.concat(sessionPrefix, user,
  87. "@".getBytes(), serverName.getBytes(), sessionMiddle,
  88. user, "@".getBytes(), serverName.getBytes(),
  89. sessionSuffix);
  90. break;
  91. } else if (checkForByte(request, (byte) 0x7f) && user != null
  92. && user.length != 0) {
  93. byte[] tmp = new byte[user.length - 1];
  94. System.arraycopy(user, 0, tmp, 0, user.length - 1);
  95. user = tmp;
  96. responsePackets.add(new Packet("\b \b"));
  97. break;
  98. } else if (!checkForByte(request, (byte) 0xff)) {
  99. if (user == null)
  100. user = request;
  101. else
  102. user = HelperUtils.concat(user, request);
  103. responsePackets.add(requestPacket);
  104. }
  105. break;
  106. case AUTHENTICATE:
  107. if (request == null)
  108. break;
  109. else if (checkForByte(request, (byte) 0x0d)) {
  110. responsePackets.add(new Packet("\r\n"));
  111. responsePackets.add(new Packet(sessionToken));
  112. state = STATE.LOGGED_IN;
  113. } else if (checkForByte(request, (byte) 0x7f)) {
  114. responsePackets.add(new Packet("\b \b"));
  115. }
  116. break;
  117. case LOGGED_IN:
  118. if (request == null)
  119. break;
  120. else if (checkForByte(request, (byte) 0x0d)) {
  121. if (request.length > 2) {
  122. byte[] buffer = new byte[request.length - 2];
  123. System.arraycopy(request, 0, buffer, 0, request.length - 2);
  124. command = HelperUtils.concat(command, buffer);
  125. responsePackets.add(new Packet(buffer));
  126. }
  127. if (command == null) {
  128. responsePackets.add(new Packet("\r\n"));
  129. responsePackets.add(new Packet(sessionToken));
  130. } else if (new String(command).contains("exit")) {
  131. responsePackets.add(new Packet("\r\nlogout\r\n"));
  132. state = STATE.CLOSED;
  133. } else {
  134. String bash = "\r\n-bash: " + new String(command)
  135. + ": command not found";
  136. responsePackets.add(new Packet(bash));
  137. responsePackets.add(new Packet("\r\n"));
  138. responsePackets.add(new Packet(sessionToken));
  139. command = null;
  140. }
  141. } else if (checkForByte(request, (byte) 0x7f) && command != null
  142. && command.length != 0) {
  143. byte[] tmp = new byte[command.length - 1];
  144. System.arraycopy(command, 0, tmp, 0, command.length - 1);
  145. command = tmp;
  146. responsePackets.add(new Packet("\b \b"));
  147. break;
  148. } else if (!checkForByte(request, (byte) 0xff)) {
  149. if (command == null)
  150. command = request;
  151. else
  152. command = HelperUtils.concat(command, request);
  153. responsePackets.add(requestPacket);
  154. }
  155. break;
  156. default:
  157. responsePackets.add(new Packet("\r\nlogout\r\n"));
  158. state = STATE.CLOSED;
  159. break;
  160. }
  161. return responsePackets;
  162. }
  163. @Override
  164. public String toString() {
  165. return "TELNET";
  166. }
  167. @Override
  168. public TALK_FIRST whoTalksFirst() {
  169. return TALK_FIRST.SERVER;
  170. }
  171. /**
  172. * Checks a byte array for occurence of one byte.
  173. *
  174. * @param bytes
  175. * byte array that is checked.
  176. * @param b
  177. * searched byte.
  178. * @return true if the byte was found, else false.
  179. */
  180. private boolean checkForByte(byte[] bytes, byte b) {
  181. for (byte oneByte : bytes) {
  182. if (oneByte == b)
  183. return true;
  184. }
  185. return false;
  186. }
  187. /**
  188. * Determines which options that are requested by the client will be done
  189. * and which not
  190. *
  191. * @param request
  192. * requested options
  193. * @return accepted and unaccepted options
  194. */
  195. private byte[] getOptionResponse(byte[] request) {
  196. List<byte[]> responseList = new ArrayList<byte[]>();
  197. byte[] requestInverse;
  198. for (int i = 0; i < request.length - 2; i += 3) {
  199. if (request[i] == (byte) 0xff && request[i + 2] != 0x03
  200. && request[i + 2] != 0x01) {
  201. requestInverse = new byte[3];
  202. requestInverse[0] = request[i];
  203. requestInverse[1] = request[i + 1] == (byte) 0xfd ? (byte) 0xfc
  204. : (byte) 0xfe;
  205. requestInverse[2] = request[i + 2];
  206. responseList.add(requestInverse);
  207. }
  208. }
  209. byte[] optionResponse = new byte[0];
  210. for (byte[] response : responseList) {
  211. optionResponse = HelperUtils.concat(optionResponse, response);
  212. }
  213. return optionResponse;
  214. }
  215. }