NBDS.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package de.tudarmstadt.informatik.hostage.protocol.smbutils;
  2. import java.nio.ByteBuffer;
  3. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  4. /**
  5. * NetBIOS Datagram Service.
  6. * @author Wulf Pfeiffer
  7. */
  8. public class NBDS {
  9. private String dst;
  10. private byte[] type;
  11. private byte[] flags;
  12. private byte[] transactID;
  13. private byte[] srcIP;
  14. private byte[] srcPort;
  15. private byte[] length;
  16. private byte[] offset;
  17. private byte[] srcName;
  18. private byte[] dstName;
  19. private int nbdstype;
  20. private SMBPacket smb;
  21. public NBDS(byte[] addr, String src, String dst) {
  22. this.dst = dst;
  23. type = new byte[]{0x11};
  24. flags = new byte[]{0x0a};
  25. srcIP = addr;
  26. srcPort = new byte[]{0x00, (byte) 0x8a};
  27. offset = new byte[]{0x00, 0x00};
  28. length = new byte[2];
  29. srcName = NMBStringCoder.wrapNBNSName(NMBStringCoder.encodeNBNSName(src.getBytes()), NBNSService.WORKSTATION);
  30. smb = new SMBPacket(null, src, dst);
  31. }
  32. /**
  33. * Prepares the content for the next packet.
  34. */
  35. private void preparePacket() {
  36. transactID = NMB.getAndIncTransactID();
  37. if (nbdstype == NBDSType.REQUEST_ANNOUNCEMENT || nbdstype == NBDSType.LOCAL_MASTER_ANNOUNCEMENT_ALL
  38. || nbdstype == NBDSType.LOCAL_MASTER_ANNOUNCEMENT) {
  39. dstName = NMBStringCoder.wrapNBNSName(NMBStringCoder.encodeNBNSName(dst.getBytes()), NBNSService.BROWSER_ELECTION);
  40. } else if (nbdstype == NBDSType.DOMAIN_ANNOUNCEMENT) {
  41. dstName = HelperUtils.concat(new byte[]{0x20, 0x41, 0x42, 0x41, 0x43}, NMBStringCoder.encodeNBNSName("__MSBROWSE__".getBytes()),
  42. new byte[]{0x41, 0x43, 0x41, 0x42, 0x00});
  43. } else {
  44. dstName = NMBStringCoder.wrapNBNSName(NMBStringCoder.encodeNBNSName(dst.getBytes()), NBNSService.LOCAL_MASTER_BROWSER);
  45. }
  46. smb.prepareNextResponse(nbdstype);
  47. byte[] buffer = HelperUtils.concat(srcName, dstName, smb.getTrans());
  48. byte[] lengthBuffer = ByteBuffer.allocate(4).putInt(buffer.length).array();
  49. length[0] = lengthBuffer[2];
  50. length[1] = lengthBuffer[3];
  51. }
  52. /**
  53. * @return next Packet.
  54. */
  55. public byte[] getNextPacket() {
  56. preparePacket();
  57. return getBytes();
  58. }
  59. /**
  60. * @return content of the packet.
  61. */
  62. private byte[] getBytes() {
  63. return HelperUtils.concat(type, flags, transactID, srcIP, srcPort, length,
  64. offset, srcName, dstName, smb.getTrans());
  65. }
  66. /**
  67. * Set the NBDSType.
  68. * @param nbdstype.
  69. */
  70. public void setNbdstype(int nbdstype) {
  71. this.nbdstype = nbdstype;
  72. }
  73. }