[Openvas-commits] r2968 - in trunk/winslad: . john

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Apr 1 02:34:43 CEST 2009


Author: doj
Date: 2009-04-01 02:34:42 +0200 (Wed, 01 Apr 2009)
New Revision: 2968

Modified:
   trunk/winslad/TODO.txt
   trunk/winslad/client.cpp
   trunk/winslad/john/plugin.xml
   trunk/winslad/struct.h
Log:
plugins can be executed

Modified: trunk/winslad/TODO.txt
===================================================================
--- trunk/winslad/TODO.txt	2009-03-31 22:20:56 UTC (rev 2967)
+++ trunk/winslad/TODO.txt	2009-04-01 00:34:42 UTC (rev 2968)
@@ -4,14 +4,5 @@
  + http://www.kpym.com/2/kpym/index.htm
  + http://www.itefix.no/i2/taxonomy/term/33
 
-- Ausführungsergebnisse werden als File gespeichert
-- Prozess starten, chdir to work directory, stdout nach file umleiten
- + http://support.microsoft.com/kb/q190351/
- + http://www.codeproject.com/KB/threads/redir.aspx
- + http://www.codeproject.com/KB/threads/consolepipe.aspx
- + CreateFile http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
-
- nach start ausgabe von "200"
- beim Start check ob Ergebnis schon vorliegt, wenn ja ausgabe von "403"
- File wird mit "R:<id>" geschrieben
- nach Beenden des Programs nach "T:<id>" umbenannt
+- background Prozess
+- find correct path to cmd.exe

Modified: trunk/winslad/client.cpp
===================================================================
--- trunk/winslad/client.cpp	2009-03-31 22:20:56 UTC (rev 2967)
+++ trunk/winslad/client.cpp	2009-04-01 00:34:42 UTC (rev 2968)
@@ -37,6 +37,31 @@
 #include "foreach.hpp"
 #include "path.hpp"
 
