MySQLFormatter.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package de.tudarmstadt.informatik.hostage.format;
  2. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  3. /**
  4. * MySQL log view formatter.
  5. * @author Wulf Pfeiffer
  6. */
  7. public class MySQLFormatter implements ProtocolFormatter {
  8. public String format(String packet) {
  9. byte[] bytes = HelperUtils.hexStringToBytes(packet);
  10. String command = "Command: " + getCommand(bytes) + "\n";
  11. String content = "Content: " + HelperUtils.byteToStr(bytes) + "\n";
  12. return command + content;
  13. }
  14. /**
  15. * Checks a packet for its command code and returns the name of this command.
  16. * @param bytes to check.
  17. * @return name of the command.
  18. */
  19. private String getCommand(byte[] bytes) {
  20. //if packet number is 1 server started conversation so it must be login
  21. if(bytes.length < 5) return "";
  22. if(bytes[3] == 0x01) return "Login request";
  23. //else check for command code
  24. switch(bytes[4]) {
  25. case 0x00: return "COM_SLEEP";
  26. case 0x01: return "COM_QUIT";
  27. case 0x02: return "COM_INIT_DB";
  28. case 0x03: return "COM_QUERY";
  29. case 0x04: return "COM_FIELD_LIST";
  30. case 0x05: return "COM_CREATE_DB";
  31. case 0x06: return "COM_DROP_DB";
  32. case 0x07: return "COM_REFRESH";
  33. case 0x08: return "COM_SHUTDOWN";
  34. case 0x09: return "COM_STATISTICS";
  35. case 0x0a: return "COM_PROCESS_INFO";
  36. case 0x0b: return "COM_CONNECT";
  37. case 0x0c: return "COM_PROCESS_KILL";
  38. case 0x0d: return "COM_DEBUG";
  39. case 0x0e: return "COM_PING";
  40. case 0x0f: return "COM_TIME";
  41. case 0x10: return "COM_DELAYED_INSERT";
  42. case 0x11: return "COM_CHANGE_USER";
  43. case 0x12: return "COM_BINLOG_DUMP";
  44. case 0x13: return "COM_TABLE_DUMP";
  45. case 0x14: return "COM_CONNECT_OUT";
  46. case 0x15: return "COM_REGISTER_SLAVE";
  47. case 0x16: return "COM_STMT_PREPARE";
  48. case 0x17: return "COM_STMT_EXECUTE";
  49. case 0x18: return "COM_STMT_SEND_LONG_DATA";
  50. case 0x19: return "COM_STMT_CLOSE";
  51. case 0x1a: return "COM_STMT_RESET";
  52. case 0x1b: return "COM_SET_OPTION";
  53. case 0x1c: return "COM_STMT_FETCH";
  54. case 0x1d: return "COM_DAEMON";
  55. case 0x1e: return "COM_BINLOG_DUMP_GTID";
  56. default: return "unkown command";
  57. }
  58. }
  59. }