<?php
//
// $Id: results.php,v 1.4 2004/11/22 01:23:33 greg_k Exp $
//

/***********************************************************/
/* last update: 12.17.2002                                 */
/* --------------------------------------------------------*/
/*                    Inprotect                            */
/* --------------------------------------------------------*/
/* Copyright (C) 2002 Inprotect.com                        */
/*                                                         */
/* This program is free software; you can redistribute it  */
/* and/or modify it under the terms of version 2 of the    */
/* GNU General Public License as published by the Free     */
/* Software Foundation.                                    */
/* This program is distributed in the hope that it will be */
/* useful, but WITHOUT ANY WARRANTY; without even the      */
/* implied warranty of MERCHANTABILITY or FITNESS FOR A    */
/* PARTICULAR PURPOSE. See the GNU General Public License  */
/* for more details.                                       */
/*                                                         */
/* You should have received a copy of the GNU General      */
/* Public License along with this program; if not, write   */
/* to the Free Software Foundation, Inc., 59 Temple Place, */
/* Suite 330, Boston, MA 02111-1307 USA                    */
/*                                                         */
/* Contact Information:                                    */
/* Alex Zimin (alex@inprotect.com)                         */
/* http://www.inprotect.com                                */
/***********************************************************/
/* See the README.txt and/or help files for more           */
/* information on how to use & config.                     */
/* See the LICENSE.txt file for more information on the    */
/* License this software is distributed under.             */
/*                                                         */
/* This program is intended for use in an authorized       */
/* manner only, and the author can not be held liable for  */
/* anything done with this program, code, or items         */
/* discovered with this program's use.                     */
/***********************************************************/

include ('header.php');

