SMBPacket.java 29 KB

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