SMB.java 25 KB

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