if ($scantime=="") {

    echo "<font face=\"Verdana\" color=\"#666666\" size=\"4\">Nessus scan results:</font><BR>
    <table summary=\"Nessus scan results\" border=1 cellspacing=\"0\" cellpadding=\"0\" style=\"border-collapse: collapse\">
    <tr><td><font face=\"Verdana\" color=\"#666666\" size=\"2\">&nbsp;</font></td><td>
    <font face=\"Verdana\" color=\"#666666\" size=\"2\"><b>Date/Time</b></font></td><td>
    <font face=\"Verdana\" color=\"#666666\" size=\"2\"><b>Schedule</b></font></td><td>
    <font face=\"Verdana\" color=\"#666666\" size=\"2\"><b>Profile</b></font></td><td>
    <font face=\"Verdana\" color=\"#666666\" size=\"2\">&nbsp;</td></tr>";

    $query="select distinct t1.scantime, t1.sched_id, t2.name, t3.name 
    from nessus_results as t1, nessus_schedule as t2, nessus_settings as t3 
    where t1.username='$username' 
    and t1.scantype='A' 
    and t1.sched_id = t2.id 
    and t1.sid=t3.id 
    order by scantime DESC";

    $result=$dbconn->execute($query);
    while(list($scantime, $schedid, $sname, $sidname)= $result->fields) {
        $fscantime = dateformat($scantime);
        echo "<tr><td>
	<a href=\"results.php?scantime=$scantime&amp;scantype=A&amp;schedid=$schedid\"><img alt=\"HTML\" src=\"images/html.png\" border=0 title=\"Report in HTML format\"></A><a href=\"respdf.php?scantime=$scantime&amp;scantype=A&amp;schedid=$schedid\"><img alt=\"PDF\" src=\"images/pdf.png\" border=0 title=\"Report in PDF format\"></A></td><td><font face=\"Verdana\" color=\"#666666\" size=\"2\">$fscantime</font></td><td><font face=\"Verdana\" color=\"#666666\" size=\"2\">$sname</font></td><td><font face=\"Verdana\" color=\"#666666\" size=\"2\">$sidname</font></td><td><font face=\"Verdana\" color=\"#666666\" size=\"2\"><a href=\"results.php?scantime=$scantime&amp;schedid=$schedid&amp;op=delete\"><img alt=\"Delete\" src=\"images/delete.png\" border=0 title=\"Delete report\"></a></font></td></tr>";
        $result->MoveNext();
    }
    echo "</table>";
}
//	else {
else if ($scantime<>"" and $scantype=="A" and $schedid<>"") {

    if (!is_numeric($schedid)) {die("Can not access this page");}
    $query="select count(id) from nessus_schedule where username='$username' and id=$schedid";
    $result=$dbconn->execute($query);

    list($allowaccess)=$result->fields;
    if ($allowaccess<>1) {die("Can not access this page");}


    $scantime=htmlspecialchars($scantime);
    $scantime=mysql_escape_string($scantime);


    $query="select count(scantime) from nessus_results where scantime='$scantime' and username='$username' and sched_id=$schedid";
    $result=$dbconn->execute($query);

    list($numofresults)=$result->fields;

    if ($numofresults<1) {die("No vulnerabilities recorded");}

    if ($fp<>"") {
        if(!is_numeric($fp)) {die("Can not access this page");}

        $query="update nessus_results set falsepositive='Y' where result_id=$fp and username='$username' and sched_id=$schedid";
        $result=$dbconn->execute($query);

    }

    if ($nfp<>"") {
        if(!is_numeric($nfp)) {die("Can not access this page");}

        $query="update nessus_results set falsepositive='N' where result_id=$nfp and username='$username' and sched_id=$schedid";
        $result=$dbconn->execute($query);

    }
    $query="select distinct t2.name, t3.name, t3.description from nessus_results as t1, nessus_schedule as t2, nessus_settings as t3 where t1.username='$username' and t1.scantype='A' and t1.sched_id = t2.id and t1.sid=t3.id and scantime='$scantime' order by scantime DESC";
    $result=$dbconn->execute($query);
    list ($sched_name, $profile_name, $profile_desc)=$result->fields;

    echo "<font face=\"Verdana\" color=\"#666666\" size=\"4\"><b>Nessus scan results:</b></font><br>";
    echo "<font face=\"Verdana\" color=\"#666666\" size=\"2\">Scan time: $scantime</font><br>";
    echo "<font face=\"Verdana\" color=\"#666666\" size=\"2\">Profile: $profile_name - $profile_desc</font><br>
    <font face=\"Verdana\" color=\"#666666\" size=\"2\">Schedule: $sched_name</font><br>";
    $result=$dbconn->Execute("SELECT count(risk) as count, risk FROM `nessus_results` where scantime='$scantime' and username='$username' and sched_id=$schedid and falsepositive<>'Y' and scriptid <> 10180 group by risk");

    $prevrisk=0;
    $chartimg="graph1.php?graph=1";
    while (list($riskcount, $risk)=$result->fields) {

        for ($i=0;$i<$risk-$prevrisk-1;$i++) {

            $missedrisk=$prevrisk+$i+1;
            $chartimg.="&amp;risk$missedrisk=0";
        }

        $prevrisk=$risk;

        $chartimg.="&amp;risk$risk=$riskcount";


        $result->MoveNext();
    }


    echo "<img alt=\"Chart\" src=\"$chartimg\">";



    echo "<br><br><font color=\"red\"><b>Summary of scanned hosts</b></font><table summary=\"Summary of scanned hosts\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" style=\"border-collapse: collapse\">";

    $result=$dbconn->execute("select distinct host from nessus_results where scantime='$scantime' and username='$username' and sched_id=$schedid order by INET_ATON(host) ASC");


    echo "<tr><td>Host</td><td>Serious</td><td>High</td><td>Medium</td><td>Medium/Low</td><td>Low/Medium</td><td>Low</td><td>Info</td><td>Trend</td></tr>";

    while(list($hostip)=$result->fields) {
        echo "<tr><td><a href=\"results.php?scantime=$scantime&amp;scantype=A&amp;schedid=$schedid#$hostip\">$hostip</a></td>";

        $prevrisk=0;
        $result1=$dbconn->execute("SELECT count(risk) as count, risk FROM `nessus_results` where scantime='$scantime' and username='$username' and sched_id=$schedid and host='$hostip' and falsepositive<>'Y' and scriptid <>10180 group by risk");

        $drawtable=0;

        while(list($riskcount, $risk)=$result1->fields) {

            $drawtable=1;

            for ($i=0;$i<$risk-$prevrisk-1;$i++) {
                echo "<td>-</td>";
            }

            echo "<td>$riskcount</td>";
            $prevrisk=$risk;

            $result1->MoveNext();
        }

        if ($drawtable==0) {
            echo "<td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td>";
        }
        echo "<td><a href=\"trend.php?submit=Submit&amp;type=1&amp;ip=$hostip&amp;days=30\"><img alt=\"Trend\" border=0 src=\"images/trend.png\"></a></td>";
        echo "</tr>";
        $result->MoveNext();
    }
    echo "</table><br>";

    echo "<img alt=\"True\" src=\"images/true.png\" border=0> - True result<br>
    <img alt=\"False\" src=\"images/false.png\" border=0> - False positive result<br>
    <img alt=\"Note\" src=\"images/note.png\" border=0> - Add a custom note<br>
    <img alt=\"Info\" src=\"images/info.png\" border=0> - Additional information is available<br>";



    $result=$dbconn->execute("select distinct host from nessus_results where scantime='$scantime' and username='$username' and sched_id=$schedid order by INET_ATON(host) ASC");




    while(list($hostip)=$result->fields) {

        echo "<br><font color=\"red\"><b><a name=\"$hostip\">$hostip</a></b></font>";

        echo "<table summary=\"$hostip\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" style=\"border-collapse: collapse\">";



        $result1=$dbconn->execute("select service, risk, falsepositive, result_id from nessus_results where scantime='$scantime' and username='$username' and sched_id=$schedid and host='$hostip' and msg='' order by result_id ASC");
        while(list($service, $risk, $falsepositive, $resid)=$result1->fields) {

            echo "<tr><td>$service</td><td colspan=2>Open Port ";

            if ($falsepositive=="Y") {

                echo " <a href=\"results.php?scantime=$scantime&amp;scantype=$scantype&amp;schedid=$schedid&amp;nfp=$resid\"><img alt=\"Clear false positive\" src=\"images/false.png\" title=\"Clear false positive\" border=0></A>";
            }
            else {

                echo " <a href=\"results.php?scantime=$scantime&amp;scantype=$scantype&amp;schedid=$schedid&amp;fp=$resid\"><img alt=\"Mark as false positive\" src=\"images/true.png\" title=\"Mark as false positive\" border=0></A>";

            }
            echo "</td></tr>";
            $result1->MoveNext();
        }


        echo "<tr><td><font face=\"Verdana\" color=\"#666666\" size=\"2\"><b>Service</b></font></td><td><font face=\"Verdana\" color=\"#666666\" size=\"2\"><b>Severity</b></font></td><td><font face=\"Verdana\" color=\"#666666\" size=\"2\"><b>Description</b></font></td></tr>";

        $result1=$dbconn->execute("select service, risk, falsepositive, result_id, msg, scriptid from nessus_results where scantime='$scantime' and username='$username' and sched_id=$schedid and host='$hostip' and msg<>'' order by risk ASC, result_id ASC");
        while(list($service, $risk, $falsepositive, $resid, $msg, $scriptid)=$result1->fields) {
            $msg=htmlspecialchars($msg);

            $result_note=$dbconn->execute("select count(note) from nessus_notes where username='$username' and pid=$scriptid");
            list($notecount)=$result_note->fields;
            if ($notecount>0) {

                $msg.="\nCustom Notes:";
                $note_num=1;
                $query_note="select note from nessus_notes where username='$username' and pid=$scriptid";
                $result_note=$dbconn->execute($query_note);
                while(!$result_note->EOF) {
                    list($customnote)=$result_note->fields;
                    $msg.="\n$note_num. $customnote";
                    $note_num++;
                    $result_note->MoveNext();
                }
            }

            $msg=wordwrap(preg_replace("/\n/","<br>",$msg),50,"<br>",1);


            echo "<tr><td>$service</td><td>".getrisk($risk)."</td><td>$msg";


            echo "<br><a href=\"lookup.php?id=$scriptid\"><img alt=\"Info\" src=\"images/info.png\" title=\"More info...\" border=0></a>";
            echo " <a href=\"notes.php?op=add&amp;pid=$scriptid\"><img alt=\"Note\" src=\"images/note.png\" title=\"Add note\" border=0></a>";



            if ($falsepositive=="Y") {

                echo " <a href=\"results.php?scantime=$scantime&amp;scantype=$scantype&amp;schedid=$schedid&amp;nfp=$resid\"><img alt=\"Clear false positive\" src=\"images/false.png\" title=\"Clear false positive\" border=0></A>";
            }
            else {

                echo " <a href=\"results.php?scantime=$scantime&amp;scantype=$scantype&amp;schedid=$schedid&amp;fp=$resid\"><img alt=\"Mark as false positive\" src=\"images/true.png\" title=\"Mark as false positive\" border=0></A>";

            }
            echo "</td></tr>";
            $result1->MoveNext();
        }
        echo "</table>";
        $result->MoveNext();
    }
}

