Created
April 21, 2015 03:16
-
-
Save Ethan-Bierlein/d04fd4c2ad9c520a2d31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
namespace HackTheAdmin | |
{ | |
/// <summary> | |
/// Utilities that HackTheAdmin needs. | |
/// </summary> | |
class Util | |
{ | |
/// <summary> | |
/// Generates an 8 character string. | |
/// </summary> | |
/// <returns>8 Character long string.</returns> | |
public static string GenerateRandomString() | |
{ | |
string characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | |
Random random = new Random(); | |
string result = new string(Enumerable.Repeat(characters, 8).Select(s => s[random.Next(s.Length)]).ToArray()); | |
return result; | |
} | |
/// <summary> | |
/// Encodes a provided string. | |
/// </summary> | |
/// <param name="stringToEncode">String to "encode".</param> | |
/// <returns>An encoded string.</returns> | |
public static string EncodeString(string stringToEncode) | |
{ | |
string encodedString = ""; | |
foreach (char character in stringToEncode) | |
encodedString += System.Convert.ToString(System.Convert.ToInt32(character) + 1); | |
return encodedString; | |
} | |
/// <summary> | |
/// Tokenize command input from the user. | |
/// </summary> | |
/// <param name="commandInput">User's command input.</param> | |
/// <returns>A tokenized List<string> in the form of { keyword, args, ... }</returns> | |
public static List<string> TokenizeCommand(string commandInput) | |
{ | |
List<string> tokenizedInput = new List<string>(); | |
string[] splitString = commandInput.Split(new Char[] {' ', ':'}); | |
return tokenizedInput; | |
} | |
} | |
/// <summary> | |
/// This class stores certain data that | |
/// would be associated with a file. | |
/// </summary> | |
class EnvironmentFile | |
{ | |
public string filename { get; set; } | |
public string fileContents { get; set; } | |
/// <summary> | |
/// Initalize a file. | |
/// </summary> | |
/// <param name="filename">File's name.</param> | |
/// <param name="fileContents">Contents of the file.</param> | |
public EnvironmentFile(string filename, string fileContents) | |
{ | |
this.filename = filename; | |
this.fileContents = fileContents; | |
} | |
} | |
/// <summary> | |
/// The user class manags aspects of the user | |
/// such as access level, password, name, etc. | |
/// </summary> | |
class User | |
{ | |
public string username { get; set; } | |
public string password { get; set; } | |
public int accessLevel { get; set; } | |
/// <summary> | |
/// Initalizes a user. | |
/// </summary> | |
/// <param name="username">User's username.</param> | |
/// <param name="password">User's password.</param> | |
/// <param name="accessLevel">User's access level.</param> | |
public User(string username, string password, int accessLevel) | |
{ | |
this.username = username; | |
this.password = password; | |
this.accessLevel = accessLevel; | |
} | |
} | |
/// <summary> | |
/// The environment class manages things such as | |
/// program data and running commands. | |
/// </summary> | |
class Environment | |
{ | |
public List<User> environmentUsers { get; set; } | |
public List<EnvironmentFile> fileData { get; set; } | |
/// <summary> | |
/// Initalizes an environment. | |
/// </summary> | |
/// <param name="environmentUsers">Users in the environment.</param> | |
/// <param name="fileData">Files in the environment.</param> | |
public Environment(List<User> environmentUsers, List<EnvironmentFile> fileData) | |
{ | |
this.environmentUsers = environmentUsers; | |
this.fileData = fileData; | |
} | |
/// <summary> | |
/// Generates a "random" environment for the user | |
/// to use. | |
/// </summary> | |
/// <returns>A randomly generated environment.</returns> | |
public static Environment GenerateEnvironment() | |
{ | |
Random random = new Random(); | |
List<User> userList = new List<User>(); | |
List<EnvironmentFile> fileList = new List<EnvironmentFile>(); | |
// Generate the users | |
User guestUser = new User("Guest", "password", 1); | |
User adminUser1 = new User("AdminUser1", Util.GenerateRandomString(), 2); | |
User adminUser2 = new User("AdminUser2", Util.GenerateRandomString(), 3); | |
User adminUser3 = new User("AdminUser3", Util.GenerateRandomString(), 4); | |
User rootUser = new User("RootUser", Util.GenerateRandomString(), 5); | |
userList.Add(guestUser); | |
userList.Add(adminUser1); | |
userList.Add(adminUser2); | |
userList.Add(adminUser3); | |
userList.Add(rootUser); | |
// Generate a couple of inital files | |
string encodedPasswordsContent = String.Format( | |
"\n {0} \n {1} \n {2} \n {3} \n", | |
Util.EncodeString(adminUser1.password), | |
Util.EncodeString(adminUser2.password), | |
Util.EncodeString(adminUser3.password), | |
Util.EncodeString(rootUser.password) | |
); | |
EnvironmentFile encodedPasswords = new EnvironmentFile("Passwords", encodedPasswordsContent); | |
EnvironmentFile systemInfo = new EnvironmentFile("SystemInfo", ""); | |
fileList.Add(encodedPasswords); | |
fileList.Add(systemInfo); | |
return new Environment(userList, fileList); | |
} | |
} | |
/// <summary> | |
/// Main class where HackTheAdmin is run. | |
/// </summary> | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment