NBNS.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package de.tudarmstadt.informatik.hostage.protocol.smbutils;
  2. import java.nio.ByteBuffer;
  3. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  4. /**
  5. * NetBIOS Name Service.
  6. * @author Wulf Pfeiffer
  7. */
  8. public class NBNS {
  9. private byte[] transactID;
  10. private byte[] flags;
  11. private byte[] questions;
  12. private byte[] answerRRs;
  13. private byte[] authorityRRs;
  14. private byte[] additionalRRs;
  15. private byte[] payload;
  16. private byte[] additional;
  17. private byte[] addr;
  18. private byte[] name;
  19. private int type;
  20. private int service;
  21. public NBNS(byte[] addr) {
  22. this.addr = addr;
  23. }
  24. /**
  25. * Prepares the content for the next packet regarding of the set type.
  26. */
  27. private void preparePacket() {
  28. transactID = NMB.getAndIncTransactID();
  29. switch (type) {
  30. case NBNSType.REGISTRATION_UNIQUE:
  31. prepareRegistrationPacket();
  32. break;
  33. case NBNSType.REGISTRATION_GROUP:
  34. prepareRegistrationPacket();
  35. break;
  36. case NBNSType.NAME_QUERY:
  37. prepareNameQueryPacket();
  38. break;
  39. case NBNSType.REGISTRATION_MSBROWSE:
  40. prepareRegistrationMsBrowse();
  41. break;
  42. default:
  43. }
  44. }
  45. /**
  46. * Prepares the content for the next registration packet.
  47. */
  48. private void prepareRegistrationPacket() {
  49. flags = new byte[]{0x29, 0x10};
  50. questions = new byte[]{0x00, 0x01};
  51. answerRRs = new byte[]{0x00, 0x00};
  52. authorityRRs = new byte[]{0x00, 0x00};
  53. additionalRRs = new byte[]{0x00, 0x01};
  54. payload = getPayload();
  55. additional = getAdditionalRecords();
  56. }
  57. /**
  58. * Prepares the content for the next name query packet.
  59. */
  60. private void prepareNameQueryPacket() {
  61. flags = new byte[] {0x01, 0x10};
  62. questions = new byte[]{0x00, 0x01};
  63. answerRRs = new byte[]{0x00, 0x00};
  64. authorityRRs = new byte[]{0x00, 0x00};
  65. additionalRRs = new byte[]{0x00, 0x00};
  66. payload = getPayload();
  67. }
  68. /**
  69. * Prepares the content for the next MSBROWSE registration packet.
  70. */
  71. private void prepareRegistrationMsBrowse() {
  72. flags = new byte[]{0x29, 0x10};
  73. questions = new byte[]{0x00, 0x01};
  74. answerRRs = new byte[]{0x00, 0x00};
  75. authorityRRs = new byte[]{0x00, 0x00};
  76. additionalRRs = new byte[]{0x00, 0x01};
  77. payload = HelperUtils.concat(new byte[]{0x20, 0x41, 0x42, 0x41, 0x43}, name,
  78. new byte[]{0x41, 0x43, 0x41, 0x42, 0x00, 0x00, 0x20, 0x00, 0x01});
  79. additional = getAdditionalRecords();
  80. }
  81. /**
  82. * Prepares the content for the next response packet.
  83. */
  84. public void prepareResponsePacket(byte[] packet, byte[] myAddr) {
  85. this.transactID = new byte[]{packet[0], packet[1]};
  86. flags = new byte[]{(byte) 0x85, (byte) 0x80};
  87. questions = new byte[]{0x00, 0x00};
  88. answerRRs = new byte[]{0x00, 0x01};
  89. authorityRRs = new byte[]{0x00, 0x00};
  90. additionalRRs = new byte[]{0x00, 0x00};
  91. byte[] nameBytes = new byte[32];
  92. System.arraycopy(packet, 13, nameBytes, 0, 32);
  93. String name = NMBStringCoder.decodeNBNSName(nameBytes);
  94. byte[] query = new byte[50];
  95. System.arraycopy(packet, 12, query, 0, 38);
  96. // time to live
  97. query[38] = 0x00;
  98. query[39] = 0x00;
  99. query[40] = 0x02;
  100. query[41] = 0x58;
  101. // data length
  102. query[42] = 0x00;
  103. query[43] = 0x06;
  104. // name flags
  105. query[44] = (byte) 0x80;
  106. query[45] = 0x00;
  107. // addr
  108. query[46] = myAddr[0];
  109. query[47] = myAddr[1];
  110. query[48] = myAddr[2];
  111. query[49] = myAddr[3];
  112. payload = query;
  113. }
  114. /**
  115. * Builds the payload for the packet.
  116. * @return payload.
  117. */
  118. private byte[] getPayload() {
  119. byte[] payload = NMBStringCoder.wrapNBNSName(this.name, service);
  120. byte[] type = {0x00, 0x20};
  121. byte[] nbnsclass = {0x00, 0x01};
  122. return HelperUtils.concat(payload, type, nbnsclass);
  123. }
  124. /**
  125. * Builds the additional records field.
  126. * @return additional records.
  127. */
  128. private byte[] getAdditionalRecords() {
  129. byte[] name = {(byte) 0xc0, 0x0c};
  130. byte[] type = {0x00, 0x20};
  131. byte[] nbnsclass = {0x00, 0x01};
  132. byte[] timeToLive = {0x00, 0x00, 0x00, 0x00};
  133. byte[] nameFlags = ((this.type == NBNSType.REGISTRATION_UNIQUE) ? new byte[]{0x00, 0x00} : new byte[]{(byte) 0x80, 0x00});
  134. byte[] buffer = ByteBuffer.allocate(4).putInt(nameFlags.length + addr.length).array();
  135. byte[] length = {buffer[2], buffer[3]};
  136. return HelperUtils.concat(name, type, nbnsclass, timeToLive, length, nameFlags, addr);
  137. }
  138. /**
  139. * Returns the next packet.
  140. * Use only after the name, type and service were set.
  141. * @return next packet.
  142. */
  143. public byte[] getNextPacket() {
  144. preparePacket();
  145. return getBytes();
  146. }
  147. /**
  148. * Returns the next response packet.
  149. * @param packet request packet.
  150. * @param myAddr your current ip in bytes.
  151. * @return next response packet.
  152. */
  153. public byte[] getNextResponse(byte[] packet, byte[] myAddr) {
  154. prepareResponsePacket(packet, myAddr);
  155. return getBytes();
  156. }
  157. /**
  158. * Returns the content of the packet in bytes.
  159. * @return packet content.
  160. */
  161. private byte[] getBytes() {
  162. return HelperUtils.concat(transactID, flags, questions, answerRRs, authorityRRs, additionalRRs, payload, additional);
  163. }
  164. /**
  165. * Set the name for the payload.
  166. * @param name.
  167. */
  168. public void setName(String name) {
  169. this.name = name.getBytes();
  170. this.name = NMBStringCoder.encodeNBNSName(this.name);
  171. }
  172. /**
  173. * Set the NBNSType.
  174. * @param type NBNSType.
  175. */
  176. public void setType(int type) {
  177. this.type = type;
  178. }
  179. /**
  180. * Set the NBNSService.
  181. * @param service NBNSService.
  182. */
  183. public void setService(int service) {
  184. this.service = service;
  185. }
  186. }