TELNET.java 6.2 KB

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