首先要知道为什么要在openwrt搭建PHP环境并且识别验证码

因为很多宽带在登录上为了防止路由器或者其他的原因,添加了验证码,而我们就是不想去手动输入,有人问为什么不用ocr,目前我见到的是用Python在Linux上运行ocr,问题是openwrt的内存太小了,装Python是老鼠吃大象(当然可以挂载U盘去装)。

QQ截图20160502000757.png

首先更新openwrt的软件源

opkg update

安装PHP环境

opkg install php5 php5-mod-gd php5-mod-curl php5-mod-session php5-mod-pdo php5-mod-mcrypt php5-mod-mbstring php5-fastcgi php5-cgi php5-mod-ctype php5-mod-exif php5-mod-iconv php5-mod-json php5-mod-sockets php5-mod-sqlite3 php5-mod-tokenizer php5-mod-zip

在/etc/httpd.conf最后面添加如下

config uhttpd web
    list    listen_http 0.0.0.0:88
    option  home    /www/php
    option  cgi_prefix  /cgi-bin
    option  index_page  "index.html index.php"
    list    interpreter ".php=/usr/bin/php-cgi"

上面监听http的88端口

设置88端口的根目录为/www/php

然后重启http服务

 /etc/init.d/uhttpd restart

然后打开/www/php

在里面添加php文件,然后添加如下的识别验证码的代码

(PHP验证码识别由HS5233提供)

先点击下载识别验证码源码

注意!!!只能识别特别方正的数字验证码!!!

放好以后,可以通过 路由器IP:88 访问PHP页面


最后写一个shell脚本,让路由器启动就自行登录

#! /bin/sh -
while true
do
pinga=`ping 180.76.76.76 -c 1`
case "$pinga"
     in
  *ttl* ) ;;
  *  ) pingb=`ping 180.97.33.107 -c 1`
        case "$pingb"
                 in
          *ttl* ) ;;
          *  ) curl -g "http://192.168.1.1:88/index.php";;
        esac
esac
done

然后把本shell添加到路由器启动项里面即可!


我在验证码里面改动了一下,让他识别后帮我提交:

<?php
    error_reporting(0);
	$cookie = dirname(__FILE__)."/valid.tmp";//注意要先给当前目录777权限!
    
    //执行
    $validCode=getIdentify($cookie);
    //需要开启PHP的shell_exec
    $ip=shell_exec("ifconfig eth1| grep '10.1' | cut -f 2 -d ':' | cut -f 1 -d ' '");
    $ip=substr($ip,0,-1);
    //此处用var_dump($ip)查看截取多少位
    
	$url='http://125.88.59.131:10001/login.do';
	
	$username="";//账号
	$pwd="";//密码
	
	$data="edubas=113.98.13.29&eduuser=".$ip."&userName1=".$username."&password1=".base64_encode($pwd)."&rand=".$validCode;
        $result=http_post_request($url,$data,$cookie);
       
    $str1=mb_convert_encoding($result, "utf-8", "gb2312");
    if(preg_match('/success/',$str1)){
		echo "login success!";
    }else{
		echo "login failed!";
    }
function http_post_request($url,$data,$cookie){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_REFERER, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
    curl_close($curl);
    return $result;
}
    	
	
function getIdentify($cookie){
		$t=time();
        $url = "http://125.88.59.131:10001/common/image.jsp?time=".$t;       //验证码地址

		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
		curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
		$img = curl_exec($curl);
		curl_close($curl);
        $fp = fopen("identify.jpg","w");
        fwrite($fp,$img);
        fclose($fp);

        $res = imagecreatefromjpeg("identify.jpg");     //创建图像   
        $size = getimagesize("identify.jpg");           //获得图像大小
		// print_r($size);
        //echo '<img src="identify.jpg"/><br/>';                 //显示验证码
		// exit;
        $img_w = $size[0]-5;       //图像宽度
        $img_h = $size[1]-4;        //图像高度

        //二值化
        for($i=3,$_i=0;$i < $img_h;$_i++,$i++){
            for($j=7,$_j=0; $j < $img_w;$_j++,$j++){
                $rgb = imagecolorat($res,$j,$i);                 //返回 image 所指定的图形中指定位置像素的颜色索引值。
                $rgbarray = imagecolorsforindex($res, $rgb);     //本函数用来取得调色盘上指定索引的颜色值。
                if($rgbarray['red']+$rgbarray['green']+$rgbarray['blue']<400){
                    $data_array[$_i][$_j] = 1;  //验证码部分为1
					// echo '■';
                }else{
                    $data_array[$_i][$_j] = 0;  //空白为0
					// echo '□';
                }
            }
			// echo '<br/>';
        }
		// exit;
        $img_h -= 3;
        $img_w -= 7;
		
        // 切割
        for($i=0;$i<$img_w;$i++){
            for($j=0;$j<$img_h;$j++){
				if($i<9){
					$Vword[0] .= $data_array[$j][$i];
				}else if($i>=13&&$i<22){
					$Vword[1] .= $data_array[$j][$i];
				}else if($i>=26&&$i<35){
					$Vword[2] .= $data_array[$j][$i];
				}else if($i>=39){
					$Vword[3] .= $data_array[$j][$i];
				}
            }
        }

        //四个字符查看,教学使用
        // echo $Vword[0].'|'.strlen($Vword[0]).'<br/>';
        // echo $Vword[1].'|'.strlen($Vword[1]).'<br/>';
        // echo $Vword[2].'|'.strlen($Vword[2]).'<br/>';
        // echo $Vword[3].'|'.strlen($Vword[3]).'<br/>';
		// exit;

        //字典
        $array[0] = array(
            '000111111100001111111111100110000000110110000000001111000000000111100000000011011000000011001111111111100001111111000'
        );
        $array[1] = array(
            '000000000000001100000000110110000000011111000000001111111111111111111111111111000000000001100000000000110000000000011'
        );
        $array[2] = array(
            '011000000011111000000011111100000011011110000011001111000011000111100011000011011111000001100111000000110000000000000'
        );
        $array[3] = array(
            '011000000011011000110000111100011000011110001100001111000110000111100111100111011110111111001110001111000000000000000'
        );
        $array[4] = array(
            '000000001100000000011110000000111111000000111001100001110000110001111111111111111111111111100000000110000000000011000'
        );
        $array[5] = array(
            '111111100011011111110000111100011000011110001100001111000110000111100001100111110000111111011000001111000000000000000'
        );
        $array[6] = array(
            '000011111100000111111111100111001100110110001100001111000110000111100011000011110001110011101100011111100000000111100'
        );
        $array[7] = array(
            '000000000000011000000000001100000000011110000000111111000001111001100011100000110011000000011111000000001110000000000'
        );
        $array[8] = array(
            '000000001110001111001111101111111100111110011100001111000110000111100011100011111111110011101110001111100000000011100'
        );
        $array[9] = array(
            '001111000000001111110001101110011100011110000110001111000011000111100001100011011001100111001111111111000001111110000'
        );

        //根据字符串相似度识别
        for($tmp_count=0;$tmp_count<4;$tmp_count++){
            foreach($array as $key=>$value){
                foreach($value as $v){
                    similar_text($Vword[$tmp_count],$v,$similarResult);
                    if($similarResult>$result[$tmp_count]['similar']){
                        $result[$tmp_count]['similar'] = $similarResult;
                        $result[$tmp_count]['num'] = $key;
                    }
                }
            }
        }
        return $result[0]['num'].$result[1]['num'].$result[2]['num'].$result[3]['num'];
}
?>