TELNET.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package de.tudarmstadt.informatik.hostage.protocol;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import javax.net.ssl.SSLContext;
  5. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  6. import de.tudarmstadt.informatik.hostage.wrapper.ByteArray;
  7. /**
  8. * TELNET protocol
  9. * @author Wulf Pfeiffer
  10. */
  11. public final class TELNET implements Protocol<ByteArray> {
  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 byte[] user;
  24. /** last command sent by the client */
  25. private byte[] command;
  26. /** name of the server */
  27. private String server = "raspberrypi";
  28. /** command line prefix */
  29. private byte[] sessionToken = null;
  30. @Override
  31. public int getPort() {
  32. return 23;
  33. }
  34. @Override
  35. public TALK_FIRST whoTalksFirst() {
  36. return TALK_FIRST.SERVER;
  37. }
  38. @Override
  39. public List<ByteArray> processMessage(ByteArray message) {
  40. byte[] request = null;
  41. if(message != null) request = message.get();
  42. List<ByteArray> response = new ArrayList<ByteArray>();
  43. switch (state) {
  44. case NONE:
  45. response.add(new ByteArray(optionRequest));
  46. state = STATE.OPEN;
  47. break;
  48. case OPEN:
  49. if(request != null) {
  50. response.add(new ByteArray(getOptionResponse(request)));
  51. response.add(new ByteArray("Debian GNU/Linux 7.0\r\n"));
  52. response.add(new ByteArray(server + "login: "));
  53. state = STATE.LOGIN;
  54. }
  55. break;
  56. case LOGIN:
  57. if(request == null) break;
  58. else if(request[0] == 0x0d) {
  59. response.add(new ByteArray("\r\n"));
  60. response.add(new ByteArray("Password: "));
  61. state = STATE.AUTHENTICATE;
  62. sessionToken = HelperUtils.concat(sessionPrefix, "@".getBytes(), server.getBytes(), sessionMiddle, "@".getBytes(), server.getBytes(), sessionSuffix);
  63. break;
  64. } else if (request[0] == 0x7f) {
  65. byte[] tmp = new byte[user.length - 1];
  66. System.arraycopy(user, 0, tmp, 0, user.length - 1);
  67. user = tmp;
  68. response.add(new ByteArray("\b \b"));
  69. } else if (request[0] != (byte) 0xff){
  70. if(user == null)
  71. user = request;
  72. else
  73. user = HelperUtils.concat(user, request);
  74. response.add(message);
  75. }
  76. break;
  77. case AUTHENTICATE:
  78. if(request == null) break;
  79. else if(request[0] == 0x0d) {
  80. response.add(new ByteArray("Last Login: \r\nLinux" + server + " 3.6.11+\r\n"));
  81. response.add(new ByteArray(sessionToken));
  82. state = STATE.LOGGED_IN;
  83. } else if (request[0] == 0x7f) {
  84. response.add(new ByteArray("\b \b"));
  85. }
  86. break;
  87. case LOGGED_IN:
  88. if(request == null) break;
  89. else if(request[0] == 0x0d) {
  90. if(command == null) {
  91. response.add(new ByteArray("\r\n"));
  92. response.add(new ByteArray(sessionToken));
  93. } else if (new String(command).contains("exit")) {
  94. response.add(new ByteArray("\r\nlogout\r\n"));
  95. state = STATE.CLOSED;
  96. } else {
  97. String bash = "\r\n-bash: " + new String(command)+ ": command not found";
  98. response.add(new ByteArray(bash));
  99. response.add(new ByteArray("\r\n"));
  100. response.add(new ByteArray(sessionToken));
  101. command = null;
  102. }
  103. } else if (request[0] == 0x7f) {
  104. byte[] tmp = new byte[command.length - 1];
  105. System.arraycopy(command, 0, tmp, 0, command.length - 1);
  106. command = tmp;
  107. response.add(new ByteArray("\b \b"));
  108. } else if (request[0] != (byte) 0xff){
  109. if(command == null)
  110. command = request;
  111. else
  112. command = HelperUtils.concat(command, request);
  113. response.add(message);
  114. }
  115. break;
  116. default:
  117. response.add(new ByteArray("\r\nlogout\r\n"));
  118. state = STATE.CLOSED;
  119. break;
  120. }
  121. return response;
  122. }
  123. @Override
  124. public boolean isClosed() {
  125. return (state == STATE.CLOSED);
  126. }
  127. @Override
  128. public boolean isSecure() {
  129. return false;
  130. }
  131. @Override
  132. public String toString() {
  133. return "TELNET";
  134. }
  135. @Override
  136. public Class<ByteArray> getType() {
  137. return ByteArray.class;
  138. }
  139. /**
  140. * Determines which options that are requested by the client will be done and which not
  141. * @param request requested options
  142. * @return accepted and unaccepted options
  143. */
  144. private byte[] getOptionResponse(byte[] request) {
  145. List<byte[]> respList = new ArrayList<byte[]>();
  146. byte[] cmdResp;
  147. for(int i = 0; i < request.length - 2; i += 3) {
  148. if(request[i] == (byte) 0xff && request[i+2] != 0x03 && request[i+2] != 0x01) {
  149. cmdResp = new byte[3];
  150. cmdResp[0] = request[i];
  151. cmdResp[1] = request[i + 1] == (byte) 0xfd ? (byte) 0xfc : (byte) 0xfe;
  152. cmdResp[2] = request[i+2];
  153. respList.add(cmdResp);
  154. }
  155. }
  156. byte[] response = new byte[0];
  157. for(byte[] resp : respList) {
  158. response = HelperUtils.concat(response, resp);
  159. }
  160. return response;
  161. }
  162. /** options requested by the server */
  163. private final byte[] optionRequest = {(byte) 0xff, (byte) 0xfb, 0x03,
  164. (byte) 0xff, (byte) 0xfb, 0x01};
  165. //session token prefix, mid and suffix
  166. private final byte[] sessionPrefix = {0x1b, 0x5d, 0x30, 0x3b};
  167. private final byte[] sessionMiddle = {0x40, 0x72, 0x61, 0x73,
  168. 0x70, 0x62, 0x65, 0x72, 0x72, 0x79, 0x70, 0x69, 0x3a, 0x20, 0x7e, 0x07, 0x1b, 0x5b, 0x30, 0x31,
  169. 0x3b, 0x33, 0x32, 0x6d};
  170. private final byte[] sessionSuffix = {0x1b, 0x5b, 0x30, 0x30, 0x6d, 0x20, 0x1b, 0x5b, 0x30, 0x31,
  171. 0x3b, 0x33, 0x34, 0x6d, 0x7e, 0x20, 0x24, 0x1b, 0x5b, 0x30, 0x30, 0x6d, 0x20};
  172. }