search
HomeBackend DevelopmentPHP TutorialPHP implements verification code recognition (primary)_PHP tutorial

PHP implements verification code recognition (primary)_PHP tutorial

Jul 13, 2016 pm 05:49 PM
phponeprimaryaccomplishofKnowledgecodeResearchbreakthroughRecordidentifyverify

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         { 
            for($j=0; $j             { 
                $rgb = imagecolorat($res,$j,$i); 
                $rgbarray = imagecolorsforindex($res, $rgb); 
                if($rgbarray['red']                 || $rgbarray['blue']                 { 
                    $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         { 
            $x = ($i*(WORD_WIDTH+WORD_SPACING))+OFFSET_X; 
            $y = OFFSET_Y; 
            for($h = $y; $h             { 
                for($w = $x; $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; $iImageSize[1]; ++$i) 
        { 
            for($j=0; $jImageSize[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
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool