📄 osscan.cc
字号:
if (!FP) return "(None)";if(FP->OS_name && *(FP->OS_name)) { len = Snprintf(str, 128, "FingerPrint %s\n", FP->OS_name); if (len < 0) fatal("OS name too long"); p += len;}for(current = FP; current ; current = current->next) { Strncpy(p, current->name, sizeof(str) - (p-str)); p += strlen(p); assert(p-str < (int) sizeof(str) - 30); *p++='('; for(AV = current->results; AV; AV = AV->next) { Strncpy(p, AV->attribute, sizeof(str) - (p-str)); p += strlen(p); assert(p-str < (int) sizeof(str) - 30); *p++='='; Strncpy(p, AV->value, sizeof(str) - (p-str)); p += strlen(p); assert(p-str < (int) sizeof(str) - 30); *p++ = '%'; } if(*(p-1) != '(') p--; /* Kill the final & */ *p++ = ')'; *p++ = '\n';}*p = '\0';return str;}/* Parse a 'Class' line found in the fingerprint file into the current FP. Classno is the number of 'class' lines found so far in the current fingerprint. The function quits if there is a parse error */static void parse_classline(FingerPrint *FP, char *thisline, int lineno, int *classno) { char *p, *q;// Wtf???? fflush(stdout); if (!thisline || strncmp(thisline, "Class ", 6) == 1) { fatal("Bogus line #%d (%s) passed to %s()", lineno, thisline, __func__); } if (*classno >= MAX_OS_CLASSIFICATIONS_PER_FP) fatal("Too many Class lines in fingerprint (line %d: %s), remove some or increase MAX_OS_CLASSIFICATIONS_PER_FP", lineno, thisline); p = thisline + 6; /* First lets get the vendor name */ while(*p && isspace(*p)) p++; q = strchr(p, '|'); if (!q) { fatal("Parse error on line %d of fingerprint: %s\n", lineno, thisline); } // Trim any trailing whitespace q--; while(isspace(*q)) q--; q++; if (q < p) { fatal("Parse error on line %d of fingerprint: %s\n", lineno, thisline); } FP->OS_class[*classno].OS_Vendor = (char *) cp_alloc(q - p + 1); memcpy(FP->OS_class[*classno].OS_Vendor, p, q - p); FP->OS_class[*classno].OS_Vendor[q - p] = '\0'; /* Next comes the OS Family */ p = q; while(*p && !isalnum(*p)) p++; q = strchr(p, '|'); if (!q) { fatal("Parse error on line %d of fingerprint: %s\n", lineno, thisline); } // Trim any trailing whitespace q--; while(isspace(*q)) q--; q++; if (q < p) { fatal("Parse error on line %d of fingerprint: %s\n", lineno, thisline); } FP->OS_class[*classno].OS_Family = (char *) cp_alloc(q - p + 1); memcpy(FP->OS_class[*classno].OS_Family, p, q - p); FP->OS_class[*classno].OS_Family[q - p] = '\0'; /* And now the the OS generation, if available */ p = q; while(*p && *p != '|') p++; if (*p) p++; while(*p && isspace(*p) && *p != '|') p++; if (*p == '|') { FP->OS_class[*classno].OS_Generation = NULL; q = p; } else { q = strpbrk(p, " |"); if (!q) { fatal("Parse error on line %d of fingerprint: %s\n", lineno, thisline); } // Trim any trailing whitespace q--; while(isspace(*q)) q--; q++; if (q < p) { fatal("Parse error on line %d of fingerprint: %s\n", lineno, thisline); } FP->OS_class[*classno].OS_Generation = (char *) cp_alloc(q - p + 1); memcpy(FP->OS_class[*classno].OS_Generation, p, q - p); FP->OS_class[*classno].OS_Generation[q - p] = '\0'; } /* And finally the device type */ p = q; while(*p && !isalnum(*p)) p++; q = strchr(p, '|'); if (!q) { q = p; while(*q) q++; } // Trim any trailing whitespace q--; while(isspace(*q)) q--; q++; if (q < p) { fatal("Parse error on line %d of fingerprint: %s\n", lineno, thisline); } FP->OS_class[*classno].Device_Type = (char *) cp_alloc(q - p + 1); memcpy(FP->OS_class[*classno].Device_Type, p, q - p); FP->OS_class[*classno].Device_Type[q - p] = '\0'; // printf("Got classification #%d for the OS %s: VFGT: %s * %s * %s * %s\n", *classno, FP->OS_name, FP->OS_class[*classno].OS_Vendor, FP->OS_class[*classno].OS_Family, FP->OS_class[*classno].OS_Generation? FP->OS_class[*classno].OS_Generation : "(null)", FP->OS_class[*classno].Device_Type); (*classno)++; FP->num_OS_Classifications++;}/* Parses a single fingerprint from the memory region given. If a non-null fingerprint is returned, the user is in charge of freeing it when done. This function does not require the fingerprint to be 100% complete since it is used by scripts such as scripts/fingerwatch for which some partial fingerpritns are OK. *//* This function is not currently used by Nmap, but it is present here because it is used by fingerprint utilities that link with Nmap object files. */FingerPrint *parse_single_fingerprint(char *fprint_orig) { int lineno = 0; int classno = 0; /* Number of Class lines dealt with so far */ char *p, *q; char *thisline, *nextline; char *fprint = strdup(fprint_orig); /* Make a copy we can futz with */ FingerPrint *FP; FingerPrint *current; /* Since a fingerprint is really a linked list of FingerPrint structures */ current = FP = (FingerPrint *) safe_zalloc(sizeof(FingerPrint)); thisline = fprint; do /* 1 line at a time */ { nextline = strchr(thisline, '\n'); if (nextline) *nextline++ = '\0'; /* printf("Preparing to handle next line: %s\n", thisline); */ while(*thisline && isspace((int) *thisline)) thisline++; if (!*thisline) { fatal("Parse error on line %d of fingerprint: %s", lineno, nextline); } if (strncmp(thisline, "Fingerprint ", 12) == 0) { p = thisline + 12; while(*p && isspace((int) *p)) p++; q = strchr(p, '\n'); if (!q) q = p + strlen(p); while(isspace(*(--q))) ; if (q < p) fatal("Parse error on line %d of fingerprint: %s", lineno, nextline); FP->OS_name = (char *) cp_alloc(q - p + 2); memcpy(FP->OS_name, p, q - p + 1); FP->OS_name[q - p + 1] = '\0'; } else if (strncmp(thisline, "Class ", 6) == 0) { parse_classline(FP, thisline, lineno, &classno); } else if ((q = strchr(thisline, '('))) { *q = '\0'; if(current->name) { current->next = (FingerPrint *) safe_zalloc(sizeof(FingerPrint)); current = current->next; } current->name = strdup(thisline); p = q+1; *q = '('; q = strchr(p, ')'); if (!q) { fatal("Parse error on line %d of fingerprint: %s\n", lineno, thisline); } *q = '\0'; current->results = str2AVal(p); } else { fatal("Parse error line line #%d of fingerprint", lineno); } thisline = nextline; /* Time to handle the next line, if there is one */ lineno++; } while (thisline && *thisline); return FP;}void free_fingerprint_file(FingerPrintDB *DB) { FingerPrint **FPs = DB->prints; FingerPrint **current; FingerPrint *c, *d; struct AVal *avc; struct AVal *avd; for(current = FPs; *current != NULL; current++){ for(c = *current; c; c=d){ d = c->next; if(c->name) free((void*)c->name); //strdup if(c->results){ for(avc = c->results; avc; avc = avd) { avd = avc->next; if(avc->attribute) free(avc->attribute); } free(c->results); } free(c); } } free(FPs); if (DB->MatchPoints) { for(c = DB->MatchPoints; c; c=d){ d = c->next; if(c->name) free((void*)c->name); //strdup if(c->results){ for(avc = c->results; avc; avc = avd) { avd = avc->next; if(avc->attribute) free(avc->attribute); } free(c->results); } free(c); } } free(DB);}FingerPrintDB *parse_fingerprint_file(char *fname) {FingerPrintDB *DB = NULL;FingerPrint *current;FILE *fp;int max_records = 4096; char line[512];int numrecords = 0;int lineno = 0; bool parsingMatchPoints = false;int classno = 0; /* Number of Class lines dealt with so far */ DB = (FingerPrintDB *) safe_zalloc(sizeof(FingerPrintDB));char *p, *q; /* OH YEAH!!!! */ if (!DB) fatal("non-allocated DB passed to %s", __func__); DB->prints = (FingerPrint **) safe_zalloc(sizeof(FingerPrint *) * max_records); fp = fopen(fname, "r"); if (!fp) fatal("Unable to open Nmap fingerprint file: %s", fname); top:while(fgets(line, sizeof(line), fp)) { lineno++; /* Read in a record */ if (*line == '\n' || *line == '#') continue; fparse: if (strncasecmp(line, "FingerPrint", 11) == 0) { parsingMatchPoints = false; } else if (strncasecmp(line, "MatchPoints", 11) == 0) { if (DB->MatchPoints) fatal("Found MatchPoints directive on line %d of %s even though it has previously been seen in the file", lineno, fname); parsingMatchPoints = true; } else { error("Parse error on line %d of nmap-os-db file: %s", lineno, line); continue; } current = (FingerPrint *) safe_zalloc(sizeof(FingerPrint)); if (parsingMatchPoints) { current->OS_name = NULL; DB->MatchPoints = current; } else { DB->prints[numrecords] = current; p = line + 12; while(*p && isspace((int) *p)) p++; q = strpbrk(p, "\n#"); if (!p) fatal("Parse error on line %d of fingerprint: %s", lineno, line); while(isspace(*(--q))) ; if (q < p) fatal("Parse error on line %d of fingerprint: %s", lineno, line); current->OS_name = (char *) cp_alloc(q - p + 2); memcpy(current->OS_name, p, q - p + 1); current->OS_name[q - p + 1] = '\0'; } current->line = lineno; classno = 0; /* Now we read the fingerprint itself */ while(fgets(line, sizeof(line), fp)) { lineno++; if (*line == '#') continue; if (*line == '\n') break; if (!strncmp(line, "FingerPrint ",12)) { goto fparse; } else if (strncmp(line, "Class ", 6) == 0) { parse_classline(current, line, lineno, &classno); } else { p = line; q = strchr(line, '('); if (!q) { error("Parse error on line %d of nmap-os-db file: %s", lineno, line); goto top; } *q = '\0'; if(current->name) { current->next = (FingerPrint *) safe_zalloc(sizeof(FingerPrint)); current = current->next; } current->name = strdup(p); p = q+1; *q = '('; q = strchr(p, ')'); if (!q) { error("Parse error on line %d of nmap-os-db file: %s", lineno, line); goto top; } *q = '\0'; current->results = str2AVal(p); } } /* printf("Read in fingerprint:\n%s\n", fp2ascii(DB->prints[numrecords])); */ if (!parsingMatchPoints) numrecords++; if (numrecords >= max_records) fatal("Too many OS fingerprints -- 0verflow"); } fclose(fp); DB->prints[numrecords] = NULL; return DB;}FingerPrintDB *parse_fingerprint_reference_file(char *dbname) {char filename[256];if (nmap_fetchfile(filename, sizeof(filename), dbname) != 1){ fatal("OS scan requested but I cannot find %s file. It should be in %s, ~/.nmap/ or .", dbname, NMAPDATADIR);}/* Record where this data file was found. */o.loaded_data_files[dbname] = filename; return parse_fingerprint_file(filename);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -