Home Backend Development PHP Tutorial PHP implements verification code recognition (primary)_PHP tutorial

PHP implements verification code recognition (primary)_PHP tutorial

Jul 13, 2016 pm 05:49 PM
php one primary accomplish of Knowledge code Research breakthrough Record identify verify

Recently researched some knowledge on breaking through the verification code and recorded it. On the one hand, it is a summary of the knowledge learned in the past few days to help myself understand; on the other hand, I hope it will be helpful to technical students who are studying this aspect; on the other hand, I also hope to attract the attention of website administrators and take more into consideration when providing verification codes. Since I have just come into contact with this aspect of knowledge, my understanding is relatively simple, so mistakes are inevitable. Feel free to comment.
The function of the verification code: It can effectively prevent a hacker from making continuous login attempts on a specific registered user by using a specific program to brute force. In fact, modern verification codes generally prevent machines from registering in batches and preventing machines from posting replies in batches. At present, many websites use verification code technology to prevent users from using robots to automatically register, log in, and spam.
The so-called verification code is to generate a picture from a string of randomly generated numbers or symbols. Some interference pixels are added to the picture (to prevent OCR). The user can identify the verification code information with the naked eye, enter the form and submit it to the website for verification. The verification is successful. before you can use a function.
Our most common verification codes
1. Four digits, a random one-digit string, the most primitive verification code, and the verification effect is almost zero.
2. Random digital picture verification code. The characters on the picture are quite regular, some may have some random interferon added, and some have random character colors, so the verification effect is better than the previous one. People without basic knowledge of graphics and imagery cannot break it!
3. Random numbers in various picture formats + random uppercase English letters + random interference pixels + random positions.
4. Chinese characters are the latest verification code for registration. They are randomly generated, which makes it more difficult to type and affects the user experience. Therefore, it is generally used less frequently.
For the sake of simplicity, the main object of our explanation this time is the second type. Let’s first look at some pictures of this kind of verification code that are more common on the Internet.

(I don’t know what happened, but CSDN can’t upload pictures. I put these four kinds of pictures in the download package. You can download them and compare them)
These four styles can basically represent the types of verification codes mentioned in 2. Initially, it seems that the first picture is the easiest to crack, the second is second, the third is more difficult, and the fourth is the most difficult.
What's the real situation? In fact, these three types of images are equally difficult to crack.
The first picture is the easiest. The background and numbers of the picture use the same color, the characters are regular, and the characters are in the same position.
The second picture may not seem easy. In fact, if you study it carefully, you will find the rules. No matter how the background color and interferon change, it is verified that the characters are regular and the same color, so it is very easy to eliminate interferon, as long as all non-character pigments are eliminated. .
The third picture seems to be more complicated. In addition to the background color and interferon changing as mentioned above, the color of the verification characters also changes, and the colors of each character are also different. It seems impossible to break through this verification code. This article will take this type of verification code as an example. In the fourth picture, students can create it themselves.
In the fourth picture, in addition to the features mentioned in the third picture, two straight lines of interference rate are added to the text. This may seem difficult but is actually easy to remove.
Verification code identification is generally divided into the following steps:
1. Take out the font
2. Binarization
3. Calculate features
4. Control sample
1: Take out the font
After all, identifying the verification code is not a professional OCR recognition, and since the verification codes of each website are different, the most common method is to establish a feature code library of this verification code. When removing the fonts, we need to download a few more pictures so that all the characters are included in these pictures. The letters we have here only have pictures, so we only need to collect pictures including 0-9.
2: Binarization
Binarization is to use a number to represent each pixel on the verification number on the picture as 1, and other parts as 0. In this way, each digital font can be calculated, recorded, and used as a key.
3: Calculating features
Binarize the image to be identified to obtain the image features.
4: Control sample
Compare the image feature code in step 3 with the font pattern of the verification code to obtain the numbers on the verification image.
Using the current method, the recognition of verification codes can basically be 100%.
After going through the above steps, you may have said that you have not discovered how to remove interferon! In fact, the method to remove interferon is very simple. An important feature of interferon is that it cannot affect the display effect of the verification code, so when making interferon, its RGB may be lower or higher than a certain value, such as in the example I gave In the picture, the RGB values ​​of interferon will not exceed 125, so we can easily remove the interferon.
php code


