Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.io.filefilter.DirectoryFileFilter;
- import org.apache.commons.io.filefilter.RegexFileFilter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.nio.charset.Charset;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Comparator;
- class CommandClass
- {
- //Add your commands here
- public void Initialize()
- {
- AddCommand(new ExitCommand());
- AddCommand(new PlatformCommand());
- AddCommand(new find_files());
- AddCommand(new HelloCommand());
- AddCommand(new addcommand());
- AddCommand(new subcommand());
- AddCommand(new multcommand());
- //AddCommand(new user_recon());
- AddCommand(new user_recon_template());
- }
- //region Add your classes here
- private class ExitCommand extends CommandBase
- {
- public ExitCommand()
- {
- super("exit", "Exit environment");
- }
- public void Windows(String[] args)
- {
- System.exit(0);
- }
- public void Linux(String[] args)
- {
- Windows(args);
- }
- }
- private class PlatformCommand extends CommandBase
- {
- public PlatformCommand()
- {
- super("platform", "args W or L");
- }
- public void Windows(String[] args)
- {
- String arg = args[0].trim().toLowerCase();
- if (args.length != 1 || (!arg.equals("w") && !arg.equals("l")))
- {
- System.out.println("Invalid Arguments provided! Expected W or L but got '" + arg.toUpperCase() + "'");
- return;
- }
- CommandClass.IsWindows = args[0].trim().toLowerCase().equals("w");
- }
- public void Linux(String[] args)
- {
- Windows(args);
- }
- }
- private class HelloCommand extends CommandBase
- {
- public HelloCommand()
- {
- super("Hello", "Prints Hello World");
- }
- public void Windows(String[] args)
- {
- System.out.println("Hello World");
- }
- public void Linux(String[] args)
- {
- Windows(args);
- }
- }
- private class addcommand extends CommandBase
- {
- public addcommand()
- {
- super("add", "adds numbers");
- }
- public void Windows(String[] args)
- {
- int a, b;
- if (args.length != 2)
- {
- System.out.println("wrong amount of args");
- return;
- }
- try
- {
- a = Integer.valueOf(args[0]);
- b = Integer.valueOf(args[1]);
- } catch (Exception e)
- {
- System.out.println("invalid argument");
- return;
- }
- System.out.println(a + " + " + b + " = " + (a + b));
- }
- public void Linux(String[] args)
- {
- Windows(args);
- }
- }
- private class subcommand extends CommandBase
- {
- public subcommand()
- {
- super("subtract", "subtracts numbers");
- }
- public void Windows(String[] args)
- {
- int a, b;
- if (args.length != 2)
- {
- System.out.println("wrong amount of args");
- return;
- }
- try
- {
- a = Integer.valueOf(args[0]);
- b = Integer.valueOf(args[1]);
- } catch (Exception e)
- {
- System.out.println("invalid argument");
- return;
- }
- System.out.println(a + " - " + b + " = " + (a - b));
- }
- public void Linux(String[] args)
- {
- Windows(args);
- }
- }
- private class multcommand extends CommandBase
- {
- public multcommand()
- {
- super("multiply", "multiplies numbers");
- }
- public void Windows(String[] args)
- {
- int a, b;
- if (args.length != 2)
- {
- System.out.println("wrong amount of args");
- return;
- }
- try
- {
- a = Integer.valueOf(args[0]);
- b = Integer.valueOf(args[1]);
- } catch (Exception e)
- {
- System.out.println("invalid argument");
- return;
- }
- System.out.println(a + " * " + b + " = " + (a * b));
- }
- public void Linux(String[] args)
- {
- Windows(args);
- }
- }
- private class find_files extends CommandBase
- {
- public find_files()
- {
- super("find-files", "<dir> <ext,...> <outpath?>");
- }
- public void Windows(String[] args)
- {
- if (args.length < 2 || args.length > 3)
- {
- System.out.println("Bad arguments. Please enter <dir> <regex>");
- return;
- }
- File f = new File(args[0]);
- if (!f.exists())
- {
- System.out.println("Directory: " + args[0] + " doesn't exist!");
- return;
- }
- String regex = ".*\\.(";
- String[] split = (args[1] == null ? "" : args[1]).split(",");
- for (int i = 0; i < split.length; i++)
- {
- regex += split[i] + (i + 1 == split.length ? ")" : "|");
- }
- Collection<File> files = null;
- try
- {
- files = FileUtils.listFiles(
- new File(args[0]),
- new RegexFileFilter(regex),
- DirectoryFileFilter.DIRECTORY
- );
- } catch (java.util.regex.PatternSyntaxException e)
- {
- System.out.println("Bad regex!");
- return;
- }
- String result = "";
- for (File file : files)
- {
- result += file.getAbsolutePath() + "\n";
- }
- if (args.length == 3)
- {
- try
- {
- FileUtils.writeStringToFile(new File(args[2]), result, Charset.defaultCharset());
- } catch (Exception e)
- {
- System.out.println("Failed to save file...");
- return;
- }
- System.out.println("File saved.");
- } else
- {
- System.out.println(result);
- }
- }
- public void Linux(String[] args)
- {
- Windows(args);
- }
- }
- private class user_recon extends CommandBase
- {
- public user_recon()
- {
- super("user-recon", "<input>");
- }
- private boolean IsWin = true;
- public void Windows(String[] args)
- {
- if (args.length != 1)
- {
- System.out.println("Bad arguments");
- return;
- }
- if (!new File(args[0]).exists())
- {
- System.out.println("Input file doesn't exist!!!");
- return;
- }
- ArrayList<UserInfo> Actions = new ArrayList<>();
- UserInfo[] u_info = IsWin ? GetWindowsUsers() : GetLinuxUsers();
- UserInfo[] cp_info = GetCyberPatriotUsers(args[0]);
- for (UserInfo u : u_info)
- {
- UserInfo match = null;
- for (UserInfo c : cp_info)
- {
- if (c.Name.toLowerCase().equals(u.Name.toLowerCase()))
- {
- match = c;
- break;
- }
- }
- if (match == null)
- {
- u.ActionTaken = "delete user";
- Actions.add(u);
- } else
- {
- if (match.IsAdmin != u.IsAdmin)
- {
- u.ActionTaken = (match.IsAdmin && !u.IsAdmin) ?
- "Elevate to administrator" :
- "Change to normal user";
- Actions.add(u);
- }
- }
- }
- for (UserInfo c : cp_info)
- {
- UserInfo match = null;
- for (UserInfo u : u_info)
- {
- if (c.Name.toLowerCase().equals(u.Name.toLowerCase()))
- {
- match = c;
- break;
- }
- }
- if (match == null)
- {
- c.ActionTaken = "Add user as " + (c.IsAdmin ? "an administrator" : "a normal user");
- Actions.add(c);
- }
- }
- for (UserInfo u : Actions)
- {
- if (u.ActionTaken == null)
- {
- u.ActionTaken = "unknown action";
- }
- System.out.println("User <" + u.Name + "> : Action <" + u.ActionTaken + ">");
- }
- }
- public void Linux(String[] args)
- {
- IsWin = false;
- Windows(args);
- }
- private UserInfo[] GetWindowsUsers()
- {
- String[] users_raw = Env_GetCommandOutput("net", "user");
- ArrayList<UserInfo> users = new ArrayList<>();
- byte ReadMode = 0;
- for (int i = 0; i < users_raw.length; i++)
- {
- if (ReadMode == 0) //searching
- {
- if (users_raw[i].contains("---"))
- ReadMode = 1;
- } else if (ReadMode == 1)
- {
- if (users_raw[i].toLowerCase().contains("the command completed"))
- ReadMode = 2;
- else
- {
- String[] UserList = users_raw[i].split("\\s+");
- for (String user : UserList)
- {
- String[] userout = Env_GetCommandOutput("net", "user " + user);
- if (userout.length < 1 || userout[0].contains("could not be"))
- continue;
- UserInfo NewUser = new UserInfo();
- NewUser.Name = user.toLowerCase();
- for (String s : userout)
- {
- if (s.toLowerCase().contains("administrator"))
- NewUser.IsAdmin = true;
- }
- users.add(NewUser);
- }
- }
- } else
- {
- break;
- }
- }
- return users.toArray(new UserInfo[users.size()]);
- }
- private String[] Env_GetCommandOutput(String command, String args)
- {
- ProcessBuilder processBuilder = null;
- if(args.isEmpty())
- processBuilder = new ProcessBuilder(command);
- else
- processBuilder = new ProcessBuilder(command, args);
- processBuilder.redirectErrorStream(true);
- Process p = null;
- try
- {
- p = processBuilder.start();
- } catch (Exception e)
- {
- return new String[0];
- }
- java.io.BufferedReader reader =
- new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
- ArrayList<String> lines = new ArrayList<>();
- String line = null;
- try
- {
- while ((line = reader.readLine()) != null)
- {
- lines.add(line);
- }
- } catch (Exception e)
- {
- return new String[0];
- }
- return lines.toArray(new String[lines.size()]);
- }
- private UserInfo[] GetLinuxUsers()
- {
- String[] users_raw = Env_GetCommandOutput("awk -F\'[/:]\' \'{if ($3 >= 1000 && $3 != 65534) print $1}\' /etc/passwd", "");
- ArrayList<UserInfo> users = new ArrayList<>();
- for (String u : users_raw)
- {
- UserInfo userInfo = new UserInfo();
- userInfo.Name = u;
- String[] Info = Env_GetCommandOutput("groups", u);
- for(String sub_info : Info)
- {
- if(sub_info.contains("sudo"))
- userInfo.IsAdmin = true;
- }
- users.add(userInfo);
- }
- return users.toArray(new UserInfo[users.size()]);
- }
- private UserInfo[] GetCyberPatriotUsers(String fname)
- {
- ArrayList<UserInfo> users = new ArrayList<>();
- try
- {
- java.util.Scanner lines = new java.util.Scanner(new File(fname));
- byte ReadMode = 0;
- while (true)
- {
- String line = lines.nextLine();
- if (line.isEmpty())
- continue;
- if (line.trim().toLowerCase().contains("authorized administrators"))
- {
- ReadMode = 1;
- continue;
- } else if (line.trim().toLowerCase().contains("authorized users"))
- {
- ReadMode = 2;
- continue;
- }
- if (ReadMode == 1)
- {
- if (Character.isDigit(line.charAt(0)) || Character.isLetter(line.charAt(0)))
- {
- UserInfo u = new UserInfo();
- u.Name = "";
- for (int i = 0; i < line.length(); i++)
- {
- if (Character.isDigit(line.charAt(i)) || Character.isLetter(line.charAt(i)))
- u.Name += line.charAt(i);
- else
- break;
- }
- u.IsAdmin = true;
- users.add(u);
- }
- } else if (ReadMode == 2)
- {
- if (Character.isDigit(line.charAt(0)) || Character.isLetter(line.charAt(0)))
- {
- UserInfo u = new UserInfo();
- u.Name = "";
- for (int i = 0; i < line.length(); i++)
- {
- if (Character.isDigit(line.charAt(i)) || Character.isLetter(line.charAt(i)))
- u.Name += line.charAt(i);
- else
- break;
- }
- u.IsAdmin = false;
- users.add(u);
- }
- }
- }
- } catch (java.util.NoSuchElementException e)
- {
- UserInfo[] ui = new UserInfo[users.size()];
- return users.toArray(ui);
- } catch (FileNotFoundException e)
- {
- return new UserInfo[0];
- }
- }
- private class UserInfo
- {
- public String Name;
- public boolean IsAdmin;
- public String ActionTaken;
- }
- }
- private class user_recon_template extends CommandBase
- {
- public user_recon_template()
- {
- super("user-recon","<input>");
- }
- public void Windows(String[] args) //TODO:
- {
- //1. Must have 1 argument
- //2. Must have a valid argument (file exists)
- //3. Create a list to hold UserInfo objects that need to change
- //4. Get all users on current platform
- //5. Get all users in the list of users
- //6. Compare users in lists to achieve the following goals (hint: use ActionTaken to track what is wrong)
- //6.1. Users that shouldnt exist are added to your list
- //6.2. Users that don't have proper privileges should be added to your list
- //6.3. Users that need to be created should be added to your list
- //7. Print all the users and actions required to the screen
- }
- public void Linux(String[] args) //TODO:
- {
- //hint: make a variable to determine if the platform is windows and call the windows function
- }
- private UserInfo[] GetWindowsUsers()
- {
- String[] users_raw = Env_GetCommandOutput("net", "user");
- ArrayList<UserInfo> users = new ArrayList<>();
- byte ReadMode = 0;
- for (int i = 0; i < users_raw.length; i++)
- {
- if (ReadMode == 0) //searching
- {
- if (users_raw[i].contains("---"))
- ReadMode = 1;
- } else if (ReadMode == 1)
- {
- if (users_raw[i].toLowerCase().contains("the command completed"))
- ReadMode = 2;
- else
- {
- String[] UserList = users_raw[i].split("\\s+");
- for (String user : UserList)
- {
- String[] userout = Env_GetCommandOutput("net", "user " + user);
- if (userout.length < 1 || userout[0].contains("could not be"))
- continue;
- UserInfo NewUser = new UserInfo();
- NewUser.Name = user.toLowerCase();
- for (String s : userout)
- {
- if (s.toLowerCase().contains("administrator"))
- NewUser.IsAdmin = true;
- }
- users.add(NewUser);
- }
- }
- } else
- {
- break;
- }
- }
- return users.toArray(new UserInfo[users.size()]);
- }
- private String[] Env_GetCommandOutput(String command, String args)
- {
- ProcessBuilder processBuilder = null;
- if(args.isEmpty())
- processBuilder = new ProcessBuilder(command);
- else
- processBuilder = new ProcessBuilder(command, args);
- processBuilder.redirectErrorStream(true);
- Process p = null;
- try
- {
- p = processBuilder.start();
- } catch (Exception e)
- {
- return new String[0];
- }
- java.io.BufferedReader reader =
- new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
- ArrayList<String> lines = new ArrayList<>();
- String line = null;
- try
- {
- while ((line = reader.readLine()) != null)
- {
- lines.add(line);
- }
- } catch (Exception e)
- {
- return new String[0];
- }
- return lines.toArray(new String[lines.size()]);
- }
- private UserInfo[] GetLinuxUsers()
- {
- String[] users_raw = Env_GetCommandOutput("awk -F\'[/:]\' \'{if ($3 >= 1000 && $3 != 65534) print $1}\' /etc/passwd", "");
- ArrayList<UserInfo> users = new ArrayList<>();
- for (String u : users_raw)
- {
- UserInfo userInfo = new UserInfo();
- userInfo.Name = u;
- String[] Info = Env_GetCommandOutput("groups", u);
- for(String sub_info : Info)
- {
- if(sub_info.contains("sudo"))
- userInfo.IsAdmin = true;
- }
- users.add(userInfo);
- }
- return users.toArray(new UserInfo[users.size()]);
- }
- private UserInfo[] GetCyberPatriotUsers(String fname) //TODO:
- {
- ArrayList<UserInfo> users = new ArrayList<>(); //List to return
- try
- {
- java.util.Scanner lines = new java.util.Scanner(new File(fname));
- while(true)
- {
- String line = lines.nextLine();
- if(line.isEmpty())
- continue;
- //Steps:
- //1. Determine if you are reading an admin or a normal user
- //2. Read only the user name from the line
- //3. Add a userinfo object to the list provided
- }
- }
- catch(java.util.NoSuchElementException e)
- {
- //4. return the users collected
- }
- catch(FileNotFoundException e)
- {
- return new UserInfo[0]; //file wasnt found, returning empty list
- }
- return new UserInfo[0];
- }
- private class UserInfo
- {
- public String Name;
- public boolean IsAdmin;
- public String ActionTaken;
- }
- }
- //endregion
- //region CORE
- public static boolean IsWindows = true;
- public ArrayList<CommandBase> Commands;
- public abstract class CommandBase
- {
- public String Command = "none";
- public String Description = "invalid command";
- public abstract void Windows(String[] args);
- public abstract void Linux(String[] args);
- public void ExecuteCommand(String[] args)
- {
- if (CommandClass.IsWindows)
- Windows(args);
- else
- Linux(args);
- }
- public CommandBase(String cmd, String desc)
- {
- Command = cmd;
- Description = desc;
- }
- @Override
- public String toString()
- {
- return Command;
- }
- }
- public void AddCommand(CommandBase command)
- {
- if (Commands == null)
- Commands = new ArrayList<>();
- Commands.add(command);
- Commands.sort(new CommandComparator());
- }
- private class CommandComparator implements Comparator<CommandBase>
- {
- @Override
- public int compare(CommandBase o1, CommandBase o2)
- {
- return o1.toString().toLowerCase().compareTo(o2.toString().toLowerCase());
- }
- }
- //endregion
- }
Add Comment
Please, Sign In to add comment