+/** Displays the error number and corresponding message.
+    @author: http://support.microsoft.com/kb/q190351/
+*/
+void DisplayError(char *pszAPI)
+{
+  LPVOID lpvMessageBuffer;
+  CHAR szPrintBuffer[512];
+  DWORD nCharsWritten;
+
+  FormatMessage(
+                FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
+                NULL, GetLastError(),
+                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+                (LPTSTR)&lpvMessageBuffer, 0, NULL);
+
+  wsprintf(szPrintBuffer,
+	   "ERROR: API    = %s.\n   error code = %d.\n   message    = %s.\n",
+	   pszAPI, GetLastError(), (char *)lpvMessageBuffer);
+
+  WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),szPrintBuffer,
+	       lstrlen(szPrintBuffer),&nCharsWritten,NULL);
+
+  LocalFree(lpvMessageBuffer);
+}
+
 static int show_plugins (sladd_config_t * config)
 {
   assert(config);
@@ -182,98 +207,150 @@
 
 static int start_plugin(slad_plugin_t * p, slad_plugin_set_t * s, slad_plugin_entry_t * e, sladd_config_t * config)
 {
+  if(!p) return -1;
+  if(!e) return -2;
+  if(!config) return -3;
+
+  const std::string pluginName=std::string(p->name)+':'+s->name+':'+e->name;
+  const std::string pluginPath=config->pluginpath+'\\'+p->path;
 #if 0
-  char path[256];
-  char * argv[MAX_ARGV], * cmdline;
-  int argc, i;
-  char msg[MAX_MSG];
-  sladd_job_t * job = NULL;
-  struct stat statbuf;
-  pid_t pid;
-  struct passwd * passwd = NULL;
-  struct group * group = NULL;
-  char pluginname[1024];
+  PRINTF("pluginName: %s\n", pluginName.c_str());
+  PRINTF("path: %s\n", pluginPath.c_str());
+  PRINTF("binary: %s\n", e->binary);
+  PRINTF("cmdline: %s\n", e->cmdline);
+#endif
 
-  snprintf (pluginname, sizeof(pluginname), "%s:%s:%s", p->name, s->name, e->name);
+  std::string resultFN=std::string("R$")+plugin2filename(pluginName);
+  const std::string resultPath=config->resultpath+'\\'+resultFN;
+  resultFN[0]='T';
+  const std::string finishedPath=config->resultpath+'\\'+resultFN;
 
-  for (job = jobs; job; job = job->next)
-    if (!strcasecmp (pluginname, job->pluginid))
-      break;
+  // check if result file already exists
+  if(doj::isFile(finishedPath))
+    {
+      printf(MSG_ALREADY);
+      return 1;
+    }
 
-  if (job) {
-    write (socket, MSG_ALREADY, strlen (MSG_ALREADY));
-    return -1;
-  }
+  // \todo create background process
+  if(0)
+    {
 
-  strcpy (path, config->pluginpath);
-  if (p->path) {
-    strcat (path, "/");
-    strcat (path, p->path);
-  }
-  strcat (path, "/");
-  strcat (path, e->binary);
 
-  if (stat (path, &statbuf)) {
-    snprintf (msg, sizeof(msg), MSG_CANTRUN " - stat(%s): %s\n",
-	      path, strerror (errno));
-    write (socket, msg, strlen (msg));
-    return -1;
-  }
-  if (!S_ISREG (statbuf.st_mode)) {
-    snprintf (msg, sizeof(msg), MSG_CANTRUN " - %s is not a regular file\n", path);
-    write (socket, msg, strlen (msg));
-    return -1;
-  }
-  if (!(statbuf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
-    snprintf (msg, sizeof(msg), MSG_CANTRUN " - %s is not executable\n", path);
-    write (socket, msg, strlen (msg));
-    return -1;
-  }
+      printf(MSG_OK);
+      return 0;
+    }
 
-  argv[0] = path;
-  for (argc = 1, cmdline = e->cmdline; argc < MAX_ARGV - 1; cmdline = NULL)
-    if (!(argv[argc++] = strtok (cmdline, " \t\n")))
-      break;
-  argv[MAX_ARGV-1] = NULL;
+  std::string cmd;
 
-  if (e->runasuser)
-    passwd = getpwnam (e->runasuser);
-  if (e->runasgroup)
-    group = getgrnam (e->runasgroup);
+  // check for batch files
+  LPCTSTR lpApplicationName=NULL;
+  const std::string binary=e->binary;
+  if(binary.size()>4 && binary[binary.size()-4]=='.')
+    {
+      std::string ext;
+      ext+=static_cast<const char>(tolower(binary[binary.size()-3]));
+      ext+=static_cast<const char>(tolower(binary[binary.size()-2]));
+      ext+=static_cast<const char>(tolower(binary[binary.size()-1]));
+      if(ext=="bat" || ext=="cmd" || ext=="btm")
+	{
+	  // \todo find correct path to cmd.exe
+	  lpApplicationName="c:\\windows\\system32\\cmd.exe";
+	  cmd="/c ";
+	}
+    }
 
-#ifdef DOJDEBUG
-  switch (pid = commfork (socket, pluginname)) {
-#else
-    switch (pid = commfork (pluginname)) {
+  cmd+=pluginPath+'\\'+e->binary+' '+e->cmdline;
+#if 0
+  PRINTF("%s %s > %s\n",
+	 lpApplicationName ? lpApplicationName : "",
+	 cmd.c_str(),
+	 resultFN.c_str());
 #endif
 
-    case 0:
-      if (group)
-	setgid (group->gr_gid);
-      if (passwd)
-	setuid (passwd->pw_uid);
-      /* no provileged operations after this point */
-      for (i=3; i < FD_SETSIZE; i++)
-	close (i);
-      PRINTF("execvp(%s) pid=%i\n", argv[0], getpid());
-      execvp (argv[0], argv);
-      snprintf (msg, sizeof(msg), "execvp(): %s\n", strerror (errno));
-      write (socket, msg, strlen (msg));
-      _exit(1);
-    case -1:
-      write (socket, MSG_CANTRUN " - fork error\n", strlen (MSG_CANTRUN)+14);
+  // Info on creating console processes with redirected handles: http://support.microsoft.com/kb/q190351/
+
+  // Info on CreateFile: http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
+
+  // enable the file to be inherited
+  SECURITY_ATTRIBUTES SecAttr; memset(&SecAttr, 0, sizeof(SecAttr));
+  SecAttr.nLength=sizeof(SecAttr);
+  SecAttr.bInheritHandle=TRUE;
+
+  // open result file for writing
+  HANDLE result_h=CreateFile(
+			     resultPath.c_str(),
+			     GENERIC_WRITE,
+			     FILE_SHARE_READ,
+			     &SecAttr,
+			     CREATE_ALWAYS,
+			     FILE_ATTRIBUTE_NORMAL,
+			     NULL // hTemplateFile
+			     );
+  if(result_h == INVALID_HANDLE_VALUE)
+    {
+      DisplayError("CreateFile");
+      PRINTF("CreateFile(%s) failed\n", resultPath.c_str());
       return -1;
-      break;
-    default:
-      snprintf (msg, sizeof(msg), "%s %s\n", MSG_OK, p->desc);
-      write (socket, msg, strlen (msg));
+    }
+
+  // Info on CreateProcess: http://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx
+
+  // set result file as STDOUT, STDERR for new Process
+  STARTUPINFO StartupInfo; memset(&StartupInfo, 0, sizeof(StartupInfo));
+  StartupInfo.dwFlags=STARTF_USESTDHANDLES;
+  StartupInfo.hStdInput=GetStdHandle(STD_INPUT_HANDLE);
+  StartupInfo.hStdOutput=result_h;
+  StartupInfo.hStdError=result_h;
+
+  PROCESS_INFORMATION ProcInfo; memset(&ProcInfo, 0, sizeof(ProcInfo));
+
+  bool runSuccess=false;
+
+  if(CreateProcess(
+		   lpApplicationName,
+		   strdup(cmd.c_str()), // I know, memory leak here, but for this one-shot program it is not relevant
+		   NULL, // LPSECURITY_ATTRIBUTES lpProcessAttributes,
+		   NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes,
+		   TRUE, // bInheritHandles,
+		   NORMAL_PRIORITY_CLASS, // dwCreationFlags,
+		   NULL, // lpEnvironment,
+		   pluginPath.c_str(), // lpCurrentDirectory,
+		   &StartupInfo,
+		   &ProcInfo
+		   )
+     )
+    {
+      PRINTF("Created Process %i ; Thread %i\n", ProcInfo.dwProcessId, ProcInfo.dwThreadId);
+
+      // Wait until child process exits.
+      WaitForSingleObject( ProcInfo.hProcess, INFINITE );
+
+      // Close process and thread handles.
+      CloseHandle( ProcInfo.hProcess );
+      CloseHandle( ProcInfo.hThread );
+
+      runSuccess=true;
+    }
+  else
+    DisplayError("CreateProcess");
+
+  // close result file
+  if(!CloseHandle(result_h))
+    DisplayError("CloseHandle");
+
+  // if child process executed successfully, rename result file
+  if(runSuccess)
+    {
+      // rename result file to indicate this job is done
+      unlink(finishedPath.c_str());
+      rename(resultPath.c_str(), finishedPath.c_str());
       return 0;
     }
-  }
 
-#else
-  return 0;
-#endif
+  // delete (empty) result file
+  unlink(resultPath.c_str());
+  return 1;
 }
 
 static void findplugin (const std::string& pluginName, sladd_config_t * config,

Modified: trunk/winslad/john/plugin.xml
===================================================================
--- trunk/winslad/john/plugin.xml	2009-03-31 22:20:56 UTC (rev 2967)
+++ trunk/winslad/john/plugin.xml	2009-04-01 00:34:42 UTC (rev 2968)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE plugin SYSTEM "plugin.dtd">
 
-<slad:plugin xmlns:slad="http://slad.dnsystems.org/slad" id="winjohn">
+<slad:plugin xmlns:slad="http://slad.dnsystems.org/slad" id="john">
 	<slad:pluginDescription>Execute John-the-Ripper to find weak user passwords</slad:pluginDescription>
 	<slad:pluginSet id="fastjohn">
 		<slad:pluginSetDescription>Fast-Crack - tries only usernames and variations of these</slad:pluginSetDescription>

Modified: trunk/winslad/struct.h
===================================================================
--- trunk/winslad/struct.h	2009-03-31 22:20:56 UTC (rev 2967)
+++ trunk/winslad/struct.h	2009-04-01 00:34:42 UTC (rev 2968)
@@ -38,8 +38,6 @@
 #define MSG_CANTRUN   "500 internal error"
 #define MSG_MAXCONNS  "501 Connection limit reached\n"
 
-#define RECVBUFSIZE 1024
-
 /*
  * A plugin entry is the atom of a slad plugin. It contains the necessary
  * information to run a plugin.
@@ -98,33 +96,4 @@
   sladd_config_t();
 };
 
-#if 0
-
-/*
- * Information about a job
- */
-
-typedef struct sladd_job {
-  pid_t pid;                            /* Pid the job is running at */
-  int running;                          /* 0=completed, 1=forked, 2=exec'ed */
-  int socket;                           /* The socket for reading data */
-  char * pluginid;                      /* The plugin this belongs to */
-  char * output;                        /* The output the program had */
-  int outputlen;                        /* Length of output */
-  struct sladd_job * next;              /* For linked list usage */
-} sladd_job_t;
-
-/*
- * A structure for the information related to command connections
- */
-
-typedef struct receive_buffer {
-  int socket;                           /* The socket accept() gave */
-  char recvbuf[RECVBUFSIZE];            /* A receive buffer in case of fragments */
-  int recvlen;                          /* The number of characters buffered */
-  time_t last_event;                    /* time() of the last event on the socket */
-} receive_buffer_t;
-
 #endif
-
-#endif



More information about the Openvas-commits mailing list