Guest User

Untitled

a guest
Nov 14th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.65 KB | None | 0 0
  1. import org.apache.commons.io.FileUtils;
  2. import org.apache.commons.io.filefilter.DirectoryFileFilter;
  3. import org.apache.commons.io.filefilter.RegexFileFilter;
  4.  
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.nio.charset.Charset;
  9. import java.util.ArrayList;
  10. import java.util.Collection;
  11. import java.util.Comparator;
  12.  
  13. class CommandClass
  14. {
  15. //Add your commands here
  16. public void Initialize()
  17. {
  18. AddCommand(new ExitCommand());
  19. AddCommand(new PlatformCommand());
  20. AddCommand(new find_files());
  21. AddCommand(new HelloCommand());
  22. AddCommand(new addcommand());
  23. AddCommand(new subcommand());
  24. AddCommand(new multcommand());
  25. //AddCommand(new user_recon());
  26.  
  27. AddCommand(new user_recon_template());
  28. }
  29.  
  30. //region Add your classes here
  31.  
  32. private class ExitCommand extends CommandBase
  33. {
  34.  
  35. public ExitCommand()
  36. {
  37. super("exit", "Exit environment");
  38. }
  39.  
  40. public void Windows(String[] args)
  41. {
  42. System.exit(0);
  43. }
  44.  
  45. public void Linux(String[] args)
  46. {
  47. Windows(args);
  48. }
  49. }
  50.  
  51. private class PlatformCommand extends CommandBase
  52. {
  53.  
  54. public PlatformCommand()
  55. {
  56. super("platform", "args W or L");
  57. }
  58.  
  59. public void Windows(String[] args)
  60. {
  61. String arg = args[0].trim().toLowerCase();
  62. if (args.length != 1 || (!arg.equals("w") && !arg.equals("l")))
  63. {
  64. System.out.println("Invalid Arguments provided! Expected W or L but got '" + arg.toUpperCase() + "'");
  65. return;
  66. }
  67. CommandClass.IsWindows = args[0].trim().toLowerCase().equals("w");
  68. }
  69.  
  70. public void Linux(String[] args)
  71. {
  72. Windows(args);
  73. }
  74. }
  75.  
  76. private class HelloCommand extends CommandBase
  77. {
  78.  
  79. public HelloCommand()
  80. {
  81. super("Hello", "Prints Hello World");
  82. }
  83.  
  84. public void Windows(String[] args)
  85. {
  86. System.out.println("Hello World");
  87. }
  88.  
  89. public void Linux(String[] args)
  90. {
  91. Windows(args);
  92. }
  93. }
  94.  
  95. private class addcommand extends CommandBase
  96. {
  97.  
  98. public addcommand()
  99. {
  100. super("add", "adds numbers");
  101. }
  102.  
  103. public void Windows(String[] args)
  104. {
  105. int a, b;
  106. if (args.length != 2)
  107. {
  108. System.out.println("wrong amount of args");
  109. return;
  110. }
  111. try
  112. {
  113. a = Integer.valueOf(args[0]);
  114. b = Integer.valueOf(args[1]);
  115. } catch (Exception e)
  116. {
  117. System.out.println("invalid argument");
  118. return;
  119. }
  120. System.out.println(a + " + " + b + " = " + (a + b));
  121. }
  122.  
  123. public void Linux(String[] args)
  124. {
  125. Windows(args);
  126. }
  127. }
  128.  
  129. private class subcommand extends CommandBase
  130. {
  131.  
  132. public subcommand()
  133. {
  134. super("subtract", "subtracts numbers");
  135. }
  136.  
  137. public void Windows(String[] args)
  138. {
  139. int a, b;
  140. if (args.length != 2)
  141. {
  142. System.out.println("wrong amount of args");
  143. return;
  144. }
  145. try
  146. {
  147. a = Integer.valueOf(args[0]);
  148. b = Integer.valueOf(args[1]);
  149. } catch (Exception e)
  150. {
  151. System.out.println("invalid argument");
  152. return;
  153. }
  154. System.out.println(a + " - " + b + " = " + (a - b));
  155. }
  156.  
  157. public void Linux(String[] args)
  158. {
  159. Windows(args);
  160. }
  161. }
  162.  
  163. private class multcommand extends CommandBase
  164. {
  165.  
  166. public multcommand()
  167. {
  168. super("multiply", "multiplies numbers");
  169. }
  170.  
  171. public void Windows(String[] args)
  172. {
  173. int a, b;
  174. if (args.length != 2)
  175. {
  176. System.out.println("wrong amount of args");
  177. return;
  178. }
  179. try
  180. {
  181. a = Integer.valueOf(args[0]);
  182. b = Integer.valueOf(args[1]);
  183. } catch (Exception e)
  184. {
  185. System.out.println("invalid argument");
  186. return;
  187. }
  188. System.out.println(a + " * " + b + " = " + (a * b));
  189. }
  190.  
  191. public void Linux(String[] args)
  192. {
  193. Windows(args);
  194. }
  195. }
  196.  
  197. private class find_files extends CommandBase
  198. {
  199.  
  200. public find_files()
  201. {
  202. super("find-files", "<dir> <ext,...> <outpath?>");
  203. }
  204.  
  205. public void Windows(String[] args)
  206. {
  207. if (args.length < 2 || args.length > 3)
  208. {
  209. System.out.println("Bad arguments. Please enter <dir> <regex>");
  210. return;
  211. }
  212.  
  213. File f = new File(args[0]);
  214.  
  215. if (!f.exists())
  216. {
  217. System.out.println("Directory: " + args[0] + " doesn't exist!");
  218. return;
  219. }
  220.  
  221. String regex = ".*\\.(";
  222. String[] split = (args[1] == null ? "" : args[1]).split(",");
  223. for (int i = 0; i < split.length; i++)
  224. {
  225. regex += split[i] + (i + 1 == split.length ? ")" : "|");
  226. }
  227.  
  228.  
  229. Collection<File> files = null;
  230. try
  231. {
  232. files = FileUtils.listFiles(
  233. new File(args[0]),
  234. new RegexFileFilter(regex),
  235. DirectoryFileFilter.DIRECTORY
  236. );
  237. } catch (java.util.regex.PatternSyntaxException e)
  238. {
  239. System.out.println("Bad regex!");
  240. return;
  241. }
  242.  
  243. String result = "";
  244.  
  245. for (File file : files)
  246. {
  247. result += file.getAbsolutePath() + "\n";
  248. }
  249.  
  250. if (args.length == 3)
  251. {
  252. try
  253. {
  254. FileUtils.writeStringToFile(new File(args[2]), result, Charset.defaultCharset());
  255. } catch (Exception e)
  256. {
  257. System.out.println("Failed to save file...");
  258. return;
  259. }
  260. System.out.println("File saved.");
  261. } else
  262. {
  263. System.out.println(result);
  264. }
  265. }
  266.  
  267. public void Linux(String[] args)
  268. {
  269. Windows(args);
  270. }
  271. }
  272.  
  273. private class user_recon extends CommandBase
  274. {
  275.  
  276. public user_recon()
  277. {
  278. super("user-recon", "<input>");
  279. }
  280.  
  281. private boolean IsWin = true;
  282.  
  283. public void Windows(String[] args)
  284. {
  285. if (args.length != 1)
  286. {
  287. System.out.println("Bad arguments");
  288. return;
  289. }
  290. if (!new File(args[0]).exists())
  291. {
  292. System.out.println("Input file doesn't exist!!!");
  293. return;
  294. }
  295.  
  296. ArrayList<UserInfo> Actions = new ArrayList<>();
  297. UserInfo[] u_info = IsWin ? GetWindowsUsers() : GetLinuxUsers();
  298. UserInfo[] cp_info = GetCyberPatriotUsers(args[0]);
  299.  
  300. for (UserInfo u : u_info)
  301. {
  302. UserInfo match = null;
  303. for (UserInfo c : cp_info)
  304. {
  305. if (c.Name.toLowerCase().equals(u.Name.toLowerCase()))
  306. {
  307. match = c;
  308. break;
  309. }
  310. }
  311. if (match == null)
  312. {
  313. u.ActionTaken = "delete user";
  314. Actions.add(u);
  315. } else
  316. {
  317. if (match.IsAdmin != u.IsAdmin)
  318. {
  319. u.ActionTaken = (match.IsAdmin && !u.IsAdmin) ?
  320. "Elevate to administrator" :
  321. "Change to normal user";
  322. Actions.add(u);
  323. }
  324. }
  325. }
  326. for (UserInfo c : cp_info)
  327. {
  328. UserInfo match = null;
  329. for (UserInfo u : u_info)
  330. {
  331. if (c.Name.toLowerCase().equals(u.Name.toLowerCase()))
  332. {
  333. match = c;
  334. break;
  335. }
  336. }
  337. if (match == null)
  338. {
  339. c.ActionTaken = "Add user as " + (c.IsAdmin ? "an administrator" : "a normal user");
  340. Actions.add(c);
  341. }
  342. }
  343.  
  344.  
  345. for (UserInfo u : Actions)
  346. {
  347. if (u.ActionTaken == null)
  348. {
  349. u.ActionTaken = "unknown action";
  350. }
  351. System.out.println("User <" + u.Name + "> : Action <" + u.ActionTaken + ">");
  352. }
  353. }
  354.  
  355. public void Linux(String[] args)
  356. {
  357. IsWin = false;
  358. Windows(args);
  359. }
  360.  
  361. private UserInfo[] GetWindowsUsers()
  362. {
  363. String[] users_raw = Env_GetCommandOutput("net", "user");
  364. ArrayList<UserInfo> users = new ArrayList<>();
  365.  
  366. byte ReadMode = 0;
  367.  
  368. for (int i = 0; i < users_raw.length; i++)
  369. {
  370. if (ReadMode == 0) //searching
  371. {
  372. if (users_raw[i].contains("---"))
  373. ReadMode = 1;
  374. } else if (ReadMode == 1)
  375. {
  376. if (users_raw[i].toLowerCase().contains("the command completed"))
  377. ReadMode = 2;
  378. else
  379. {
  380. String[] UserList = users_raw[i].split("\\s+");
  381. for (String user : UserList)
  382. {
  383. String[] userout = Env_GetCommandOutput("net", "user " + user);
  384.  
  385. if (userout.length < 1 || userout[0].contains("could not be"))
  386. continue;
  387. UserInfo NewUser = new UserInfo();
  388. NewUser.Name = user.toLowerCase();
  389.  
  390. for (String s : userout)
  391. {
  392. if (s.toLowerCase().contains("administrator"))
  393. NewUser.IsAdmin = true;
  394. }
  395.  
  396. users.add(NewUser);
  397. }
  398. }
  399. } else
  400. {
  401. break;
  402. }
  403. }
  404.  
  405. return users.toArray(new UserInfo[users.size()]);
  406. }
  407.  
  408. private String[] Env_GetCommandOutput(String command, String args)
  409. {
  410. ProcessBuilder processBuilder = null;
  411. if(args.isEmpty())
  412. processBuilder = new ProcessBuilder(command);
  413. else
  414. processBuilder = new ProcessBuilder(command, args);
  415.  
  416. processBuilder.redirectErrorStream(true);
  417.  
  418. Process p = null;
  419.  
  420. try
  421. {
  422. p = processBuilder.start();
  423. } catch (Exception e)
  424. {
  425. return new String[0];
  426. }
  427.  
  428. java.io.BufferedReader reader =
  429. new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
  430.  
  431. ArrayList<String> lines = new ArrayList<>();
  432.  
  433. String line = null;
  434.  
  435. try
  436. {
  437. while ((line = reader.readLine()) != null)
  438. {
  439. lines.add(line);
  440. }
  441. } catch (Exception e)
  442. {
  443. return new String[0];
  444. }
  445.  
  446. return lines.toArray(new String[lines.size()]);
  447. }
  448.  
  449. private UserInfo[] GetLinuxUsers()
  450. {
  451. String[] users_raw = Env_GetCommandOutput("awk -F\'[/:]\' \'{if ($3 >= 1000 && $3 != 65534) print $1}\' /etc/passwd", "");
  452.  
  453. ArrayList<UserInfo> users = new ArrayList<>();
  454.  
  455. for (String u : users_raw)
  456. {
  457. UserInfo userInfo = new UserInfo();
  458. userInfo.Name = u;
  459.  
  460. String[] Info = Env_GetCommandOutput("groups", u);
  461.  
  462. for(String sub_info : Info)
  463. {
  464. if(sub_info.contains("sudo"))
  465. userInfo.IsAdmin = true;
  466. }
  467.  
  468. users.add(userInfo);
  469. }
  470.  
  471. return users.toArray(new UserInfo[users.size()]);
  472. }
  473.  
  474. private UserInfo[] GetCyberPatriotUsers(String fname)
  475. {
  476. ArrayList<UserInfo> users = new ArrayList<>();
  477.  
  478. try
  479. {
  480. java.util.Scanner lines = new java.util.Scanner(new File(fname));
  481.  
  482. byte ReadMode = 0;
  483.  
  484. while (true)
  485. {
  486. String line = lines.nextLine();
  487. if (line.isEmpty())
  488. continue;
  489. if (line.trim().toLowerCase().contains("authorized administrators"))
  490. {
  491. ReadMode = 1;
  492. continue;
  493. } else if (line.trim().toLowerCase().contains("authorized users"))
  494. {
  495. ReadMode = 2;
  496. continue;
  497. }
  498. if (ReadMode == 1)
  499. {
  500. if (Character.isDigit(line.charAt(0)) || Character.isLetter(line.charAt(0)))
  501. {
  502. UserInfo u = new UserInfo();
  503. u.Name = "";
  504. for (int i = 0; i < line.length(); i++)
  505. {
  506. if (Character.isDigit(line.charAt(i)) || Character.isLetter(line.charAt(i)))
  507. u.Name += line.charAt(i);
  508. else
  509. break;
  510. }
  511. u.IsAdmin = true;
  512. users.add(u);
  513. }
  514. } else if (ReadMode == 2)
  515. {
  516. if (Character.isDigit(line.charAt(0)) || Character.isLetter(line.charAt(0)))
  517. {
  518. UserInfo u = new UserInfo();
  519. u.Name = "";
  520. for (int i = 0; i < line.length(); i++)
  521. {
  522. if (Character.isDigit(line.charAt(i)) || Character.isLetter(line.charAt(i)))
  523. u.Name += line.charAt(i);
  524. else
  525. break;
  526. }
  527. u.IsAdmin = false;
  528. users.add(u);
  529. }
  530. }
  531. }
  532.  
  533. } catch (java.util.NoSuchElementException e)
  534. {
  535. UserInfo[] ui = new UserInfo[users.size()];
  536. return users.toArray(ui);
  537. } catch (FileNotFoundException e)
  538. {
  539. return new UserInfo[0];
  540. }
  541. }
  542.  
  543. private class UserInfo
  544. {
  545. public String Name;
  546. public boolean IsAdmin;
  547. public String ActionTaken;
  548. }
  549. }
  550.  
  551.  
  552. private class user_recon_template extends CommandBase
  553. {
  554.  
  555. public user_recon_template()
  556. {
  557. super("user-recon","<input>");
  558. }
  559.  
  560. public void Windows(String[] args) //TODO:
  561. {
  562. //1. Must have 1 argument
  563.  
  564. //2. Must have a valid argument (file exists)
  565.  
  566. //3. Create a list to hold UserInfo objects that need to change
  567.  
  568. //4. Get all users on current platform
  569.  
  570. //5. Get all users in the list of users
  571.  
  572. //6. Compare users in lists to achieve the following goals (hint: use ActionTaken to track what is wrong)
  573. //6.1. Users that shouldnt exist are added to your list
  574. //6.2. Users that don't have proper privileges should be added to your list
  575. //6.3. Users that need to be created should be added to your list
  576.  
  577. //7. Print all the users and actions required to the screen
  578.  
  579. }
  580.  
  581. public void Linux(String[] args) //TODO:
  582. {
  583. //hint: make a variable to determine if the platform is windows and call the windows function
  584. }
  585.  
  586. private UserInfo[] GetWindowsUsers()
  587. {
  588. String[] users_raw = Env_GetCommandOutput("net", "user");
  589. ArrayList<UserInfo> users = new ArrayList<>();
  590.  
  591. byte ReadMode = 0;
  592.  
  593. for (int i = 0; i < users_raw.length; i++)
  594. {
  595. if (ReadMode == 0) //searching
  596. {
  597. if (users_raw[i].contains("---"))
  598. ReadMode = 1;
  599. } else if (ReadMode == 1)
  600. {
  601. if (users_raw[i].toLowerCase().contains("the command completed"))
  602. ReadMode = 2;
  603. else
  604. {
  605. String[] UserList = users_raw[i].split("\\s+");
  606. for (String user : UserList)
  607. {
  608. String[] userout = Env_GetCommandOutput("net", "user " + user);
  609.  
  610. if (userout.length < 1 || userout[0].contains("could not be"))
  611. continue;
  612. UserInfo NewUser = new UserInfo();
  613. NewUser.Name = user.toLowerCase();
  614.  
  615. for (String s : userout)
  616. {
  617. if (s.toLowerCase().contains("administrator"))
  618. NewUser.IsAdmin = true;
  619. }
  620.  
  621. users.add(NewUser);
  622. }
  623. }
  624. } else
  625. {
  626. break;
  627. }
  628. }
  629.  
  630. return users.toArray(new UserInfo[users.size()]);
  631. }
  632.  
  633. private String[] Env_GetCommandOutput(String command, String args)
  634. {
  635. ProcessBuilder processBuilder = null;
  636. if(args.isEmpty())
  637. processBuilder = new ProcessBuilder(command);
  638. else
  639. processBuilder = new ProcessBuilder(command, args);
  640.  
  641. processBuilder.redirectErrorStream(true);
  642.  
  643. Process p = null;
  644.  
  645. try
  646. {
  647. p = processBuilder.start();
  648. } catch (Exception e)
  649. {
  650. return new String[0];
  651. }
  652.  
  653. java.io.BufferedReader reader =
  654. new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
  655.  
  656. ArrayList<String> lines = new ArrayList<>();
  657.  
  658. String line = null;
  659.  
  660. try
  661. {
  662. while ((line = reader.readLine()) != null)
  663. {
  664. lines.add(line);
  665. }
  666. } catch (Exception e)
  667. {
  668. return new String[0];
  669. }
  670.  
  671. return lines.toArray(new String[lines.size()]);
  672. }
  673.  
  674. private UserInfo[] GetLinuxUsers()
  675. {
  676. String[] users_raw = Env_GetCommandOutput("awk -F\'[/:]\' \'{if ($3 >= 1000 && $3 != 65534) print $1}\' /etc/passwd", "");
  677.  
  678. ArrayList<UserInfo> users = new ArrayList<>();
  679.  
  680. for (String u : users_raw)
  681. {
  682. UserInfo userInfo = new UserInfo();
  683. userInfo.Name = u;
  684.  
  685. String[] Info = Env_GetCommandOutput("groups", u);
  686.  
  687. for(String sub_info : Info)
  688. {
  689. if(sub_info.contains("sudo"))
  690. userInfo.IsAdmin = true;
  691. }
  692.  
  693. users.add(userInfo);
  694. }
  695.  
  696. return users.toArray(new UserInfo[users.size()]);
  697. }
  698.  
  699. private UserInfo[] GetCyberPatriotUsers(String fname) //TODO:
  700. {
  701. ArrayList<UserInfo> users = new ArrayList<>(); //List to return
  702.  
  703. try
  704. {
  705. java.util.Scanner lines = new java.util.Scanner(new File(fname));
  706.  
  707. while(true)
  708. {
  709. String line = lines.nextLine();
  710. if(line.isEmpty())
  711. continue;
  712. //Steps:
  713. //1. Determine if you are reading an admin or a normal user
  714. //2. Read only the user name from the line
  715. //3. Add a userinfo object to the list provided
  716. }
  717.  
  718. }
  719. catch(java.util.NoSuchElementException e)
  720. {
  721. //4. return the users collected
  722. }
  723. catch(FileNotFoundException e)
  724. {
  725. return new UserInfo[0]; //file wasnt found, returning empty list
  726. }
  727. return new UserInfo[0];
  728. }
  729.  
  730. private class UserInfo
  731. {
  732. public String Name;
  733. public boolean IsAdmin;
  734. public String ActionTaken;
  735. }
  736. }
  737.  
  738. //endregion
  739.  
  740.  
  741. //region CORE
  742. public static boolean IsWindows = true;
  743.  
  744. public ArrayList<CommandBase> Commands;
  745.  
  746. public abstract class CommandBase
  747. {
  748. public String Command = "none";
  749. public String Description = "invalid command";
  750.  
  751. public abstract void Windows(String[] args);
  752.  
  753. public abstract void Linux(String[] args);
  754.  
  755. public void ExecuteCommand(String[] args)
  756. {
  757. if (CommandClass.IsWindows)
  758. Windows(args);
  759. else
  760. Linux(args);
  761. }
  762.  
  763. public CommandBase(String cmd, String desc)
  764. {
  765. Command = cmd;
  766. Description = desc;
  767. }
  768.  
  769. @Override
  770. public String toString()
  771. {
  772. return Command;
  773. }
  774.  
  775. }
  776.  
  777. public void AddCommand(CommandBase command)
  778. {
  779. if (Commands == null)
  780. Commands = new ArrayList<>();
  781. Commands.add(command);
  782. Commands.sort(new CommandComparator());
  783. }
  784.  
  785. private class CommandComparator implements Comparator<CommandBase>
  786. {
  787.  
  788. @Override
  789. public int compare(CommandBase o1, CommandBase o2)
  790. {
  791. return o1.toString().toLowerCase().compareTo(o2.toString().toLowerCase());
  792. }
  793. }
  794. //endregion
  795. }
Add Comment
Please, Sign In to add comment