예제 #1
0
        public void TestCaptchaConfig()
        {
            var           con           = new ContainerBuilder();
            CaptchaConfig captchaConfig = new CaptchaConfig();

            captchaConfig.Length = 6;
            captchaConfig.Width  = 360;
            captchaConfig.Height = 120;
            con.AddCaptcha(captchaConfig);
            var icon    = con.Build();
            var Captcha = icon.Resolve <ICaptcha>();

            Assert.NotNull(Captcha);
            var result   = Captcha.GetCaptcha();
            var filepath = SaveAsFile(result);

            Assert.True(File.Exists(filepath));
            var img = Image.FromFile(filepath);

            Assert.Equal(360, img.Width);
            Assert.Equal(120, img.Height);
            Assert.Equal(6, result.CaptchaCode.Length);
            Assert.Equal(8, Captcha.CaptchaConfig.FontColors.Length);
            img.Dispose();
            File.Delete(filepath);
        }
예제 #2
0
        /// <summary>
        /// Resolve o captcha
        /// </summary>
        /// <param name="image">Imagem do captcha</param>
        /// <param name="config">Objeto de configurações</param>
        /// <returns>Solução do captcha</returns>
        public string Break(byte[] image, CaptchaConfig config)
        {
            var client = new CCProto();

            client.Login(_url.Host, _url.Port, "dummy", "dummy");
            var result = client.picture2(image, 30, (int)PictureType.Asirra);

            return(result.text);
        }
        private void AddDisturbtionLines(Graphics graphics, CaptchaConfig config)
        {
            var linesPen     = new Pen(Color.Gray, config.DisturbtionLinesWidth);
            var quaterHeight = config.ImageHeight / (config.DisturbtionLinesCount + 1);

            for (int i = 1; i <= config.DisturbtionLinesCount; i++)
            {
                graphics.DrawLine(linesPen, new Point(0, quaterHeight * i), new Point(config.ImageWidth, quaterHeight * i));
            }
        }
예제 #4
0
        public CaptchaConfig GetCaptchaConfig(ulong guildId)
        {
            //Try to load it from database, otherwise create a new one and store it.
            var document = Database.Load <CaptchaConfig>(CaptchaConfig.DocumentName(guildId));

            if (document == null)
            {
                document = new CaptchaConfig(guildId);
                Database.Store(document, CaptchaConfig.DocumentName(guildId));
            }

            return(document);
        }
예제 #5
0
        /// <summary>
        /// Método responsavel por realizar a quebra do captcha
        /// </summary>
        /// <param name="image">Byte Array da imagem captcha</param>
        /// <param name="config">Objeto contendo informações relevantes para solucionar o captcha</param>
        /// <returns>String com a solução do captcha</returns>
        public string Break(byte[] image, CaptchaConfig config)
        {
            string tempName          = Path.GetTempFileName();
            string textTempFileName  = Path.GetFileName(Path.ChangeExtension(tempName, "txt"));
            string extension         = (config.TipoImagem ?? ImageFormat.Bmp).GetExtension();
            string imageTempFilename = Path.ChangeExtension(tempName, extension);
            string imageFullPath     = string.Format(@"workspace\{0}", Path.GetFileName(imageTempFilename));

            if (!Directory.Exists("workspace"))
            {
                Directory.CreateDirectory("workspace");
            }

            File.WriteAllBytes(imageFullPath, image);

            var stream = File.Create(string.Format(@"workspace\{0}", textTempFileName));

            stream.Close();

            stream.Dispose();

            UI.CaptchaDialog cd = new UI.CaptchaDialog(imageFullPath);
            cd.ShowDialog();

            bool solutionFound = false;

            var conteudo = string.Empty;

            bool firstTime = true;

            do
            {
                if (!firstTime)
                {
                    Thread.Sleep(5000);
                }
                else
                {
                    firstTime = false;
                }

                conteudo = File.ReadAllText(string.Format(@"workspace\{0}", textTempFileName));

                if (!string.IsNullOrEmpty(conteudo))
                {
                    solutionFound = true;
                }
            } while (!solutionFound);

            return(conteudo);
        }
예제 #6
0
        public async Task <IRole> GetOrCreateCaptchaRole(CaptchaConfig config, SocketGuild guild)
        {
            IRole role;

            if (config.CaptchaTempRole == 0)
            {
                role = await guild.CreateRoleAsync("CaptchaTempRole");

                config.CaptchaTempRole = role.Id;
                SaveCaptchaConfig(config);
            }
            else
            {
                role = guild.GetRole(config.CaptchaTempRole);
                if (role == null)
                {
                    role = await guild.CreateRoleAsync("CaptchaTempRole");

                    config.CaptchaTempRole = role.Id;
                    SaveCaptchaConfig(config);
                }

                if (role.Permissions.SendMessages || role.Permissions.AddReactions || role.Permissions.Connect || role.Permissions.Speak)
                {
                    await role.ModifyAsync(x =>
                    {
                        x.Permissions = new GuildPermissions(sendMessages: false, addReactions: false, connect: false, speak: false);
                    });
                }
            }

            foreach (var channel in guild.Channels)
            {
                if (channel.PermissionOverwrites.All(x => x.TargetId != role.Id))
                {
                    var _ = Task.Run(async() => await channel.AddPermissionOverwriteAsync(role, new OverwritePermissions(sendMessages: PermValue.Deny, addReactions: PermValue.Deny, connect: PermValue.Deny, speak: PermValue.Deny)));
                }
            }

            return(role);
        }
