<?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.
**/
############################### INCLUDES ################################
include('config/general.php'); // general settings
include('config/userinput.php'); // tainted variables and functions
include('config/tokens.php'); // tokens for lexical analysis
include('config/securing.php'); // securing functions
include('config/PVF.php'); // potentially vulnerable functions
include('config/info.php'); // interesting functions
include('functions/tokens.php'); // prepare and fix token list
include('functions/scan.php'); // scan for PVF in token list
include('functions/output.php'); // output scan result
include('classes/classes.php'); // classes
############################### MAIN ####################################
$output = array();
$scan_functions = array();
$user_functions = array();
$user_functions_offset = array();
$user_input = array();
$counterlines=0;
$scanned_files=0;
if(!empty($_POST['loc']))
{
$verbosity = isset($_POST['verbosity']) ? $_POST['verbosity'] : 1;
$location = str_replace('\\', '/', $_POST['loc']);
if(is_dir($location))
{
$scan_subdirs = isset($_POST['subdirs']) ? true : false;
$data = read_recursiv($location, $scan_subdirs);
}
else if(is_file($location))
{
$data[0] = $location;
}
else
{
$data = array();
}
if(!empty($_POST['scan']))
{
switch($_POST['vector'])
{
case 'client': $scan_functions = $F_XSS; break;
case 'code': $scan_functions = $F_CODE; break;
case 'file': $scan_functions = $F_FILE; break;
case 'system': $scan_functions = $F_SYSTEM; break;
case 'database': $scan_functions = $F_DATABASE; break;
case 'connect': $scan_functions = $F_CONNECT; break;
case 'all':
$scan_functions = array_merge(
$F_CODE,
$F_FILE,
$F_SYSTEM,
$F_DATABASE,
$F_CONNECT,
$F_XSS
); break;
default: // all server side
{
$scan_functions = array_merge(
$F_CODE,
$F_FILE,
$F_SYSTEM,
$F_DATABASE,
$F_CONNECT
); break;
}
}
$F_USERINPUT = $F_OTHER_INPUT;
if( $verbosity >= 2 )
{
$F_USERINPUT = array_merge($F_OTHER_INPUT, $F_FILE_INPUT, $F_DATABASE_INPUT);
}
foreach($data as $file_name)
{
if(in_array(substr($file_name, strrpos($file_name, '.')), $GLOBALS['filetypes']))
{
$userfunction_secures = false;
$userfunction_taints = false;
$scanned_files++;
scan_file($file_name, $scan_functions,
$T_FUNCTIONS, $T_ASSIGNMENT, $T_IGNORE,
$T_INCLUDES, $T_XSS, $T_IGNORE_STRUCTURE);
}
}
}
}
$elapsed = microtime(TRUE) - $start;
############################### OUTPUT ####################################
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/default.css" />
<?php
$default_stylesheet = isset($_POST['stylesheet']) ? $_POST['stylesheet'] : $default_stylesheet;
foreach($stylesheets as $stylesheet)
{
echo "\t<link type=\"text/css\" href=\"css/$stylesheet.css\" rel=\"";
if($stylesheet != $default_stylesheet) echo "alternate ";
echo "stylesheet\" title=\"$stylesheet\" />\n";
}
?>
<script src="js/script.js"></script>
<script src="js/exploit.js"></script>
</head>
<body onload="draginit();">
<div class="menu">
<div style="float:left; width:100%;">
<table width="100%">
<tr><td width="70%" nowrap>
<form action="" method="post">
<table class="menutable" width="50%">
<tr>
<td nowrap><b>Directory / File:</b></td>
<td colspan="2" nowrap><input type="text" size=80 name='loc' value='<?php
echo isset($_POST['loc']) ? htmlentities($_POST['loc'], ENT_QUOTES, 'UTF-8') : $basedir;
?>'>
</td>
<td nowrap><input type="checkbox" name="subdirs" value="1" <?php
echo isset($_POST['subdirs']) ? 'checked' : '';
?>/>subdirs</td>
</tr>
<tr>
<td nowrap>verbosity level:</td>
<td nowrap>
<select name="verbosity">
<?php
$c_verbosity = isset($_POST['verbosity']) ? $_POST['verbosity'] : 0;
$verbosities = array(
1 => '1. user tainted only',
2 => '2. file/DB tainted +1',
3 => '3. show secured +1,2',
4 => '4. info gathering +1,2,3',
5 => '5. untainted +1,2,3,4'
);
foreach($verbosities as $level=>$description)
{
echo "<option value=\"$level\" ";
if($level == $c_verbosity) echo 'selected';
echo ">$description</option>";
}
?>
</select>
</td>
<td align="right">
vuln type:
<select name="vector">
<?php
$c_vector = isset($_POST['vector']) ? $_POST['vector'] : '';
$vectors = array(
'server' => 'All server side',
'code' => '- code execution',
'file' => '- file poisoning',
'system' => '- system poisoning',
'database' => '- database poisoning',
'connect' => '- connection poisoning',
'client' => 'All client side',
'all' => 'All'
);
foreach($vectors as $vector=>$description)
{
echo "<option value=\"$vector\" ";
if($vector == $c_vector) echo 'selected';
echo ">$description</option>";
}
?>
</select>
</td>
<td><input type="submit" name="scan" value="scan" class="Button" /></td>
</tr>
<tr>
<td nowrap>code style:</td>
<td nowrap><select name="stylesheet" id="css" onchange="setActiveStyleSheet(this.value);">
<?php
foreach($stylesheets as $stylesheet)
{
echo "<option value=\"$stylesheet\" ";
if($stylesheet == $default_stylesheet) echo 'selected';
echo ">$stylesheet</option>\n";
}
?>
</select></td>
<td align="right">
<?php
if(!empty($user_input))
{
echo '<input type="button" class="Button" value="user input" ',
'onClick="openUserinput()" /> ';
}
if(!empty($user_functions_offset))
{
echo '<input type="button" class="Button" value="function list" ',
'onClick="openFunctions()" />';
}
?>
</td>
<td></td>
</tr>
</table>
</form>
</td>
<td width="30%" align="center" nowrap>
<span class="Title"><?php echo $title ?></span>
</td></tr>
</table>
</div>
<div class="footer">
<?php printf("<p>Scanned $counterlines lines in $scanned_files files for ".
count($scan_functions).' functions in %.03f seconds.</p>', $elapsed); ?>
</div>
<div style="clear:left;"></div>
</div>
<div class="menushade"></div>
<div id="window1" name="window">
<div class="windowtitlebar">
<div id="windowtitle1" onClick="top(1)" onmousedown="dragstart(1)" class="windowtitle"></div>
<input type="button" class="closebutton" value="x" onClick="closeWindow(1)" />
</div>
<div id="windowcontent1" class="windowcontent"></div>
<div class="windowfooter" onmousedown="resizestart()"></div>
</div>
<div id="window2" name="window">
<div class="windowtitlebar">
<div id="windowtitle2" onClick="top(2)" onmousedown="dragstart(2)" class="windowtitle"></div>
<input type="button" class="closebutton" value="x" onClick="closeWindow(2)" />
</div>
<div id="windowcontent2" class="windowcontent"></div>
<div class="windowfooter" onmousedown="resizestart()"></div>
</div>
<div id="window3" name="window">
<div class="funclisttitlebar">
<div id="windowtitle3" onClick="top(3)" onmousedown="dragstart(3)" class="funclisttitle">
user defined functions
</div>
<input type="button" class="closebutton" value="x" onClick="closeWindow(3)" />
</div>
<div id="windowcontent3" class="funclistcontent">
<?php
createFunctionList($user_functions_offset);
?>
</div>
<div class="funclistfooter" onmousedown="resizestart()"></div>
</div>
<div id="window4" name="window">
<div class="funclisttitlebar">
<div id="windowtitle4" onClick="top(4)" onmousedown="dragstart(4)" class="funclisttitle">
user input
</div>
<input type="button" class="closebutton" value="x" onClick="closeWindow(4)" />
</div>
<div id="windowcontent4" class="funclistcontent">
<?php
createUserinputList($user_input);
?>
</div>
<div class="funclistfooter" onmousedown="resizestart()"></div>
</div>
<div id="funccode" onmouseout="closeFuncCode()">
<div id="funccodetitle"></div>
<div id="funccodecontent"></div>
</div>
<?php
if(isset($_POST['loc']))
{
@printoutput($output, $verbosity);
}
else
{
echo '<div style="margin-left:30px;color:#000000">',
'Locate your PHP file(s), choose the vulnerability type and verbosity level and hit scan!<br><br>',
'</div>';
}
?>
</body>
</html>