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