Example #1
0
        public void DictionaryMatch(string word, int count)
        {
            var dm   = new DictionaryMatcher("test", "test_dictionary.txt");
            var leet = new L33tMatcher(dm);

            leet.MatchPassword(word).Should().HaveCount(count);
        }
        public void UsesTheUserInputDictionary()
        {
            var matcher = new DictionaryMatcher("user_inputs", new[] { "foo", "bar" });

            var result = matcher.MatchPassword("foobar").OfType <DictionaryMatch>().ToList();

            var expected = new[]
            {
                new DictionaryMatch
                {
                    DictionaryName = "user_inputs",
                    i           = 0,
                    j           = 2,
                    MatchedWord = "foo",
                    Rank        = 1,
                    Reversed    = false,
                    L33t        = false,
                    Token       = "foo",
                },
                new DictionaryMatch
                {
                    DictionaryName = "user_inputs",
                    i           = 3,
                    j           = 5,
                    MatchedWord = "bar",
                    Rank        = 2,
                    Reversed    = false,
                    L33t        = false,
                    Token       = "bar",
                },
            };

            result.Should().BeEquivalentTo(expected);
        }
Example #3
0
        /// <summary>
        /// Generates the match between textentities and dictionary entities.
        /// </summary>
        /// <param name="options">Options.</param>
        public override void Run()
        {
            if (options.Verbose)
            {
                Console.Error.WriteLine("Option 3.");
            }

            if (options.Dictionary == null)
            {
                Console.Error.WriteLine("Dictionary required. Exiting...");
                return;
            }

            TextWriter output;

            if (string.IsNullOrEmpty(options.Output))
            {
                output = new StreamWriter(Console.OpenStandardOutput());
            }
            else
            {
                output = new StreamWriter(options.Output);
            }

            foreach (string dic in FilesUtils.GetFiles(options.Dictionary))
            {
                DictionaryMatcher.MatchEntitiesInFiles(options.InputFile, dic, output, options.Separator, options.Language);
            }


            output.Close();
        }
Example #4
0
        /// <summary>
        /// Get instances of pattern matchers, adding in per-password matchers on userInputs (and userInputs with l33t substitutions)
        /// </summary>
        /// <param name="userInputs">Enumerable of user information</param>
        /// <returns>Enumerable of matchers to use</returns>
        public IEnumerable <IMatcher> CreateMatchers(IEnumerable <string> userInputs)
        {
            var userInputDict = new DictionaryMatcher("user_inputs", userInputs);
            var leetUser      = new L33tMatcher(userInputDict);

            return(matchers.Union(new List <IMatcher> {
                userInputDict, leetUser
            }));
        }
        public void MatchesWithProvidedUserInputDictionary()
        {
            var matcher = new DictionaryMatcher("user", new[] { "foo", "bar" });
            var result  = matcher.MatchPassword("foobar").OfType <DictionaryMatch>().Where(m => m.DictionaryName == "user").ToList();

            result.Should().HaveCount(2);

            result[0].Token.Should().Be("foo");
            result[0].MatchedWord.Should().Be("foo");
            result[0].Rank.Should().Be(1);
            result[0].i.Should().Be(0);
            result[0].j.Should().Be(2);

            result[1].Token.Should().Be("bar");
            result[1].MatchedWord.Should().Be("bar");
            result[1].Rank.Should().Be(2);
            result[1].i.Should().Be(3);
            result[1].j.Should().Be(5);
        }
        public void DictionaryTest()
        {
            var dm = new DictionaryMatcher("test", "test_dictionary.txt");

            var res = dm.MatchPassword("NotInDictionary");

            Assert.AreEqual(0, res.Count());

            res = dm.MatchPassword("choreography");
            Assert.AreEqual(1, res.Count());

            res = dm.MatchPassword("ChOrEograPHy");
            Assert.AreEqual(1, res.Count());


            var leet = new L33tMatcher(dm);

            res = leet.MatchPassword("3mu");
            Assert.AreEqual(1, res.Count());

            res = leet.MatchPassword("3mupr4nce|egume");
        }