else if ($scantime<>"" and $schedid<>"" and $op=="delete") {

    if (!is_numeric($schedid)) {die("Can not access this page");}
    $query="select count(id) from nessus_schedule where username='$username' and id=$schedid";
    $result=$dbconn->execute($query);

    list($allowaccess)=$result->fields;
    if ($allowaccess<>1) {die("Can not access this page");}


    $scantime=htmlspecialchars($scantime);
    $scantime=mysql_escape_string($scantime);


    $query="select count(scantime) from nessus_results where scantime='$scantime' and username='$username' and sched_id=$schedid";
    $result=$dbconn->execute($query);

    list($numofresults)=$result->fields;

    if ($numofresults<1) {die("Can not access this page");}

    $query="delete from nessus_results where sched_id = $schedid and username='$username' and scantime='$scantime'";
    $result=$dbconn->execute($query);
    echo "<font face=\"Verdana\" color=\"#666666\" size=\"2\">Vulnerability test results have been deleted.<BR></font>";

}

echo "<BR>";

include ('footer.php');


function getrisk($risk) {

    switch ($risk) {
    case 1:
            $risk="Serious";
        break;
    case 2:
        $risk="High";
        break;
    case 3:
        $risk="Medium";
        break;
    case 4:
        $risk="Medium/Low";
        break;
    case 5:
        $risk="Low/Medium";
        break;
    case 6:
        $risk="Low";
        break;
    case 7:
        $risk="Info";
        break;
    }

    return $risk;
}
function dateformat($scantime) {
    if (strlen($scantime) != 14) {
        return $scantime;
    }
    $rval=substr($scantime,0,4)."-"
          .substr($scantime,4,2)."-"
          .substr($scantime,6,2)." "
          .substr($scantime,8,2).":"
          .substr($scantime,10,2).":"
          .substr($scantime,12,2);
    return $rval;

}
?>

