Feature req./patch: explicit input format for report conversion

"Gabriel L. Somlo" <[email protected]>
Newsgroups gmane.comp.security.nessus.devel
Message-ID <[email protected]>
Renaud & crew,

I would like to use report conversion as a filter, e.g.:

cat some-nbe-file | nessus -i - -T html -o - | some-other-processig

The problem is that in conversion mode, nessus currently won't accept
stdin (i.e., "-i -").

I've created a patch to allow that, which adds the "-I <input-type>"
flag (where <input-type> is either 'nbe' or 'nsr'), so we won't have
to depend on the input filename only to guess the type of input. If no
-I option is provided, we fall back on the file extension.

The way this would then work is as follows:

cat something | nessus -I nbe -i - -T html -o - | some-other-thing

Here's why I would really like this feature:

I want to generate HTML reports which contain security holes only. For
this, I do the following:

nessus -T nbe -q localhost 1241 <user> <password> ./targets - \
    | grep '|Security Hole|' \
    | nessus -I nbe -i - -T html -o report-holes-only.html

I've attached the patch providing "-I nsr|nbe" and "-i -"
to nessus-core-2.0.1 at the end of this email.

Please let me know what you think.

Thanks,

Gabriel

-- 
-----------------------------------------------------------------------
Gabriel L. Somlo               Academic Computing & Networking Services
Colorado State University      
601 Howes St., Room 612A       
Fort Collins, CO 80523-2028            e-mail: [email protected]
-----------------------------------------------------------------------
nessus-core-2.0.1-stdin.patch (text/plain, 5.7 KB)
diff -rU4 nessus-core/nessus/backend.c nessus-core-stdin/nessus/backend.c
--- nessus-core/nessus/backend.c	2003-02-10 17:33:28.000000000 -0700
+++ nessus-core-stdin/nessus/backend.c	2003-03-06 10:14:58.000000000 -0700
@@ -531,26 +531,42 @@
 
 
 
 int 
-backend_import_report(fname)
+backend_import_report(fname, input_type)
  char * fname;
+ char * input_type;
 {
- char *ext = strrchr(fname, '.');
- if(!ext)
+ if(!input_type)
  {
-  show_error("Unknown report type - please set an extension to the filename");
-  return -1;
- }
+  char *ext = strrchr(fname, '.');
+  if(!ext)
+  {
+   show_error("Unknown report type - please set an extension to the filename");
+   return -1;
+  }
  
- if(!strcmp(ext, ".nsr"))
- {
-  return nsr_to_backend(fname);
- }
+  if(!strcmp(ext, ".nsr"))
+  {
+   return nsr_to_backend(fname);
+  }
  
- if(!strcmp(ext, ".nbe"))
+  if(!strcmp(ext, ".nbe"))
+  {
+   return nbe_to_backend(fname);
+  }
+ }
+ else
  {
-  return nbe_to_backend(fname);
+  if(!strcmp(input_type, "nsr"))
+  {
+   return nsr_to_backend(fname);
+  }
+ 
+  if(!strcmp(input_type, "nbe"))
+  {
+   return nbe_to_backend(fname);
+  }
  }
  
  show_error("This file format can not be read back by the Nessus client");
  return -1;
diff -rU4 nessus-core/nessus/backend.h nessus-core-stdin/nessus/backend.h
--- nessus-core/nessus/backend.h	2002-05-24 04:36:25.000000000 -0600
+++ nessus-core-stdin/nessus/backend.h	2003-03-06 10:14:58.000000000 -0700
@@ -54,8 +54,8 @@
 int backend_clear_all();
 
 int backend_fd(int);
 
-int backend_import_report(char*);
+int backend_import_report(char*, char*);
 struct arglist * backend_convert(int);
 
 #endif
