SMB.java 29 KB

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