SMB.java 26 KB

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