SMB.java 28 KB

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