SMB.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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 javax.net.ssl.SSLContext;
  9. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  10. import de.tudarmstadt.informatik.hostage.wrapper.ByteArray;
  11. /**
  12. * SMB protocol
  13. * @author Wulf Pfeiffer
  14. */
  15. public final class SMB implements Protocol<ByteArray> {
  16. /**
  17. * Represents the states of the protocol
  18. */
  19. private static enum STATE {
  20. NONE, CONNECTED, AUTHENTICATED, LISTING, DISCONNECTED, CLOSED
  21. }
  22. /**
  23. * Denotes in which state the protocol is right now
  24. */
  25. private STATE state = STATE.NONE;
  26. @Override
  27. public int getPort() {
  28. return 445;
  29. }
  30. @Override
  31. public TALK_FIRST whoTalksFirst() {
  32. return TALK_FIRST.CLIENT;
  33. }
  34. private SMBPacket packet = new SMBPacket();
  35. @Override
  36. public List<ByteArray> processMessage(ByteArray message) {
  37. byte[] primitiveByteArray = message.get();
  38. packet.newMsg(primitiveByteArray);
  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. @Override
  107. public boolean isClosed() {
  108. return (state == STATE.CLOSED);
  109. }
  110. @Override
  111. public boolean isSecure() {
  112. return false;
  113. }
  114. @Override
  115. public Class<ByteArray> getType() {
  116. return ByteArray.class;
  117. }
  118. @Override
  119. public String toString() {
  120. return "SMB";
  121. }
  122. @Override
  123. public SSLContext getSSLContext() {
  124. return null;
  125. }
  126. /**
  127. * Converts the current system time into a byte[] with windows specific time
  128. * @return current system time in windows format as byte[]
  129. */
  130. private byte[] getTimeInBytes() {
  131. long time = System.currentTimeMillis();
  132. Calendar calend = Calendar.getInstance();
  133. calend.setTimeZone(TimeZone.getTimeZone("UTC"));
  134. calend.set(1601, 0, 01, 00, 00, 00);
  135. time -= calend.getTimeInMillis();
  136. time *= 10000;
  137. byte[] b = new byte[8];
  138. byte[] b2 = ByteBuffer.allocate(8).putLong(time).array();
  139. for (int i = 0, j = 7; i < 8 && j > -1; i++, j--) {
  140. b[i] = (byte) (b2[j] & 0xff);
  141. }
  142. return b;
  143. }
  144. /**
  145. * Generates a random byte[] of a specified size
  146. * @param size of the byte[]
  147. * @return random byte[]
  148. */
  149. private byte[] randomBytes(int size) {
  150. byte[] bytes = new byte[size];
  151. Random rdm = new Random();
  152. rdm.nextBytes(bytes);
  153. return bytes;
  154. }
  155. /**
  156. * Denotes a SMB packet
  157. */
  158. private class SMBPacket {
  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(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 setup challange packet
  281. * @return 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 = {(byte) 0xc7, 0x00};
  290. byte[] byteCount = {0x35, 0x01};
  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[] respToken = {0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00, 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00,
  298. 0x38, 0x00, 0x00, 0x00, 0x15, (byte) 0x82, (byte) 0x8a, 0x62};
  299. byte[] challenge = randomBytes(8);
  300. byte[] respToken2 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x60, 0x00, 0x48, 0x00, 0x00, 0x00,
  301. 0x06, 0x01, (byte) 0xb0, 0x1d, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x00, 0x55, 0x00, 0x53, 0x00, 0x49, 0x00,
  302. 0x4e, 0x00, 0x45, 0x00, 0x53, 0x00, 0x53, 0x00, 0x02, 0x00, 0x10, 0x00, 0x42, 0x00, 0x55, 0x00,
  303. 0x53, 0x00, 0x49, 0x00, 0x4e, 0x00, 0x45, 0x00, 0x53, 0x00, 0x53, 0x00, 0x01, 0x00, 0x10, 0x00,
  304. 0x42, 0x00, 0x55, 0x00, 0x53, 0x00, 0x49, 0x00, 0x4e, 0x00, 0x45, 0x00, 0x53, 0x00, 0x53, 0x00,
  305. 0x04, 0x00, 0x10, 0x00, 0x42, 0x00, 0x55, 0x00, 0x53, 0x00, 0x49, 0x00, 0x4e, 0x00, 0x45, 0x00,
  306. 0x53, 0x00, 0x53, 0x00, 0x03, 0x00, 0x10, 0x00, 0x42, 0x00, 0x55, 0x00, 0x53, 0x00, 0x49, 0x00,
  307. 0x4e, 0x00, 0x45, 0x00, 0x53, 0x00, 0x53, 0x00, 0x07, 0x00, 0x08, 0x00};
  308. byte[] timeStamp = getTimeInBytes();
  309. byte[] respToken3 = {0x00, 0x00, 0x00, 0x00};
  310. byte[] nativOS = {0x57, 0x00, 0x69, 0x00, 0x6e, 0x00, 0x64, 0x00,
  311. 0x6f, 0x00, 0x77, 0x00, 0x73, 0x00, 0x20, 0x00, 0x37, 0x00, 0x20, 0x00, 0x50, 0x00, 0x72, 0x00,
  312. 0x6f, 0x00, 0x66, 0x00, 0x65, 0x00, 0x73, 0x00, 0x73, 0x00, 0x69, 0x00, 0x6f, 0x00, 0x6e, 0x00,
  313. 0x61, 0x00, 0x6c, 0x00, 0x20, 0x00, 0x37, 0x00, 0x36, 0x00, 0x30, 0x00, 0x30, 0x00, 0x00, 0x00}; //Windows 7 Professional 7600
  314. byte[] nativLanMngr = {0x57, 0x00, 0x69, 0x00, 0x6e, 0x00,
  315. 0x64, 0x00, 0x6f, 0x00, 0x77, 0x00, 0x73, 0x00, 0x20, 0x00, 0x37, 0x00, 0x20, 0x00, 0x50, 0x00,
  316. 0x72, 0x00, 0x6f, 0x00, 0x66, 0x00, 0x65, 0x00, 0x73, 0x00, 0x73, 0x00, 0x69, 0x00, 0x6f, 0x00,
  317. 0x6e, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x20, 0x00, 0x36, 0x00, 0x2e, 0x00, 0x31, 0x00, 0x00, 0x00}; //Windows 7 Professional 6.1
  318. ntStat = new byte[]{0x16, 0x00, 0x00, (byte) 0xc0};
  319. userID = new byte[]{0x00, 0x08};
  320. byte[] response = HelperUtils.concat(wordCount, andXCommand, reserved, andXOffset, action, secBlobLength,
  321. byteCount, secBlob, negToken, negResult, negToken2, supportedMech, negToken3,
  322. respToken, challenge, respToken2, timeStamp, respToken3, nativOS, nativLanMngr);
  323. return wrapNetbios(wrapHeader(response));
  324. }
  325. /**
  326. * Builds the setup authentication packet
  327. * @return setup authentication packet
  328. */
  329. private byte[] getSetupAuth() {
  330. byte[] wordCount = {0x04};
  331. byte[] andXCommand = {(byte) 0xff};
  332. byte[] reserved = {0x00};
  333. byte[] andXOffset = {(byte) 0xa2, 0x00};
  334. byte[] action = {0x01, 0x00};
  335. byte[] secBlobLength = {0x09, 0x00};
  336. byte[] byteCount = {(byte) 0x77, 0x00};
  337. byte[] secBlob = {(byte) 0xa1, 0x07, 0x30, 0x05, (byte) 0xa0, 0x03, 0x0a, 0x01, 0x00};
  338. byte[] nativOS = {0x57, 0x00, 0x69, 0x00, 0x6e, 0x00, 0x64, 0x00,
  339. 0x6f, 0x00, 0x77, 0x00, 0x73, 0x00, 0x20, 0x00, 0x37, 0x00, 0x20, 0x00, 0x50, 0x00, 0x72, 0x00,
  340. 0x6f, 0x00, 0x66, 0x00, 0x65, 0x00, 0x73, 0x00, 0x73, 0x00, 0x69, 0x00, 0x6f, 0x00, 0x6e, 0x00,
  341. 0x61, 0x00, 0x6c, 0x00, 0x20, 0x00, 0x37, 0x00, 0x36, 0x00, 0x30, 0x00, 0x30, 0x00, 0x00, 0x00}; //Windows 7 Professional 7600
  342. byte[] nativLanMngr = {0x57, 0x00, 0x69, 0x00, 0x6e, 0x00,
  343. 0x64, 0x00, 0x6f, 0x00, 0x77, 0x00, 0x73, 0x00, 0x20, 0x00, 0x37, 0x00, 0x20, 0x00, 0x50, 0x00,
  344. 0x72, 0x00, 0x6f, 0x00, 0x66, 0x00, 0x65, 0x00, 0x73, 0x00, 0x73, 0x00, 0x69, 0x00, 0x6f, 0x00,
  345. 0x6e, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x20, 0x00, 0x36, 0x00, 0x2e, 0x00, 0x31, 0x00, 0x00, 0x00}; //Windows 7 Professional 6.1
  346. byte[] response = HelperUtils.concat(wordCount, andXCommand, reserved, andXOffset, action, secBlobLength,
  347. byteCount, secBlob, nativOS, nativLanMngr);
  348. return wrapNetbios(wrapHeader(response));
  349. }
  350. /**
  351. * Builds the tree connect packet
  352. * @return tree connect packet
  353. */
  354. public byte[] getTreeCon() {
  355. String str = toString();
  356. byte[] wordCount = {0x00};
  357. byte[] andXCommand = {0x00, 0x00};
  358. byte[] response = null;
  359. if(str.contains("IPC$") || str.contains("DOCS")) {
  360. wordCount = new byte[] {0x07};
  361. andXCommand = new byte[] {(byte) 0xff};
  362. byte[] reserved = {0x00};
  363. byte[] andXOffset = {0x38, 0x00};
  364. byte[] optionalSupport = {0x01, 0x00};
  365. byte[] maxShareAccess = {(byte) 0xff, (byte) 0xff, 0x1f, 0x00};
  366. byte[] guestMaxShareAccess = {(byte) 0xff, (byte) 0xff, 0x1f, 0x00};
  367. byte[] byteCount = {0x07, 0x00};
  368. byte[] service = {0x49, 0x50, 0x43, 0x00};
  369. byte[] extraParameters = {0x00, 0x00, 0x00};
  370. treeID = new byte[]{0x00, 0x08};
  371. response = HelperUtils.concat(wordCount, andXCommand, reserved, andXOffset, optionalSupport, maxShareAccess,
  372. guestMaxShareAccess, byteCount, service, extraParameters);
  373. } else if(str.contains("C$") || str.contains("ADMIN$")) {
  374. ntStat = new byte[] {0x22, 0x00, 0x00, (byte) 0xc0};
  375. response = HelperUtils.concat(wordCount, andXCommand);
  376. } else {
  377. ntStat = new byte[] {(byte) 0xcc, 0x00, 0x00, (byte) 0xc0};
  378. response = HelperUtils.concat(wordCount, andXCommand);
  379. }
  380. return wrapNetbios(wrapHeader(response));
  381. }
  382. /**
  383. * Builds the nt create packet
  384. * @return nt create packet
  385. */
  386. public byte[] getNTCreate() {
  387. byte[] wordCount = {0x22};
  388. byte[] andXCommand = {(byte) 0xff};
  389. byte[] reserved = {0x00};
  390. byte[] andXOffset = {0x67, 0x00};
  391. byte[] oplockLevel = {0x00};
  392. byte[] fid = {(byte) 0x00, 0x40};
  393. byte[] createAction = {0x01, 0x00, 0x00, 0x00};
  394. byte[] created = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  395. byte[] lastAccess = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  396. byte[] lastWrite = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  397. byte[] change = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  398. byte[] fileAttributes = {(byte) 0x80, 0x00, 0x00, 0x00};
  399. byte[] allocationSize = {0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  400. byte[] endOfFile = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  401. byte[] fileType = {0x02, 0x00};
  402. byte[] ipcState = {(byte) 0xff, 0x05};
  403. byte[] isDirectory = {0x00};
  404. byte[] byteCount = {0x00, 0x00};
  405. byte[] response = HelperUtils.concat(wordCount, andXCommand, reserved, andXOffset, oplockLevel, fid,
  406. createAction, created, lastAccess, lastWrite, change, fileAttributes, allocationSize,
  407. endOfFile, fileType, ipcState, isDirectory, byteCount);
  408. return wrapNetbios(wrapHeader(response));
  409. }
  410. /**
  411. * Builds the trans packet
  412. * @return trans packet
  413. */
  414. public byte[] getTrans() {
  415. byte[] transSub = getTransSub();
  416. byte[] response = null;
  417. if(transSub[0] == 0x00 && transSub[1] == 0x0b) {
  418. byte[] wordCount = {0x0a};
  419. byte[] totalParamCount = {0x00,0x00};
  420. byte[] totalDataCount = {0x44,0x00};
  421. byte[] reserved = {0x00, 0x00};
  422. byte[] paramCount = {0x00, 0x00};
  423. byte[] paramOffset = {0x38, 0x00};
  424. byte[] paramDisplace = {0x00, 0x00};
  425. byte[] dataCount = {0x44, 0x00};
  426. byte[] dataOffset = {0x38, 0x00};
  427. byte[] dataDisplace = {0x00, 0x00};
  428. byte[] setupCount = {0x00};
  429. byte[] reserved2 = {0x00};
  430. byte[] byteCount = {0x45, 0x00};
  431. byte[] padding = {0x00};
  432. byte[] dcerpc = {0x05, 0x00,
  433. 0x0c, 0x03, 0x10, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, (byte) 0xb8, 0x10,
  434. (byte) 0xb8, 0x10, 0x4a, 0x41, 0x00, 0x00, 0x0d, 0x00, 0x5c, 0x50, 0x49, 0x50, 0x45, 0x5c, 0x73, 0x72,
  435. 0x76, 0x73, 0x76, 0x63, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x5d,
  436. (byte) 0x88, (byte) 0x8a, (byte) 0xeb, 0x1c, (byte) 0xc9, 0x11, (byte) 0x9f, (byte) 0xe8, 0x08, 0x00, 0x2b, 0x10, 0x48, 0x60, 0x02, 0x00,
  437. 0x00, 0x00};
  438. response = HelperUtils.concat(wordCount, totalParamCount, totalDataCount, reserved, paramCount, paramOffset,
  439. paramDisplace, dataCount, dataOffset, dataDisplace, setupCount, reserved2, byteCount, padding, dcerpc);
  440. } else if(transSub[0] == 0x00 && transSub[1] == 0x00) {
  441. byte[] wordCount = {0x0a};
  442. byte[] totalParamCount = {0x00, 0x00};
  443. byte[] totalDataCount = {0x54, 0x01};
  444. byte[] reserved = {0x00, 0x00};
  445. byte[] paramCount = {0x00, 0x00};
  446. byte[] paramOffset = {0x38, 0x00};
  447. byte[] paramDisplace = {0x00, 0x00};
  448. byte[] dataCount = {0x54, 0x01};
  449. byte[] dataOffset = {0x38, 0x00};
  450. byte[] dataDisplace = {0x00, 0x00};
  451. byte[] setupCount = {0x00};
  452. byte[] reserved2 = {0x00};
  453. byte[] byteCount = {0x55, 0x01};
  454. byte[] padding = {0x00};
  455. byte[] dcerpc = {0x05, 0x00,
  456. 0x02, 0x03, 0x10, 0x00, 0x00, 0x00, 0x54, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3c, 0x01,
  457. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  458. byte[] serverService = {0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  459. 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00,
  460. 0x02, 0x00, 0x00, 0x00, 0x00, (byte) 0x80, 0x0c, 0x00, 0x02, 0x00, 0x10, 0x00, 0x02, 0x00, 0x00, 0x00,
  461. 0x00, (byte) 0x80, 0x14, 0x00, 0x02, 0x00, 0x18, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x00,
  462. 0x02, 0x00, 0x20, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, (byte) 0x80, 0x24, 0x00, 0x02, 0x00, 0x07, 0x00,
  463. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x41, 0x00, 0x44, 0x00, 0x4d, 0x00,
  464. 0x49, 0x00, 0x4e, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
  465. 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x52, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x6f, 0x00, 0x74, 0x00,
  466. 0x65, 0x00, 0x20, 0x00, 0x41, 0x00, 0x64, 0x00, 0x6d, 0x00, 0x69, 0x00, 0x6e, 0x00, 0x00, 0x00,
  467. 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x43, 0x00,
  468. 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00,
  469. 0x00, 0x00, 0x44, 0x00, 0x65, 0x00, 0x66, 0x00, 0x61, 0x00, 0x75, 0x00, 0x6c, 0x00, 0x74, 0x00,
  470. 0x20, 0x00, 0x73, 0x00, 0x68, 0x00, 0x61, 0x00, 0x72, 0x00, 0x65, 0x00, 0x00, 0x00, 0x05, 0x00,
  471. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x64, 0x00, 0x6f, 0x00, 0x63, 0x00,
  472. 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  473. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00,
  474. 0x00, 0x00, 0x49, 0x00, 0x50, 0x00, 0x43, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00,
  475. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x52, 0x00, 0x65, 0x00, 0x6d, 0x00,
  476. 0x6f, 0x00, 0x74, 0x00, 0x65, 0x00, 0x20, 0x00, 0x49, 0x00, 0x50, 0x00, 0x43, 0x00, 0x00, 0x00};
  477. byte[] totalEntries = {0x00, 0x00, 0x04, 0x00, 0x00, 0x00};
  478. byte[] referentID = {0x28, 0x00, 0x02, 0x00};
  479. byte[] resumeHandle = {0x00, 0x00, 0x00, 0x00};
  480. byte[] windowsError = {0x00, 0x00, 0x00, 0x00};
  481. response = HelperUtils.concat(wordCount, totalParamCount, totalDataCount, reserved, paramCount, paramOffset,
  482. paramDisplace, dataCount, dataOffset, dataDisplace, setupCount, reserved2, byteCount, padding, dcerpc,
  483. serverService, totalEntries, referentID, resumeHandle, windowsError);
  484. }
  485. return wrapNetbios(wrapHeader(response));
  486. }
  487. /**
  488. * Builds the close packet
  489. * @return close packet
  490. */
  491. public byte[] getClose() {
  492. byte[] wordCount = {0x00};
  493. byte[] byteCount = {0x00, 0x00};
  494. smbCommand = new byte[]{0x04};
  495. byte[] response = HelperUtils.concat(wordCount, byteCount);
  496. return wrapNetbios(wrapHeader(response));
  497. }
  498. /**
  499. * Builds the tree disconnect packet
  500. * @return tree disconnect packet
  501. */
  502. public byte[] getTreeDisc() {
  503. byte[] wordCount = {0x00};
  504. byte[] byteCount = {0x00, 0x00};
  505. smbCommand[0] = 0x71;
  506. byte[] response = HelperUtils.concat(wordCount, byteCount);
  507. return wrapNetbios(wrapHeader(response));
  508. }
  509. /**
  510. * Builds the echo packet
  511. * @return echo packet
  512. */
  513. public byte[] getEcho() {
  514. byte[] wordCount = {0x01};
  515. byte[] echoSeq = {0x01, 0x00};
  516. byte[] byteCount = {0x10, 0x00};
  517. byte[] echoData = {(byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0,
  518. (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0};
  519. byte[] response = HelperUtils.concat(wordCount, echoSeq, byteCount, echoData);
  520. return wrapNetbios(wrapHeader(response));
  521. }
  522. /**
  523. * Builds the trans2 packet
  524. * @return trans2 packet
  525. */
  526. public byte[] getTrans2() {
  527. byte[] response = null;
  528. byte[] wordCount = {0x00};
  529. byte[] andXCommand = {0x00, 0x00};
  530. ntStat = new byte[] {0x22, 0x00, 0x00, (byte) 0xc0};
  531. response = HelperUtils.concat(wordCount, andXCommand);
  532. return wrapNetbios(wrapHeader(response));
  533. }
  534. /**
  535. * Builds the trans sub packet
  536. * @return trans sub packet
  537. */
  538. private byte[] getTransSub() {
  539. byte[] transSub = new byte[2];
  540. if(smbCommand[0] == 0x32) transSub = new byte[]{message[66], message[65]};
  541. else if(smbCommand[0] == 0x25) transSub = new byte[]{0x00, message[90]};
  542. else transSub = new byte[]{0x00, 0x00};
  543. return transSub;
  544. }
  545. @Override
  546. public String toString() {
  547. return byteToStr(message);
  548. }
  549. /**
  550. * Converts a byte[] to a String, but only characters in ASCII between 32 and 127
  551. * @param bytes that are converted
  552. * @return converted String
  553. */
  554. private String byteToStr(byte[] bytes) {
  555. char[] chars = new char[bytes.length];
  556. for (int i = 0, j = 0; i < bytes.length && j < chars.length; i++) {
  557. if (isLetter((char) bytes[i])) {
  558. chars[j] = (char) bytes[i];
  559. j++;
  560. }
  561. }
  562. return new String(chars);
  563. }
  564. /**
  565. * Determines if a character is in ASCII between 32 and 127
  566. * @param character that is checked
  567. * @return true if the character is between 32 and 127, else false
  568. */
  569. private boolean isLetter(char character) {
  570. return (character >= 32 && character <= 127);
  571. }
  572. /**
  573. * Returns the command number from the current message
  574. * @return command number
  575. */
  576. public byte getSmbCommand() {
  577. return smbCommand[0];
  578. }
  579. }
  580. }