nmapfe_sig.c

来自「Ubuntu packages of security software。 相」· C语言 代码 · 共 1,309 行 · 第 1/3 页

C
1,309
字号
  }  if (GTK_WIDGET_SENSITIVE(opt.useDecoy) &&      GTK_TOGGLE_BUTTON(opt.useDecoy)->active) {  const char *val = gtk_entry_get_text(GTK_ENTRY(opt.Decoy));    if (val && *val) {         strcat(command, "-D ");      strcat(command, val);      strcat(command, " ");    }  }  if (GTK_WIDGET_SENSITIVE(opt.useSourceDevice) &&      GTK_TOGGLE_BUTTON(opt.useSourceDevice)->active) {  const char *val = gtk_entry_get_text(GTK_ENTRY(opt.SourceDevice));    if (val && *val) {         strcat(command, "-e ");      strcat(command, val);      strcat(command, " ");    }  }  if (GTK_WIDGET_SENSITIVE(opt.useSourceIP) &&      GTK_TOGGLE_BUTTON(opt.useSourceIP)->active) {  const char *val = gtk_entry_get_text(GTK_ENTRY(opt.SourceIP));    if (val && *val) {         strcat(command, "-S ");      strcat(command, val);      strcat(command, " ");    }  }  if (GTK_WIDGET_SENSITIVE(opt.useSourcePort) &&      GTK_TOGGLE_BUTTON(opt.useSourcePort)->active) {  const char *val = gtk_entry_get_text(GTK_ENTRY(opt.SourcePort));    if (val && *val) {         strcat(command, "-g ");      strcat(command, val);      strcat(command, " ");    }  }  if (GTK_WIDGET_SENSITIVE(opt.useFragments) &&      GTK_TOGGLE_BUTTON(opt.useFragments)->active)    strcat(command, "-f ");  if (GTK_WIDGET_SENSITIVE(opt.useIPv6) &&      GTK_TOGGLE_BUTTON(opt.useIPv6)->active)    strcat(command, "-6 ");  if (GTK_WIDGET_SENSITIVE(opt.useOrderedPorts) &&      GTK_TOGGLE_BUTTON(opt.useOrderedPorts)->active)    strcat(command, "-r ");  if (GTK_WIDGET_SENSITIVE(opt.randomizeHosts) &&      GTK_TOGGLE_BUTTON(opt.randomizeHosts)->active)    strcat(command, "--randomize-hosts ");  if (GTK_WIDGET_SENSITIVE(opt.useInputFile) &&      GTK_TOGGLE_BUTTON(opt.useInputFile)->active) {  const char *val = gtk_entry_get_text(GTK_ENTRY(opt.inputFilename));    if (val && *val) {         strcat(command, "-iL ");      strcat(command, val);      strcat(command, " ");    }  }  if (GTK_WIDGET_SENSITIVE(opt.useOutputFile) &&      GTK_TOGGLE_BUTTON(opt.useOutputFile)->active) {  const char *val = gtk_entry_get_text(GTK_ENTRY(opt.outputFilename));    if (val && *val) {         if (opt.outputFormatValue == NORMAL_OUTPUT)        strcat(command, "-oN ");      else if (opt.outputFormatValue == GREP_OUTPUT)        strcat(command, "-oG ");      else if (opt.outputFormatValue == XML_OUTPUT)        strcat(command, "-oX ");      else if (opt.outputFormatValue == ALL_OUTPUT)        strcat(command, "-oA ");      else if (opt.outputFormatValue == SKIDS_OUTPUT)        strcat(command, "-oS ");      strcat(command, val);      strcat(command, " ");      if (GTK_TOGGLE_BUTTON(opt.outputAppend)->active)        strcat(command, "--append-output ");    }  }   strcat(command, gtk_entry_get_text(GTK_ENTRY(opt.targetHost)));  return(command);}static void print_line (GtkTextBuffer *buffer,             char          *line){  GtkTextIter iter;  gtk_text_buffer_get_end_iter (buffer, &iter);    if (opt.viewValue == 1) {      char token[BUFSIZ+1];      char *str;      while (((str = next_token(line, token, sizeof(token) / sizeof(*token))) != NULL) && (*str != '\0')) {          /* Catch stuff */          if (strstr(str, "http://") ||                  strstr(str, "PORT") ||                  strstr(str, "PROTOCOL") ||                  strstr(str, "STATE") ||                  strstr(str, "SERVICE") ||                  strstr(str, "VERSION") ||                  strstr(str, "(RPC)") ||                  strstr(str, "OWNER") ||                  strstr(str, "fingerprint")) {              gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, str, -1,                      "bold", NULL);              /* Color the ports... */          } else if (strstr(str, "sftp") ||                  strstr(str, "mftp") ||                  strstr(str, "bftp") ||                  strstr(str, "tftp") ||                  strstr(str, "ftp") ||                  strstr(str, "NetBus") ||                  strstr(str, "kshell") ||                  strstr(str, "shell") ||                  strstr(str, "klogin") ||                  strstr(str, "login") ||                  strstr(str, "rtelnet") ||                  strstr(str, "telnet") ||                  strstr(str, "exec") ||                  strstr(str, "ssh") ||                  strstr(str, "linuxconf")) {              gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, str, -1,                      "red", NULL);          } else if (strstr(str, "imap2") ||                  strstr(str, "pop-3") ||                  strstr(str, "imap3") ||                  strstr(str, "smtps") ||                  strstr(str, "smtp") ||                  strstr(str, "pop-2")) {              gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, str, -1,                      "blue", NULL);          } else if (strstr(str, "systat") ||                  strstr(str, "netstat") ||                  strstr(str, "cfingerd") ||                  strstr(str, "finger") ||                  strstr(str, "netbios") ||                  strstr(str, "X11") ||                  strstr(str, "nfs") ||                  strstr(str, "sunrpc") ||                  strstr(str, "kpasswds") ||                  strstr(str, "https") ||                  strstr(str, "http")) {              gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, str, -1,                      "bold", NULL);      /******* BEGIN OS COLOR CODING *****************/		              /* Color the Operating systems */          } else if (strstr(str, "Linux") ||                  strstr(str, "FreeBSD") ||                  strstr(str, "Win") ||                  strstr(str, "MacOS") ||                  strstr(str, "OpenBSD") ||                  strstr(str, "IRIX") ||                  strstr(str, "Windows")) {              gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, str, -1,                      "green", NULL);          } else {               gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, str, -1,                      "normal", NULL);          }      }  } else {      gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, line, -1,            "normal", NULL);  }}void scanButton_toggled_cb(GtkButton *button, void *ignored){  if(GTK_TOGGLE_BUTTON(button)->active) {  char *command = build_command();  if(!(opt.appendLog))          gtk_text_buffer_set_text (GTK_TEXT_BUFFER(opt.buffer), "\0", -1);    nmap_pid = execute(command);}  else {    if (stop_scan()) {    static char string[256];      strcpy(string, "CANCELLED!\n\n");      print_line(GTK_TEXT_BUFFER(opt.buffer), string);}}}void saveLog (char *filename){    GtkTextIter start, end;    if (filename && *filename) {        FILE *file;        if ((file = fopen(filename, "w"))) {	  gchar *text;            gtk_text_buffer_get_start_iter(opt.buffer, &start);            gtk_text_buffer_get_end_iter(opt.buffer, &end);            text = gtk_text_buffer_get_text(opt.buffer,                    &start, &end, FALSE);            fputs(text, file);            fclose(file);            free(text);        }    }}void openLog(char *filename){  if (filename && *filename) {  FILE *file;       if (!opt.appendLog)           gtk_text_buffer_set_text (GTK_TEXT_BUFFER(opt.buffer), "\0", -1);	    if((file = fopen(filename, "r"))) {    char buf[BUFSIZ+1];      while(fgets(buf, BUFSIZ, file) != NULL) {        print_line(GTK_TEXT_BUFFER(opt.buffer), buf);      }      fclose(file);    }  }}void okButton_clicked_cb(GtkWidget *window, GtkButton *button){const char *selected = gtk_file_selection_get_filename(GTK_FILE_SELECTION(window));void (*action)() = (void (*)())g_object_get_data(G_OBJECT(window), "NmapFE_action");GtkEntry *entry = g_object_get_data(G_OBJECT(window), "NmapFE_entry");char *filename = g_object_get_data(G_OBJECT(window), "NmapFE_filename");  if (filename && selected) {    strncpy(filename, selected, FILENAME_MAX);    filename[FILENAME_MAX-1] = '\0';    if (action)      (*action)(filename);    if (entry)      gtk_entry_set_text(GTK_ENTRY(entry), filename);  }}/* split buf into first line and remainder by   copying the first line into line and stripping it from str;   return the first line from str or NULL if str contains no full line.   bufsz is the number of chars in buf. */static char *next_line(char *buf, int bufsz, char *line){  if ((buf != NULL) && (line != NULL)) {  char *eol = strchr(buf, '\n');    if (eol != NULL) {      char *bol = buf;    int linelen = MIN(bufsz - 1, eol - buf + 1); // we can't exceed buffer size      /* copy line including \n to line */      memcpy(line, buf, linelen);      line[linelen] = '\0';      eol = buf + linelen;      /* remove line from str */      while (*eol != '\0')	*bol++ = *eol++;      *bol = '\0';            return(line);    }    return(buf);  }  return(NULL);}/* The read_from_pipe functions (UNIX & Win versions) do a non-blocking read from the pipe   given into the buffer given up to a maximum read length of bufsz.  The number of bytes    read is returned.  -1 is returned in the case of heinous error.  Returned buffer is NOT   NUL terminated */#ifdef WIN32static int read_from_pipe(HANDLE pipe, char *buf, int bufsz){int ret;int count = 0;/* First lets check if anything is ready for us.   Note: I don't know if this technique even works! */  ret = WaitForSingleObject(pipe, 0);  if ( ret == WAIT_OBJECT_0 ) {    /* Apparently the pipe is available for reading -- Read up to # of bytes in buffer */    if (!ReadFile(pipe, buf, bufsz, &count, NULL)) {      if (GetLastError() != ERROR_BROKEN_PIPE)	pfatal("ReadFile on Nmap process pipe failed!");    }  }  return count;}#else/* NOTE:  pipefd must be in O_NONBLOCK mode ( via fcntl ) */static int read_from_pipe(int pipefd, char *buf, int bufsz){int count;  if (pipefd == -1) return -1;  count = read(pipefd, buf, bufsz);  if (count == -1 && errno != EINTR && errno != EAGAIN) {    pfatal("Failed to read from nmap process pipe");  }  return count;}#endif /* read_from_pipe Win32/UNIX selector */static gint read_data(gpointer data){  static char buf[BUFSIZ+1] = "";  static int buflen = 0;  char line[BUFSIZ+1];int count;#ifdef WIN32  int rc;  char *p=NULL, *q=NULL;#endif /* WIN32 */  while((count = read_from_pipe(pipes[0], buf+buflen, sizeof(buf) - buflen - 1 )) > 0) {  char *str;    /* fprintf(stderr, "\nCount was %d\n", count); */    buflen += count;    buf[buflen] = '\0';#ifdef WIN32    /* For windows, I have to squeeze \r\n back into \n */    p = q = buf;    while(*q) { if (*q == '\r') q++; else *p++ = *q++; }    *p = '\0';#endif /* WIN32 */    for (str = next_line(buf, sizeof(buf) / sizeof(*buf), line);          (str != buf) && (str != NULL);         str = next_line(buf, sizeof(buf) / sizeof(*buf), line)) {      buflen = strlen(buf);      print_line(opt.buffer, str);    }    }   /*  fprintf(stderr, "Below loop: Count was %d\n", count); */  if (buflen > 0) {  char *str;    while ((str = next_line(buf, sizeof(buf) / sizeof(*buf), line)) != NULL) {      buflen = strlen(buf);      print_line(opt.buffer, str);        if (str == buf)          break;    }  }#ifdef WIN32  if (nmap_pid) {    rc = WaitForSingleObject(NmapHandle, 0);    if (rc == WAIT_FAILED) {      pfatal("Failed in WaitForSingleObject to see if Nmap process has died");    }  }  if (!nmap_pid || rc == WAIT_OBJECT_0) {    CloseHandle(NmapHandle);    CloseHandle(pipes[0]);    nmap_pid = 0;    buflen = 0;    buf[buflen] = '\0';    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(opt.scanButton), 0);    return 0;  }#else  if (!nmap_pid || (waitpid(0, NULL, WNOHANG) == nmap_pid)) {    /* fprintf(stderr, "Program gone, dead, kablooey!\n"); */    nmap_pid = 0;    if (pipes[0] != -1) {      close(pipes[0]);      pipes[0] = -1;    }    buflen = 0;    buf[buflen] = '\0';    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(opt.scanButton), 0);    return 0;  }#endif /* waitpid unix/windoze selector */  return(1);	}/* The idea of execute() is to create an Nmap process running in the background with its stdout    connected to a pipe we can poll many times per second to collect any new output.  Admittedly 	there are much more elegant ways to do this, but this is how it works now.  The functions	return the process ID of nmap.  This process is	different enough between windows & UNIX that I have two functions for doing it: */static int execute_unix(char *command){#ifdef WIN32  fatal("The execute_unix function should not be called from Windows!");  return -1;#else  /* Many thanks to Fyodor for helping with the piping */  if (pipe(pipes) == -1) {    perror("poopy pipe error");

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?