TELNET.java 6.5 KB

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