[php]
define('WORD_WIDTH',9); 
define('WORD_HIGHT',13); 
define('OFFSET_X',7); 
define('OFFSET_Y',3); 
define('WORD_SPACING',4); 
class valite 

    public function setImage($Image) 
    { 
        $this->ImagePath = $Image; 
    } 
    public function getData() 
    { 
        return $data; 
    } 
    public function getResult() 
    { 
        return $DataArray; 
    } 
    public function getHec() 
    { 
        $res = imagecreatefromjpeg($this->ImagePath); 
        $size = getimagesize($this->ImagePath); 
        $data = array(); 
        for($i=0; $i < $size[1]; ++$i) 
        { 
            for($j=0; $j < $size[0]; ++$j) 
            { 
                $rgb = imagecolorat($res,$j,$i); 
                $rgbarray = imagecolorsforindex($res, $rgb); 
                if($rgbarray['red'] < 125 || $rgbarray['green']<125 
                || $rgbarray['blue'] < 125) 
                { 
                    $data[$i][$j]=1; 
                }else{ 
                    $data[$i][$j]=0; 
                } 
            } 
        } 
        $this->DataArray = $data; 
        $this->ImageSize = $size; 
    } 
    public function run() 
    { 
        $result=""; 
        // 查找4个数字 
        $data = array("","","",""); 
        for($i=0;$i<4;++$i) 
        { 
            $x = ($i*(WORD_WIDTH+WORD_SPACING))+OFFSET_X; 
            $y = OFFSET_Y; 
            for($h = $y; $h < (OFFSET_Y+WORD_HIGHT); ++ $h) 
            { 
                for($w = $x; $w < ($x+WORD_WIDTH); ++$w) 
                { 
                    $data[$i].=$this->DataArray[$h][$w]; 
                } 
            } 
             
        } 
        // 进行关键字匹配 
        foreach($data as $numKey => $numString) 
        { 
            $max=0.0; 
            $num = 0; 
            foreach($this->Keys as $key => $value) 
            { 
                $percent=0.0; 
                similar_text($value, $numString,$percent); 
                if(intval($percent) > $max) 
                { 
                    $max = $percent; 
                    $num = $key; 
                    if(intval($percent) > 95) 
                        break; 
                } 
            } 
            $result.=$num; 
        } 
        $this->data = $result; 
        // 查找最佳匹配数字 
        return $result; 
    } 
    public function Draw() 
    { 
        for($i=0; $i<$this->ImageSize[1]; ++$i) 
        { 
            for($j=0; $j<$this->ImageSize[0]; ++$j) 
            { 
                echo $this->DataArray[$i][$j]; 
            } 
            echo "/n"; 
        } 
    } 
    public function __construct() 
    { 
        $this->Keys = array( 
        '0'=>'000111000011111110011000110110000011110000011110000011110000011110000011110000011110000011011000110011111110000111000', 
        '1'=>'000111000011111000011111000000011000000011000000011000000011000000011000000011000000011000000011000011111111011111111', 
        '2'=>'011111000111111100100000110000000111000000110000001100000011000000110000001100000011000000110000000011111110111111110', 
        '3'=>'011111000111111110100000110000000110000001100011111000011111100000001110000000111000000110100001110111111100011111000', 
        '4'=>'000001100000011100000011100000111100001101100001101100011001100011001100111111111111111111000001100000001100000001100', 
        '5'=> 
       '111111110111111110110000000110000000110000000111110000111111100000001110000000111000000110100001110111111100011111000', 
        '6'=>'000111100001111110011000010011000000110000000110111100111111110111000111110000011110000011011000111011111110000111100', 
'7'=>'01111111101111111100000001100000001000000011000000110000000100000001100000001000000011000000011000000110 0000001100000',
'8'=>'00111110001111111001100011001100011001110111000111110000111110001110111011000001111000001111100011101111 1110001111100',
'9'=>'00111100001111111011100011111000001111000001111100011101111111100111101100000001100000011001000011001111 1100001111000',
);
}  
protected $ImagePath;
protected $DataArray;
protected $ImageSize;
protected $data;
protected $Keys;
protected $NumStringArray;
}
?>

I made an example, you can download it from here http://www.BkJia.com/uploadfile/2012/0316/20120316110154186.rar

After cracking the above verification code, we can use snoopy (lighter than curl, so I like it) to simulate the browser and access the website.


Excerpted from ugg’s column

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478322.htmlTechArticleRecently researched some knowledge about breaking through the verification code and recorded it. On the one hand, it is a summary of the knowledge learned in the past few days to help myself understand; on the other hand, I hope it will be useful to technical students who are studying this aspect...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles