SMB.java 23 KB

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