Пример #1
0
        public string ProcessFactionMessage(HttpContext httpContext, FactionMessage factionMessage)
        {
            string resultContent = State.Profile.Server.MessageConfig.Default;

            // Add any headers
            if (State.Profile.Server.Headers.Count > 0)
            {
                foreach (KeyValuePair <string, string> header in State.Profile.Server.Headers)
                {
                    httpContext.Response.Headers.TryAdd(header.Key, header.Value);
                }
            }

            // Set any Cookies
            if (State.Profile.Server.Cookies.Count > 0)
            {
                foreach (KeyValuePair <string, string> cookie in State.Profile.Server.Cookies)
                {
                    httpContext.Response.Cookies.Append(cookie.Key, cookie.Value);
                }
            }

            // If Faction replies with a message for the agent, add the defined Prepend and Append strings
            if (!String.IsNullOrEmpty(factionMessage.Message))
            {
                if (factionMessage.Message.Length > 2)
                {
                    resultContent = $"{State.Profile.Server.MessageConfig.Prepend}{factionMessage.Message}{State.Profile.Server.MessageConfig.Append}";
                }
            }
            Logger.LogInfo($"Returning Message to Client: {resultContent}");
            return(resultContent);
        }
Пример #2
0
        public Task Handle(HttpContext httpContext)
        {
            string route = httpContext.Request.Path.ToString();

            CheckinMessage checkinMessage = GetCheckinMessageFromRequest(httpContext.Request);

            // If the route is not incldued in the list of valid routes, return a default page
            if (!State.Profile.Server.URLs.Contains(route) || !checkinMessage.IsValid)
            {
                return(DefaultResponse(httpContext));
            }

            FactionMessage factionMessage = new FactionMessage();

            if (!String.IsNullOrEmpty(checkinMessage.StagingName))
            {
                factionMessage = _factionAPIHandler.HandleStage(checkinMessage.StagingName, checkinMessage.StagingId, checkinMessage.Message, httpContext.Connection.RemoteIpAddress.ToString());
            }
            else
            {
                factionMessage = _factionAPIHandler.HandleBeacon(checkinMessage.AgentName, checkinMessage.Message, httpContext.Connection.RemoteIpAddress.ToString());
            }

            // Convert Faction Message to response as defined by the profile
            string responseContent = _responseHandler.ProcessFactionMessage(httpContext, factionMessage);

            if (String.IsNullOrEmpty(responseContent))
            {
                return(DefaultResponse(httpContext));
            }
            return(httpContext.Response.WriteAsync(responseContent));
        }
        public FactionMessage HandleStage(string StageName, string StagingId, string Message, string SourceIp)
        {
            string stagingUrl = $"{State.ServerConfiguration.FactionURL}/api/v1/staging/{StageName}/{StagingId}/";

            WebClient wc = CreateWebClient();

            FactionMessage factionResponse = new FactionMessage();
            string         jsonMessage     = $"{{\"TransportId\": {State.ServerConfiguration.TransportId},\"Message\": \"{Message}\", \"SourceIp\":\"{SourceIp}\"}}";

            try
            {
                Logger.LogInfo($"Staging URL: {stagingUrl}");
                Logger.LogInfo($"Key Name: {State.ServerConfiguration.FactionAPIKey}");
                Logger.LogInfo($"Sending Staging Message: {jsonMessage}");
                string response = wc.UploadString(stagingUrl, jsonMessage);
                Logger.LogInfo($"Got Response: {response}");
                factionResponse = JsonConvert.DeserializeObject <FactionMessage>(response);
            }
            catch (Exception e)
            {
                Logger.LogError($"Error handling agent staging request: {e.Message}");
                factionResponse.Message = "ERROR";
            }
            return(factionResponse);
        }
        public FactionMessage HandleBeacon(string AgentName, string Message, string SourceIp)
        {
            WebClient wc = CreateWebClient();

            string         beaconUrl       = $"{State.ServerConfiguration.FactionURL}/api/v1/agent/{AgentName}/checkin/";
            FactionMessage factionResponse = new FactionMessage();

            if (!String.IsNullOrEmpty(Message))
            {
                try
                {
                    string jsonMessage = $"{{\"TransportId\": {State.ServerConfiguration.TransportId},\"Message\": \"{Message}\", \"SourceIp\":\"{SourceIp}\"}}";
                    Logger.LogInfo($"Building AgentCheckin Message - URL: {beaconUrl}");
                    Logger.LogInfo($"POSTing AgentCheckin to {beaconUrl}");
                    Logger.LogInfo($"AgentCheckin Message: {jsonMessage}");
                    string response = wc.UploadString(beaconUrl, jsonMessage);
                    factionResponse = JsonConvert.DeserializeObject <FactionMessage>(response);
                }
                catch (Exception e)
                {
                    Logger.LogError($"Error POSTing agent checkin to Faction API: {e.Message}");
                    factionResponse.Message = "ERROR";
                }
            }
            else
            {
                try
                {
                    Logger.LogInfo($"Perfoming GET Checkin against {beaconUrl}");
                    string response = wc.DownloadString($"{beaconUrl}?TransportId={State.ServerConfiguration.TransportId}&SourceIp={SourceIp}");
                    factionResponse = JsonConvert.DeserializeObject <FactionMessage>(response);
                }
                catch (Exception e)
                {
                    Logger.LogError($"Error from Faction API while handling agent Checkin: {e.Message}");
                    factionResponse.Message = "ERROR";
                }
            }
            return(factionResponse);
        }