diff --git a/lynis-report-converter.pl b/lynis-report-converter.pl index d1744d8..e87416c 100755 --- a/lynis-report-converter.pl +++ b/lynis-report-converter.pl @@ -10,17 +10,21 @@ use Getopt::Long qw( :config no_ignore_case bundling ); use Data::Dumper; use Module::Load::Conditional qw( can_load check_install requires ); -my ($help,$verbose,$excel,$output,$pdf); +my ($help,$verbose,$excel,$output,$pdf,$debug,$json,$quiet,$xml); GetOptions( 'h|help' => \$help, 'v|verbose+' => \$verbose, 'E|excel' => \$excel, 'o|output=s' => \$output, 'p|pdf' => \$pdf, + 'D|debug' => \$debug, + 'j|json' => \$json, + 'x|xml' => \$xml, + 'q|quiet' => \$quiet, ); &usage if ($help); -&usage if (!$output); +&usage if ((!$output) and (!$json)); my %to_bool = ( 0 => 'false', 1 => 'true' ); my %vm_mode = ( 0 => 'false', 1 => 'guest', 2 => 'host' ); @@ -32,6 +36,8 @@ my %systemd_uf_status_color = ( 'masked' => 'goldenrod' ); +if ($json) { $quiet = 1; } + my ($basename, $path, $suffix, $htmldoc); if ($excel) { @@ -59,18 +65,22 @@ if (( -e $lynis_report) and ( ! -z $lynis_report )) { } if (($audit_run) and ($audit_run >= 1)) { - print colored("Looks like the audit has been run.", "bold green"); - print "\n"; + print colored("Looks like the audit has been run.", "bold green") unless ($quiet); + print "\n" unless ($quiet); } else { - print colored("Couldn't find one or more of the lynis output files. Try running the audit again. \n", "bold red"); + warn colored("Couldn't find one or more of the lynis output files. Try running the audit again. \n", "bold red"); } -print colored("Outputting report to $output, in ", "bold green"); -if ($excel) { print colored("Excel ", "bold green"); } -elsif ($pdf) { print colored("PDF ", "bold green)"); } -else { print colored("HTML ", "bold green"); } -print colored("format.", "bold green"); -print "\n"; +unless ($quiet) { + print colored("Outputting report to $output, in ", "bold green"); + if ($excel) { print colored("Excel ", "bold green"); } + elsif ($pdf) { print colored("PDF ", "bold green)"); } + elsif ($xml) { print colored("XML ", "bold green"); } + elsif ($json) { print colored("JSON ", "bold green"); } + else { print colored("HTML ", "bold green"); } + print colored("format.", "bold green"); + print "\n"; +} # the report is easy to process, and actually doesn't contain the "audit findings"....just the data. # but it is not our job to draw conclusions here, just present the findings of the tool. @@ -100,6 +110,8 @@ while (my $line = ) { } close RPT or die colored("There was a problem closing the lynis report: $! \n", "bold red"); + + @{$lynis_report_data{'automation_tool_running[]'}} = &dedup_array($lynis_report_data{'automation_tool_running[]'}) if (ref($lynis_report_data{'automation_tool_running[]'}) eq 'ARRAY'); @{$lynis_report_data{'boot_service[]'}} = &dedup_array($lynis_report_data{'boot_service[]'}) if (ref($lynis_report_data{'boot_service[]'}) eq "ARRAY"); @{$lynis_report_data{'cronjob[]'}} = &dedup_array($lynis_report_data{'cronjob[]'}) if (ref($lynis_report_data{'cronjob[]'}) eq 'ARRAY'); @@ -135,6 +147,51 @@ delete($lynis_report_data{'tests_skipped'}); @tests_executed = @{$lynis_report_data{'tests_executed'}}; delete($lynis_report_data{'tests_executed'}); +if ($debug) { + print Dumper(\%lynis_report_data); + exit 1; +} + +if ($json) { + require JSON; + if ($output) { + # open the file and write to it + } + # it's moe likely JSON consumers would want to pipe the output to another process + # so print to STDOUT + my $json_obj = JSON->new->allow_nonref; + my $json_text = $json_obj->encode( \%lynis_report_data ); + print $json_text; + exit 0; +} + +if ($xml) { + require XML::Writer; + my $writer = XML::Writer->new('CONTENT'=>'self','DATA_MODE'=>1,'DATA_INDENT'=>2,); + $writer->xmlDecl('UTF-8'); + $writer->startTag('lynisReportData'); + foreach my $key ( sort keys %lynis_report_data ) { + if (ref($lynis_report_data{$key}) eq 'ARRAY') { + my $tmpkey = $key; + $tmpkey =~ s/\[\]//g; + $writer->startTag("${tmpkey}s"); + foreach my $ele ( sort @{$lynis_report_data{$key}} ) { + $writer->dataElement($tmpkey, $ele); + } + $writer->endTag("${tmpkey}s"); + } else { + $writer->dataElement($key, $lynis_report_data{$key}); + } + } + $writer->endTag('lynisReportData'); + my $xml = $writer->end(); + if ($output) { + # open the file and write to it + } + print $xml; + exit 0; +} + if ($excel) { require Excel::Writer::XLSX; my $i = 0; @@ -2010,6 +2067,11 @@ if ($verbose) { # subs ############################################################################### sub usage { + + if (!$output) { + print colored("You must specify an output file.\n", "bold yellow"); + } + print <