diff -rU4 nessus-core/nessus/nbe_output.c nessus-core-stdin/nessus/nbe_output.c
--- nessus-core/nessus/nbe_output.c	2002-09-26 15:08:48.000000000 -0600
+++ nessus-core-stdin/nessus/nbe_output.c	2003-03-06 10:14:59.000000000 -0700
@@ -44,14 +44,19 @@
 int 
 nbe_to_backend(filename)
  char * filename;
 {
- int fd = open(filename, O_RDONLY);
+ int fd;
  int be = backend_init(NULL);
  int befd = backend_fd(be);
  off_t tot = 0;
  char buf[4096];
  struct stat stat;
+
+ if(strcmp(filename,"-") == 0)
+  fd = 0; /* stdin */
+ else
+  fd = open(filename, O_RDONLY);
  
  lseek(fd, 0, SEEK_SET);
  fstat(fd, &stat);
  while(tot < stat.st_size)
diff -rU4 nessus-core/nessus/nessus.c nessus-core-stdin/nessus/nessus.c
--- nessus-core/nessus/nessus.c	2003-01-20 11:03:10.000000000 -0700
+++ nessus-core-stdin/nessus/nessus.c	2003-03-06 10:14:59.000000000 -0700
@@ -679,8 +679,9 @@
  printf("General options :\n");
  printf("\tv : shows version number\n");
  printf("\th : shows this help\n"); 
  printf("\tn : No pixmaps\n");
+ printf("\tI : Input format: 'nbe' or 'nsr'\n");
  printf("\tT : Output format: 'nbe', 'html', 'html_graph', 'text', 'xml',\n");
  printf("\t    'old-xml' 'tex' or 'nsr'\n");
  printf("\tV : make the batch mode display status messages\n");
  printf("\t    to the screen.\n");
@@ -761,8 +762,9 @@
 {
   int i, xac;
   char *myself, **xav;
   int gui = 1;
+  char * input_type = NULL;
   char * output_type = NULL;
   int opt_m = 0;
   int list_sessions = 0;
   int list_plugins = 0;
@@ -831,8 +833,9 @@
 #endif
       {"batch-mode",           no_argument, 0, 'q'},
       {"make-config-file",     no_argument, 0, 'm'},
       {"config-file", 	 required_argument, 0, 'c'},
+      {"input-type",	 required_argument, 0, 'I'},
       {"output-type",	 required_argument, 0, 'T'},
       {"verbose",		no_argument,0, 'V'},
       {"list-plugins",		no_argument,0, 'p'},
       {"list-prefs",		no_argument,0, 'P'},
@@ -847,9 +850,9 @@
       {0, 0, 0, 0}
     };
 
     if ((i = getopt_long 
-	 (argc, argv, "Ppc:T:Vvhqn?r:01sR:Smi:o:x", long_options, &option_index)) == EOF)
+	 (argc, argv, "Ppc:I:T:Vvhqn?r:01sR:Smi:o:x", long_options, &option_index)) == EOF)
       break;
      else
       
     switch(i) {
@@ -873,8 +876,17 @@
 	 exit(1);
 	}
 	outf = estrdup(optarg);
 	break;
+     case 'I' :
+       if(!optarg)
+       {
+        display_help("nessus");
+	exit (1);
+       }
+        if(optarg[0]=='=')inc_optind(); /* no optind++ on Win32 -- jordan */
+	input_type = optarg;
+	break;    
      case 'T' :
        if(!optarg)
        {
         display_help("nessus");
@@ -963,9 +975,9 @@
    {
     display_help("nessus");
     exit(1);
    }
-  be = backend_import_report(inf);
+  be = backend_import_report(inf, input_type);
   if(be >= 0)
   {
    char * type;
    if(!output_type)
@@ -1265,9 +1277,9 @@
    * all the options have been taken in account... Now, the user
    * may want us to open a previously saved file
    */
   for (i = 1; i < xac; i ++) {
-    int be = backend_import_report(xav[i]);
+    int be = backend_import_report(xav[i], NULL);
     if(be >= 0)
      report_tests_ng (be, 0);
    } 
 
diff -rU4 nessus-core/nessus/nsr_output.c nessus-core-stdin/nessus/nsr_output.c
--- nessus-core/nessus/nsr_output.c	2002-09-10 16:01:25.000000000 -0600
+++ nessus-core-stdin/nessus/nsr_output.c	2003-03-06 10:14:59.000000000 -0700
@@ -86,11 +86,17 @@
 
 int nsr_to_backend(filename)
  char * filename;
 {
- FILE * f = fopen(filename, "r");
+ FILE * f;
  char buf[32768];
  int be;
+
+ if(strcmp(filename,"-") == 0)
+  f = stdin;
+ else
+  f = fopen(filename, "r");
+
  if(!f)
  {
   perror("fopen ");
   show_error("Could not open report");
diff -rU4 nessus-core/nessus/report.c nessus-core-stdin/nessus/report.c
--- nessus-core/nessus/report.c	2002-09-26 14:57:54.000000000 -0600
+++ nessus-core-stdin/nessus/report.c	2003-03-06 10:15:00.000000000 -0700
@@ -791,9 +791,9 @@
  * Opens the report
  */
 void open_report(GtkWidget * dontcare, GtkWidget *nsr)
 {
-int be = backend_import_report(gtk_file_selection_get_filename(GTK_FILE_SELECTION(nsr)));
+int be = backend_import_report(gtk_file_selection_get_filename(GTK_FILE_SELECTION(nsr)), NULL);
 if(be >= 0)report_tests_ng(be, 0);
 }
 
 /*
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.