TELNET.java 6.2 KB

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