SMB.java 24 KB

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