SMB.java 28 KB

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