Heim >Backend-Entwicklung >PHP-Tutorial >验证码破解和反破解的思路

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

WBOY
WBOYOriginal
2016-07-25 08:51:051617Durchsuche
使用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 $color = imagecolorat($img, $i, $middle);// 读取中线的像素颜色,中线肯定会切到字符。。。
  16. $colors[] = $color;
  17. }
  18. $mainColor = getMostRepeated($colors);// 分离出字符颜色
  19. $pic = array();
  20. for ($x = 0; $x $flag = true;
  21. for ($y = 0; $y $currentColor = imagecolorat($img, $x, $y);
  22. $pos = 0;// 原图片用的是斜体,为了方便分割字符,转成正体,所以用斜体是没用的。。。
  23. if($y $pos = 0;
  24. }elseif($y >= 16 && $y $pos = 1;
  25. $pic[$y][0] = 0;
  26. }elseif($y >= 21 && $y $pos = 2;
  27. $pic[$y][0] = $pic[$y][1] = 0;
  28. }else{
  29. $pos = 3;
  30. $pic[$y][0] = $pic[$y][1] = $pic[$y][2] = 0;
  31. }
  32. // 因为干扰像素和字符的颜色完全不同,轻而易举的分离出字符色,字符像素点置1,所以干扰像素颜色至少要有部分是和字符颜色相同才行!
  33. if ($currentColor !== $mainColor) {
  34. $pic[$y][$x+$pos] = 0;
  35. }else{
  36. $pic[$y][$x+$pos] = 1;
  37. $flag = false;
  38. }
  39. }
  40. }
  41. return $pic;
  42. }
  43. // 传说中的特征码
  44. $char = array(
  45. 'A'=>'000000000000000000010000000000000000010110000000000000001111100000000000000011101000000000000011111000000000000001111110000000000001011100110000000000111110000110000000001110100000110000000111100000000110000000111111000000110000000111111111000110000000000011111110110000000000000111111110000000000000000111111110000000000000001111111000000000000000011111110000000000000000001110000000000000000000110',
  46. 'B'=>'111111111111111111110111111111111111111110111111111111111111110110000000110000000110110000000110000000110110000000110000000110110000001110000000110111000011111000000110111111111001100011110011111110001111111100001110100001111111100000000000000111101000',
  47. 'C'=>'000001011110000000000000011111111111110000000111111111111110000001110100001111111000011100000000000011100011000000000000001100011000000000000001110110000000000000001110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000001100011000000000000001100011000000000000011000',
  48. 'D'=>'111111111111111111110111111111111111111110111111111111111111110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000000110111000000000000001110011000000000000011100011110000000000111100001111000000001111000000111111111111111000000011111111111100000000000111111110000000',
  49. 'E'=>'111111111111111111110111111111111111111110111111111111111111110110000000110000000110110000000110000000110110000000110000000110110000000110000000110110000000110000000110110000000110000000110110000000110000000110110000000000000000110000000000000000000110',
  50. 'F'=>'111111111111111111110111111111111111111110111111111111111111110110000000110000000000110000000110000000000110000000110000000000110000000110000000000110000000110000000000110000000110000000000110000000110000000000110000000000000000000',
  51. 'G'=>'000001011110000000000000011111111111110000000111111111111110000001110100001111111000011100000000000011100011000000000000001100011000000000000001110110000000000000001110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000001111111110011000000001111111100011000000001111111100',
  52. 'H'=>'111111111111111111110111111111111111111110111111111111111111110000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000111111111111111111110111111111111111111110111111111111111111110',
  53. 'I'=>'111111111111111111110111111111111111111110111111111111111111110',
  54. 'J'=>'111111111111111111111111111111111111111111111111111111111111110',
  55. 'K'=>'111111111111111111110111111111111111111110111111111111111111110000000000110000000000000000001110000000000000000011011000000000000001111001110000000000011110001111000000000111100000111100000001110000000001110000011100000000000111000111000000000000011100110000000000000001110100000000000000000110000000000000000000010',
  56. 'L'=>'111111111111111111110111111111111111111110111111111111111111110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110',
  57. 'M'=>'111111111111111111110111111111111111111110111111000000000000000111111100000000000000000111111110000000000000000111111000000000000000011111111100000000000000001111110000000000000000111111100000000000000000111100000000000000011111100000000000010111000000000000001111100000000000011111101000000000000111110000000000000011100000000000000000111111111111111111110111111111111111111110111111111111111111110',
  58. 'N'=>'111111111111111111110111111111111111111110111110000000000000000011111000000000000000000111100000000000000000001110000000000000000000111100000000000000000011110000000000000000000111000000000000000000001110000000000000000001111100000000000000000111110000000000000000001111000000000000000000011110111111111111111111110111111111111111111110',
  59. 'O'=>'000001011110000000000000011111111111100000000111111111111110000001110100000011111000011100000000000011100011000000000000001100011000000000000001110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000000110111000000000000000110011000000000000011100011110000000000111100001111000000001111000000111111111111111000000001111111111100000000000111111110000000',
  60. 'P'=>'111111111111111111110111111111111111111110111111111111111111110110000000011000000000110000000011000000000110000000011000000000110000000111000000000110000000111000000000111100001110000000000011111111100000000000011111111000000000000000111110000000000000',
  61. 'Q'=>'000001011110000000000000011111111111100000000111111111111110000001110100001111111000011100000000000011100011000000000000001100010000000000000001110110000000000000000110110000000000000000110110000000000000000110110000000000000000110110000000000000000110111000000000000000111011000000000000011111011110000000000111101001111000000001111001000111111111111110000000001111111111000000000000111111110000000',
  62. 'R'=>'111111111111111111110111111111111111111110111111111111111111110110000000110000000000110000000110000000000110000000110000000000110000000110000000000110000001111100000000111001011101111000000111111111001111110000011111111000011110000001110110000000111100000000000000000001110000000000000000001110000000000000000000010',
  63. 'S'=>'000111000000000001000001111110000000001100011111111000000001100011000111100000000110110000011110000000110110000001110000000110110000001110000000110110000000111000000110110000000011100011100111000000011111111100011000000001111111000000000000001111100000',
  64. 'T'=>'110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000111111111111111111110111111111111111111110111111111111111111110110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000',
  65. 'T '=>'110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000111111111111111111110111111111111111111110111111111111111111110110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000',//vt时t左边会被侵占掉一列
  66. 'U'=>'111111111111111110000111111111111111110000111111111111111111100000000000000000001100000000000000000001110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000000110000000000000000001100000000000000000111100111111111111111111000111111111111111000000',
  67. 'V'=>'111100000000000000000111111000000000000000111111111000000000000000001111110000000000000000111111100000000000000000111111110000000000000001111111000000000000000001111110000000000000000011110000000000000001111110000000000010111101000000000000111110000000000001011111000000000000111111000000000000001110100000000000000111100000000000000000',//少读一列
  68. 'W'=>'111111000000000000000111111111000000000000111111111110000000000000000111111111110000000000001111111110000000000000001111111110000000000000000111110000000000000011111110000000001111111001000000011111111100000000011111110000000000000111111000000000000000111111000000000000000111111111110000000000000000111111111110000000000011111111111000000000000001111111110000000000000000111110000000000000011111110000000001111111001000000011111111100000000001111111000000000000111110100000000000000',
  69. 'X'=>'000000000000000000010110000000000000000110111100000000000011100111111000000000111000001111000000001100000000011110000011000000000000111110110000000000000111111100000000000000001111000000000000000001111100000000000001111001111000000000011110001111110000000111100000011110000001110000000000111100011100000000000001110110000000000000001110100000000000000000010',
  70. 'Y'=>'110000000000000000000111100000000000000000111111000000000000000001111000000000000000000011111000000000000000000111110000000000000000111111111111110000000000111111111110000000001111111111110000000011100000000000000001111000000000000000111100000000000000001110000000000000000011100000000000000000110000000000000000000100000000000000000000',
  71. 'Z'=>'000000000000000000110110000000000000011110110000000000001111110110000000000011111110110000000000111100110110000000011110000110110000000111100000110110000011111000000110110001111100000000110110011111000000000110110111100000000000110111110000000000000110111100000000000000110111000000000000000110',
  72. '0'=>'000001011110100000000000111111111111110000001111111101111110000011100000000000011100011000000000000001100110000000000000001110110000000000000000110110000000000000000110110000000000000000110111000000000000011100011111000000000111100001111111110111111000000011111111111100000000000111111110000000',
  73. '1'=>'001100000000000000110001100000000000000110011000000000000000110011000000000000000110011111111111111111110011111111111111111110111111111111111111110000000000000000000110000000000000000000110000000000000000000110000000000000000000110',
  74. '2'=>'000000000000000000110001100000000000111110011000000000000111110011000000000001101110110000000000011000110110000000000111000110110000000001110000110110000000011100000110111000000111000000110111100001111000000110011111111100000000110001111111000000000110000111110000000000110',
  75. '3'=>'011000000000000001100010000000000000001100110000000110000000110110000000110000000110110000000110000000110110000000110000000110110000001110000000110111000011011000001110111111111011000011100011111110001111111100001110100001111111000000000000000111101000',
  76. '4'=>'000000000000110000000000000000011110000000000000000111110000000000000001100110000000000001111000110000000000011110000110000000000111100000110000000001110000000110000000011100000000110000000111111111111111111110111111111111111111110111111111111111111110000000000000110000000000000000000110000000000000000000110000000',
  77. '5'=>'111111111000000001100111111111000000001110110000011000000000110110000011000000000110110000011000000000110110000011000000000110110000001100000000110110000001110000001110110000001110000111100110000000111111111100110000000011111111000110000000001111100000',
  78. '6'=>'000001011110111000000000011111111111110000001111111111111111000001110100101000011100011000001100000001100011000001000000001110110000011000000000110110000011000000000110110000011000000000110110000011100000000110110000011110000011100111000001111111111100011000000111111111000000000000001111000000',
  79. '7'=>'110000000000000000000110000000000000000010110000000000000111110110000000000011111110110000000000111111100110000000011111000000110000001111100000000110000011101000000000110001111000000000000110111110000000000000111110000000000000000111100000000000000000111000000000000000000',
  80. '8'=>'000000000000011110000000111000000111111000001111110011111111100011111111111100001100011000111111000001110110000011100000000110110000011110000000110110000001110000000110110000001111000000110111001011111000000110111111110011100011100011111110001111111100001110100001111111000000000000000111100000',
  81. '9'=>'000011111100000000000001111111110000001100001111111110000001100011100000111000000110111000000011100000110110000000001100000110110000000001100000110110000000001100000110110000000001100001100111000000011000011100011111000111001111000001111111111111111000000111111111111000000000000111111110000000',
  82. );
  83. $arr = fixImg("https://xxxxxxxxxx");// 哦,这里隐去某社区域名和验证码地址,为了支持https,你的php环境要开启openssl
  84. for($i = 0;$i unset($arr[$i]);// 前10行是空白
  85. $y = 0;// 采用从上到下,从左到右顺序读特征码,从第0行开始(实际是第10行)
  86. $len = 31;// 多读一行,JQ超过20行,且J后面几行占了上个字符的位置
  87. $code = array();// 分离出来的字符特征码
  88. $str = '';
  89. while($y $flag = true;// 全0是空白竖线
  90. $line = '';
  91. for($i = 10;$i if($arr[$i][$y])
  92. $flag = false;
  93. $line .= $arr[$i][$y];
  94. }
  95. $isw = false;
  96. $isy = false;
  97. // 对vw的特殊处理
  98. if($str === $char['V'] || $str === $char['W']){
  99. $flag = true;
  100. $isw = true;
  101. }elseif($str === $char['Y'] || $str === $char['A']){
  102. $isy = true;
  103. $flag = true;
  104. }
  105. if($flag){
  106. if(strlen($str) > 21)
  107. $code[] = $str;
  108. $str = '';
  109. }else{
  110. $str .= $line;
  111. }
  112. if($isw){
  113. $str = '00'.substr($line,2,strlen($line));
  114. if($str === '000000000000000000000')
  115. $str = '';
  116. $isw = false;
  117. }
  118. if($isy){
  119. $str = $line;
  120. if($str === '000000000000000000000')
  121. $str = '';
  122. $isy = false;
  123. }
  124. $y++;
  125. }
  126. // 输出字符
  127. foreach($code as $v){
  128. $match = false;
  129. foreach($char as $key => $v2){
  130. if($v === $v2){
  131. echo $key;
  132. $match = true;
  133. }
  134. }
  135. if(!$match)
  136. echo '?';// 没匹配到的字符输出问号
  137. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn