Home  >  Article  >  Backend Development  >  Ideas for cracking and anti-cracking verification codes

Ideas for cracking and anti-cracking verification codes

WBOY
WBOYOriginal
2016-07-25 08:51:051587browse
There is only one way to use PHP to crack the verification code, which is to collect the signatures of characters appearing in the verification code, and then find a way to separate the characters in the verification code. What kind of artificial intelligence is obviously something that PHP cannot do, so as long as you can collect all the signatures of the characters that appear in the verification code and find a way to separate the characters in the verification code, you can crack it.
The anti-cracking idea mainly revolves around these two steps, using more characters, such as Chinese, and using more fonts and deformations, so that the same character has multiple feature codes, or there is no fixed feature code. Increase the difficulty of separating characters. The position, size and spacing of characters appearing in the verification code are not fixed. Interference pixels must be able to play a role.
The following takes the verification code of a certain website as an example to explain how to crack the verification code through the above two steps.
Note: Part of the code comes from phper "Baba", special thanks!
  1. /* Sort the numerical values ​​and find the second most element, because the first most element is the background color, and the second most is the character color. In other words, if each character They all use different colors, or the background color is not so monotonous. How can I separate the character colors? */
  2. function getMostRepeated($array){
  3. $count = array_count_values($array);
  4. arsort($count);
  5. $keys = array_keys($count);
  6. return $keys[1];
  7. }
  8. / * Read the verification code image, read the pixel RGB into a two-dimensional array, and then separate the valid characters */
  9. function fixImg($url){
  10. $img = imagecreatefrompng($url);
  11. $with = imagesx($ img);
  12. $height = imagesy($img);
  13. $middle = $height / 2;
  14. $colors = array();
  15. for ($i = 0; $i < $with; ++$ i) {
  16. $color = imagecolorat($img, $i, $middle);// Read the pixel color of the center line, the center line will definitely cut into the characters. . .
  17. $colors[] = $color;
  18. }
  19. $mainColor = getMostRepeated($colors);//Separate the character color
  20. $pic = array();
  21. for ($x = 0; $x < $ with; ++$x) {
  22. $flag = true;
  23. for ($y = 0; $y < $height; ++$y) {
  24. $currentColor = imagecolorat($img, $x, $y) ;
  25. $pos = 0;// The original image is in italics. In order to facilitate character separation, it is converted into regular font, so using italics is useless. . .
  26. if($y < 16){
  27. $pos = 0;
  28. }elseif($y >= 16 && $y <= 20){
  29. $pos = 1;
  30. $pic[$y][0 ] = 0;
  31. }elseif($y >= 21 && $y <= 26){
  32. $pos = 2;
  33. $pic[$y][0] = $pic[$y][1] = 0;
  34. }else{
  35. $pos = 3;
  36. $pic[$y][0] = $pic[$y][1] = $pic[$y][2] = 0;
  37. }
  38. // Because the colors of interference pixels and characters are completely different, it is easy to separate the character color and set the character pixel point to 1, so at least part of the interference pixel color must be the same as the character color!
  39. if ($currentColor !== $mainColor) {
  40. $pic[$y][$x+$pos] = 0;
  41. }else{
  42. $pic[$y][$x+$pos] = 1;
  43. $flag = false;
  44. }
  45. }
  46. }
  47. return $pic;
  48. }
  49. // The legendary feature code
  50. $char = array(
  51. 'A'=>'000000000000000000010000000000000000001011000000000000000111110000000000000001110100000 000000001111100000000000000111110000000000001011100110000000000111110000110000000001110100000110000000111100000000110000 00011111100000011000000011111111000110000000000011111110110000000000000111111100000000000000000111111100000000000000011 11111000000000000000011111110000000000000000001110000000000000000000110',
  52. 'B'=>'11111111111111111111011111111111111 1111110111111111111111111110110000000110000000110110000000110000000110110000000110000000110110000001110000000110111000011 1110000001101111111110011000111100111111000111111100001110 100000000000000111101000',
  53. 'C'=>'000001011110 000000000000011111111111110000000111111111111100000011101000011111110000111000000000000111000110000000000000011000110000 00000000001110110000000000000001110110000000000000000110110000000000000000110110000000000000000011011000000000000000011011 0000000000000000110110000000000000001100011000000000000001100011000000000000011000',
  54. 'D '=>'111111111111111111110111111111111111111110111111111111111111110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000000110111000000000000001110011000000000000011100011110000000000111100001111000000001111000000111111111111111000000011111111111100000000000111111110000000',
  55. 'E'=>'111111111111111111110111111111111111111110111111111111111111110110000000110000000110110000000110000000110110000000110000000110110000000110000000110110000000110000000110110000000110000000110110000000110000000110110000000000000000110000000000000000000110',
  56. 'F'=>'111111111111111111110111111111111111111110111111111111111111110110000000110000000000110000000110000000000110000000110000000000110000000110000000000110000000110000000000110000000110000000000110000000110000000000110000000000000000000',
  57. 'G'=>'000001011110000000000000011111111111110000000111111111111110000001110100001111111000011100000000000011100011000000000000001100011000000000000001110110000000000000001110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000001111111110011000000001111111100011000000001111111100',
  58. 'H'=>'111111111111111111110111111111111111111110111111111111111111110000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000111111111111111111110111111111111111111110111111111111111111110 ',
  59. 'I'=>'111111111111111111110111111111111111111110111111111111111111110',
  60. 'J'=>'111111111111111111111111111111111111111111111111111111111111110',
  61. 'K'=>'111111111111111111110111111111111111111110111111111111111111110000000000110000000000000000001110000000000000000011011000000000000001111001110000000000011110001111000000000111100000111100000001110000000001110000011100000000000111000111000000000000011100110000000000000001110100000000000000000110000000000000000000010',
  62. 'L'=>'111111111111111111110111111111111111111110111111111111111111110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110',
  63. 'M' =>'1111111111111111111101111111111111111111101111110000000000000001111110000000000000000011111111000000000000000011111 1000000000000000011111110000000000000000111110000000000000 000111111000000000000000001111000000000000000111110000000000 00101110000000000000011111000000000000111111010000000000001111000000000000001110000000000000000001111111111111111110111 1111111111111111011111111111111111110',
  64. 'N'=>'11111111110111111111111111111011111000000000000000011111 0000000000000000011110000000000000000001110000000000000000 00111100000000000000000011110000000000000000000111000000000000 00000000111000000000000000000111110000000000000000111110000000000000000001111000000000000000000000111101111111111111111111 1011111111111111111110',
  65. 'O'=>'0000010111100000000000000 111111111110000000011111111111100000011101000000111110000 111000000000000111000110000000000000110001100000000000000111 0110000000000000000110110000000000000000110110000000000000001 10110000000000000000110110000000000000000110111000000000000000110011000000000000011100011110000000000011110000111100000000 111100000011111111111111000000001111111110000000000011111 1110000000',
  66. 'P'=>'1111111111111111111011111111111111111 111011111111111111111101100000000110000000011000 000000110000000011000000000110000000011000000011100 000000011110000111000000000001111111100000000000011111111000000000000000111110000000000000',
  67. 'Q'=>'000001011110000000000000011111111111100000000111111111111110000001110100001111111000011100000000000011100011000000000000001100010000000000000001110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000000110111000000000000000111011000000000000011111011110000000000111101001111000000001111001000111111111111110000000001111111111000000000000111111110000000',
  68. 'R'=>'111111111111111111110111111111111111111110111111111111111111110110000000110000000000110000000110000000000110000000110000000000110000000110000000000110000001111100000000111001011101111000000111111111001111110000011111111000011110000001110110000000111100000000000000000001110000000000000000001110000000000000000000010',
  69. 'S'=>'000111000000000001000001111110000000001100011111111000000001100011000111100000000110110000011110000000110110000001110000000110110000001110000000110110000000111000000110110000000011100011100111000000011111111100011000000001111111000000000000001111100000',
  70. 'T'=>'110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000111111111111111111110111111111111111111110111111111111111111110110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000',
  71. 'T '=>'110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000111111111111111111110111111111111111111110111111111111111111110110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000',//vt时t左边会被侵占掉一列
  72. 'U'=>'111111111111111110000111111111111111110000111111111111111111100000000000000000001100000000000000000001110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000001100000000000000000111100111111111111111111000111111111111111000000',
  73. 'V'=>'111100000000000000000111111000000000000000111111111000000000000000001111110000000000000000111111100000000000000000111111110000000000000001111111000000000000000001111110000000000000000011110000000000000001111110000000000010111101000000000000111110000000000001011111000000000000111111000000000000001110100000000000000111100000000000000000',//少读一列
  74. 'W'=>'111111000000000000000111111111000000000000111111111110000000000000000111111111110000000000001111111110000000000000001111111110000000000000000111110000000000000011111110000000001111111001000000011111111100000000011111110000000000000111111000000000000000111111000000000000000111111111110000000000000000111111111110000000000011111111111000000000000001111111110000000000000000111110000000000000011111110000000001111111001000000011111111100000000001111111000000000000111110100000000000000',
  75. 'X'=>'000000000000000000010110000000000000000110111100000000000011100111111000000000111000001111000000001100000000011110000011000000000000111110110000000000000111111100000000000000001111000000000000000001111100000000000001111001111000000000011110001111110000000111100000011110000001110000000000111100011100000000000001110110000000000000001110100000000000000000010',
  76. 'Y'=>'110000000000000000000111100000000000000000111111000000000000000001111000000000000000000011111000000000000000000111110000000000000000111111111111110000000000111111111110000000001111111111110000000011100000000000000001111000000000000000111100000000000000001110000000000000000011100000000000000000110000000000000000000100000000000000000000',
  77. 'Z'=>'000000000000000000110110000000000000011110110000000000001111110110000000000011111110110000000000111100110110000000011110000110110000000111100000110110000011111000000110110001111100000000110110011111000000000110110111100000000000110111110000000000000110111100000000000000110111000000000000000110',
  78. '0'=>'000001011110100000000000111111111111110000001111111101111110000011100000000000011100011000000000000001100110000000000000001110110000000000000000110110000000000000000110110000000000000000110111000000000000011100011111000000000111100001111111110111111000000011111111111100000000000111111110000000',
  79. '1'=>'001100000000000000110001100000000000000110011000000000000000110011000000000000000110011111111111111111110011111111111111111110111111111111111111110000000000000000000110000000000000000000110000000000000000000110000000000000000000110',
  80. '2'=>'000000000000000000110001100000000000111110011000000000000111110011000000000001101110110000000000011000110110000000000111000110110000000001110000110110000000011100000110111000000111000000110111100001111000000110011111111100000000110001111111000000000110000111110000000000110',
  81. '3'=>'011000000000000001100010000000000000001100110000000110000000110110000000110000000110110000000110000000110110000000110000000110110000001110000000110111000011011000001110111111111011000011100011111110001111111100001110100001111111000000000000000111101000',
  82. '4'=>'000000000000110000000000000000011110000000000000000111110000000000000001100110000000000001111000110000000000011110000110000000000111100000110000000001110000000110000000011100000000110000000111111111111111111110111111111111111111110111111111111111111110000000000000110000000000000000000110000000000000000000110000000',
  83. '5'=>'111111111000000001100111111111000000001110110000011000000000110110000011000000000110110000011000000000110110000011000000000110110000001100000000110110000001110000001110110000001110000111100110000000111111111100110000000011111111000110000000001111100000',
  84. '6'=>'000001011110111000000000011111111111110000001111111111111111000001110100101000011100011000001100000001100011000001000000001110110000011000000000110110000011000000000110110000011000000000110110000011100000000110110000011110000011100111000001111111111100011000000111111111000000000000001111000000',
  85. '7'=>'110000000000000000000110000000000000000010110000000000000111110110000000000011111110110000000000111111100110000000011111000000110000001111100000000110000011101000000000110001111000000000000110111110000000000000111110000000000000000111100000000000000000111000000000000000000',
  86. '8'=>'000000000000011110000000111000000111111000001111110011111111100011111111111100001100011000111111000001110110000011100000000110110000011110000000110110000001110000000110110000001111000000110111001011111000000110111111110011100011100011111110001111111100001110100001111111000000000000000111100000',
  87. '9'=>'000011111100000000000001111111110000001100001111111110000001100011100000111000000110111000000011100000110110000000001100000110110000000001100000110110000000001100000110110000000001100001100111000000011000011100011111000111001111000001111111111111111000000111111111111000000000000111111110000000',
  88. );
  89. $arr = fixImg("https://xxxxxxxxxx");// 哦,这里隐去某社区域名和验证码地址,为了支持https,你的php环境要开启openssl
  90. for($i = 0;$i < 10;$i++)
  91. unset($arr[$i]);// 前10行是空白
  92. $y = 0;// 采用从上到下,从左到右顺序读特征码,从第0行开始(实际是第10行)
  93. $len = 31;// 多读一行,JQ超过20行,且J后面几行占了上个字符的位置
  94. $code = array();// 分离出来的字符特征码
  95. $str = '';
  96. while($y < count($arr[10])){
  97. $flag = true;// 全0是空白竖线
  98. $line = '';
  99. for($i = 10;$i < $len;$i++){
  100. if($arr[$i][$y])
  101. $flag = false;
  102. $line .= $arr[$i][$y];
  103. }
  104. $isw = false;
  105. $isy = false;
  106. // 对vw的特殊处理
  107. if($str === $char['V'] || $str === $char['W']){
  108. $flag = true;
  109. $isw = true;
  110. }elseif($str === $char['Y'] || $str === $char['A']){
  111. $isy = true;
  112. $flag = true;
  113. }
  114. if($flag){
  115. if(strlen($str) > 21)
  116. $code[] = $str;
  117. $str = '';
  118. }else{
  119. $str .= $line;
  120. }
  121. if($isw){
  122. $str = '00'.substr($line,2,strlen($line));
  123. if($str === '000000000000000000000')
  124. $str = '';
  125. $isw = false;
  126. }
  127. if($isy){
  128. $str = $line;
  129. if($str === '000000000000000000000')
  130. $str = '';
  131. $isy = false;
  132. }
  133. $y++;
  134. }
  135. // 输出字符
  136. foreach($code as $v){
  137. $match = false;
  138. foreach($char as $key => $v2){
  139. if($v === $v2){
  140. echo $key;
  141. $match = true;
  142. }
  143. }
  144. if(!$match)
  145. echo '?';// 没匹配到的字符输出问号
  146. }
复制代码


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