当前位置:Gxlcms > PHP教程 > 验证码破解和反破解的思路

验证码破解和反破解的思路

时间:2021-07-01 10:21:17 帮助过:20人阅读

使用php破解验证码,只有一种方法,就是收集验证码里出现字符的特征码,然后想办法分离出验证码里的字符。什么人工智能,显然是php做不到的,所以,只要能收集齐验证码里出现字符的特征码,又能找到方法分离出验证码里的字符,就可以破解。
反破解的思路也主要是围绕这两步,使用更多的字符,比如使用中文,使用更多的字体和变形,使得同一个字符有多个特征码,或者没有固定的特征码。增加分离字符的难度,字符出现在验证码中的位置不固定,大小不固定,间距不固定,干扰像素要能起到作用。
下面以某网站验证码为例,讲解如何通过以上两步破解验证码。
注:部分代码来源于phper“罢巴”,特别鸣谢!
  1. /* 对数值数值进行排序,找到第二多的元素,因为第一多的元素是背景色,第二多才是字符颜色,换句话说,如果每个字符都使用不同的颜色,或者背景色不是那么单调,我还怎么分离出字符颜色呢? */
  2. function getMostRepeated($array){
  3. $count = array_count_values($array);
  4. arsort($count);
  5. $keys = array_keys($count);
  6. return $keys[1];
  7. }
  8. /* 读取验证码图片,并将像素RGB读入二维数组,然后分离出有效字符 */
  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);// 读取中线的像素颜色,中线肯定会切到字符。。。
  17. $colors[] = $color;
  18. }
  19. $mainColor = getMostRepeated($colors);// 分离出字符颜色
  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;// 原图片用的是斜体,为了方便分割字符,转成正体,所以用斜体是没用的。。。
  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. // 因为干扰像素和字符的颜色完全不同,轻而易举的分离出字符色,字符像素点置1,所以干扰像素颜色至少要有部分是和字符颜色相同才行!
  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. // 传说中的特征码
  50. $char = array(
  51. 'A'=>'000000000000000000010000000000000000010110000000000000001111100000000000000011101000000000000011111000000000000001111110000000000001011100110000000000111110000110000000001110100000110000000111100000000110000000111111000000110000000111111111000110000000000011111110110000000000000111111110000000000000000111111110000000000000001111111000000000000000011111110000000000000000001110000000000000000000110',
  52. 'B'=>'111111111111111111110111111111111111111110111111111111111111110110000000110000000110110000000110000000110110000000110000000110110000001110000000110111000011111000000110111111111001100011110011111110001111111100001110100001111111100000000000000111101000',
  53. 'C'=>'000001011110000000000000011111111111110000000111111111111110000001110100001111111000011100000000000011100011000000000000001100011000000000000001110110000000000000001110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000001100011000000000000001100011000000000000011000',
  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'=>'111111111111111111110111111111111111111110111111000000000000000111111100000000000000000111111110000000000000000111111000000000000000011111111100000000000000001111110000000000000000111111100000000000000000111100000000000000011111100000000000010111000000000000001111100000000000011111101000000000000111110000000000000011100000000000000000111111111111111111110111111111111111111110111111111111111111110',
  64. 'N'=>'111111111111111111110111111111111111111110111110000000000000000011111000000000000000000111100000000000000000001110000000000000000000111100000000000000000011110000000000000000000111000000000000000000001110000000000000000001111100000000000000000111110000000000000000001111000000000000000000011110111111111111111111110111111111111111111110',
  65. 'O'=>'000001011110000000000000011111111111100000000111111111111110000001110100000011111000011100000000000011100011000000000000001100011000000000000001110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000000110111000000000000000110011000000000000011100011110000000000111100001111000000001111000000111111111111111000000001111111111100000000000111111110000000',
  66. 'P'=>'111111111111111111110111111111111111111110111111111111111111110110000000011000000000110000000011000000000110000000011000000000110000000111000000000110000000111000000000111100001110000000000011111111100000000000011111111000000000000000111110000000000000',
  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. }

人气教程排行