SMB.java 28 KB

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