Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as PHP by KAXD ( 17 years ago )
private function crack_captcha() {

		// log time
		$timestart = microtime(true);
		
		// Init
		$tmpfile = $this->curl_tmpdir.$this->curl_tmpfileprefix.time().'.jpg';
				
		// Open filehandle for captcha File
		$out = @fopen($tmpfile, 'wb');
		if ($out == false) {
		  $this->verbose('ERROR: Cant write to tmp captcha file');
		  return false;
		}
		
		// Save captcha to filehandle
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_FILE, 				$out);
		curl_setopt($ch, CURLOPT_HEADER,			false);
		curl_setopt($ch, CURLOPT_URL,           	'http://www.pennergame.de/security/captcha'.time().'.jpg');
		curl_setopt($ch, CURLOPT_TIMEOUT,       	$this->curl_timeout);
		curl_setopt($ch, CURLOPT_USERAGENT,     	$this->curl_useragent);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION,	true);
		curl_setopt($ch, CURLOPT_COOKIEJAR, 		$this->curl_cookiefile);
		curl_setopt($ch, CURLOPT_COOKIEFILE, 		$this->curl_cookiefile);
		curl_exec($ch);
		curl_close($ch);
		@fclose($out);
		
		// Load Image with GDlib
		$img_file = @imagecreatefromjpeg($tmpfile);
		// delete tmpfile
		@unlink($tmpfile);

		// No image loaded = error
		if(!$img_file) {
		  $this->verbose('ERROR: Captcha image error');
		  return false;
		}

		// Set Captcha vars
		$c_radius_min = 7;
		$c_radius_max = 21;
		$c_img_width = 204;
		$c_img_height = 54;
		$c_circle_minfound = 12; // value for a found circle
		
		// We save all pixel values in a array
		// that is way faster than keeping the image
		$img_arr = array();
		// for width do
		for($x=0;$x<=$c_img_width;$x++) {
			// for height do
			for($y=0;$y<=$c_img_height;$y++) {
				// get colours
				$cols = imagecolorsforindex($img_file, imagecolorat($img_file, $x, $y));
				// calcualte grey
			  	$grey = (int)(($cols['red']+$cols['green']+$cols['blue'])/3);
			  	// will it be a black or a white pixel?
			  	if ($grey <= 188) {
			  		// black
			  		$img_arr[$x][$y] = 0;
				}else{
					// white
					$img_arr[$x][$y] = 1;
				}
			}
		}
		// unload gd image
		@imagedestroy($img_file);

		// Search for circles
		$circles = array();
		$lastx = 0;
		$lasty = 0;
		// Foreach defined radius search circles
		$r = $c_radius_min;
		while ($r <= $c_radius_max) {
			$x = $r;
			while ($x <= $c_img_width-$r) {
				$y = $r;
				while ($y <= $c_img_height-$r) {
					
					// calculate crooked distances with sinus
					$s  = (int)round(sin(deg2rad(45))   * $r);
					$ts = (int)round(sin(deg2rad(22.5)) * $r);
					$tb = (int)round(sin(deg2rad(67.5)) * $r);		
					
					$found = 0;
					
					if($img_arr[$x][$y+$r] == 1) { $found++; } // Top center
					if($img_arr[$x+$r][$y] == 1) { $found++; } // Right center
					if($img_arr[$x][$y-$r] == 1) { $found++; } // Bottom center
					if($img_arr[$x-$r][$y] == 1) { $found++; } // Left center
					
					//  45&Atilde;‚&Acirc;&deg; Degrees
					// Open rechts
					if($img_arr[$x+$s][$y+$s] == 1) { $found++; } // Top right
					if($img_arr[$x-$s][$y+$s] == 1) { $found++; } // Top left
					if($img_arr[$x+$s][$y-$s] == 1) { $found++; } // Bottom right
					if($img_arr[$x-$s][$y-$s] == 1) { $found++; } // bottom left

					// 22,5&Atilde;‚&Acirc;&deg; Degrees
					if($img_arr[$x+$tb][$y+$ts] == 1) { $found++; } // Top right		
					if($img_arr[$x-$tb][$y+$ts] == 1) { $found++; } // Top left
					if($img_arr[$x-$tb][$y-$ts] == 1) { $found++; }	// Bottom left				
					if($img_arr[$x+$tb][$y-$ts] == 1) { $found++; }	// Bottom right	

					// 64,5&Atilde;‚&Acirc;&deg; Degrees
					if($img_arr[$x+$ts][$y+$tb] == 1) { $found++; } // Top right
					if($img_arr[$x+$ts][$y-$tb] == 1) { $found++; }	// Bottom right				
					if($img_arr[$x-$ts][$y+$tb] == 1) { $found++; } // Top left
					if($img_arr[$x-$ts][$y-$tb] == 1) { $found++; }	// Bottom left
						
					// check if enough pixels found
					if ($found >= $c_circle_minfound) {
						// Add current coordinates to circles array
						$circles[$x][$y] = $r;
						$circles[$x][$y] = $r;
					}
					$y++;
				}	
				$x++;
			}
			$r++;
		}
		
		// Search for circles with a missing part
		$aims = array();
		foreach($circles AS $x => $xarray) {
			foreach($xarray AS $y => $r) {
							
				// Count all white and black pixels 
				$pixel_white = 0;
				$pixel_black = 0;
				$last = '';
				
				// try to adjust our position if we somehow missed the center of our circle
				// too right
				if ( ($img_arr[$x+$r][$y] == 0) &&  ($img_arr[$x+$r-1][$y] == 1) ) $x--;
				// too low
				if ( ($img_arr[$x][$y+$r] == 0) &&  ($img_arr[$x][$y+$r-1] == 1) ) $y--;
				// too left
				if ( ($img_arr[$x-$r][$y] == 0) &&  ($img_arr[$x-$r+1][$y] == 1) ) $x--;
				// too high
				if ( ($img_arr[$x][$y-$r] == 0) &&  ($img_arr[$x][$y-$r+1] == 1) ) $y--;
					
				// Check all pixel in a circle around current coords.						
				for($d=0; $d<=3600; $d++) {
					
					$xp = (int)round($r * sin(deg2rad($d/10)));
					$yp = (int)round($r * cos(deg2rad($d/10)));
					
					// reduce unnecessary calculations
					if($last == $xp.':'.$yp) continue; 
					$last = $xp.':'.$yp;
					
					// Count pixels that are inside our image
					if( ($x+$xp)>0 && ($x+$xp)<=$c_img_width && ($y+$yp)>0 && ($y+$yp)<=$c_img_height ) {
						if( $img_arr[$x+$xp][$y+$yp] == 0) {
							$pixel_black++;
						}else{
							$pixel_white++;
						}
					}
				}
				// Save current circle with pixel percents in aims array
				$pixel_total = $pixel_black+$pixel_white;
				$pixel_percent = (($pixel_white/$pixel_total)*100);
				$aims[$pixel_percent] = $x.':'.$y;
			}
		}
		
		// Search for the aim with the best percentage
		$best_distance = 99999999;
		$best_percent = $this->captcha_best_percent;
		$avg_percent = $best_percent;
		$best_x = 0;
		$best_y = 0;
		foreach($aims AS $aim => $c) {
			$distance = $avg_percent - $aim;
			if($distance < 0 ) $distance = $distance * -1;
			if($distance < $best_distance) {
				$best_distance = $distance;
				$best_percent = $aim;
				list($best_x,$best_y) = explode(':',$c);
			}
		}
		
		// output time info
		$timeend = microtime(true);
		$this->verbose('Captcha took me '.number_format(($timeend - $timestart), 2, '.', '').' seconds');
		
		// return coordinates if found 
		if($best_x != 0) {
			$this->verbose('Cracked captcha - X='. $best_x.' and Y='. $best_y);
			return array( 	'x' => $best_x,
							'y' => $best_y,
							'd' => $best_distance,
							'p' => $best_percent );
		}
		return false;
	}

 

Revise this Paste

Your Name: Code Language: