예제 #1
0
        private bool AddFailedLoginForEventViewerXml(IPAddressEvent info, XmlDocument doc)
        {
            if (string.IsNullOrWhiteSpace(info.IPAddress))
            {
                return(false);
            }
            else if (string.IsNullOrWhiteSpace(info.Source))
            {
                XmlNode sourceNode = doc.SelectSingleNode("//Source");
                if (sourceNode != null)
                {
                    info.Source = sourceNode.InnerText.Trim();
                }
            }
            if (string.IsNullOrWhiteSpace(info.UserName))
            {
                XmlNode userNameNode = doc.SelectSingleNode("//Data[@Name='TargetUserName']");
                if (userNameNode == null)
                {
                    userNameNode = doc.SelectSingleNode("//TargetUserName");
                }
                if (userNameNode != null)
                {
                    info.UserName = userNameNode.InnerText.Trim();
                }
            }

            service.HandleIPAddressEvent(info);

            return(true);
        }
예제 #2
0
 Task IIPAddressEventHandler.HandleIPAddressEvent(IPAddressEvent info)
 {
     if (info.Flag.HasFlag(IPAddressEventFlag.SuccessfulLogin))
     {
         successIPAddresses.Add(info);
     }
     else
     {
         failedIPAddresses.Add(info);
     }
     return(Task.CompletedTask);
 }
 private bool ParseRegex(Regex regex, string line, bool notifyOnly)
 {
     if (regex != null)
     {
         IPAddressEvent info = IPBanService.GetIPAddressInfoFromRegex(dns, regex, line);
         if (info.FoundMatch)
         {
             info.Flag   = (notifyOnly ? IPAddressEventFlag.SuccessfulLogin : IPAddressEventFlag.FailedLogin);
             info.Source = info.Source ?? Source;
             IPBanLog.Debug("Log file found match, ip: {0}, user: {1}, source: {2}, count: {3}, flag: {4}",
                            info.IPAddress, info.UserName, info.Source, info.Count, info.Flag);
             loginHandler.HandleIPAddressEvent(info);
             return(true);
         }
     }
     return(false);
 }
예제 #4
0
        /// <summary>
        /// Process event viewer XML
        /// </summary>
        /// <param name="xml">XML</param>
        public void ProcessEventViewerXml(string xml)
        {
            IPBanLog.Info("Processing event viewer xml: {0}", xml);

            XmlDocument    doc  = ParseXml(xml);
            IPAddressEvent info = ExtractEventViewerXml(doc);

            if (info != null && info.FoundMatch)
            {
                if (info.Flag.HasFlag(IPAddressEventFlag.FailedLogin))
                {
                    // if fail to add the failed login (bad ip, etc.) exit out
                    if (!AddFailedLoginForEventViewerXml(info, doc))
                    {
                        return;
                    }
                }
                else
                {
                    service.IPBanDelegate?.LoginAttemptSucceeded(info.IPAddress, info.Source, info.UserName).ConfigureAwait(false).GetAwaiter();
                }
                IPBanLog.Info("Event viewer found: {0}, {1}, {2}", info.IPAddress, info.Source, info.UserName);
            }
        }
예제 #5
0
        private IPAddressEvent ExtractEventViewerXml(XmlDocument doc)
        {
            XmlNode keywordsNode = doc.SelectSingleNode("//Keywords");
            string  keywordsText = keywordsNode.InnerText;

            if (keywordsText.StartsWith("0x"))
            {
                keywordsText = keywordsText.Substring(2);
            }
            ulong          keywordsULONG   = ulong.Parse(keywordsText, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
            IPAddressEvent info            = null;
            bool           foundNotifyOnly = false;

            if (keywordsNode != null)
            {
                // we must match on keywords
                foreach (EventViewerExpressionGroup group in service.Config.WindowsEventViewerGetGroupsMatchingKeywords(keywordsULONG))
                {
                    foreach (EventViewerExpression expression in group.Expressions)
                    {
                        // find all the nodes, try and get an ip from any of them, all must match
                        XmlNodeList nodes = doc.SelectNodes(expression.XPath);

                        if (nodes.Count == 0)
                        {
                            IPBanLog.Info("No nodes found for xpath {0}", expression.XPath);
                            info = null;
                            break;
                        }

                        // if there is a regex, it must match
                        if (string.IsNullOrWhiteSpace(expression.Regex))
                        {
                            // count as a match, do not modify the ip address if it was already set
                            IPBanLog.Info("No regex, so counting as a match");
                        }
                        else
                        {
                            info = null;

                            // try and find an ip from any of the nodes
                            foreach (XmlNode node in nodes)
                            {
                                // if we get a match, stop checking nodes
                                info = IPBanService.GetIPAddressInfoFromRegex(service.DnsLookup, expression.RegexObject, node.InnerText);
                                if (info.FoundMatch)
                                {
                                    if (group.NotifyOnly)
                                    {
                                        foundNotifyOnly = true;
                                    }
                                    else if (foundNotifyOnly)
                                    {
                                        throw new InvalidDataException("Conflicting expressions in event viewer, both failed and success logins matched keywords " + group.Keywords);
                                    }
                                    break;
                                }
                            }

                            if (info != null && !info.FoundMatch)
                            {
                                // match fail, null out ip, we have to match ALL the nodes or we get null ip and do not ban
                                IPBanLog.Info("Regex {0} did not match any nodes with xpath {1}", expression.Regex, expression.XPath);
                                info            = null;
                                foundNotifyOnly = false;
                                break;
                            }
                        }
                    }

                    if (info != null && info.FoundMatch && info.IPAddress != null)
                    {
                        info.Source = info.Source ?? group.Source;
                        break;
                    }
                    info = null; // set null for next attempt
                }
            }

            if (info != null)
            {
                info.Flag = (foundNotifyOnly ? IPAddressEventFlag.SuccessfulLogin : IPAddressEventFlag.FailedLogin);
            }
            return(info);
        }