php captcha question

Status
Not open for further replies.

weblucky

New member
Jun 15, 2008
3
0
0
Hello,

Could anybody advise me what I am doing wrong.
I created a php script, which grabs form on a page and also gets captcha and brakes it.
But when I send all data to post back, it is saying that captcha is not broken.
I think this is happening, because it doesn't associate the captcha I grab with the session.
Here is what I do:
1. read the page to open a session.
2. recognize url to captcha script and grab the captcha using it with the same session id.
3. break captcha (script really recognizes it, because I compared it with the saved image) and post data back.

Any ideas would be great.

Thanks.
 


If I had enough experience I wouldn't have asked the question.

I am using curl with CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE parameters to manage cookies.

I checked and all three requests return the same PHPSESSID.
 
make sure you arent starting a new curl session between downloading the captcha and opening it. KEEP THE PAGE OPEN. this is how i would do something like this, assuming the crack is in python

Code:
<?php
/* curl to open page, download captcha */

sh_cmd("path/to/crack.py /path/to/captcha.png %> /dev/null &");
$pid = get_last_pid("crack.py"); // replace crack.py with whatever your scripts "comm" alias is...you gotta figure that out

while(process_is_open($pid))
{
   sleep(1);
}

$captcha_solve = file_get_contents("path/to/crack/log");
// curl to post solved captcha
?>

FUNCTIONS YOU NEED:

Code:
<?php

/*
    * process_is_open($pid)
    * checks if the process w/ PID $pid is open
*/
function process_is_open($pid)
{
    $cmd = sh_cmd('ps -p ' . $pid . ' -o pid,stat', $code);
    if($cmd == "  PID STAT\n") return 0;
    else return 1;
    return 0;    
}

/*
    * get_last_pid($comm) - gets last PID of command $comm i.e. get_last_pid('ffmpeg')
*/
function get_last_pid($comm)
{
    $cmd = sh_cmd('ps -A -o pid,comm | grep ' . $comm, $code);
    $pids = explode("\n", rtrim($cmd, "\n"));
    $pid = str_replace($comm, '', str_replace(' ', '', $pids[count($pids)-1]));
    return $pid;
}

/*
    * sh_cmd($cmd, $code) - replacement function for shell_exec/exec
    * returns full output in $cmd, status in $code
*/
function sh_cmd($cmd, &$code) {
    $descriptorspec = array(
        0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
        1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
        2 => array("pipe", "w") // stderr is a file to write to
    );
   
    $pipes= array();
    $process = proc_open($cmd, $descriptorspec, $pipes);
   
    $output= "";
   
    if (!is_resource($process)) return false;
   
    #close child's input imidiately
    fclose($pipes[0]);
   
    stream_set_blocking($pipes[1],false);
    stream_set_blocking($pipes[2],false);
   
    $todo= array($pipes[1],$pipes[2]);
   
    while( true ) {
        $read= array();
        if( !feof($pipes[1]) ) $read[]= $pipes[1];
        if( !feof($pipes[2]) ) $read[]= $pipes[2];
       
        if (!$read) break;
       
        $ready= stream_select($read, $write=NULL, $ex= NULL, 2);
       
        if ($ready === false) {
            break; #should never happen - something died
        }
       
        foreach ($read as $r) {
            $s= fread($r,1024);
            $output.= $s;
        }
    }
   
    fclose($pipes[1]);
    fclose($pipes[2]);
   
    $code= proc_close($process);
   
    return $output;
}

?>

obviously this can be adjusted. i sent the CAPTCHA cracker to the background so you can do 50 at once if you want...

peace
 
Status
Not open for further replies.