nmapfe_sig.c

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

C
1,309
字号
    exit(1);  }  if (!(pid = fork())) {    char **argv;    int argc;    argc = arg_parse(command, &argv);		    if (argc <= 0)      exit(1);    dup2(pipes[1], 1);    dup2(pipes[1], 2);    fcntl(pipes[0], F_SETFL, O_NDELAY);    if (execvp("nmap", argv) == -1) {      fprintf(stderr, "Nmap execution failed.  errno=%d (%s)\n", errno, strerror(errno));      exit(1);    }    /*exit(127);*/  }  if (pid == -1) {    fprintf(stderr, "fork() failed.  errno=%d (%s)", errno, strerror(errno));    pid = 0;    close(pipes[0]);    pipes[0] = -1;  }  close(pipes[1]);  pipes[1] = -1;  return(pid);#endif}#ifdef WIN32/* Parts cribbed from _Win32 System Programming Second Edition_ pp 304 */static int execute_win(char *command){/* For pipes[] array:  0 == READ; 1 == WRITE *//* To ensure pipe handles are inheritable */SECURITY_ATTRIBUTES PipeSA = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };PROCESS_INFORMATION Nmap_Proc;STARTUPINFO Nmap_Start;  GetStartupInfo(&Nmap_Start);  /* Create our pipe for reading Nmap output */  if (!CreatePipe(&pipes[0], &pipes[1], &PipeSA, 8196))    pfatal("execute_win: Failed to create pipes!");  /* Insure that stdout/stderr for Nmap will go to our pipe */  Nmap_Start.hStdInput = GetStdHandle(STD_INPUT_HANDLE);  Nmap_Start.hStdError = pipes[1];  Nmap_Start.hStdOutput = pipes[1];  Nmap_Start.dwFlags = STARTF_USESTDHANDLES;  /* Start up Nmap! */  if (!CreateProcess ( NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &Nmap_Start, &Nmap_Proc))    pfatal("execute_win: Failed to start Nmap process with command '%s'", command);  /* I don't care about the thread handle or the write pipe anymore */  CloseHandle(Nmap_Proc.hThread);   CloseHandle(pipes[1]);  /* I'm gonna squirrel away the Nmap process handle in a global variable.     All this nonsense needs to be redone */   NmapHandle = Nmap_Proc.hProcess;  return Nmap_Proc.dwProcessId;}#endif /* WIN32 */int execute(char *command){#ifdef WIN32int pid = execute_win(command);#elseint pid = execute_unix(command);#endif /* WIN32 *//* timer for calling our read function to poll for new data 8 times per second */ g_timeout_add(125, read_data, NULL);  return(pid);}void display_nmap_command(){char *command = build_command();  gtk_entry_set_text(GTK_ENTRY(opt.commandEntry), command);}void display_nmap_command_cb(GtkWidget *target_option, void *ignored){  display_nmap_command();}void browseButton_pressed_cb(GtkWidget *widget, GtkWidget *text){static char filename[FILENAME_MAX+1] = "";const char *name = gtk_entry_get_text(GTK_ENTRY(text));  if (name && *name) {    strncpy(filename, name, FILENAME_MAX);    filename[FILENAME_MAX] = '\0';  }  gtk_widget_show(create_fileSelection("Select File", filename, NULL, GTK_ENTRY(text)));}void scanType_cb (GtkComboBox *w, gpointer data){	    Entry *user = data;    gint i = 0, j, k;    j = gtk_combo_box_get_active(w);    if (opt.isr00t) {        k = j;    } else {        for (k = 0; user[k].scantype; k++) {            if (user[k].rootonly != TRUE) {                if (i == j) {                    break;                }                i++;            }        }    }    opt.scanValue = user[k].scantype;    if ((opt.scanValue == PING_SCAN) || (opt.scanValue == LIST_SCAN)) {      // gtk_widget_set_sensitive(GTK_WIDGET(opt.protportFrame), FALSE);      gtk_widget_set_sensitive(GTK_WIDGET(opt.protportType), FALSE);      gtk_widget_set_sensitive(GTK_WIDGET(opt.protportLabel), FALSE);      gtk_widget_set_sensitive(GTK_WIDGET(opt.protportRange), FALSE);      gtk_widget_set_sensitive(GTK_WIDGET(opt.OSInfo), FALSE);    } else {      // gtk_widget_set_sensitive(GTK_WIDGET(opt.protportFrame), TRUE);      gtk_widget_set_sensitive(GTK_WIDGET(opt.protportType), TRUE);      gtk_widget_set_sensitive(GTK_WIDGET(opt.protportLabel),                               (opt.protportValue == GIVEN_PROTPORT));      gtk_widget_set_sensitive(GTK_WIDGET(opt.protportRange),                               (opt.protportValue == GIVEN_PROTPORT));      gtk_widget_set_sensitive(GTK_WIDGET(opt.OSInfo), TRUE);    }    if ((opt.scanValue == PING_SCAN) ||             (opt.scanValue == LIST_SCAN) || (opt.scanValue == PROT_SCAN)) {      gtk_widget_set_sensitive(GTK_WIDGET(opt.RPCInfo), FALSE);      gtk_widget_set_sensitive(GTK_WIDGET(opt.VersionInfo), FALSE);    } else {      gtk_widget_set_sensitive(GTK_WIDGET(opt.RPCInfo), TRUE);      gtk_widget_set_sensitive(GTK_WIDGET(opt.VersionInfo), TRUE);    }    if ((opt.scanValue == CONNECT_SCAN) || (opt.scanValue == BOUNCE_SCAN)) {      gtk_widget_set_sensitive(GTK_WIDGET(opt.useDecoy), FALSE);      gtk_widget_set_sensitive(GTK_WIDGET(opt.Decoy), FALSE);    } else if (opt.isr00t) {      gtk_widget_set_sensitive(GTK_WIDGET(opt.useDecoy), TRUE);      gtk_widget_set_sensitive(GTK_WIDGET(opt.Decoy), TRUE);    }    if ((opt.scanValue != ACK_SCAN) &&             (opt.scanValue != MAIMON_SCAN) && (opt.scanValue != FIN_SCAN) &&            (opt.scanValue != SYN_SCAN) && (opt.scanValue != NULL_SCAN) &&             (opt.scanValue != XMAS_SCAN) && (opt.scanValue != WIN_SCAN))      gtk_widget_set_sensitive(GTK_WIDGET(opt.useFragments), FALSE);    else if (opt.isr00t)      gtk_widget_set_sensitive(GTK_WIDGET(opt.useFragments), TRUE);    if ((opt.scanValue == BOUNCE_SCAN) || (opt.scanValue == IDLE_SCAN)) {      gtk_label_set_text(GTK_LABEL(opt.scanRelayLabel),                          (opt.scanValue == BOUNCE_SCAN) ? "Bounce Host:" : "Zombie Host:");      gtk_widget_set_sensitive(GTK_WIDGET(opt.scanRelayLabel), TRUE);      gtk_widget_set_sensitive(GTK_WIDGET(opt.scanRelay), TRUE);      gtk_widget_grab_focus(GTK_WIDGET(opt.scanRelay));    } else {      gtk_widget_set_sensitive(GTK_WIDGET(opt.scanRelayLabel), FALSE);      gtk_label_set_text(GTK_LABEL(opt.scanRelayLabel), "Relay Host:");      gtk_widget_set_sensitive(GTK_WIDGET(opt.scanRelay), FALSE);    }    g_object_set(G_OBJECT(opt.protportFrame), "label",                   (opt.scanValue == PROT_SCAN) ? "Scanned Protocols" : "Scanned Ports", NULL);  display_nmap_command();}void pingButton_toggled_cb(GtkWidget *ping_button, void *ignored){gboolean status = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ping_button));  if (ping_button == opt.dontPing) {  gboolean localstatus = (GTK_TOGGLE_BUTTON(opt.tcpPing)->active) && (!status);    gtk_widget_set_sensitive(GTK_WIDGET(opt.tcpPing), !status);    gtk_widget_set_sensitive(GTK_WIDGET(opt.tcpPingLabel), localstatus);    gtk_widget_set_sensitive(GTK_WIDGET(opt.tcpPingPorts), localstatus);    if (opt.isr00t) {      gtk_widget_set_sensitive(GTK_WIDGET(opt.icmpechoPing), !status);      gtk_widget_set_sensitive(GTK_WIDGET(opt.icmpmaskPing), !status);      gtk_widget_set_sensitive(GTK_WIDGET(opt.icmptimePing), !status);      localstatus = (GTK_TOGGLE_BUTTON(opt.synPing)->active) && (!status);      gtk_widget_set_sensitive(GTK_WIDGET(opt.synPing), !status);      gtk_widget_set_sensitive(GTK_WIDGET(opt.synPingLabel), localstatus);      gtk_widget_set_sensitive(GTK_WIDGET(opt.synPingPorts), localstatus);      localstatus = (GTK_TOGGLE_BUTTON(opt.udpPing)->active) && (!status);      gtk_widget_set_sensitive(GTK_WIDGET(opt.udpPing), !status);      gtk_widget_set_sensitive(GTK_WIDGET(opt.udpPingLabel), localstatus);      gtk_widget_set_sensitive(GTK_WIDGET(opt.udpPingPorts), localstatus);    }  }  else if (ping_button == opt.tcpPing) {    gtk_widget_set_sensitive(GTK_WIDGET(opt.tcpPingLabel), status);    gtk_widget_set_sensitive(GTK_WIDGET(opt.tcpPingPorts), status);  }  else if ((ping_button == opt.synPing) && (opt.isr00t)) {    gtk_widget_set_sensitive(GTK_WIDGET(opt.synPingLabel), status);    gtk_widget_set_sensitive(GTK_WIDGET(opt.synPingPorts), status);  }  else if ((ping_button == opt.udpPing) && (opt.isr00t)) {    gtk_widget_set_sensitive(GTK_WIDGET(opt.udpPingLabel), status);    gtk_widget_set_sensitive(GTK_WIDGET(opt.udpPingPorts), status);  }    display_nmap_command();}void throttleType_cb (GtkComboBox *w, gpointer data){	    opt.throttleValue = gtk_combo_box_get_active(w);    display_nmap_command();}void resolveType_cb (GtkComboBox *w, gpointer data){    opt.resolveValue = gtk_combo_box_get_active(w);    display_nmap_command();}void protportType_cb(GtkComboBox *w, gpointer d){    opt.protportValue = gtk_combo_box_get_active(w);    gtk_widget_set_sensitive(GTK_WIDGET(opt.protportLabel),             (opt.protportValue == GIVEN_PROTPORT));    gtk_widget_set_sensitive(GTK_WIDGET(opt.protportRange),             (opt.protportValue == GIVEN_PROTPORT));    if (opt.protportValue == GIVEN_PROTPORT)      gtk_widget_grab_focus(GTK_WIDGET(opt.protportRange));    display_nmap_command();}/* callback for factory generated menu items: set variable to action */void outputFormatType_cb(GtkComboBox *w, gpointer d){  opt.outputFormatValue = gtk_combo_box_get_active(w);  display_nmap_command();}/* callback for toggle buttons: control other objects seneistivity */void toggle_button_set_sensitive_cb(GtkWidget *master, GtkWidget *slave){  if ((master != NULL) && (slave != NULL) && GTK_IS_TOGGLE_BUTTON(master))    gtk_widget_set_sensitive(GTK_WIDGET(slave), GTK_TOGGLE_BUTTON(master)->active);  display_nmap_command();}void validate_file_change(GtkWidget *button, void *ignored){	gboolean status = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));  if (button == opt.useInputFile) {    gtk_widget_set_sensitive(GTK_WIDGET(opt.targetHost), !status);    gtk_widget_set_sensitive(GTK_WIDGET(opt.inputFilename), status);    gtk_widget_set_sensitive(GTK_WIDGET(opt.inputBrowse), status);  } else if (button == opt.useOutputFile) {    gtk_widget_set_sensitive(GTK_WIDGET(opt.outputFilename), status);    gtk_widget_set_sensitive(GTK_WIDGET(opt.outputBrowse), status);    gtk_widget_set_sensitive(GTK_WIDGET(opt.outputFormatLabel), status);    gtk_widget_set_sensitive(GTK_WIDGET(opt.outputFormatType), status);    gtk_widget_set_sensitive(GTK_WIDGET(opt.outputAppend), status);  }  display_nmap_command();}void validate_option_change(GtkWidget *target_option, void *ignored){	gboolean status = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(target_option));  if ((target_option == opt.useInputFile) && (status))    gtk_entry_set_text(GTK_ENTRY(opt.targetHost), "");  display_nmap_command();}gboolean stop_scan(){  /*  fprintf(stderr, "stop scan called -- pid == %d\n", nmap_pid); */  if (nmap_pid) {#ifdef WIN32    TerminateProcess(NmapHandle, 1);    CloseHandle(NmapHandle);    CloseHandle(pipes[0]);#else    kill(nmap_pid, 9);    if (pipes[0] != -1) {      close(pipes[0]);      pipes[0] = -1;    }#endif /* Win32/UNIX Selector for killing Nmap */    nmap_pid = 0;    return(TRUE);  }  return(FALSE);}void on_verb_activate(GtkMenuItem *menuitem, gpointer user_data){  /* toggle verb */  verb = (verb) ? 0 : 1;  display_nmap_command();}/***************************************************************//* This function takes a command and the address of an uninitialized   char ** .  It parses the command (by seperating out whitespace)   into an argv[] style char **, which it sets the argv parameter to.   The function returns the number of items filled up in the array   (argc), or -1 in the case of an error.  This function allocates   memory for argv and thus it must be freed -- use argv_parse_free()   for that.  If arg_parse returns <1, then argv does not need to be freed.   The returned arrays are always terminated with a NULL pointer */int arg_parse(const char *command, char ***argv){char **myargv = NULL;int argc = 0;char mycommand[4096];char *start, *end;char oldend;  *argv = NULL;  if (Strncpy(mycommand, command, 4096) == -1) {          return -1;  }  myargv = calloc(MAX_PARSE_ARGS + 2, sizeof(char *));  myargv[0] = (char *) 0x123456; /* Integrity checker */  myargv++;  start = mycommand;  while(start && *start) {    while(*start && isspace(*start))      start++;    if (*start == '"') {      start++;      end = strchr(start, '"');    } else if (*start == '\'') {      start++;      end = strchr(start, '\'');          } else if (!*start) {      continue;    } else {      end = start+1;      while(*end && !isspace(*end)) {      	end++;      }    }    if (!end) {      arg_parse_free(myargv);      return -1;    }    if (argc >= MAX_PARSE_ARGS) {      arg_parse_free(myargv);      return -1;    }    oldend = *end;    *end = '\0';    myargv[argc++] = strdup(start);    if (oldend)      start = end + 1;    else start = end;  }  myargv[argc+1] = 0;  *argv = myargv;  return argc;}/* Free an argv allocated inside arg_parse */void arg_parse_free(char **argv){char **current;  /* Integrity check */  argv--;  assert(argv[0] == (char *) 0x123456);  current = argv + 1;  while(*current) {    free(*current);    current++;  }  free(argv);}#endif /* MISSING_GTK */

⌨️ 快捷键说明

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