//------------------------------------------------------------------------- // // Copyright (c) Intel Corporation, 2011 - 2014 All Rights Reserved. // // File: SystemDefenseSample.cs // // Contents: Sample code for Intel(R) Active Management Technology // (IntelĀ® AMT) SystemDefense Sample. // // Notes: This sample demonstrates how to use various commands of // the SystemDefense and CBHeuristic services over WSMan. // //------------------------------------------------------------------------- using System; using System.Runtime.InteropServices; using Utils; namespace SystemDefense { class SystemDefense_Sample { #region CONSTANTS // Command line arguments private const string OPT_CREATE_POLICY = "createpolicy"; private const string OPT_CONFIG_HEURISTIC_TO_POLICY = "configheuristic"; private const string OPT_REMOVE_HEURISTIC_FROM_POLICY = "removeheuristic"; private const string OPT_ENABLE_HEURISTIC_SETTINGS = "enableheuristic"; private const string OPT_DISABLE_HEURISTIC_SETTINGS = "disableheuristic"; private const string OPT_DISABLE_POLICY = "disablepolicy"; private const string OPT_DELETE_POLICY = "deletepolicy"; private const string OPT_PRINT_AVAILABLE_POLICIES = "printpolicies"; private const string OPT_API = "api"; private const string OPT_API_HEURISTIC = "apiheuristic"; // Exit Codes Types private enum exitCodes { EXIT_SUCCESS = 0, EXIT_FAILURE, EXIT_USAGE, EXIT_COMMUNICATION_ERROR, EXIT_ARGUMENT_ERROR } #endregion CONSTANTS #region PRIVATE_DATA_MEMBERS // User parameters private static CmdLineArguments Params = new CmdLineArguments(); #endregion #region MAIN [DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetDefaultDllDirectories(int directoryFlags); static int Main(string[] args) { // set default dll lookup directory to system SetDefaultDllDirectories(0x00000800); //LOAD_LIBRARY_SEARCH_SYSTEM32 exitCodes exitCode = exitCodes.EXIT_SUCCESS; //SystemDefense_Sample service = null; SystemDefense_Api api = null; #region INIT_FUNCTIONS // Add command line argument options. Params.init_functions(); // Add options to activate. Params.AddArg(OPT_CREATE_POLICY, false, false, "Create and enable a policy"); Params.AddArg(OPT_CONFIG_HEURISTIC_TO_POLICY, false, false, "Add heuristic to a policy"); Params.AddArg(OPT_REMOVE_HEURISTIC_FROM_POLICY, false, false, "Remove heuristic from a policy"); Params.AddArg(OPT_ENABLE_HEURISTIC_SETTINGS, false, false, "Enable heuristic packet filter settings"); Params.AddArg(OPT_DISABLE_HEURISTIC_SETTINGS, false, false, "Disable heuristic packet filter settings"); Params.AddArg(OPT_DISABLE_POLICY, false, false, "Disable the policy"); Params.AddArg(OPT_DELETE_POLICY, false, false, "Delete a policy"); Params.AddArg(OPT_PRINT_AVAILABLE_POLICIES, false, false, "Print all available policies"); Params.AddArg(OPT_API, false, false, "Run an API example"); Params.AddArg(OPT_API_HEURISTIC, false, false, "Run an API example with heuristic commands"); #endregion try { string assembly = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; string usage = string.Empty; string additionalData = "Notes:"; additionalData += "\n1. All Heuristics options are not supported on wireless interfaces."; additionalData += "\n2. -" + OPT_CREATE_POLICY + " option should be used before using -" + OPT_CONFIG_HEURISTIC_TO_POLICY + " or -" + OPT_REMOVE_HEURISTIC_FROM_POLICY + " options."; additionalData += "\n3. In order to successfully configure heuristic, the heuristic packet filter settings must be enabled."; additionalData += "\n Use -" + OPT_ENABLE_HEURISTIC_SETTINGS + " option to enable the heuristic packet filter settings."; additionalData += "\n Important: using -" + OPT_ENABLE_HEURISTIC_SETTINGS + " or -" + OPT_DISABLE_HEURISTIC_SETTINGS + " options will overwrite the existing heuristic settings."; // If no params were given, just print the usage. if (args.Length == 0) { usage = Params.CreateUsage(assembly, additionalData, true, true); Console.WriteLine("\n" + usage); return (int)exitCodes.EXIT_SUCCESS; } try { // Verify command line arguments. Params.Parse(args); } catch (Exception e) { Console.WriteLine(e.Message); Params.CreateUsage(assembly, additionalData, false, true); return 0; } //Create the Wsman Connection Object based on TLS/Non-TLS option. if (Params.Selected(CmdLineArguments.OPT_SECURE) == false) { api = new SystemDefense_Api(Params[CmdLineArguments.OPT_HOST], Params[CmdLineArguments.OPT_USER], Params[CmdLineArguments.OPT_PASS], Params.Selected(CmdLineArguments.OPT_KRB), Params.GetWebProxy(), Params.Selected(CmdLineArguments.ACCEPT_SELF_SIGNED_CERTIFICATE)); } else { api = new SystemDefense_Api(Params[CmdLineArguments.OPT_HOST], Params[CmdLineArguments.OPT_USER], Params[CmdLineArguments.OPT_PASS], Params[CmdLineArguments.OPT_CERT], Params.Selected(CmdLineArguments.OPT_KRB), Params.GetWebProxy(), Params.Selected(CmdLineArguments.ACCEPT_SELF_SIGNED_CERTIFICATE)); } // Executing users choices. bool verbose = Params.Selected(CmdLineArguments.OPT_VERBOSE); //Check for only 1 parameter. switch (args[0].Trim('-')) { //Call Consent Status. case OPT_CREATE_POLICY: api.EnableSystemDefencePolicy(api.CreatePolicy(verbose), Params.Selected(CmdLineArguments.OPT_WIRELESS)); break; case OPT_DISABLE_POLICY: api.DisablePolicy(verbose, Params.Selected(CmdLineArguments.OPT_WIRELESS)); break; //Notes: // 1. OPT_CREATE_POLICY option should be used before using this option. // 2. In the event that the heuristic packet filter settings are disabled, OPT_ENABLE_HEURISTIC_SETTINGS // option should be used before using this option. case OPT_CONFIG_HEURISTIC_TO_POLICY: api.ConfigureHeuristic(verbose); break; case OPT_REMOVE_HEURISTIC_FROM_POLICY: //Note: OPT_CREATE_POLICY option should be used before using this option. api.RemoveHeuristic(verbose); break; case OPT_ENABLE_HEURISTIC_SETTINGS: api.EnableHeuristic(verbose); break; case OPT_DISABLE_HEURISTIC_SETTINGS: api.DisableHeuristic(verbose); break; case (OPT_DELETE_POLICY): api.DeletePolicy(verbose); break; case (OPT_PRINT_AVAILABLE_POLICIES): api.PrintAvailablePolicies(Params.Selected(CmdLineArguments.OPT_WIRELESS)); break; case (OPT_API): api.ApiFlow(verbose, false, Params.Selected(CmdLineArguments.OPT_WIRELESS)); break; case (OPT_API_HEURISTIC): api.ApiFlow(verbose, true, Params.Selected(CmdLineArguments.OPT_WIRELESS)); break; default: Console.WriteLine("Could not find a value for the option."); break; } //end switch case } catch (Exception e) { //Check for the Type of Exception created during execution. exitCode = (exitCodes)Params.catchType(e, null); } finally { api?.Dispose(); } return (int)exitCode; } #endregion MAIN } }