Dec 27 2012

merge nessus file

i want to combine merge append put together multiple .nessus files into 1. i come accross manual way, remove some lines and cat file1 > file2. the same way, but i put into php script.

Here is it. feel free to use, mod, update, change. Do share it helps you with or without tweaking. use at your own risk =)

<?php
/*
Developed for personal use to help combine multiple .nessus files into single .nessus file
*/
$searchreportname = ‘<Report name=’;

$currentdir = getcwd();
$currentfile = __FILE__;
$numfile = count($argv);
$lastfile = $numfile-1;
$finalfile = ‘output-’ . date(Ymdhis) . ‘.nessus’;

$filelist = $argv;

if ($filelist[1] == “-h”) {
echo “Usage:\n”;
echo ”  $currentfile <file1> <file2> <file3>  …\n\n”;
echo “-readme info\n”;
exit();
}

if ($filelist[1] == “-readme”) {
echo “This CLI PHP-based tool developed to help me to merge multiple nessus files into single .nessus file. \nThere was no comprehensive testing being done to check for its compatibility with all Nessus version, OS, PHP version n etc. But tested and works in Windows 7, PHP 5.3.17, output report works when uploaded into Nessus 5.0.2. \nOutput file is not useable if input file is nessus v1 report.\nAs for now, the nessus report name of final output file will be the report name of the first input file.\n”;
echo “Todo:\n”;
echo “-Process all files in folder instead of specifying one by one\n”;
echo “-Able to change nessus report name of output file”;
exit();
}

##for first file
$file = $filelist[1];
echo “Processing $file (1/” . $lastfile . “)…\n”;
$tmpfile = “tmp1″;
$content = file_get_contents($file);
$totline = COUNT(FILE($file));
$content = explode(“\n”, $content);
$delfromline = $totline-2;
array_splice($content, $delfromline, 2);
$newcontent = implode(“\n”, $content);
file_put_contents($tmpfile, $newcontent);

##process the rest
if ($numfile > 3) {
for ($i=2; $i<$numfile-1; $i++)
{
$file = $filelist[$i];
echo “Processing $file (” . $i . “/” . $lastfile . “)…\n”;
$tmpfile = “tmp” . $i;
$content = file_get_contents($file);
$totline = COUNT(FILE($file));
$reportnameline = getLineWithString($file, $searchreportname) + 1;
$content = explode(“\n”, $content);
$delfromline = $totline-2;
array_splice($content, $delfromline, 2);
array_splice($content, 0, $reportnameline);
$newcontent = implode(“\n”, $content);
file_put_contents($tmpfile, $newcontent);
}
}

##for last file
$file = $filelist[$lastfile];
echo “Processing $file (” . $lastfile . “/” . $lastfile . “)…\n\n”;
$tmpfile = “tmp” . $lastfile;
$content = file_get_contents($file);
$totline = COUNT(FILE($file));
$reportnameline = getLineWithString($file, $searchreportname) + 1;
$content = explode(“\n”, $content);
$delfromline = $totline-2;
array_splice($content, 0, $reportnameline);
$newcontent = implode(“\n”, $content);
file_put_contents($tmpfile, $newcontent);

#combine all file and delete tmp file
echo “Generating output file…\n\n”;
for ($i=1; $i<$numfile; $i++)
{
exec(‘more tmp’ . $i . ‘ >> ‘ . $finalfile);
exec(‘del tmp’ . $i);
}

echo “Created \”$currentdir\\$finalfile\” \n”;

function getLineWithString($fileName, $str) {
$lines = file($fileName);
foreach ($lines as $lineNumber => $line) {
if (strpos($line, $str) !== false) {
return $lineNumber;
}
}
return -1;
}

?>

 

Binfer

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment