Home >Backend Development >PHP Tutorial >PHP implements verification code recognition (primary)_PHP tutorial
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.