342 lines
15 KiB
C#
342 lines
15 KiB
C#
//----------------------------------------------------------------------------
|
|
//
|
|
// Copyright (c) Intel Corporation, 2011 - 2015 All Rights Reserved.
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Intel.Manageability;
|
|
using Intel.Manageability.AgentPresence;
|
|
using Intel.Manageability.SystemDefense;
|
|
using System.Collections.ObjectModel;
|
|
using Intel.Manageability.Exceptions;
|
|
using System.Collections;
|
|
|
|
|
|
namespace AgentPresenceRemoteSample
|
|
{
|
|
static class AgentPresenceRemoteFunctionality
|
|
{
|
|
public static void CreateOrUpdateAgent(IAMTInstance amt, string agentName)
|
|
{
|
|
// --------------------
|
|
// Create Agent
|
|
// --------------------
|
|
|
|
var actions = new List<AgentAction>
|
|
{
|
|
new AgentAction(AgentState.NotStarted, AgentState.Running, true, ActionSystemDefense.DeactivatePolicy,
|
|
false),
|
|
new AgentAction(AgentState.Stopped, AgentState.Expired | AgentState.Stopped, false,
|
|
ActionSystemDefense.ActivatePolicy, false)
|
|
};
|
|
|
|
Console.Write("Enter agent startup timeout in seconds (default is 120): ");
|
|
if (!ushort.TryParse(Console.ReadLine(), out var startupTimer))
|
|
startupTimer = 120;
|
|
Console.WriteLine();
|
|
|
|
Console.Write("Enter agent heartbeat timeout in seconds (default is 120): ");
|
|
if (!ushort.TryParse(Console.ReadLine(), out var intervalTimer))
|
|
intervalTimer = 120;
|
|
Console.WriteLine();
|
|
|
|
// Create policy and pass the action list as a parameter
|
|
Agent agent = new Agent(agentName, actions, startupTimer, intervalTimer);
|
|
|
|
// -------------------------------------------
|
|
// Add the agent to the Intel AMT instance
|
|
// -------------------------------------------
|
|
|
|
try
|
|
{
|
|
amt.AgentPresence.Remote.CreateOrUpdateAgent(agent);
|
|
Console.WriteLine("Create agent completed successfully.");
|
|
}
|
|
catch (ManageabilityException ex)
|
|
{
|
|
Console.WriteLine("Create agent failed with exception: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public static void DisplayAgents(IAMTInstance amt)
|
|
{
|
|
try
|
|
{
|
|
// Retrieve all agents
|
|
List<Agent> agents = amt.AgentPresence.Remote.GetAllAgents();
|
|
|
|
Console.WriteLine("Agents:");
|
|
Console.WriteLine("-------");
|
|
|
|
// Go over the agents and display their capabilities
|
|
IEnumerator agentEnumerator = agents.GetEnumerator();
|
|
while (agentEnumerator.MoveNext())
|
|
{
|
|
PrintAgentContent(agentEnumerator.Current as Agent);
|
|
}
|
|
}
|
|
catch (ManageabilityException ex)
|
|
{
|
|
Console.WriteLine("Display agents failed with exception: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public static void GetAgent(IAMTInstance amt, string agentName)
|
|
{
|
|
try
|
|
{
|
|
// Retrieve all agents
|
|
Agent agent = amt.AgentPresence.Remote.GetAgent(agentName);
|
|
Console.WriteLine("\nAgent:");
|
|
Console.WriteLine("-------");
|
|
|
|
PrintAgentContent(agent);
|
|
}
|
|
catch (ManageabilityException ex)
|
|
{
|
|
Console.WriteLine("Display agent failed with exception: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public static void DisplayCapabalities(IAMTInstance amt)
|
|
{
|
|
// Display general agent presence capabilities
|
|
AgentPresenceCapabilities generalCapabilities = amt.AgentPresence.Remote.GetGeneralCapabilities();
|
|
Console.WriteLine("\nGeneral capabilities:");
|
|
Console.WriteLine("\tMax Supported Agents: {0} ", generalCapabilities.MaxTotalAgents);
|
|
Console.WriteLine("\tMax Supported Actions: {0} ", generalCapabilities.MaxTotalActions);
|
|
Console.WriteLine("\tMax Supported EAC Agents: {0} ", generalCapabilities.MaxToatalEacAgents);
|
|
Console.WriteLine("\tMin Supported Actions: {0} ", generalCapabilities.MinAgentActions);
|
|
}
|
|
|
|
public static void DeleteAgent(IAMTInstance amt, string agentName)
|
|
{
|
|
try
|
|
{
|
|
amt.AgentPresence.Remote.DeleteAgent(agentName);
|
|
Console.WriteLine("Delete agent completed successfully.");
|
|
}
|
|
catch (ManageabilityException ex)
|
|
{
|
|
Console.WriteLine("Delete agent failed with exception: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public static void DeleteAllAgent(IAMTInstance amt)
|
|
{
|
|
try
|
|
{
|
|
amt.AgentPresence.Remote.DeleteAllAgents();
|
|
Console.WriteLine("Delete all agents completed successfully.");
|
|
}
|
|
catch (ManageabilityException ex)
|
|
{
|
|
Console.WriteLine("Delete agents failed with exception: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public static void SetAgentPresencePolicy(IAMTInstance amt)
|
|
{
|
|
Collection<Policy> policies = new Collection<Policy>();
|
|
Collection<UserFilter> filters = new Collection<UserFilter>();
|
|
|
|
// ----------------------
|
|
// Create Policy #1
|
|
// ----------------------
|
|
|
|
// Prepare filter list (create ipfilter for block ipv4 packets and ethernet filter for rarp packets)
|
|
IPFilter ipFilter = FilterFactory.CreateIPFilter("ipFilter", FilterType.Drop, FilterDirection.Both, true, IPVersion.IPv4);
|
|
EthernetFilter etherFilter = FilterFactory.CreateEthernetFilter("etherFilter", FilterType.StatisticsAndDrop, FilterDirection.Incoming, false, (ushort)EtherTypes.ETH_TYPE_RARP);
|
|
|
|
// Add the filters we just created to the filter list
|
|
filters.Add(ipFilter);
|
|
filters.Add(etherFilter);
|
|
|
|
// Create policy and pass the filters list as a parameter
|
|
policies.Add(new Policy("MyPolicy", 10, 150, filters));
|
|
|
|
// Add the policy to the Intel AMT instance
|
|
try
|
|
{
|
|
amt.SystemDefense.PolicyStore.CreateOrUpdatePolicies(policies);
|
|
Console.WriteLine("Create policy completed successfully.");
|
|
}
|
|
catch (ManageabilityException ex)
|
|
{
|
|
Console.WriteLine("Create policy failed with exception: " + ex.Message);
|
|
}
|
|
|
|
try
|
|
{
|
|
// Set the policy to agent presence on the supported interface
|
|
amt.AgentPresence.Remote.SetSystemDefensePolicy("MyPolicy", Intel.Manageability.AgentPresence.NetworkInterface.Wired);
|
|
Console.WriteLine("Set the policy to Agent Presence completed successfully.");
|
|
}
|
|
catch (ManageabilityException ex)
|
|
{
|
|
Console.WriteLine("Set the policy to Agent Presence failed with exception: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public static void GetAgentPresencePolicy(IAMTInstance amt)
|
|
{
|
|
try
|
|
{
|
|
// Get the policy from agent presence on the supported interface
|
|
Policy policy = amt.AgentPresence.Remote.GetSystemDefensePolicy(Intel.Manageability.AgentPresence.NetworkInterface.Wired);
|
|
Console.WriteLine("\nAgent Presence Policy:");
|
|
Console.WriteLine("----------------------");
|
|
PrintAgentPolicy(policy);
|
|
}
|
|
catch (ManageabilityException ex)
|
|
{
|
|
Console.WriteLine("Get the policy from Agent Presence failed with exception: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public static void RemoveAgentPresencePolicy(IAMTInstance amt)
|
|
{
|
|
try
|
|
{
|
|
// Remove the policy from agent presence on the supported interface
|
|
amt.AgentPresence.Remote.RemoveSystemDefensePolicy(Intel.Manageability.AgentPresence.NetworkInterface.Wired);
|
|
Console.WriteLine("Remove the policy from Agent Presence completed successfully.");
|
|
}
|
|
catch (ManageabilityException ex)
|
|
{
|
|
Console.WriteLine("Remove the policy from Agent Presence failed with exception: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public static void SetExpirationActionOnAgentPresenceWatchDog(IAMTInstance amt,string agentName)
|
|
{
|
|
try
|
|
{
|
|
//set expiration action properties
|
|
WatchDogExpirationAction expirationAction = new WatchDogExpirationAction(ExpirationAction.Reboot, ExpirationAction.Reboot, 60, true);
|
|
amt.AgentPresence.Remote.SetExpirationAction(expirationAction);
|
|
//apply expiration action on specific agent
|
|
amt.AgentPresence.Remote.ApplyActionOnWatchDog(agentName, true);
|
|
Console.WriteLine("Set expiration action completed successfully.");
|
|
}
|
|
catch (ManageabilityException ex)
|
|
{
|
|
Console.WriteLine("Set expiration action failed with exception: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
private static void PrintAgentContent(Agent agent)
|
|
{
|
|
Console.WriteLine("Agent Name: {0} \n", agent.ApplicationName);
|
|
Console.WriteLine("\tDevice ID: {0} ", agent.DeviceID);
|
|
Console.WriteLine("\tState: {0} ", agent.CurrentState);
|
|
Console.WriteLine("\tStartup Interval: {0} ", agent.StartupInterval);
|
|
Console.WriteLine("\tTimeout Interval: {0} ", agent.TimeoutInterval);
|
|
|
|
// Go over the actions and display their capabilities
|
|
IEnumerator actionEnumerator = agent.Actions.GetEnumerator();
|
|
Console.WriteLine("Actions:");
|
|
while (actionEnumerator.MoveNext())
|
|
{
|
|
var action = actionEnumerator.Current as AgentAction;
|
|
if (action == null)
|
|
{
|
|
Console.WriteLine("\tError: Failed to convert action to AgentAction");
|
|
break;
|
|
}
|
|
PrintAction(actionEnumerator.Current as AgentAction);
|
|
}
|
|
}
|
|
|
|
private static void PrintAction(AgentAction action)
|
|
{
|
|
Console.WriteLine("\n\t* Old State: {0} ", action.OldState);
|
|
Console.WriteLine("\t New State: {0} ", action.NewState);
|
|
Console.WriteLine("\t Create Event: {0} ", action.EventOnTransition);
|
|
Console.WriteLine("\t System Defense Action: {0} ", action.ActionSystemDefense);
|
|
Console.WriteLine("\t EAC Action: {0} \n", action.ActionEAC);
|
|
}
|
|
|
|
private static void PrintAgentPolicy(Policy policy)
|
|
{
|
|
Console.WriteLine("Policy Name: {0} ", policy.Name);
|
|
Console.WriteLine("\tPrecedence: {0} ", policy.Precedence);
|
|
Console.WriteLine("\tantiSpoofingSupport: {0} ", policy.antiSpoofingSupport);
|
|
Console.WriteLine("\tTimeout: {0} ", policy.Timeout);
|
|
Console.WriteLine("\tTx filter:\n\t\tDefaultCount: {0}\n\t\tDefaultDrop: {1}\n\t\tDefaultMatchEvent: {2}", policy.TxFilter.DefaultCount, policy.TxFilter.DefaultDrop, policy.TxFilter.DefaultMatchEvent);
|
|
Console.WriteLine("\tRx filter:\n\t\tDefaultCount: {0}\n\t\tDefaultDrop: {1}\n\t\tDefaultMatchEvent: {2}", policy.RxFilter.DefaultCount, policy.RxFilter.DefaultDrop, policy.RxFilter.DefaultMatchEvent);
|
|
|
|
// Go over the filters and display their capabilities
|
|
IEnumerator filterEnumerator = policy.Filters.GetEnumerator();
|
|
while (filterEnumerator.MoveNext())
|
|
{
|
|
Filter curFilter = filterEnumerator.Current as Filter;
|
|
|
|
if (curFilter is IPFilter)
|
|
{
|
|
PrintIPFilter(curFilter as IPFilter);
|
|
}
|
|
else if (curFilter is EthernetFilter)
|
|
{
|
|
PrintEthernetFilter(curFilter as EthernetFilter);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void PrintEthernetFilter(EthernetFilter ethernetFilter)
|
|
{
|
|
Console.WriteLine("\tFilter Name: {0} ", ethernetFilter.Name);
|
|
Console.WriteLine("\t\tFilter Direction: {0}", ethernetFilter.Direction.ToString());
|
|
Console.WriteLine("\t\tCreate Event: {0}", ethernetFilter.CreateEvent);
|
|
Console.WriteLine("\t\tFilter Profile: {0}", ethernetFilter.Profile.ToString());
|
|
if (ethernetFilter.Profile == FilterType.RateLimit)
|
|
Console.WriteLine("\t\tEvents Per Second: {0}", ethernetFilter.EventsPerSecond);
|
|
|
|
Console.WriteLine("\t\tProtocol ID: {0}", ethernetFilter.ProtocolID);
|
|
}
|
|
|
|
private static void PrintIPFilter(IPFilter ipFilter)
|
|
{
|
|
Console.WriteLine("\tFilter Name: {0} ", ipFilter.Name);
|
|
Console.WriteLine("\t\tFilter Direction: {0}", ipFilter.Direction.ToString());
|
|
Console.WriteLine("\t\tCreate Event: {0}", ipFilter.CreateEvent);
|
|
Console.WriteLine("\t\tFilter Profile: {0}", ipFilter.Profile.ToString());
|
|
if (ipFilter.Profile == FilterType.RateLimit)
|
|
Console.WriteLine("\t\tEvents Per Second: {0}", ipFilter.EventsPerSecond);
|
|
|
|
Console.WriteLine("\t\tIP Version: {0}", ipFilter.IpVersion.ToString());
|
|
if (ipFilter.IPAddress != null)
|
|
{
|
|
Console.WriteLine("\t\tIP Header Address: {0}", ipFilter.IPHeaderAddress.ToString());
|
|
Console.WriteLine("\t\tIP Address: {0}", ipFilter.IPAddress.ToString());
|
|
}
|
|
if (ipFilter.IPMask != null)
|
|
Console.WriteLine("\t\tIP Mask: {0}", ipFilter.IPMask.ToString());
|
|
|
|
if (ipFilter is ProtocolFilter)
|
|
Console.WriteLine("\t\tProtocol ID: {0}", ((ProtocolFilter)ipFilter).ProtocolID);
|
|
else if (ipFilter is TCPFilter)
|
|
{
|
|
Console.WriteLine("\t\tProtocol: TCP");
|
|
if (((TCPFilter)ipFilter).PortFilterExists)
|
|
{
|
|
Console.WriteLine("\t\tHeader Port Address: {0}", ((TCPFilter)ipFilter).HeaderPortAddress);
|
|
Console.WriteLine("\t\tPort1: {0}", ((TCPFilter)ipFilter).Port1);
|
|
Console.WriteLine("\t\tPort2: {0}", ((TCPFilter)ipFilter).Port2);
|
|
}
|
|
Console.WriteLine("\t\tFlags On: {0}", ((TCPFilter)ipFilter).FlagsOn);
|
|
Console.WriteLine("\t\tFlags Off: {0}", ((TCPFilter)ipFilter).FlagsOff);
|
|
}
|
|
else if (ipFilter is UDPFilter)
|
|
{
|
|
Console.WriteLine("\t\tProtocol: UDP");
|
|
Console.WriteLine("\t\tHeader Port Address: {0}", ((UDPFilter)ipFilter).HeaderPortAddress);
|
|
Console.WriteLine("\t\tPort1: {0}", ((UDPFilter)ipFilter).Port1);
|
|
Console.WriteLine("\t\tPort2: {0}", ((UDPFilter)ipFilter).Port2);
|
|
}
|
|
}
|
|
}
|
|
}
|