mirror of
https://github.com/nmap/nmap.git
synced 2025-12-25 17:09:02 +00:00
Update fingermatch to accept wrapped fingerprints, factor out fingerprint reading code into fingerlib so it can be shared by fingerfix/fingermatch (and soon fingerdiff)
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include "nmap.h"
|
||||
#include "osscan.h"
|
||||
#include "MACLookup.h"
|
||||
#include "fingerlib.h"
|
||||
|
||||
// attribute value length
|
||||
#define AVLEN 128
|
||||
@@ -13,190 +14,6 @@ void usage() {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static int checkFP(char *FP) {
|
||||
char *p;
|
||||
char macbuf[16];
|
||||
u8 macprefix[3];
|
||||
char tmp;
|
||||
bool founderr = false;
|
||||
int i;
|
||||
|
||||
// SCAN
|
||||
p = strstr(FP, "SCAN(");
|
||||
if(!p) {
|
||||
founderr = true;
|
||||
printf("[WARN] SCAN line is missing");
|
||||
} else {
|
||||
// SCAN.G: whether the fingerprint is good
|
||||
p = strstr(FP, "%G=");
|
||||
if(!p) p = strstr(FP, "(G=");
|
||||
if(!p) {
|
||||
printf("[WARN] Attribute G is missing in SCAN line\n");
|
||||
founderr = true;
|
||||
} else {
|
||||
tmp = *(p+3);
|
||||
if(tmp != 'Y') {
|
||||
printf("[WARN] One fingerprint is not good\n");
|
||||
founderr = true;
|
||||
}
|
||||
}
|
||||
|
||||
// SCAN.M: mac prefix of the target.
|
||||
// if there is a MAC prefix, print the vendor name
|
||||
p = strstr(FP, "%M=");
|
||||
if(!p) p = strstr(FP, "(M=");
|
||||
if(p) {
|
||||
p = p + 3;
|
||||
for(i = 0; i < 6; i++) {
|
||||
if(!p[i] || !isxdigit(p[i])) {
|
||||
printf("[WARN] Invalid value (%s) occurs in SCAN.M\n", p);
|
||||
founderr = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!founderr) {
|
||||
strncpy(macbuf, p, 6);
|
||||
i = strtol(macbuf, NULL, 16);
|
||||
macprefix[0] = i >> 16;
|
||||
macprefix[1] = (i >> 8) & 0xFF;
|
||||
macprefix[2] = i & 0xFF;
|
||||
printf("[INFO] Vendor Info: %s\n", MACPrefix2Corp(macprefix));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Now we validate that all elements are present */
|
||||
p = FP;
|
||||
if (!strstr(p, "SEQ(") || !strstr(p, "OPS(") || !strstr(p, "WIN(") ||
|
||||
!strstr(p, "ECN(") || !strstr(p, "T1(") || !strstr(p, "T2(") ||
|
||||
!strstr(p, "T3(") || !strstr(p, "T4(") || !strstr(p, "T5(") ||
|
||||
!strstr(p, "T6(") || !strstr(p, "T7(") || !strstr(p, "U1(") ||
|
||||
!strstr(p, "IE(")) {
|
||||
/* This ought to get my attention :) */
|
||||
founderr = true;
|
||||
printf("[WARN] Fingerprint is missing at least 1 element\n");
|
||||
}
|
||||
|
||||
if(founderr) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Returns -1 (or exits) for failure */
|
||||
static int readFP(FILE *filep, char *FP, int FPsz ) {
|
||||
char line[512];
|
||||
int linelen = 0;
|
||||
int lineno = 0;
|
||||
char *p, *q;
|
||||
char *oneFP;
|
||||
char *dst = FP;
|
||||
char tmp[16];
|
||||
int i;
|
||||
bool isInWrappedFP = false; // whether we are currently reading in a
|
||||
// wrapped fingerprint
|
||||
|
||||
if(FPsz < 50) return -1;
|
||||
FP[0] = '\0';
|
||||
|
||||
while((fgets(line, sizeof(line), filep))) {
|
||||
lineno++;
|
||||
linelen = strlen(line);
|
||||
p = line;
|
||||
if (*p == '\n' || *p == '.') {
|
||||
// end of input
|
||||
|
||||
if(isInWrappedFP) {
|
||||
// We have just completed reading in a wrapped fp. Because a
|
||||
// wrapped fp is submitted by user, so we check if there is a
|
||||
// SCAN line in it. If yes, look inside the scan line.
|
||||
*dst = '\0';
|
||||
checkFP(oneFP);
|
||||
isInWrappedFP = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
while(*p && isspace(*p)) p++;
|
||||
if (*p == '#')
|
||||
continue; // skip the comment line
|
||||
|
||||
if (dst - FP + linelen >= FPsz - 5)
|
||||
fatal("[ERRO] Overflow!\n");
|
||||
|
||||
if(strncmp(p, "OS:", 3) == 0) {
|
||||
// the line is start with "OS:"
|
||||
if(!isInWrappedFP) {
|
||||
// just enter a wrapped fp area
|
||||
oneFP = dst;
|
||||
isInWrappedFP = true;
|
||||
}
|
||||
p += 3;
|
||||
while(*p != '\r' && *p != '\n') {
|
||||
*dst++ = toupper(*p);
|
||||
if(*p == ')') *dst++ = '\n';
|
||||
p++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// this line is not start with "OS:"
|
||||
if(isInWrappedFP) {
|
||||
// We have just completed reading in a wrapped fp. Because a
|
||||
// wrapped fp is submitted by user, so we check if there is a
|
||||
// SCAN line in it. If yes, look inside the scan line.
|
||||
*dst = '\0';
|
||||
checkFP(oneFP);
|
||||
isInWrappedFP = false;
|
||||
}
|
||||
|
||||
q = p; i = 0;
|
||||
while(q && *q && i<12)
|
||||
tmp[i++] = toupper(*q++);
|
||||
tmp[i] = '\0';
|
||||
if(strncmp(tmp, "FINGERPRINT", 11) == 0) {
|
||||
q = p + 11;
|
||||
while(*q && isspace(*q)) q++;
|
||||
if (*q) { // this fingeprint line is not empty
|
||||
strncpy(dst, "Fingerprint", 11);
|
||||
dst += 11;
|
||||
p += 11;
|
||||
while(*p) *dst++ = *p++;
|
||||
}
|
||||
continue;
|
||||
} else if(strncmp(tmp, "CLASS", 5) == 0) {
|
||||
q = p + 5;
|
||||
while(*q && isspace(*q)) q++;
|
||||
if (*q) {// this class line is not empty
|
||||
strncpy(dst, "Class", 5);
|
||||
dst += 5;
|
||||
p += 5;
|
||||
while(*p) *dst++ = *p++;
|
||||
}
|
||||
continue;
|
||||
} else if(strchr(p, '(')) {
|
||||
while(*p) *dst++ = toupper(*p++);
|
||||
} else {
|
||||
printf("[WARN] Skip bogus line: %s\n", p);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Now we validate that all elements are present. Though this maybe
|
||||
// redundant because we have checked it for those wrapped FPs, it
|
||||
// doesn't hurt to give a duplicated warning here.
|
||||
p = FP;
|
||||
if (!strstr(p, "SEQ(") || !strstr(p, "OPS(") || !strstr(p, "WIN(") ||
|
||||
!strstr(p, "ECN(") || !strstr(p, "T1(") || !strstr(p, "T2(") ||
|
||||
!strstr(p, "T3(") || !strstr(p, "T4(") || !strstr(p, "T5(") ||
|
||||
!strstr(p, "T6(") || !strstr(p, "T7(") || !strstr(p, "U1(") ||
|
||||
!strstr(p, "IE(")) {
|
||||
/* This ought to get my attention :) */
|
||||
printf("[WARN] Fingerprint is missing at least 1 element\n");
|
||||
}
|
||||
|
||||
if (dst - FP < 1)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
STR, DECNUM, HEXNUM
|
||||
} SortAs;
|
||||
|
||||
Reference in New Issue
Block a user