MySQL.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package de.tudarmstadt.informatik.hostage.protocol;
  2. import java.nio.ByteBuffer;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  6. import de.tudarmstadt.informatik.hostage.wrapper.ByteArray;
  7. /**
  8. * MySQL protocol
  9. * @author Wulf Pfeiffer
  10. */
  11. public class MySQL implements Protocol<ByteArray>{
  12. /**
  13. * Represents the states of the protocol
  14. */
  15. private enum STATE {
  16. NONE, CONNECTED, LOGIN_INFO, CLOSED
  17. }
  18. /**
  19. * Denotes in which state the protocol is right now
  20. */
  21. private STATE state = STATE.NONE;
  22. /** last request from client */
  23. private byte[] request;
  24. @Override
  25. public List<ByteArray> processMessage(ByteArray request) {
  26. List<ByteArray> response = new ArrayList<ByteArray>();
  27. if(request != null) {
  28. this.request = request.get();
  29. }
  30. switch(state) {
  31. case NONE:
  32. response.add(new ByteArray(greeting()));
  33. state = STATE.CONNECTED;
  34. break;
  35. case CONNECTED:
  36. response.add(new ByteArray(responseOK()));
  37. state = STATE.LOGIN_INFO;
  38. break;
  39. case LOGIN_INFO:
  40. if(this.request[4] == 0x01) {
  41. state = STATE.CLOSED;
  42. } else if(this.request[4] == 0x03) {
  43. response.add(new ByteArray(responseOK()));
  44. } else {
  45. response.add(new ByteArray(responseError()));
  46. }
  47. break;
  48. default:
  49. state = STATE.CLOSED;
  50. break;
  51. }
  52. return response;
  53. }
  54. @Override
  55. public TALK_FIRST whoTalksFirst() {
  56. return TALK_FIRST.SERVER;
  57. }
  58. @Override
  59. public String toString(){
  60. return "MySQL";
  61. }
  62. @Override
  63. public int getPort() {
  64. return 3306;
  65. }
  66. @Override
  67. public boolean isClosed() {
  68. return state == STATE.CLOSED;
  69. }
  70. /**
  71. * Wraps the response packet with the packet length and number
  72. * @param packet that is wrapped
  73. * @return wrapped packet
  74. */
  75. private byte[] wrapPacket(byte[] packet) {
  76. byte[] buffer = ByteBuffer.allocate(4).putInt(packet.length).array();
  77. byte[] packetLength = {buffer[3], buffer[2], buffer[1]};
  78. byte[] packetNumber = new byte[1];
  79. if(request != null) packetNumber[0] = (byte) (request[3] + 1);
  80. else packetNumber[0] = 0x00;
  81. byte[] response = HelperUtils.concat(packetLength, packetNumber, packet);
  82. return response;
  83. }
  84. /**
  85. * Builds the greeting packet that the server sends as first packet
  86. * @return greeting packet
  87. */
  88. private byte[] greeting() {
  89. byte[] protocol = {0x0a};
  90. String version = "5.5.31-0+wheezy1";
  91. byte[] versionFin = {0x00};
  92. byte[] thread = {0x2a, 0x00, 0x00, 0x00};
  93. byte[] salt = {0x44, 0x64, 0x49, 0x7e, 0x60, 0x48, 0x25, 0x7e, 0x00};
  94. byte[] capabilities = {(byte) 0xff, (byte) 0xf7};
  95. byte[] language = {0x08};
  96. byte[] status = {0x02, 0x00};
  97. byte[] unused = {0x0f, (byte) 0x80, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  98. byte[] salt2 = {0x6c, 0x26, 0x71, 0x2c, 0x25, 0x72, 0x31, 0x3d, 0x7d, 0x21, 0x26, 0x3b, 0x00};
  99. String payload = "mysql_native_password";
  100. byte[] fin = {0x00};
  101. byte[] response = HelperUtils.concat(protocol, version.getBytes(),versionFin, thread, salt, capabilities, language, status, unused, salt2, payload.getBytes(), fin);
  102. return wrapPacket(response);
  103. }
  104. /**
  105. * Builds the ok-response packet
  106. * @return ok-response packet
  107. */
  108. private byte[] responseOK() {
  109. byte[] affectedRows = {0x00, 0x00, 0x00};
  110. byte[] status = {0x02, 0x00};
  111. byte[] warnings = {0x00, 0x00};
  112. byte[] response = HelperUtils.concat(affectedRows, status, warnings);
  113. return wrapPacket(response);
  114. }
  115. /**
  116. * Builds the error-response packet
  117. * @return error-response packet
  118. */
  119. private byte[] responseError() {
  120. byte[] fill1 = {(byte) 0xff};
  121. byte[] code = {0x17, 0x04};
  122. byte[] fill2 = {0x23};
  123. String state = "08S01";
  124. String msg = "Unknown command";
  125. byte[] response = HelperUtils.concat(fill1, code, fill2, state.getBytes(), msg.getBytes());
  126. return wrapPacket(response);
  127. }
  128. @Override
  129. public boolean isSecure() {
  130. return false;
  131. }
  132. @Override
  133. public Class<ByteArray> getType() {
  134. return ByteArray.class;
  135. }
  136. }