Location: PHPKode > projects > RIPS > functions/output.php
<?php
/** 

RIPS - A static source code analyser for vulnerabilities in PHP scripts 
	by Johannes Dahse (hide@address.com)
			
			
Open source under the BSD License.

Copyright (c) 2010, Johannes Dahse
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.		

**/
	
	// array to concated string
	function createlist($list)
	{
		$out = '';
		if(is_array($list))
		{
			$list = array_unique($list);
			foreach($list as $element)
			{
				$out .= "$element,";
			}
			$out = substr($out, 0, -1);
		}
		return $out;
	}
	
	// reference function declaration with function calls
	function makefunclink($function_obj)
	{
		if($function_obj->marker !== 3)
		{
			$link = '<A NAME="'.$function_obj->name.'_declare"></A>';
			$link.= '<a href="#'.$function_obj->name.'_call" title="jump to call">';
			$link.= highlightline($function_obj->value, $function_obj->lines[0]).'</a>';
			$function_obj->value = $link;
			// mark as potential userinput
			$function_obj->marker = 3;
		}
	}
	
	// prepare output to style with CSS
	function highlightline($line, $line_nr, $title=false, $udftitle=false)
	{
		$reference = true;
		$tokens = @token_get_all('<? '.trim($line).' ?>');
		$output = "<span class=\"linenr\">$line_nr:</span>&nbsp;";
		if($title)
		{
			$output.='<a class="link" href="'.$GLOBALS['doku'].$title.'" title="open php documentation" target=_blank>';
			$output.="$title</a>&nbsp;";
		} 
		else if($udftitle)
		{
			$output.='<a class="link" href="#'.$udftitle.'_declare" title="jump to declaration">'.$udftitle.'</a>&nbsp;';
		}
		foreach ($tokens as $token)
		{
			if (is_string($token))
			{		
				$output .= '<span class="phps-code">'.htmlentities($token, ENT_QUOTES, 'utf-8').'</span>';
			} 
			else if (is_array($token) 
			&& $token[0] !== T_OPEN_TAG
			&& $token[0] !== T_CLOSE_TAG) 
			{
				$text = htmlentities($token[1], ENT_QUOTES, 'utf-8');
				$text = str_replace(array(' ', "\n"), array('&nbsp;', '<br/>'), $text);
				
				if($token[0] === T_FUNCTION)
				{
					$reference = false;
				}
				if($token[0] === T_STRING && $reference 
				&& isset($GLOBALS['user_functions_offset'][$text]))
				{				
					$text = @"<span onmouseover=\"getFuncCode(this,'{$GLOBALS['user_functions_offset'][$text][0]}','{$GLOBALS['user_functions_offset'][$text][1]}','{$GLOBALS['user_functions_offset'][$text][2]}')\" style=\"text-decoration:underline\" class=\"phps-".str_replace('_', '-', strtolower(token_name($token[0])))."\">$text</span>";
				}	
				else if ($token[0] !== T_WHITESPACE)
				{
					$text = '<span class="phps-'.str_replace('_', '-', strtolower(token_name($token[0])))."\">$text</span>";
				}
				$output .= $text;
			}
		}
		return $output;
	}
	
	// traced parameter output
	function depthFirstTraverse($tree) 
	{
		echo '<ul';
		switch($tree->marker) 
		{
			case 1: echo ' class="userinput"'; break;
			case 2: echo ' class="validated"'; break;
			case 3: echo ' class="functioninput"'; break;
			case 4: echo ' class="persistent"'; break;
		}
		echo '><li>' . $tree->value;

		foreach ($tree->children as $child) 
		{
			depthFirstTraverse($child);
		}
		
		echo '</li></ul>',"\n";
	}

	// requirements output
	function depthSecondTraverse($tree) 
	{
		if(!empty($tree->dependencies))
		{
			echo '<ul><li><span class="requires">requires:</span>';

			foreach ($tree->dependencies as $linenr=>$dependency) 
			{
				if(!empty($dependency))
				{
					echo '<ul><li>'.highlightline($dependency, $linenr).'</li></ul>';
				}
			}

			echo '</li></ul>',"\n";
		}
	}
	
	// print the scanresult
	function printoutput($output, $verbosity)
	{
		do
		{
			if(!empty($output))
			{
				if($verbosity < 5)
				{
					// clean output (vulnerable function declaration with no calls)
					for($i=count($output[key($output)])-1; $i>=0; $i--)
					{		
						$func_depend = $output[key($output)][$i]->funcdepend;
						if( $func_depend 
						&& !isset($GLOBALS['user_functions'][key($output)][$func_depend]['called']))
						{
							$value = $output[key($output)][$i]->name;
							unset($output[key($output)][$i]);
							if( isset($GLOBALS['user_functions'][key($output)][$value]) )
								unset($GLOBALS['user_functions'][key($output)][$value]);	
						}
					}
				}	
					
				if(key($output) != "" && !empty($output[key($output)]) )
				{
					echo '<div class="filebox">',
					'<span class="filename">File: '.key($output).'</span><br>',
					'<div id="'.key($output).'"><br>';
	
					foreach($output[key($output)] as $tree)
					{		
						echo '<div class="codebox"><table border=0>',"\n",
						'<tr><td valign="top" nowrap>',"\n",
						'<div class="fileico" onClick="openCodeViewer(this,\'',
						$tree->filename ? $tree->filename : key($output), '\',\'',
						implode(',', $tree->lines), '\');"></div>'."\n",
						'<div id="pic',key($output),$tree->lines[0],'" class="minusico"',
						' onClick="hide(\'',key($output),$tree->lines[0],'\')"></div>',"\n";
					
						if(!empty($tree->get) || !empty($tree->post) 
						|| !empty($tree->cookie) || !empty($tree->files))
						{
							echo '<br><div class="exploit" ',
							'onClick="openExploitCreator(this, \'',
							$tree->filename ? $tree->filename : key($output),
							'\',\'',createlist($tree->get),'\',\'',createlist($tree->post),
							'\',\'',createlist($tree->cookie),'\',\'',createlist($tree->files),'\');">',
							'</div>',"\n";
						}
					
						echo '</td><td><div class="code" id="'.key($output).$tree->lines[0].'">',"\n";

						depthFirstTraverse($tree);

						echo '<ul><li>',"\n";
						depthSecondTraverse($tree);
						echo '</li></ul>',"\n",	'</div>',"\n", '</td></tr></table></div>',"\n";
					}

					echo '</div><div class="buttonbox">',"\n",
					'<input type="submit" class="Button" value="hide all" ',
					'onClick="hide(\'',key($output),'\')">',"\n",
					'</div></div><hr>',"\n";
				}	
				else if(count($output) == 1)
				{
					echo '<div style="margin-left:30px;color:#000000">Nothing vulnerable found. Change the verbosity level and try again.</div>';
				}
			}
			else if($GLOBALS['scanned_files'] > 0)
			{
				echo '<div style="margin-left:30px;color:#000000">Nothing vulnerable found. Change the verbosity level and try again.</div>';
			}
			else
			{
				echo '<div style="margin-left:30px;color:#000000">Nothing to scan.</div>';
			}
		}
		while(next($output));
	}
	
	// build list of available functions
	function createFunctionList($user_functions_offset)
	{
		if(!empty($user_functions_offset))
		{
			ksort($user_functions_offset);
			echo '<table>';
			foreach($user_functions_offset as $func_name => $info)
			{
				echo '<tr><td><div class="funclistline" title="',$info[0],'" ',
				'onClick="openCodeViewer(3, \'',$info[0],'\', \'',($info[1]+1),
				',',(!empty($info[2]) ? $info[2]+1 : 0),'\')">',$func_name,'</div></td></tr>';
			}
			echo '</table>';
		}
	}
	
	function createUserinputList($user_input)
	{
		if(!empty($user_input))
		{
			ksort($user_input);
			echo '<table>';
			foreach($user_input as $input_name => $file)
			{
				$finds = array();
				foreach($file as $file_name => $lines)
				{
					foreach($lines as $line)
					{
						$finds[] = "<span class=\"funclistline\" title=\"$file_name\" onClick=\"openCodeViewer(4, '$file_name', '$line')\">$line</span>";
					}
				}
				echo "<tr><td nowrap>$input_name</td><td nowrap>",createlist($finds),'</td></tr>';

			}
			echo '</table>';
		}
	}
	
?>	
Return current item: RIPS