SMB.java 29 KB

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