« Best Laptop(s) Revealed: Lenovo Tapes | ^ Main | Stock Photos provided? »


Obfuscated PHP fun with AZPHP

On the AZPHP list Alex Dean wanted folks to get involved in creating obfuscated PHP code and everyone trying to figure out what it does. The first example was quite easy to figure out but Alex posted one that was quite the interesting piece.

<?
	$m = 'hi AzPhP';
	$str=strlen;
	function q($a){
 		for($i=0;$i<strlen($a);$i++){
  			$r[] = str_pad(decbin($a[$i]),3,0,STR_PAD_LEFT);
  		}
 		return $r;
 	}
	$s=array('h'=>'446555','i'=>'030333','A'=>'255755','z'=>'713647','P'=>'655644',''=>'000000');
	for($i=0;$i<$str($m);$i++){
 		$tmp = q($s[$m[$i]]);
 		foreach($tmp as $k=>$v){
  			echo $s[$i] . " : k: " . $k . " : v: " . $v . "<br />";
  			$o[$k] .= '0'.$s[$i].$v;
  		}
 	}
	foreach($o as $v){
 		for($i=0;$i<$str($v);$i++){
  			echo $v[$i]=='1' ? '**' : '&nbsp;&nbsp;';
  		}
 		echo '<br />';
 	}
?>

Can you tell what this does? In his email he didn’t have the whitespace in it but I figured I’d save you the trouble and put it here for you. :-) I also ran the file so I could see the output and here it is.

  **                **    ******  ****    **      ****  
  **        ****  **  **      **  **  **  **      **  **
  ****            **  **    ****  **  **  ****    **  **
  **  **    ****  ******  ****    ****    **  **  ****  
  **  **    ****  **  **  **      **      **  **  **    
  **  **    ****  **  **  ******  **      **  **  **    

Very clever huh? I thought so. Alex stated it is easy to figure out what it does (by running of course) but how does it do it? I took a stab at it on the list and figured I’d post here as well. Here is my email to the group.

———————————————————————————————————-
I’ll take a stab. (steps in order it runs)

1) set $m to “hi AzPhP”
2) make a reference of strlen in $str
3) setup array $s and populate with decimal representation of letter
4) loop over $m
5) pass to function q() the decimal representation of letter $m[$i] as held in the $s array
6) (in function q()) loop over string length of argument $a
7) convert decimal representation of letter into binary representation and add 3 spaces to the left
8) return result to #5
9) loop over result stored in $tmp
10) you got tricky here. you push into an array the value $v with a 0 in front. The $s[$i] has no value so nothing will be appended to the 0 show…very tricky here. The actual value would be the binary representation with a 0 in front.
11) you loop over $o (after populated) with the foreach
12) an internal for loop loops over the current string length of $v and echo’s a ** if $v[$i] is equal to 1 otherwise it echo’s 2 spaces.
13) after the inner loop is done (1 row complete) insert a line break

This is a very clever example. Hopefully I didn’t miss anything here but this did indeed intrigued me enough to add the whitespace and run it. Good stuff Alex!
———————————————————————————————————-

I’m sure he’ll post soon and let me know how close I am. :-)

Posted by John C. Bland II on April 1, 2006 11:04 AM |

TrackBack

TrackBack URL for this entry:
http://mt.katapultmedia.com/mt-tb.cgi/92

Post a comment