SMBPacket.java 25 KB

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