6 extern globalconfig config
;
8 static int parse_dhcp_sig_opts(dhcp_fp_entry
*sig
, char* p
);
9 static int parse_dhcp_sig_optreq(dhcp_fp_entry
*sig
, char* p
);
10 static dhcp_fp_entry
*alloc_dhcp_sig(dhcp_fp_entry
*e
);
11 static void free_dhcp_sigs(dhcp_fp_entry
*e
);
13 static const unsigned char vendcookie
[] = { 99, 130, 83, 99 };
14 #define BOOTP_COOKIE_SIZE 4
17 void print_dhcp_header(dhcp_header
*dhcph
)
19 plog("OP:%d\n",dhcph
->op
);
20 plog("HTYPE:%d\n",dhcph
->htype
);
21 plog("HLEN:%d\n",dhcph
->hlen
);
22 plog("HOPS:%d\n",dhcph
->hops
);
23 plog("XID:%d\n",dhcph
->xid
);
24 plog("SECS:%d\n",dhcph
->secs
);
25 plog("FLAGS:%d\n",dhcph
->flags
);
27 char dest
[INET_ADDRSTRLEN
];
28 inet_ntop(AF_INET
,&(dhcph
->ciaddr
),dest
,INET_ADDRSTRLEN
+ 1);
29 plog("CIP:%s\n",dest
);
30 inet_ntop(AF_INET
,&(dhcph
->yiaddr
),dest
,INET_ADDRSTRLEN
+ 1);
31 plog("YIP:%s\n",dest
);
32 inet_ntop(AF_INET
,&(dhcph
->siaddr
),dest
,INET_ADDRSTRLEN
+ 1);
33 plog("SIP:%s\n",dest
);
34 inet_ntop(AF_INET
,&(dhcph
->giaddr
),dest
,INET_ADDRSTRLEN
+ 1);
35 plog("GIP:%s\n",dest
);
38 for(i
= 0; i
< 6; i
++){
39 printf("%02hhx", dhcph
->chaddr
[i
]);
40 if (i
!= dhcph
->hlen
-1)
44 plog("SNAME:%s\n",dhcph
->sname
);
45 plog("FILE:%s\n",dhcph
->file
);
49 dhcp_fp_entry
*dhcp_fingerprint(packetinfo
*pi
)
51 plog("Got DHCP packet:\n");
52 config
.pr_s
.dhcp_os_assets
++;
54 uint8_t dhcp_header_length
;
56 uint8_t *dhcp_options
;
58 uint8_t dhcp_opt_type
= 0;
59 uint8_t end_opt_parsing
= 0;
63 dhcph = (dhcp_header *) (pi->payload);
64 print_dhcp_header(dhcph);
66 dhcp_header_length
= sizeof(dhcp_header
);
67 dhcp_mc
= (uint8_t *) (pi
->payload
+ dhcp_header_length
);
69 /* TODO: check and bail if not there */
70 //plog("Magic Cookie: %d%d%d%d\n", *dhcp_mc, *(dhcp_mc+1), *(dhcp_mc+2), *(dhcp_mc+3)); // 99 130 83 99
72 dhcp_options
= (uint8_t *) dhcp_mc
+ BOOTP_COOKIE_SIZE
;
73 uint8_t *optptr
= dhcp_options
;
74 uint8_t max_len
= (pi
->plen
- dhcp_header_length
- BOOTP_COOKIE_SIZE
);
76 dhcp_fp_entry dhcpfp
= {0}; //guarantee it's empty this sig
77 dhcpfp
.ttl
= pi
->ip4
->ip_ttl
;
82 while (optlen
< max_len
) {
85 uint8_t opt
= *(optptr
);
86 uint8_t optsize
= *(optptr
+1);
87 uint8_t *optdata
= optptr
+2;
89 dhcpfp
.opt
[optcnt
] = opt
;
92 case DHCP_OPTION_TYPE
: /* 53 */
94 dhcp_opt_type
= *optdata
;
95 dhcpfp
.type
= dhcp_opt_type
;
98 case DHCP_OPTION_OPTIONREQ
: /* 55 */
100 uint8_t optreqcnt
= 0;
101 for (i
=2; i
< optsize
+2; i
++) {
102 dhcpfp
.optreq
[optreqcnt
] = *(optptr
+i
);
105 dhcpfp
.optreqcnt
= optreqcnt
;
108 case DHCP_OPTION_CLASS_IDENTIFIER
: /* 60 */
110 dhcpfp
.vc
= calloc(1, optsize
+ 1);
111 strncpy(dhcpfp
.vc
, (char*) optdata
, optsize
);
114 case DHCP_OPTION_PAD
: /* 0 */
116 case DHCP_OPTION_END
: /* 255 */
124 optptr
= optptr
+ optsize
+ 2;
126 optlen
= optlen
+ optsize
+ 2;
128 if (end_opt_parsing
== 1) break;
130 /* Just to be sure */
131 if (*(optptr
) != DHCP_OPTION_END
) {
132 if (optptr
+ *(optptr
+1) + 2 > pi
->payload
+ pi
->plen
) break;
136 dhcpfp
.optcnt
= optcnt
;
138 print_dhcp_sig(&dhcpfp
);
141 dhcp_fp_entry
*match
= find_dhcp_match(&dhcpfp
, pi
);
143 #define OS_DHCP = 0x01
144 //update_asset_os(pi, OS_DHCP, NULL, &dhcpfp, tstamp);
148 dhcp_fp_entry
*find_dhcp_match(dhcp_fp_entry
*dhcpfp
, packetinfo
*pi
)
153 //uint32_t hashsize; // = config.sig_hashsize;
156 // hashsize = DHCP_SIG_HASHSIZE;
160 index
= (DHCP_SIGHASH(dhcpfp
->type
, dhcpfp
->optcnt
) % 331);
162 p
= config
.sig_dhcp
[index
];
164 /* Cheap and specific checks first... */
165 if (dhcpfp
->type
^ p
->type
) { p
= p
->next
; continue; }
166 if (dhcpfp
->optcnt
^ p
->optcnt
) { p
= p
->next
; continue; }
167 if (dhcpfp
->optreqcnt
^ p
->optreqcnt
) { p
= p
->next
; continue; }
169 /* Numbers agree. Let's check options 53 first */
170 if (dhcpfp
->optreqcnt
!= 0) {
171 for (j
=0;j
<dhcpfp
->optreqcnt
;j
++){
172 if (p
->optreq
[j
] ^ dhcpfp
->optreq
[j
]) goto continue_search
;
176 /* Let's check options */
177 if (dhcpfp
->optcnt
!= 0) {
178 for (j
=0;j
<dhcpfp
->optcnt
;j
++){
179 if (p
->opt
[j
] ^ dhcpfp
->opt
[j
]) goto continue_search
;
183 /* Numbers agree. Lets match Vendor Code */
184 if (p
->vc
!= NULL
&& dhcpfp
->vc
!= NULL
) {
185 if (strcmp(p
->vc
, dhcpfp
->vc
) == 0) {
186 /* Huston - we have a match */
187 plog("[*] We have a match (");
190 //plog("OS: %s\n",p->os);
191 //plog("DESC: %s\n",p->desc);
195 /* Huston - we have a match */
196 plog("[*] We have a match (");
199 //plog("OS: %s, ",p->os);
200 //plog("DESC: %s)\n",p->desc);
210 void print_data(const uint8_t* data
, uint16_t dlen
) {
211 uint8_t tbuf
[PKT_MAXPAY
+2];
214 uint8_t max
= dlen
> PKT_MAXPAY
? PKT_MAXPAY
: dlen
;
218 for (i
=0;i
<max
;i
++) {
219 if (isprint(*data
)) *(t
++) = *data
;
220 else if (!*data
) *(t
++) = '?';
230 int load_dhcp_sigs(const char *file
, dhcp_fp_entry
**dhcpsp
[], int hashsize
)
233 dhcp_fp_entry
**sig
; // output
235 FILE *f
= fopen(file
, "r");
239 perror("failed to open file");
243 perror("need a pointer to fill");
247 hashsize
= DHCP_SIG_HASHSIZE
;
249 *dhcpsp
= calloc(hashsize
, sizeof(dhcp_fp_entry
*));
253 while ((p
= fgets(buf
, sizeof(buf
), f
))) {
256 char opts
[MAXLINE
], optreq
[MAXLINE
], genre
[MAXLINE
], desc
[MAXLINE
];
260 dhcp_fp_entry asig
= {0}; //guarantee it's empty this sig
265 /* Remove leading and trailing blanks */
269 while (l
&& isspace(*(p
+ l
- 1)))
270 *(p
+ (l
--) - 1) = 0;
272 /* Skip empty lines and comments */
278 /* T4:64:1:60:M*,S,T,N,W7:.:Linux:2.6 (newer, 7) */
279 /* 53-OPTION:TTL:ALL-OPTIONS:55-OPTIONS:60-OPTIONS:OS:OS Details */
280 /* 1:128:53,116,61,12,60,55,43:1,15,3,6,44,46,47,31,33,121,249,43:MSFT 5.0:Windows:Windows XP SP3 */
282 (p
, "%d:%d:%[^:]:%[^:]:%[^:]:%[^:]:%[^:]",
283 &type
, &t
, opts
, optreq
, vc
, genre
, desc
) != 7)
284 fatal("Syntax error in config line %d.\n", ln
);
290 asig
.os
= strdup(gptr
);
291 asig
.desc
= strdup(desc
);
292 asig
.vc
= strdup(vc
);
295 parse_dhcp_sig_opts(&asig
, opts
);
296 parse_dhcp_sig_optreq(&asig
, optreq
);
298 uint32_t index
= (DHCP_SIGHASH(asig
.type
, asig
.optcnt
) % 331);
302 sig
[index
] = alloc_dhcp_sig(&asig
);
311 fprintf(stderr, "hash collision: %s\n", p);
313 e
->next
= alloc_dhcp_sig(&asig
);
321 for (i
= 0; i
< sigcnt
; i
++) {
322 print_dhcp_sig(&sig
[i
]);
330 printf("DHCP hash table layout: ");
331 for (i
= 0; i
< hashsize
; i
++) {
342 #endif /* DEBUG_HASH */
344 // if (check_collide)
345 // debug("[+] DHCP signature collision check successful.\n");
350 /* run through the hash, free entries, then free hash */
351 void unload_dhcp_sigs(dhcp_fp_entry
**sigp
, int size
)
359 sigp
[i
] = NULL
; // clear
365 /* alloc_sig return a newly allocated copy of *e */
366 static dhcp_fp_entry
*alloc_dhcp_sig(dhcp_fp_entry
*e
)
368 dhcp_fp_entry
*n
= calloc(1, sizeof(dhcp_fp_entry
));
373 /* recursively free signatures */
374 static void free_dhcp_sigs(dhcp_fp_entry
*e
){
376 free_dhcp_sigs(e
->next
);
380 void dump_dhcp_sigs(dhcp_fp_entry
*mysig
[], int max
)
383 for (i
= 0; i
< max
; i
++){
384 if (!mysig
[i
] || !mysig
[i
]->os
)
386 print_dhcp_sig(mysig
[i
]);
390 void print_dhcp_sig(dhcp_fp_entry
* e
)
394 plog("[%d:%d:",e
->type
,e
->ttl
);
395 for (j
=0;j
<e
->optcnt
;j
++){
396 plog("%d",e
->opt
[j
]);
397 if ((j
+1) < (e
->optcnt
)) plog(",");
400 for (j
=0;j
<e
->optreqcnt
;j
++){
401 plog("%d",e
->optreq
[j
]);
402 if ((j
+1) < (e
->optreqcnt
)) plog(",");
404 if (e
->optreqcnt
==0) plog(".");
418 if (e
->desc
== NULL
) {
425 void print_dhcp_sigs(dhcp_fp_entry
* e
)
429 print_dhcp_sigs(e
->next
);
433 /* parse the option field of the signature line */
434 static int parse_dhcp_sig_opts(dhcp_fp_entry
*sig
, char* p
)
442 if (!isdigit(*(p
))) {
443 fatal("Bogus DHCP value in line %d.\n", sig
->line
);
445 if (!isdigit(*(p
+1))) {
446 sig
->opt
[optcnt
] = atoi(p
);
447 } else if (!isdigit(*(p
+2))) {
448 sig
->opt
[optcnt
] = atoi(p
);
450 } else if (!isdigit(*(p
+3))) {
451 sig
->opt
[optcnt
] = atoi(p
);
457 if (++optcnt
>= MAXDHCPOPTS
)
459 ("Too many DHCP options specified in config line %d.\n",
466 } while (*p
&& !isdigit(*p
));
468 sig
->optcnt
= optcnt
;
472 /* parse the option field of the signature line */
473 static int parse_dhcp_sig_optreq(dhcp_fp_entry
*sig
, char* p
)
475 uint8_t optreqcnt
= 0;
481 if (!isdigit(*(p
))) {
482 fatal("Bogus DHCP value in line %d.\n", sig
->line
);
484 if (!isdigit(*(p
+ 1))) {
485 sig
->optreq
[optreqcnt
] = atoi(p
);
486 } else if (!isdigit(*(p
+ 2))) {
487 sig
->optreq
[optreqcnt
] = atoi(p
);
489 } else if (!isdigit(*(p
+ 3))) {
490 sig
->optreq
[optreqcnt
] = atoi(p
);
496 if (++optreqcnt
>= MAXDHCPOPTS
)
498 ("Too many DHCP request options specified in config line %d.\n",
505 } while (*p
&& !isdigit(*p
));
507 sig
->optreqcnt
= optreqcnt
;