SMB.java 27 KB

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