TELNET.java 6.1 KB

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