SMB.java 28 KB

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