MySQL.java 3.9 KB

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