SMB.java 29 KB

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