Exemplo n.º 1
0
        private void AutoType_SequenceQuery(object sender, SequenceQueryEventArgs e)
        {
            string entryAutoTypeSequence = e.Entry.GetAutoTypeSequence();

            string url;

            lock (mUrlForAutoTypeEvent)
            {
                if (!mUrlForAutoTypeEvent.TryGetValue(e.EventID, out url))
                {
                    return;
                }

                bool skipUserName = false;
                mSkipUserNameForSequence.TryGetValue(e.EventID, out skipUserName);

                if (skipUserName && entryAutoTypeSequence.StartsWith(UserNameAutoTypeSequenceStart, StrUtil.CaseIgnoreCmp))
                {
                    entryAutoTypeSequence = entryAutoTypeSequence.Substring(UserNameAutoTypeSequenceStart.Length);
                }
            }

            var matchFound = false;

            foreach (AutoTypeAssociation association in e.Entry.AutoType.Associations)
            {
                string strUrlSpec = association.WindowName;
                if (strUrlSpec == null)
                {
                    continue;
                }

                strUrlSpec = strUrlSpec.Trim();

                if (!strUrlSpec.StartsWith(UrlAutoTypeWindowTitlePrefix) || strUrlSpec.Length <= UrlAutoTypeWindowTitlePrefix.Length)
                {
                    continue;
                }

                strUrlSpec = strUrlSpec.Substring(7);

                if (strUrlSpec.Length > 0)
                {
                    strUrlSpec = SprEngine.Compile(strUrlSpec, new SprContext(e.Entry, e.Database, SprCompileFlags.All));
                }

                bool  bRegex   = strUrlSpec.StartsWith(@"//") && strUrlSpec.EndsWith(@"//") && (strUrlSpec.Length > 4);
                Regex objRegex = null;

                if (bRegex)
                {
                    try
                    {
                        objRegex = new Regex(strUrlSpec.Substring(2, strUrlSpec.Length - 4), RegexOptions.IgnoreCase);
                    }
                    catch (Exception)
                    {
                        bRegex = false;
                    }
                }

                if (bRegex)
                {
                    if (objRegex.IsMatch(url))
                    {
                        e.AddSequence(string.IsNullOrEmpty(association.Sequence) ? entryAutoTypeSequence : association.Sequence);
                        matchFound = true;
                    }
                }
                else if (StrUtil.SimplePatternMatch(strUrlSpec, url, StrUtil.CaseIgnoreCmp))
                {
                    e.AddSequence(string.IsNullOrEmpty(association.Sequence) ? entryAutoTypeSequence : association.Sequence);
                    matchFound = true;
                }
            }

            if (MatchUrlField)
            {
                var urlFieldValue = e.Entry.Strings.GetSafe(KeePassLib.PwDefs.UrlField).ReadString();

                var match = Regex.Match(urlFieldValue, @"^(?<scheme>\w+://)?(?<credentials>[^@/]+@)?(?<host>[^/]+?)(?<port>:\d+)?(?<path>/.*)?$");
                if (match.Success)
                {
                    // Convert URL into regex to match subdomains and sub-paths
                    var urlRegex = "^" +                                             // Must be start of string
                                   GetValueOrDefault(match, "scheme", "https?://") + // Scheme or assume http/s
                                   Regex.Escape(match.Groups["credentials"].Value) + // Credentials if present, otherwise assert none
                                   @"(\w+\.)*" +                                     // Allow any number of subdomains
                                   Regex.Escape(match.Groups["host"].Value) +        // Host part
                                   GetValueOrDefault(match, "port", @"(:\d+)?") +    // Exact port if specified, otherwise any or no port.
                                   GetValueOrDefault(match, "path", "(?:/|$)") +     // Path part as specified, or ensure host ends with / or end of url
                                   ".*$";                                            // Allow anything at the end of the url

                    matchFound = Regex.IsMatch(url, urlRegex);
                }
                else
                {
                    // Can't parse URL field value as URL, so fall back on plain equals
                    matchFound = urlFieldValue.Equals(url, StrUtil.CaseIgnoreCmp);
                }

                if (matchFound)
                {
                    e.AddSequence(entryAutoTypeSequence);
                }
            }

            if (matchFound && ShowRepeatedSearch)
            {
                lock (mUrlForAutoTypeEvent)
                {
                    mFoundSequence.Add(e.EventID);
                }
            }
        }
Exemplo n.º 2
0
        public void ResolveSequence(string winName, string sequence, SequenceQueryEventArgs e)
        {
            //clear all parms and flags
            string exeParam = string.Empty;
            string ctlParam = string.Empty;

            _reportFlag = false;

            var exePath = getExecutableFromHwnd(e.TargetWindowHandle).ToLower();

            //get the window name (this would usually contain the TITLE of the window
            //that would match
            if (winName == null)
            {
                return;
            }
            var matchTemplate = winName;

            var match = false;

            if (!match)
            {
                //next remove any app out of the window name
                //and try it

                //first compile the window name to replace all KeePass elements
                matchTemplate = SprEngine.Compile(matchTemplate, new SprContext(e.Entry, e.Database, SprCompileFlags.All));

                exeParam    = getParam(ref matchTemplate, "exe");
                ctlParam    = getParam(ref matchTemplate, "ctl");
                _reportFlag = getFlag(ref matchTemplate, "report");

                if (_reportFlag)
                {
                    if (!string.IsNullOrEmpty(exeParam))
                    {
                        ReportLine("EXE tag detected. Searching for \"" + exeParam + "\"");
                    }
                    else
                    {
                        ReportLine("No EXE tag detected.");
                    }
                    if (!string.IsNullOrEmpty(ctlParam))
                    {
                        ReportLine("CTL tag detected. Searching for \"" + ctlParam + "\"");
                    }
                    else
                    {
                        ReportLine("No CTL tag detected.");
                    }

                    ReportLine("Application of current target window: \"" + exePath + "\"");
                    ReportLine("");
                }

                if (!string.IsNullOrEmpty(exeParam))
                {
                    if (exeParam.Contains(@"\"))
                    {
                        //param looks like it's got a path element, so compare to the whole exename
                        match = (IsAMatch(exePath, exeParam));
                    }
                    else
                    {
                        //no path element, so just compare to the exe filename
                        //check if there's an ext specified
                        if (!exeParam.Contains("."))
                        {
                            //add an exe extension by default if none specified
                            exeParam += ".exe";
                        }
                        match = (IsAMatch(Path.GetFileName(exePath), exeParam));
                    }
                }
            }

            if (!match && !string.IsNullOrEmpty(ctlParam))
            {
                //no match yet, check for any child controls

                //attempt to retrieve an accessible object from the target window handle
                var uiaObject = AutomationElement.FromHandle(e.TargetWindowHandle);

                //and scan through it's child objects
                match = ScanControlTree(uiaObject, ctlParam);
            }

            //and lastly, the winName must match as well to be considered
            var title = e.TargetWindowTitle ?? string.Empty;

            match = match && IsAMatch(title, matchTemplate);

            //if reporting is on we DO NOT want to match
            //NOTE that other entries with reporting OFF +may still match+
            if (match && !_reportFlag)
            {
                e.AddSequence(string.IsNullOrEmpty(sequence) ? e.Entry.GetAutoTypeSequence() : sequence);
            }
        }