static void Main(string[] args) { //Hardcode our AES variables. Bad idea, but useful for non-interactive staging and demonstrates the principle. string key = "zFNCf6AvQI7NCQrx43x6mIVmYYdZ+O1n+8NL29+IhEo="; string iv = "AxnDDpUzglq9HkkqbjNEnA=="; //Alternatively, get the AES variables as command-line arguments. //string key = args[0]; //string iv = args[1]; //Construct a struct to wrap the packed payload. EasyNetResult packed; //Save the AES variables. packed.key = key; packed.iv = iv; //Get the packed blob from the embedded resource packed.blob = Properties.Resources.HelloWorldPacked; //Payload could be embedded as a string //packed.blob = "OE/j...hl9L4"; //Payload could be passed in at runtime as a command-line argument. //This is most flexible, but least reliable. //packed.blob = args[2]; //Payload could also be retrieved from an URL (a la SharpCradle) /*MemoryStream stream = new MemoryStream(); * WebClient client = new WebClient(); * * //Access web and read the bytes from the binary file * System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12; * stream = new MemoryStream(client.DownloadData(url); * BinaryReader reader = new BinaryReader(stream); * byte[] bytes = reader.ReadBytes(Convert.ToInt32(stream.Length)); * stream.Close(); * reader.Close(); * packed.blob = System.Text.Encoding.ASCII.GetString(bytes)*/ //Unpack the payload. byte[] payloadBytes = EasyNetPacker.Unpack(packed); //Load the assembly payload from memory using the Reflection API Assembly asm = Assembly.Load(payloadBytes); //Dynamically infer the entry point of the patload MethodInfo entry = asm.EntryPoint; //Specify any command-line arguments for the payload. string[] payloadArgs = { }; //Set up the function signature of the Main entry point. object[] entryPointArgs = { payloadArgs }; //Invoke the entry point (run the payload) from memory. entry.Invoke(null, entryPointArgs); }
/// <summary> /// Entry point. /// </summary> /// <param name="args">Command-line arguments</param> static void Main(string[] args) { if (args.Length > 0) { if (args[0] == "--pack") { //Check if we have the correct number of command-line arguments if (args.Length < 2) { Console.WriteLine("Error: Invalid number of command-line arguments."); //Exit with error Environment.Exit(1); } else { try { //Pack the file and get the results wrapped with the data necessary to unpack it. EasyNetResult packed = EasyNetPacker.Pack(File.ReadAllBytes(args[1])); //No output file specified if (args.Length == 2) { //Write the packed data to stdout Console.Write(packed.blob); } else { //Write the packed data to the output file. File.WriteAllBytes(args[2], Encoding.ASCII.GetBytes(packed.blob)); } //Formatting Console.Error.WriteLine(""); //Provide the user with the AES variables Console.Error.WriteLine("AES Key: {0}", packed.key); Console.Error.WriteLine("AES IV: {0}", packed.iv); } catch (CryptographicException except) { Console.WriteLine("Cryptographic Error: {0}", except.Message + "\n"); Environment.Exit(1); } catch (FormatException except) { Console.WriteLine("Format Error: {0}", except.Message + "\n"); Environment.Exit(1); } catch (FileNotFoundException except) { Console.WriteLine("File Error: {0}", except.Message + "\n"); Environment.Exit(1); } } } else if (args[0] == "--unpack") { //Check if we have the correct number of command-line arguments if (args.Length < 4) { Console.WriteLine("Error: Invalid number of command-line arguments."); //Exit with error Environment.Exit(1); } //Correct number of command-line arguments else { //Wrap in a try block in case base64 conversion or unpacking fails. try { //Create the struct that wraps the packed data with the data necessary to unpack it. EasyNetResult packed = new EasyNetResult(Encoding.ASCII.GetString(File.ReadAllBytes(args[3])), args[1], args[2]); //No output file specified if (args.Length == 4) { //Write the packed data to stdout Console.Write(Encoding.ASCII.GetString(EasyNetPacker.Unpack(packed))); } else { //Unpack the data and write the result to the output file. File.WriteAllBytes(args[4], EasyNetPacker.Unpack(packed)); Console.Error.WriteLine("\nUnpacked to " + args[4] + "."); } } catch (CryptographicException except) { Console.WriteLine("Cryptographic Error: {0}", except.Message + "\n"); Environment.Exit(1); } catch (FormatException except) { Console.WriteLine("Format Error: {0}", except.Message + "\n"); Environment.Exit(1); } catch (FileNotFoundException except) { Console.WriteLine("File Error: {0}", except.Message + "\n"); Environment.Exit(1); } } } //The user asked for the usage else if (args[0] == "--help" || args[0] == "-h" || args[0] == "/?") { //Print the usage PrintUsage(); //Exit Environment.Exit(0); } else { Console.WriteLine("Error: Invalid number of command-line arguments."); //Print the usage PrintUsage(); //Exit Environment.Exit(0); } } else { //Print the usage PrintUsage(); //Exit Environment.Exit(0); } }//end method