예제 #7
0
        /// <summary>
        /// Método responsavel por realizar a quebra do captcha
        /// </summary>
        /// <param name="image">Byte Array da imagem captcha</param>
        /// <param name="config">Objeto contendo informações relevantes para solucionar o captcha</param>
        /// <returns>String com a solução do captcha</returns>
        public string Break(byte[] image, CaptchaConfig config)
        {
            var tempfilename = Path.GetTempFileName();

            File.WriteAllBytes(tempfilename, image);

            var stbResult = new StringBuilder();

            using (var engine = new TesseractEngine(ConfigManager.GetInstance().CAMINHO_ARQUIVO_IDIOMA, IDIOMA_PORTUGUES, EngineMode.Default))
                using (var img = Pix.LoadFromFile(tempfilename))
                {
                    var i = 1;
                    using (var page = engine.Process(img, null))
                    {
                        stbResult.AppendLine(page.GetText());
                        if (config.NumeroDePalavras > 1)
                        {
                            using (var iter = page.GetIterator())
                            {
                                iter.Begin();
                                do
                                {
                                    if (i % 2 == 0)
                                    {
                                        do
                                        {
                                            stbResult.AppendLine(iter.GetText(PageIteratorLevel.Word));
                                        } while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word));
                                    }
                                    i++;
                                } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine));
                            }
                        }
                    }
                }

            return(stbResult.ToString().Replace("\r", string.Empty).Replace("\n", string.Empty));;
        }
예제 #8
0
 public void SaveCaptchaConfig(CaptchaConfig config)
 {
     Database.Store(config, CaptchaConfig.DocumentName(config.GuildId));
 }
        private void DrawDisturbedString(Graphics graphics, string text, Font baseFont, Brush fontBrush, Point startingPoint, CaptchaConfig config)
        {
            var textSplitted = text.ToCharArray();
            var random       = new Random();
            int yOffset      = 0;

            Point letterPoint = startingPoint;

            foreach (var letter in textSplitted)
            {
                var stringToDraw = letter.ToString();

                var letterFontSize = Math.Max(1, baseFont.Size + random.Next(-config.DisturbStringFontResizeFactor, config.DisturbStringFontResizeFactor));
                var letterFont     = new Font(baseFont.FontFamily, letterFontSize, baseFont.Style);

                graphics.RotateTransform(random.Next(-config.DisturbStringRotateFactor, config.DisturbStringRotateFactor));

                graphics.DrawString(stringToDraw, letterFont, fontBrush, letterPoint);
                graphics.ResetTransform();


                var stringSize    = graphics.MeasureString(stringToDraw, letterFont).ToSize();
                var dividedHeight = stringSize.Height / config.DisturbStringHeightDivideFactor;
                yOffset *= -1;//back to normal position
                yOffset += random.Next(-dividedHeight, dividedHeight);

                var xOffset = stringSize.Width - (stringSize.Width / config.DisturbStringWidthDivideFactor);
                letterPoint.Offset(xOffset, yOffset);
            }
        }
        private void DrawDisturbedString(Image image, string text, Font baseFont, IBrush fontBrush, Point startingPoint, CaptchaConfig config)
        {
            var textSplitted = text.ToCharArray();
            var random       = new Random();
            int yOffset      = 0;

            Point letterPoint = startingPoint;

            foreach (var letter in textSplitted)
            {
                var stringToDraw = letter.ToString();

                var letterFontSize = Math.Max(1, baseFont.Size + random.Next(-config.DisturbStringFontResizeFactor, config.DisturbStringFontResizeFactor));
                var letterFont     = new Font(baseFont.Family, letterFontSize);


                image.Mutate(x =>
                {
                    var degrees = random.Next(-config.DisturbStringRotateFactor, config.DisturbStringRotateFactor);
                    // x.Rotate(degrees);
                    x.DrawText(stringToDraw, letterFont, fontBrush, letterPoint).Rotate(degrees);
                    // x.Rotate(-degrees);
                });
                RendererOptions options = new RendererOptions(letterFont, dpi: 72)
                {
                    ApplyKerning = true,
                };
                FontRectangle rect          = TextMeasurer.Measure(stringToDraw, options);
                var           dividedHeight = (int)rect.Height / config.DisturbStringHeightDivideFactor;
                yOffset *= -1;//back to normal position
                yOffset += random.Next(-dividedHeight, dividedHeight);

                var xOffset = (int)rect.Width - ((int)rect.Width / config.DisturbStringWidthDivideFactor);
                letterPoint.Offset(xOffset, yOffset);
            }
        }