SMBPacket.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. package de.tudarmstadt.informatik.hostage.protocol.smbutils;
  2. import java.nio.ByteBuffer;
  3. import java.util.Calendar;
  4. import java.util.GregorianCalendar;
  5. import java.util.TimeZone;
  6. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  7. /**
  8. * SMBPacket
  9. *
  10. * @author Wulf Pfeiffer
  11. */
  12. public class SMBPacket {
  13. private String[] serverVersion;
  14. private static byte[] serverName;
  15. private byte[] message = null;
  16. private static final byte[] serverGUID = HelperUtils.randomBytes(16);
  17. private boolean authenticateNext = false;
  18. // components of a SMB packet
  19. private byte[] serverComp = new byte[4];
  20. private byte[] smbCommand = new byte[1];
  21. private byte[] ntStat = new byte[4];
  22. private byte[] smbFlags = new byte[1];
  23. private byte[] smbFlags2 = new byte[2];
  24. private byte[] processIDHigh = new byte[2];
  25. private byte[] signature = new byte[8];
  26. private byte[] reserved = new byte[2];
  27. private byte[] treeID = new byte[2];
  28. private byte[] processID = new byte[2];
  29. private byte[] userID = new byte[2];
  30. private byte[] multiplexID = new byte[2];
  31. //special nbds stuff
  32. private static byte[] workgroup;
  33. private int type;
  34. public SMBPacket(String[] serverVersion, String serverName, String workgroup) {
  35. this.serverVersion = serverVersion;
  36. SMBPacket.serverName = serverName.getBytes();
  37. SMBPacket.workgroup = workgroup.getBytes();
  38. }
  39. public void prepareNextResponse(int type) {
  40. serverComp = new byte[] { (byte) 0xff, 0x53, 0x4d, 0x42 };
  41. smbCommand = new byte[] { 0x25 };
  42. ntStat = new byte[] { 0x00, 0x00, 0x00, 0x00 };
  43. // | 0x80 for mark response bit
  44. smbFlags = new byte[] { 0x00 };
  45. smbFlags2 = new byte[] { 0x00, 0x00 };
  46. processIDHigh = new byte[] { 0x00, 0x00 };
  47. signature = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  48. reserved = new byte[] { 0x00, 0x00 };
  49. treeID = new byte[] { 0x00, 0x00 };
  50. processID = new byte[] { 0x00, 0x00 };
  51. userID = new byte[] { 0x00, 0x00 };
  52. multiplexID = new byte[] { 0x00, 0x00 };
  53. this.type = type;
  54. }
  55. public void prepareNextResponse(byte[] message) {
  56. this.message = message;
  57. serverComp = new byte[] { message[4], message[5], message[6],message[7] };
  58. smbCommand = new byte[] { message[8] };
  59. ntStat = new byte[] { message[9], message[10], message[11], message[12] };
  60. // | 0x80 for mark response bit
  61. smbFlags = new byte[] { (byte) (message[13] | 0x80) };
  62. smbFlags2 = new byte[] { message[14], message[15] };
  63. processIDHigh = new byte[] { message[16], message[17] };
  64. signature = new byte[] { message[18], message[19], message[20],
  65. message[21], message[22], message[23], message[24], message[25] };
  66. reserved = new byte[] { message[26], message[27] };
  67. treeID = new byte[] { message[28], message[29] };
  68. processID = new byte[] { message[30], message[31] };
  69. userID = new byte[] { message[32], message[33] };
  70. multiplexID = new byte[] { message[34], message[35] };
  71. }
  72. /**
  73. * Wraps the header around a response
  74. *
  75. * @param response
  76. * that is wrapped
  77. * @return wrapped response
  78. */
  79. private byte[] wrapHeader(byte[] response) {
  80. byte[] header = new byte[0];
  81. return HelperUtils.concat(header, serverComp, smbCommand, ntStat,
  82. smbFlags, smbFlags2, processIDHigh, signature, reserved,
  83. treeID, processID, userID, multiplexID, response);
  84. }
  85. /**
  86. * Wraps the Netbios header around a response
  87. *
  88. * @param response
  89. * that is wrapped
  90. * @return wrapped response
  91. */
  92. private byte[] wrapNetbios(byte[] response) {
  93. byte[] netbios = { 0x00 };
  94. // allocate(4) because int is 4 bytes long
  95. byte[] buffer = ByteBuffer.allocate(4).putInt(response.length).array();
  96. // only bytes 1-3 needed, byte 0 is not needed
  97. byte[] netbiosLength = { buffer[1], buffer[2], buffer[3] };
  98. return HelperUtils.concat(netbios, netbiosLength, response);
  99. }
  100. /**
  101. * Evaluates what Dialects are offered by the client and which position the
  102. * used NT LM 0.12 dialect is at
  103. *
  104. * @return position of the NT LM 0.12 dialect
  105. */
  106. private byte[] evaluateDialect() {
  107. byte[] dialectMsg = new byte[message.length - 39];
  108. System.arraycopy(message, 39, dialectMsg, 0, message.length - 39);
  109. short dialectNumber = 0;
  110. for (int i = 0, start = 0; i < dialectMsg.length; i++) {
  111. if (dialectMsg[i] == 0x00) {
  112. byte[] dialect = new byte[i - start];
  113. System.arraycopy(dialectMsg, start, dialect, 0, i - start);
  114. if (HelperUtils.byteToStr(dialect).contains("NT LM 0.12")) {
  115. return new byte[] { (byte) dialectNumber,
  116. (byte) (dialectNumber >> 8) };
  117. }
  118. start = i + 1;
  119. dialectNumber++;
  120. }
  121. }
  122. return new byte[] { 0x00, 0x00 };
  123. }
  124. /**
  125. * Builds the close packet
  126. *
  127. * @return close packet
  128. */
  129. public byte[] getClose() {
  130. byte[] wordCount = { 0x00 };
  131. byte[] byteCount = { 0x00, 0x00 };
  132. smbCommand = new byte[] { 0x04 };
  133. byte[] response = HelperUtils.concat(wordCount, byteCount);
  134. return wrapNetbios(wrapHeader(response));
  135. }
  136. /**
  137. * Builds the DCERPC packet
  138. *
  139. * @return DCERPC packet
  140. */
  141. public byte[] getDceRpc(byte[] transSub, int length) {
  142. byte[] majorVersion = { 0x05 };
  143. byte[] minorVersion = { 0x00 };
  144. byte[] packetType = null;
  145. byte[] packetFlags = { 0x03 };
  146. byte[] dataRepres = { 0x10, 0x00, 0x00, 0x00 };
  147. byte[] fragLength = null;
  148. byte[] authLength = { 0x00, 0x00 };
  149. byte[] callID = null;
  150. byte[] response = null;
  151. if (transSub[0] == 0x00 && transSub[1] == 0x0b) {
  152. packetType = new byte[] { 0x0c };
  153. fragLength = new byte[] { 0x44, 0x00 };
  154. callID = new byte[] { 0x01, 0x00, 0x00, 0x00 };
  155. byte[] maxXmitFrag = { (byte) 0xb8, 0x10 };
  156. byte[] maxRecvFrag = { (byte) 0xb8, 0x10 };
  157. byte[] assocGroup = { 0x4a, 0x41, 0x00, 0x00 };
  158. byte[] scndryAddrLen = { 0x0d, 0x00 };
  159. byte[] scndryAddr = { 0x5c, 0x50, 0x49, 0x50, 0x45, 0x5c, 0x73,
  160. 0x72, 0x76, 0x73, 0x76, 0x63, 0x00, 0x00 };
  161. byte[] numResults = { 0x01, 0x00, 0x00, 0x00 };
  162. byte[] ctxItem = { 0x00, 0x00, 0x00, 0x00, 0x04, 0x5d, (byte) 0x88,
  163. (byte) 0x8a, (byte) 0xeb, 0x1c, (byte) 0xc9, 0x11,
  164. (byte) 0x9f, (byte) 0xe8, 0x08, 0x00, 0x2b, 0x10, 0x48,
  165. 0x60, 0x02, 0x00, 0x00, 0x00 };
  166. response = HelperUtils.concat(majorVersion, minorVersion,
  167. packetType, packetFlags, dataRepres, fragLength,
  168. authLength, callID, maxXmitFrag, maxRecvFrag, assocGroup,
  169. scndryAddrLen, scndryAddr, numResults, ctxItem);
  170. } else if (transSub[0] == 0x00 && transSub[1] == 0x00) {
  171. packetType = new byte[] { 0x02 };
  172. byte[] tmp = ByteBuffer.allocate(4).putInt(length).array();
  173. fragLength = new byte[] { tmp[3], tmp[2] };
  174. callID = new byte[] { 0x02, 0x00, 0x00, 0x00 };
  175. tmp = ByteBuffer.allocate(4).putInt(length - 24).array();
  176. byte[] allocHint = new byte[] { tmp[3], tmp[2], tmp[1], tmp[0] };
  177. byte[] contextID = { 0x00, 0x00 };
  178. byte[] cancelCount = { 0x00, 0x00 };
  179. response = HelperUtils.concat(majorVersion, minorVersion,
  180. packetType, packetFlags, dataRepres, fragLength,
  181. authLength, callID, allocHint, contextID, cancelCount);
  182. }
  183. return response;
  184. }
  185. /**
  186. * Builds the echo packet
  187. *
  188. * @return echo packet
  189. */
  190. public byte[] getEcho() {
  191. byte[] wordCount = { 0x01 };
  192. byte[] echoSeq = { 0x01, 0x00 };
  193. byte[] byteCount = { 0x10, 0x00 };
  194. byte[] echoData = { (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0,
  195. (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0,
  196. (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0,
  197. (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0 };
  198. byte[] response = HelperUtils.concat(wordCount, echoSeq, byteCount,
  199. echoData);
  200. return wrapNetbios(wrapHeader(response));
  201. }
  202. /**
  203. * Builds the negotiate packet
  204. *
  205. * @return negotiate packet
  206. */
  207. public byte[] getNego() {
  208. byte[] wordCount = { 0x11 };
  209. byte[] dialect = evaluateDialect();
  210. byte[] secMode = { 0x03 };
  211. byte[] maxMpxC = { 0x32, 0x00 };
  212. byte[] maxVcs = { 0x01, 0x00 };
  213. byte[] maxBufSize = { 0x04, 0x11, 0x00, 0x00 };
  214. byte[] maxRawBuf = { 0x00, 0x00, 0x01, 0x00 };
  215. byte[] sessionKey = { 0x00, 0x00, 0x00, 0x00 };
  216. byte[] capabilities = { (byte) 0xfc, (byte) 0xe3, 0x01, (byte) 0x80 };
  217. byte[] sysTime = getTimeInBytes();
  218. byte[] timeZone = getTimeZoneInBytes();
  219. byte[] keyLength = { 0x00 };
  220. byte[] byteCount = { 0x3a, 0x00 };
  221. byte[] guid = serverGUID;
  222. byte[] secBlob = { 0x60, 0x28, 0x06, 0x06 };
  223. byte[] oid = { 0x2b, 0x06, 0x01, 0x05, 0x05, 0x02 };
  224. byte[] protectNeg = { (byte) 0xa0, 0x1e };
  225. byte[] negToken = { 0x30, 0x1c, (byte) 0xa0, 0x1a, 0x30, 0x18 };
  226. byte[] mechType = { 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01,
  227. (byte) 0x82, 0x37, 0x02, 0x02, 0x1e };
  228. byte[] mechType2 = { 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01,
  229. (byte) 0x82, 0x37, 0x02, 0x02, 0x0a };
  230. byte[] response = HelperUtils.concat(wordCount, dialect, secMode,
  231. maxMpxC, maxVcs, maxBufSize, maxRawBuf, sessionKey,
  232. capabilities, sysTime, timeZone, keyLength, byteCount, guid,
  233. secBlob, oid, protectNeg, negToken, mechType, mechType2);
  234. return wrapNetbios(wrapHeader(response));
  235. }
  236. /**
  237. * Builds the nt create packet
  238. *
  239. * @return nt create packet
  240. */
  241. public byte[] getNTCreate() {
  242. byte[] wordCount = { 0x22 };
  243. byte[] andXCommand = { (byte) 0xff };
  244. byte[] reserved = { 0x00 };
  245. byte[] andXOffset = { 0x67, 0x00 };
  246. byte[] oplockLevel = { 0x00 };
  247. byte[] fid = { (byte) 0x00, 0x40 };
  248. byte[] createAction = { 0x01, 0x00, 0x00, 0x00 };
  249. byte[] created = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  250. byte[] lastAccess = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  251. byte[] lastWrite = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  252. byte[] change = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  253. byte[] fileAttributes = { (byte) 0x80, 0x00, 0x00, 0x00 };
  254. byte[] allocationSize = { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
  255. 0x00 };
  256. byte[] endOfFile = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  257. byte[] fileType = { 0x02, 0x00 };
  258. byte[] ipcState = { (byte) 0xff, 0x05 };
  259. byte[] isDirectory = { 0x00 };
  260. byte[] byteCount = { 0x00, 0x00 };
  261. byte[] response = HelperUtils.concat(wordCount, andXCommand, reserved,
  262. andXOffset, oplockLevel, fid, createAction, created,
  263. lastAccess, lastWrite, change, fileAttributes, allocationSize,
  264. endOfFile, fileType, ipcState, isDirectory, byteCount);
  265. return wrapNetbios(wrapHeader(response));
  266. }
  267. /**
  268. * Builds the session setup packet
  269. *
  270. * @ret urn session setup packet
  271. */
  272. public byte[] getSessSetup() {
  273. if (authenticateNext) {
  274. return getSetupAuth();
  275. } else {
  276. authenticateNext = true;
  277. return getSetupChal();
  278. }
  279. }
  280. /**
  281. * Builds the session setup packet for authentication required
  282. *
  283. * @return session setup authentication packet
  284. */
  285. private byte[] getSetupAuth() {
  286. byte[] wordCount = { 0x04 };
  287. byte[] andXCommand = { (byte) 0xff };
  288. byte[] reserved = { 0x00 };
  289. byte[] andXOffset = { (byte) 0xa2, 0x00 };
  290. byte[] action = { 0x01, 0x00 };
  291. byte[] secBlobLength;
  292. byte[] byteCount;
  293. byte[] secBlob = { (byte) 0xa1, 0x07, 0x30, 0x05, (byte) 0xa0, 0x03,
  294. 0x0a, 0x01, 0x00 };
  295. byte[] nativOS = HelperUtils.fillWithZeroExtended(serverVersion[0]
  296. .getBytes());
  297. byte[] nativLanMngr = HelperUtils.fillWithZeroExtended(serverVersion[1]
  298. .getBytes());
  299. byte[] buffer = ByteBuffer.allocate(4).putInt(secBlob.length).array();
  300. secBlobLength = new byte[] { buffer[3], buffer[2] };
  301. buffer = ByteBuffer.allocate(4)
  302. .putInt(secBlob.length + nativOS.length + nativLanMngr.length)
  303. .array();
  304. byteCount = new byte[] { buffer[3], buffer[2] };
  305. byte[] response = HelperUtils.concat(wordCount, andXCommand, reserved,
  306. andXOffset, action, secBlobLength, byteCount, secBlob, nativOS,
  307. nativLanMngr);
  308. return wrapNetbios(wrapHeader(response));
  309. }
  310. /**
  311. * Builds the session setup challange packet
  312. *
  313. * @return session setup challange packet
  314. */
  315. private byte[] getSetupChal() {
  316. byte[] wordCount = { 0x04 };
  317. byte[] andXCommand = { (byte) 0xff };
  318. byte[] reserved = { 0x00 };
  319. byte[] andXOffset = { 0x60, 0x01 };
  320. byte[] action = { 0x00, 0x00 };
  321. byte[] secBlobLength;
  322. byte[] byteCount;
  323. byte[] secBlob = { (byte) 0xa1, (byte) 0x81, (byte) 0xc4 };
  324. byte[] negToken = { 0x30, (byte) 0x81, (byte) 0xc1, (byte) 0xa0, 0x03,
  325. 0x0a, 0x01 };
  326. byte[] negResult = { 0x01 };
  327. byte[] negToken2 = { (byte) 0xa1, 0x0c, 0x06, 0x0a };
  328. byte[] supportedMech = { 0x2b, 0x06, 0x01, 0x04, 0x01, (byte) 0x82,
  329. 0x37, 0x02, 0x02, 0x0a };
  330. byte[] negToken3 = { (byte) 0xa2, (byte) 0x81, (byte) 0xab, 0x04,
  331. (byte) 0x81, (byte) 0xa8 };
  332. byte[] ntlmsspId = { 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00 };
  333. byte[] nlmMsgType = { 0x02, 0x00, 0x00, 0x00 };
  334. byte[] buffer = ByteBuffer.allocate(4).putInt(serverName.length)
  335. .array();
  336. byte[] targetNameLength = new byte[] { buffer[3], buffer[2] };
  337. byte[] targetNameMaxLength = new byte[] { buffer[3], buffer[2] };
  338. byte[] targetNameOffset = { 0x38, 0x00, 0x00, 0x00 };
  339. byte[] flags = { 0x15, (byte) 0x82, (byte) 0x8a, 0x60 };
  340. if (!serverVersion[0].contains("Unix")) {
  341. flags[3] = (byte) (flags[3] | 0x02);
  342. }
  343. byte[] challenge = HelperUtils.randomBytes(8);
  344. byte[] reserved2 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  345. byte[] targetInfoLength = { 0x60, 0x00 };
  346. byte[] targetInfoMaxLength = { 0x60, 0x00 };
  347. byte[] targetInfoOffset = { 0x48, 0x00, 0x00, 0x00 };
  348. byte[] version = null;
  349. if (serverVersion[0].contains("Windows 7")
  350. || serverVersion[0].contains("Windows Server 2008")) {
  351. version = new byte[] { 0x06, 0x01, (byte) 0xb0, 0x1d, 0x00, 0x00,
  352. 0x00, 0x0f };
  353. } else if (serverVersion[0].contains("Windows 8")
  354. || serverVersion[0].contains("Windows Server 2012")) {
  355. version = new byte[] { 0x06, 0x02, (byte) 0xf0, 0x23, 0x00, 0x00,
  356. 0x00, 0x0f };
  357. }
  358. // serverName
  359. byte[] attributeNBDomain = { 0x02, 0x00, 0x10, 0x00 };
  360. // serverName
  361. byte[] attributeNBcomputer = { 0x01, 0x00, 0x10, 0x00 };
  362. // serverName
  363. byte[] attributeDNSDomain = { 0x04, 0x00, 0x10, 0x00 };
  364. // serverName
  365. byte[] attributeDNScomputer = { 0x03, 0x00, 0x10, 0x00 };
  366. // serverName
  367. byte[] attributeTimeStamp = { 0x07, 0x00, 0x08, 0x00 };
  368. byte[] timeStamp = getTimeInBytes();
  369. byte[] attributeEnd = { 0x00, 0x00, 0x00, 0x00 };
  370. secBlob = HelperUtils.concat(secBlob, negToken, negResult, negToken2,
  371. supportedMech, negToken3, ntlmsspId, nlmMsgType,
  372. targetNameLength, targetNameMaxLength, targetNameOffset, flags,
  373. challenge, reserved2, targetInfoLength, targetInfoMaxLength,
  374. targetInfoOffset, version, serverName, attributeNBDomain,
  375. serverName, attributeNBcomputer, serverName,
  376. attributeDNSDomain, serverName, attributeDNScomputer,
  377. serverName, attributeTimeStamp, timeStamp, attributeEnd);
  378. byte[] nativOS = HelperUtils.fillWithZeroExtended(serverVersion[0]
  379. .getBytes());
  380. byte[] nativLanMngr = HelperUtils.fillWithZeroExtended(serverVersion[1]
  381. .getBytes());
  382. buffer = ByteBuffer.allocate(4).putInt(secBlob.length).array();
  383. secBlobLength = new byte[] { buffer[3], buffer[2] };
  384. buffer = ByteBuffer.allocate(4)
  385. .putInt(secBlob.length + nativOS.length + nativLanMngr.length)
  386. .array();
  387. byteCount = new byte[] { buffer[3], buffer[2] };
  388. ntStat = new byte[] { 0x16, 0x00, 0x00, (byte) 0xc0 };
  389. userID = new byte[] { 0x00, 0x08 };
  390. byte[] response = HelperUtils.concat(wordCount, andXCommand, reserved,
  391. andXOffset, action, secBlobLength, byteCount, secBlob, nativOS,
  392. nativLanMngr);
  393. return wrapNetbios(wrapHeader(response));
  394. }
  395. /**
  396. * Returns the command number from the current message
  397. *
  398. * @return command number
  399. */
  400. public byte getSmbCommand() {
  401. return smbCommand[0];
  402. }
  403. /**
  404. * Builds the trans packet
  405. *
  406. * @return trans packet
  407. */
  408. public byte[] getTrans() {
  409. byte[] transSub = getTransSub();
  410. byte[] response = null;
  411. if (transSub[0] == (byte) 0xff) { // for NMB in host announcement, NOT smb protocol
  412. byte[] wordCount = { 0x11 };
  413. byte[] totalParamCount = { 0x00, 0x00 };
  414. byte[] totalDataCount = new byte[2];
  415. byte[] maxParamCount = { 0x00, 0x00 };
  416. byte[] maxDataCount = { 0x00, 0x00 };
  417. byte[] maxSetupCount = { 0x00 };
  418. byte[] reserved = { 0x00 };
  419. byte[] flags = { 0x00, 0x00 };
  420. byte[] timeout = { 0x00, 0x00, 0x00, 0x00 };
  421. byte[] reserved2 = { 0x00, 0x00 };
  422. byte[] paramCount = { 0x00, 0x00 };
  423. byte[] paramOffset = { 0x00, 0x00 };
  424. byte[] dataCount = new byte[2];
  425. byte[] dataOffset = { 0x56, 0x00 };
  426. byte[] setupCount = { 0x03 };
  427. byte[] reserved3 = { 0x00 };
  428. //SMB MailSlot
  429. byte[] opcode = new byte[]{0x01, 0x00};
  430. byte[] priority = new byte[]{0x01, 0x00};
  431. byte[] smbclass = new byte[]{0x02, 0x00};
  432. byte[] size = new byte[2];
  433. byte[] name = HelperUtils.concat("\\MAILSLOT\\BROWSE".getBytes(), new byte[]{0x00});
  434. byte[] windowsBrowser = null;
  435. if (type == NBDSType.BROWSER) {
  436. windowsBrowser = getBrowser();
  437. } else if (type == NBDSType.REQUEST_ANNOUNCEMENT) {
  438. byte[] command = {0x02};
  439. byte[] unusedFlags = {0x01, 0x00};
  440. byte[] responseCompName = serverName;
  441. windowsBrowser = HelperUtils.concat(command, unusedFlags, responseCompName);
  442. } else {
  443. windowsBrowser = getAnnouncement();
  444. }
  445. byte[] buffer = ByteBuffer.allocate(4).putInt(windowsBrowser.length).array();
  446. totalDataCount[0] = buffer[3];
  447. totalDataCount[1] = buffer[2];
  448. dataCount = totalDataCount;
  449. buffer = ByteBuffer.allocate(4).putInt(name.length + windowsBrowser.length).array();
  450. size[0] = buffer[3];
  451. size[1] = buffer[2];
  452. byte[] smbMailSlot = HelperUtils.concat(opcode, priority, smbclass, size, name);
  453. // no netbios header required for NMB!!
  454. return wrapHeader(HelperUtils.concat(wordCount, totalParamCount, totalDataCount,
  455. maxParamCount, maxDataCount, maxSetupCount, reserved, flags, timeout, reserved2,
  456. paramCount, paramOffset, dataCount, dataOffset, setupCount, reserved3, smbMailSlot,
  457. windowsBrowser));
  458. } else if (transSub[0] == 0x00 && transSub[1] == 0x0b) { // bind_ack
  459. byte[] wordCount = { 0x0a };
  460. byte[] totalParamCount = { 0x00, 0x00 };
  461. byte[] totalDataCount = { 0x44, 0x00 };
  462. byte[] reserved = { 0x00, 0x00 };
  463. byte[] paramCount = { 0x00, 0x00 };
  464. byte[] paramOffset = { 0x38, 0x00 };
  465. byte[] paramDisplace = { 0x00, 0x00 };
  466. byte[] dataCount = { 0x44, 0x00 };
  467. byte[] dataOffset = { 0x38, 0x00 };
  468. byte[] dataDisplace = { 0x00, 0x00 };
  469. byte[] setupCount = { 0x00 };
  470. byte[] reserved2 = { 0x00 };
  471. byte[] byteCount = { 0x45, 0x00 };
  472. byte[] padding = { 0x00 };
  473. byte[] dcerpc = getDceRpc(transSub, 0);
  474. response = HelperUtils.concat(wordCount, totalParamCount,
  475. totalDataCount, reserved, paramCount, paramOffset,
  476. paramDisplace, dataCount, dataOffset, dataDisplace,
  477. setupCount, reserved2, byteCount, padding, dcerpc);
  478. } else if (transSub[0] == 0x00 && transSub[1] == 0x00) { // netShareEnumAll
  479. byte[] wordCount = { 0x0a };
  480. byte[] totalParamCount = { 0x00, 0x00 };
  481. byte[] totalDataCount = { 0x20, 0x01 };
  482. byte[] reserved = { 0x00, 0x00 };
  483. byte[] paramCount = { 0x00, 0x00 };
  484. byte[] paramOffset = { 0x38, 0x00 };
  485. byte[] paramDisplace = { 0x00, 0x00 };
  486. byte[] dataCount = { 0x20, 0x01 };
  487. byte[] dataOffset = { 0x38, 0x00 };
  488. byte[] dataDisplace = { 0x00, 0x00 };
  489. byte[] setupCount = { 0x00 };
  490. byte[] reserved2 = { 0x00 };
  491. byte[] byteCount = new byte[2]/* = {0x21, 0x01} */;
  492. byte[] padding = { 0x00 };
  493. byte[] dcerpc = new byte[24];
  494. byte[] levelPointer = { 0x01, 0x00, 0x00, 0x00 };
  495. byte[] ctr = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00 };
  496. byte[] ctr1 = { 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x02, 0x00,
  497. 0x03, 0x00, 0x00, 0x00 };
  498. byte[] array1Pointer = { 0x08, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
  499. (byte) 0x80, 0x0c, 0x00, 0x02, 0x00 };
  500. byte[] array2Pointer = { 0x10, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
  501. (byte) 0x80, 0x14, 0x00, 0x02, 0x00 };
  502. byte[] array3Pointer = { 0x18, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00,
  503. (byte) 0x80, 0x1c, 0x00, 0x02, 0x00 };
  504. byte[] array1 = { 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  505. 0x07, 0x00, 0x00, 0x00, 0x41, 0x00, 0x44, 0x00, 0x4d, 0x00,
  506. 0x49, 0x00, 0x4e, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00,
  507. 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x00,
  508. 0x00, 0x00, 0x52, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x6f, 0x00,
  509. 0x74, 0x00, 0x65, 0x00, 0x20, 0x00, 0x41, 0x00, 0x64, 0x00,
  510. 0x6d, 0x00, 0x69, 0x00, 0x6e, 0x00, 0x00, 0x00 };
  511. byte[] array2 = { 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
  512. 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x43, 0x00, 0x24, 0x00,
  513. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
  514. 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x44, 0x00, 0x65, 0x00,
  515. 0x66, 0x00, 0x61, 0x00, 0x75, 0x00, 0x6c, 0x00, 0x74, 0x00,
  516. 0x20, 0x00, 0x73, 0x00, 0x68, 0x00, 0x61, 0x00, 0x72, 0x00,
  517. 0x65, 0x00 };
  518. byte[] array3 = { 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
  519. 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x49, 0x00, 0x50, 0x00,
  520. 0x43, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00,
  521. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
  522. 0x52, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x6f, 0x00, 0x74, 0x00,
  523. 0x65, 0x00, 0x20, 0x00, 0x49, 0x00, 0x50, 0x00, 0x43, 0x00,
  524. 0x00, 0x00 };
  525. byte[] totalEntries = { 0x00, 0x00, 0x03, 0x00, 0x00, 0x00 };
  526. byte[] referentID = { 0x20, 0x00, 0x02, 0x00 };
  527. byte[] resumeHandle = { 0x00, 0x00, 0x00, 0x00 };
  528. byte[] windowsError = { 0x00, 0x00, 0x00, 0x00 };
  529. int tmp = padding.length + dcerpc.length + levelPointer.length
  530. + ctr.length + ctr1.length + array1Pointer.length
  531. + array2Pointer.length + array3Pointer.length
  532. + array1.length + array2.length + array3.length
  533. + totalEntries.length + referentID.length
  534. + resumeHandle.length + windowsError.length;
  535. byte[] tmp2 = ByteBuffer.allocate(4).putInt(tmp).array();
  536. byteCount = new byte[] { tmp2[3], tmp2[2] };
  537. dcerpc = getDceRpc(transSub, tmp - 1);
  538. response = HelperUtils.concat(wordCount, totalParamCount,
  539. totalDataCount, reserved, paramCount, paramOffset,
  540. paramDisplace, dataCount, dataOffset, dataDisplace,
  541. setupCount, reserved2, byteCount, padding, dcerpc,
  542. levelPointer, ctr, ctr1, array1Pointer, array2Pointer,
  543. array3Pointer, array1, array2, array3, totalEntries,
  544. referentID, resumeHandle, windowsError);
  545. }
  546. return wrapNetbios(wrapHeader(response));
  547. }
  548. /**
  549. * Builds the trans2 packet
  550. *
  551. * @return trans2 packet
  552. */
  553. public byte[] getTrans2() {
  554. byte[] response = null;
  555. byte[] wordCount = { 0x00 };
  556. byte[] andXCommand = { 0x00, 0x00 };
  557. ntStat = new byte[] { 0x22, 0x00, 0x00, (byte) 0xc0 };
  558. response = HelperUtils.concat(wordCount, andXCommand);
  559. return wrapNetbios(wrapHeader(response));
  560. }
  561. /**
  562. * Extracts the trans sub packet from message
  563. *
  564. * @return trans sub packet
  565. */
  566. private byte[] getTransSub() {
  567. byte[] transSub = new byte[2];
  568. if (smbCommand[0] == 0x32)
  569. transSub = new byte[] { message[66], message[65] };
  570. else if (smbCommand[0] == 0x25 && message != null)
  571. transSub = new byte[] { 0x00, message[90] };
  572. else if (smbCommand[0] == 0x25 && message == null)
  573. transSub = new byte[] { (byte) 0xff };
  574. else
  575. transSub = new byte[] { 0x00, 0x00 };
  576. return transSub;
  577. }
  578. /**
  579. * For NBNS only.
  580. * @return announcement packet.
  581. */
  582. private byte[] getAnnouncement() {
  583. //Microsoft Windows Browser
  584. byte[] command = null;
  585. if(type == NBDSType.LOCAL_MASTER_ANNOUNCEMENT_ALL || type == NBDSType.LOCAL_MASTER_ANNOUNCEMENT) {
  586. command = new byte[]{0x0f};
  587. } else if (type == NBDSType.DOMAIN_ANNOUNCEMENT) {
  588. command = new byte[]{0x0c};
  589. } else {
  590. command = new byte[]{0x01};
  591. }
  592. byte[] updateCount = new byte[]{0x00};
  593. byte[] updatePeriodicity = null;
  594. if (type == NBDSType.HOST_ANNOUNCEMENT_WITH_SERVICES) {
  595. updatePeriodicity = new byte[]{0x60, (byte) 0xea, 0x00, 0x00};
  596. } else if (type == NBDSType.HOST_ANNOUNCEMENT) {
  597. updatePeriodicity = new byte[]{0x00, 0x00, 0x00, 0x00};
  598. } else if (type == NBDSType.LOCAL_MASTER_ANNOUNCEMENT_ALL || type == NBDSType.DOMAIN_ANNOUNCEMENT) {
  599. updatePeriodicity = new byte[]{(byte) 0xc0, (byte) 0xd4, 0x01, 0x00};
  600. } else if (type == NBDSType.LOCAL_MASTER_ANNOUNCEMENT) {
  601. updatePeriodicity = new byte[]{0x00, 0x00, 0x00, 0x00};
  602. }
  603. byte[] hostName = null;
  604. if (type == NBDSType.DOMAIN_ANNOUNCEMENT) {
  605. hostName = workgroup;
  606. } else {
  607. hostName = serverName;
  608. }
  609. for (int i = hostName.length; i < 16; i++) {
  610. hostName = HelperUtils.concat(hostName, new byte[]{0x00});
  611. }
  612. byte[] osMajorVersion = new byte[]{0x04};
  613. byte[] osMinorVersion = new byte[]{0x09};
  614. byte[] serverType = null;
  615. if (type == NBDSType.HOST_ANNOUNCEMENT_WITH_SERVICES || type == NBDSType.LOCAL_MASTER_ANNOUNCEMENT_ALL) {
  616. serverType = new byte[]{0x03, (byte) 0x9a, (byte) 0x81, 0x00};
  617. } else if (type == NBDSType.HOST_ANNOUNCEMENT || type == NBDSType.LOCAL_MASTER_ANNOUNCEMENT) {
  618. serverType = new byte[]{0x00, 0x00, 0x00, 0x00};
  619. } else if (type == NBDSType.DOMAIN_ANNOUNCEMENT) {
  620. serverType = new byte[]{0x00, 0x10, 0x00, (byte) 0x80};
  621. }
  622. byte[] browserProtocolMajorVer = new byte[]{0x0f};
  623. byte[] browserProtocolMinorVer = new byte[]{0x01};
  624. byte[] signature = new byte[]{0x55, (byte) 0xaa};
  625. byte[] hostComment = null;
  626. if (type == NBDSType.DOMAIN_ANNOUNCEMENT) {
  627. hostComment = HelperUtils.concat(serverName, new byte[]{0x00});
  628. } else {
  629. hostComment = HelperUtils.concat("".getBytes(), new byte[]{0x00});
  630. }
  631. return HelperUtils.concat(command, updateCount, updatePeriodicity, hostName,
  632. osMajorVersion, osMinorVersion, serverType, browserProtocolMajorVer, browserProtocolMinorVer,
  633. signature, hostComment);
  634. }
  635. /**
  636. * For NBNS only.
  637. * @return MicrosoftBrowser part in NetBIOS packets.
  638. */
  639. private byte[] getBrowser() {
  640. byte[] command = {0x08};
  641. byte[] electionVersion = {0x01};
  642. byte[] electionCriteria = {0x02, 0x0f, 0x01, 0x14};
  643. byte[] uptime = {(byte) 0xb0, 0x36, 0x00, 0x00, 0x00, 0x00,0x00, 0x00};
  644. return HelperUtils.concat(command, electionVersion, electionCriteria, uptime, serverName, new byte[]{0x00});
  645. }
  646. /**
  647. * Builds the tree connect packet
  648. *
  649. * @return tree connect packet
  650. */
  651. public byte[] getTreeCon() {
  652. String str = HelperUtils.byteToStr(message);
  653. byte[] wordCount = { 0x00 };
  654. byte[] andXCommand = { 0x00, 0x00 };
  655. byte[] response = null;
  656. if (str.contains("IPC$") || str.contains("C$")) {
  657. wordCount = new byte[] { 0x07 };
  658. andXCommand = new byte[] { (byte) 0xff };
  659. byte[] reserved = { 0x00 };
  660. byte[] andXOffset = { 0x38, 0x00 };
  661. byte[] optionalSupport = { 0x01, 0x00 };
  662. byte[] maxShareAccess = { (byte) 0xff, (byte) 0xff, 0x1f, 0x00 };
  663. byte[] guestMaxShareAccess = { (byte) 0xff, (byte) 0xff, 0x1f, 0x00 };
  664. byte[] byteCount = { 0x07, 0x00 };
  665. byte[] service = { 0x49, 0x50, 0x43, 0x00 };
  666. byte[] extraParameters = { 0x00, 0x00, 0x00 };
  667. treeID = new byte[] { 0x00, 0x08 };
  668. response = HelperUtils.concat(wordCount, andXCommand, reserved,
  669. andXOffset, optionalSupport, maxShareAccess,
  670. guestMaxShareAccess, byteCount, service, extraParameters);
  671. } else if (str.contains("ADMIN$")) {
  672. ntStat = new byte[] { 0x22, 0x00, 0x00, (byte) 0xc0 };
  673. response = HelperUtils.concat(wordCount, andXCommand);
  674. } else {
  675. ntStat = new byte[] { (byte) 0xcc, 0x00, 0x00, (byte) 0xc0 };
  676. response = HelperUtils.concat(wordCount, andXCommand);
  677. }
  678. return wrapNetbios(wrapHeader(response));
  679. }
  680. /**
  681. * Builds the tree disconnect packet
  682. *
  683. * @return tree disconnect packet
  684. */
  685. public byte[] getTreeDisc() {
  686. byte[] wordCount = { 0x00 };
  687. byte[] byteCount = { 0x00, 0x00 };
  688. smbCommand[0] = 0x71;
  689. byte[] response = HelperUtils.concat(wordCount, byteCount);
  690. return wrapNetbios(wrapHeader(response));
  691. }
  692. /**
  693. * Converts the current system time into a byte[] with windows specific time
  694. *
  695. * @return current system time in windows format as byte[]
  696. */
  697. private static byte[] getTimeInBytes() {
  698. long time = System.currentTimeMillis();
  699. Calendar calend = Calendar.getInstance();
  700. calend.setTimeZone(TimeZone.getTimeZone("UTC"));
  701. calend.set(1601, 0, 01, 00, 00, 00);
  702. time -= calend.getTimeInMillis();
  703. time *= 10000;
  704. byte[] timeInWindowsBytes = new byte[8];
  705. byte[] timeInBytes = ByteBuffer.allocate(8).putLong(time).array();
  706. for (int i = 0, j = 7; i < 8 && j > -1; i++, j--) {
  707. timeInWindowsBytes[i] = (byte) (timeInBytes[j] & 0xff);
  708. }
  709. return timeInWindowsBytes;
  710. }
  711. /**
  712. * Converts the current timezone into a byte[] with windows specific format
  713. *
  714. * @return current timezone in windows format as byte[]
  715. */
  716. private static byte[] getTimeZoneInBytes() {
  717. // get current timezone offset in minutes
  718. Integer offset = new GregorianCalendar().getTimeZone().getRawOffset() / 1000 / 60;
  719. char[] offsetChars = Integer.toBinaryString(offset).toCharArray();
  720. boolean invert = false;
  721. for (int i = offsetChars.length - 1; i > -1; i--) {
  722. if (!invert && offsetChars[i] == '1') {
  723. invert = true;
  724. } else if (invert) {
  725. offsetChars[i] = (offsetChars[i] == '0') ? '1' : '0';
  726. }
  727. }
  728. char[] extendedChars = new char[31];
  729. for (int i = 0; i < extendedChars.length - offsetChars.length; i++) {
  730. extendedChars[i] = '1';
  731. }
  732. for (int i = 0; i < offsetChars.length; i++) {
  733. extendedChars[i + extendedChars.length - offsetChars.length] = offsetChars[i];
  734. }
  735. int timezone = Integer.parseInt(new String(extendedChars), 2);
  736. byte[] timezoneBytes = new byte[2];
  737. timezoneBytes[1] = (byte) (timezone >> 8);
  738. timezoneBytes[0] = (byte) (timezone);
  739. return timezoneBytes;
  740. }
  741. }