Example #7
0
        public static DictionaryMatcher LoadData(string filename)
        {
            DictionaryMatcher dicMatcher = null;

            //from ICUBinary.java
            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                //file header
                //--------------------------------------------------------------
                BinaryReader reader     = new BinaryReader(fs);
                ushort       headerSize = reader.ReadUInt16(); //0,1
                byte         magic1     = reader.ReadByte();   //2
                byte         magic2     = reader.ReadByte();   //3
                if (magic1 != MAGIC1 && magic2 != MAGIC2)
                {
                    throw new Exception("MAGIC_NUMBER_AUTHENTICATION_FAILED_");
                }
                ushort sizeofUDataInfo = reader.ReadUInt16(); //4,5
                reader.ReadUInt16();                          //6,7
                //
                byte isBigEndian   = reader.ReadByte();       //8
                byte charsetFamily = reader.ReadByte();       //9
                byte sizeofUChar   = reader.ReadByte();       //10
                if (isBigEndian < 0 || 1 < isBigEndian ||
                    charsetFamily != CHAR_SET_ || sizeofUChar != CHAR_SIZE_)
                {
                    throw new IOException("HEADER_AUTHENTICATION_FAILED_");
                }
                if (sizeofUDataInfo < 20 || headerSize < (sizeofUDataInfo + 4))
                {
                    throw new IOException("Internal Error: Header size error");
                }
                reader.ReadByte();//11

                byte[] fourBytesBuffer = new byte[4];
                reader.Read(fourBytesBuffer, 0, 4); //12,13,14,15
                int file_dataFormat =
                    (fourBytesBuffer[0] << 24) |
                    (fourBytesBuffer[1] << 16) |
                    (fourBytesBuffer[2] << 8) |
                    (fourBytesBuffer[3]);
                if (file_dataFormat != DATA_FORMAT_ID)
                {
                    throw new IOException("HEADER_AUTHENTICATION_FAILED_");
                }

                //format version
                reader.Read(fourBytesBuffer, 0, 4);//16,17,18,19


                //must = 20
                //
                //data version
                reader.Read(fourBytesBuffer, 0, 4);
                int data_version =
                    (fourBytesBuffer[0] << 24) |
                    (fourBytesBuffer[1] << 16) |
                    (fourBytesBuffer[2] << 8) |
                    (fourBytesBuffer[3]);
                //--------------------------------------------------------------
                reader.BaseStream.Position = headerSize;
                //body
                int[] indexes = new int[IX_COUNT];
                for (int i = 0; i < IX_COUNT; ++i)
                {
                    indexes[i] = reader.ReadInt32();
                }

                int offset = indexes[IX_STRING_TRIE_OFFSET];

                if (offset >= 4 * IX_COUNT)
                {
                    //must true
                }

                if (offset > (4 * IX_COUNT))
                {
                    int diff = offset - (4 * IX_COUNT);
                    //ICUBinary.skipBytes(bytes, diff);
                    throw new NotSupportedException();
                }

                int trieType  = indexes[IX_TRIE_TYPE] & TRIE_TYPE_MASK;
                int totalSize = indexes[IX_TOTAL_SIZE] - offset;

                switch (trieType)
                {
                default: throw new NotSupportedException();

                case TRIE_TYPE_BYTES:
                {
                    int    transform = indexes[IX_TRANSFORM];
                    byte[] data      = new byte[totalSize];
                    reader.Read(data, 0, totalSize);
                    if ((transform & DictionaryData.TRANSFORM_TYPE_MASK) == DictionaryData.TRANSFORM_TYPE_OFFSET)
                    {        //must true
                    }
                    BytesDictionaryMatcher byteDicMatcher = new BytesDictionaryMatcher(data, transform);
                    byteDicMatcher.Tx2('ก');
                    dicMatcher = byteDicMatcher;
                } break;

                case TRIE_TYPE_UCHARS:
                {
                    throw new NotSupportedException();
                } break;
                }
                reader.Close();
            }
            return(dicMatcher);
        }