forked from nmap/nmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FPEngine.cc
2814 lines (2419 loc) · 108 KB
/
FPEngine.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* FPEngine.cc -- Routines used for IPv6 OS detection via TCP/IP *
* fingerprinting. * For more information on how this works in Nmap, see *
* https://nmap.org/osdetect/ *
* *
***********************IMPORTANT NMAP LICENSE TERMS************************
* *
* The Nmap Security Scanner is (C) 1996-2018 Insecure.Com LLC ("The Nmap *
* Project"). Nmap is also a registered trademark of the Nmap Project. *
* This program is free software; you may redistribute and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE *
* CLARIFICATIONS AND EXCEPTIONS DESCRIBED HEREIN. This guarantees your *
* right to use, modify, and redistribute this software under certain *
* conditions. If you wish to embed Nmap technology into proprietary *
* software, we sell alternative licenses (contact [email protected]). *
* Dozens of software vendors already license Nmap technology such as *
* host discovery, port scanning, OS detection, version detection, and *
* the Nmap Scripting Engine. *
* *
* Note that the GPL places important restrictions on "derivative works", *
* yet it does not provide a detailed definition of that term. To avoid *
* misunderstandings, we interpret that term as broadly as copyright law *
* allows. For example, we consider an application to constitute a *
* derivative work for the purpose of this license if it does any of the *
* following with any software or content covered by this license *
* ("Covered Software"): *
* *
* o Integrates source code from Covered Software. *
* *
* o Reads or includes copyrighted data files, such as Nmap's nmap-os-db *
* or nmap-service-probes. *
* *
* o Is designed specifically to execute Covered Software and parse the *
* results (as opposed to typical shell or execution-menu apps, which will *
* execute anything you tell them to). *
* *
* o Includes Covered Software in a proprietary executable installer. The *
* installers produced by InstallShield are an example of this. Including *
* Nmap with other software in compressed or archival form does not *
* trigger this provision, provided appropriate open source decompression *
* or de-archiving software is widely available for no charge. For the *
* purposes of this license, an installer is considered to include Covered *
* Software even if it actually retrieves a copy of Covered Software from *
* another source during runtime (such as by downloading it from the *
* Internet). *
* *
* o Links (statically or dynamically) to a library which does any of the *
* above. *
* *
* o Executes a helper program, module, or script to do any of the above. *
* *
* This list is not exclusive, but is meant to clarify our interpretation *
* of derived works with some common examples. Other people may interpret *
* the plain GPL differently, so we consider this a special exception to *
* the GPL that we apply to Covered Software. Works which meet any of *
* these conditions must conform to all of the terms of this license, *
* particularly including the GPL Section 3 requirements of providing *
* source code and allowing free redistribution of the work as a whole. *
* *
* As another special exception to the GPL terms, the Nmap Project grants *
* permission to link the code of this program with any version of the *
* OpenSSL library which is distributed under a license identical to that *
* listed in the included docs/licenses/OpenSSL.txt file, and distribute *
* linked combinations including the two. *
* *
* The Nmap Project has permission to redistribute Npcap, a packet *
* capturing driver and library for the Microsoft Windows platform. *
* Npcap is a separate work with it's own license rather than this Nmap *
* license. Since the Npcap license does not permit redistribution *
* without special permission, our Nmap Windows binary packages which *
* contain Npcap may not be redistributed without special permission. *
* *
* Any redistribution of Covered Software, including any derived works, *
* must obey and carry forward all of the terms of this license, including *
* obeying all GPL rules and restrictions. For example, source code of *
* the whole work must be provided and free redistribution must be *
* allowed. All GPL references to "this License", are to be treated as *
* including the terms and conditions of this license text as well. *
* *
* Because this license imposes special exceptions to the GPL, Covered *
* Work may not be combined (even as part of a larger work) with plain GPL *
* software. The terms, conditions, and exceptions of this license must *
* be included as well. This license is incompatible with some other open *
* source licenses as well. In some cases we can relicense portions of *
* Nmap or grant special permissions to use it in other open source *
* software. Please contact [email protected] with any such requests. *
* Similarly, we don't incorporate incompatible open source software into *
* Covered Software without special permission from the copyright holders. *
* *
* If you have any questions about the licensing restrictions on using *
* Nmap in other works, we are happy to help. As mentioned above, we also *
* offer an alternative license to integrate Nmap into proprietary *
* applications and appliances. These contracts have been sold to dozens *
* of software vendors, and generally include a perpetual license as well *
* as providing support and updates. They also fund the continued *
* development of Nmap. Please email [email protected] for further *
* information. *
* *
* If you have received a written license agreement or contract for *
* Covered Software stating terms other than these, you may choose to use *
* and redistribute Covered Software under those terms instead of these. *
* *
* Source is provided to this software because we believe users have a *
* right to know exactly what a program is going to do before they run it. *
* This also allows you to audit the software for security holes. *
* *
* Source code also allows you to port Nmap to new platforms, fix bugs, *
* and add new features. You are highly encouraged to send your changes *
* to the [email protected] mailing list for possible incorporation into the *
* main distribution. By sending these changes to Fyodor or one of the *
* Insecure.Org development mailing lists, or checking them into the Nmap *
* source code repository, it is understood (unless you specify *
* otherwise) that you are offering the Nmap Project the unlimited, *
* non-exclusive right to reuse, modify, and relicense the code. Nmap *
* will always be available Open Source, but this is important because *
* the inability to relicense code has caused devastating problems for *
* other Free Software projects (such as KDE and NASM). We also *
* occasionally relicense the code to third parties as discussed above. *
* If you wish to specify special license conditions of your *
* contributions, just say so when you send them. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Nmap *
* license file for more details (it's in a COPYING file included with *
* Nmap, and also available from https://svn.nmap.org/nmap/COPYING) *
* *
***************************************************************************/
/* $Id$ */
#include "FPEngine.h"
#include "Target.h"
#include "FingerPrintResults.h"
#include "NmapOps.h"
#include "nmap_error.h"
#include "osscan.h"
#include "linear.h"
#include "FPModel.h"
#include "tcpip.h"
extern NmapOps o;
#ifdef WIN32
/* Need DnetName2PcapName */
#include "libnetutil/netutil.h"
/* from libdnet's intf-win32.c */
extern "C" int g_has_npcap_loopback;
#endif
#include <math.h>
/******************************************************************************
* Globals. *
******************************************************************************/
/* This is the global network controller. FPHost classes use it to request
* network resources and schedule packet transmissions. */
FPNetworkControl global_netctl;
/******************************************************************************
* Implementation of class FPNetworkControl. *
******************************************************************************/
FPNetworkControl::FPNetworkControl() {
memset(&this->nsp, 0, sizeof(nsock_pool));
memset(&this->pcap_nsi, 0, sizeof(pcap_nsi));
memset(&this->pcap_ev_id, 0, sizeof(nsock_event_id));
this->nsock_init = false;
this->rawsd = -1;
this->probes_sent = 0;
this->responses_recv = 0;
this->probes_timedout = 0;
this->cc_cwnd = 0;
this->cc_ssthresh = 0;
}
FPNetworkControl::~FPNetworkControl() {
if (this->nsock_init) {
nsock_event_cancel(this->nsp, this->pcap_ev_id, 0);
nsock_pool_delete(this->nsp);
this->nsock_init = false;
}
}
/* (Re)-Initialize object's state (default parameter setup and nsock
* initialization). */
void FPNetworkControl::init(const char *ifname, devtype iftype) {
/* Init congestion control parameters */
this->cc_init();
/* If there was a previous nsock pool, delete it */
if (this->pcap_nsi) {
nsock_iod_delete(this->pcap_nsi, NSOCK_PENDING_SILENT);
}
if (this->nsock_init) {
nsock_event_cancel(this->nsp, this->pcap_ev_id, 0);
nsock_pool_delete(this->nsp);
}
/* Create a new nsock pool */
if ((this->nsp = nsock_pool_new(NULL)) == NULL)
fatal("Unable to obtain an Nsock pool");
nsock_set_log_function(nmap_nsock_stderr_logger);
nmap_adjust_loglevel(o.packetTrace());
nsock_pool_set_device(nsp, o.device);
if (o.proxy_chain)
nsock_pool_set_proxychain(this->nsp, o.proxy_chain);
/* Allow broadcast addresses */
nsock_pool_set_broadcast(this->nsp, 1);
/* Allocate an NSI for packet capture */
this->pcap_nsi = nsock_iod_new(this->nsp, NULL);
this->first_pcap_scheduled = false;
/* Flag it as already initialized so we free this nsp next time */
this->nsock_init = true;
/* Obtain raw socket or check that we can obtain an eth descriptor. */
if ((o.sendpref & PACKET_SEND_ETH) && (iftype == devt_ethernet
#ifdef WIN32
|| (g_has_npcap_loopback && iftype == devt_loopback)
#endif
) && ifname != NULL) {
/* We don't need to store the eth handler because FPProbes come with a
* suitable one (FPProbes::getEthernet()), we just attempt to obtain one
* to see if it fails. */
if (eth_open_cached(ifname) == NULL)
fatal("dnet: failed to open device %s", ifname);
this->rawsd = -1;
} else {
#ifdef WIN32
win32_fatal_raw_sockets(ifname);
#endif
if (this->rawsd >= 0)
close(this->rawsd);
rawsd = nmap_raw_socket();
if (rawsd < 0)
pfatal("Couldn't obtain raw socket in %s", __func__);
}
/* De-register existing callers */
while (this->callers.size() > 0) {
this->callers.pop_back();
}
return;
}
/* This function initializes the controller's congestion control parameters.
* The network controller uses TCP's Slow Start and Congestion Avoidance
* algorithms from RFC 5681 (slightly modified for convenience).
*
* As the OS detection process does not open full TCP connections, we can't just
* use ACKs (or the lack of ACKs) to increase or decrease the congestion window
* so we use probe responses. Every time we get a response to an OS detection
* probe, we treat it as if it was a TCP ACK in TCP's congestion control.
*
* Note that the initial Congestion Window is set to the number of timed
* probes that we send to each target. This is necessary since we need to
* know for sure that we can send that many packets in order to transmit them.
* Otherwise, we could fail to deliver the probes 100ms apart. */
int FPNetworkControl::cc_init() {
this->probes_sent = 0;
this->responses_recv = 0;
this->probes_timedout = 0;
this->cc_cwnd = OSSCAN_INITIAL_CWND;
this->cc_ssthresh = OSSCAN_INITIAL_SSTHRESH;
return OP_SUCCESS;
}
/* This method is used to indicate that we have scheduled the transmission of
* one or more packets. This is used in congestion control to determine the
* number of outstanding probes (number of probes sent but not answered yet)
* and therefore, the effective transmission window. @param pkts indicates the
* number of packets that were scheduled. Returns OP_SUCCESS on success and
* OP_FAILURE in case of error. */
int FPNetworkControl::cc_update_sent(int pkts = 1) {
if (pkts <= 0)
return OP_FAILURE;
this->probes_sent+=pkts;
return OP_SUCCESS;
}
/* This method is used to indicate that a drop has occurred. In TCP, drops are
* detected by the absence of an ACK. However, we can't use that, since it is
* very likely that our targets do not respond to some of our OS detection
* probes intentionally. For this reason, we consider that a drop has occurred
* when we receive a response for a probe that has already suffered one
* retransmission (first transmission got dropped in transit, some later
* transmission made it to the host and it responded). So when we detect a drop
* we do the same as TCP, adjust the congestion window and the slow start
* threshold. */
int FPNetworkControl::cc_report_drop() {
/* FROM RFC 5681
When a TCP sender detects segment loss using the retransmission timer
and the given segment has not yet been resent by way of the
retransmission timer, the value of ssthresh MUST be set to no more
than the value given in equation (4):
ssthresh = max (FlightSize / 2, 2*SMSS) (4)
where, as discussed above, FlightSize is the amount of outstanding
data in the network.
On the other hand, when a TCP sender detects segment loss using the
retransmission timer and the given segment has already been
retransmitted by way of the retransmission timer at least once, the
value of ssthresh is held constant.
*/
int probes_outstanding = this->probes_sent - this->responses_recv - this->probes_timedout;
this->cc_ssthresh = (float)MAX(probes_outstanding, OSSCAN_INITIAL_CWND);
this->cc_cwnd = OSSCAN_INITIAL_CWND;
return OP_SUCCESS;
}
/* This method is used to indicate that a response to a previous probe was
* received. For us this is like getting and ACK in TCP congestion control, so
* we update the congestion window (increase by one packet if we are in slow
* start or increase it by a small percentage of a packet if we are in
* congestion avoidance). */
int FPNetworkControl::cc_update_received() {
this->responses_recv++;
/* If we are in Slow Start, increment congestion window by one packet.
* (Note that we treat probe responses the same way TCP CC treats ACKs). */
if (this->cc_cwnd < this->cc_ssthresh) {
this->cc_cwnd += 1;
/* Otherwise we are in Congestion Avoidance and CWND is incremented slowly,
* approximately one packet per RTT */
} else {
this->cc_cwnd = this->cc_cwnd + 1/this->cc_cwnd;
}
if (o.debugging > 3) {
log_write(LOG_PLAIN, "[FPNetworkControl] Congestion Control Parameters: cwnd=%f ssthresh=%f sent=%d recv=%d tout=%d outstanding=%d\n",
this->cc_cwnd, this->cc_ssthresh, this->probes_sent, this->responses_recv, this->probes_timedout,
this->probes_sent - this->responses_recv - this->probes_timedout);
}
return OP_SUCCESS;
}
/* This method is public and can be called by FPHosts to inform the controller
* that a probe has experienced a final timeout. In other words, that no
* response was received for the probe after doing the necessary retransmissions
* and waiting for the RTO. This is used to decrease the number of outstanding
* probes. Otherwise, if no host responded to the probes, the effective
* transmission window could reach zero and prevent new probes from being sent,
* clogging the engine. */
int FPNetworkControl::cc_report_final_timeout() {
this->probes_timedout++;
return OP_SUCCESS;
}
/* This method is used by FPHosts to request permission to transmit a number of
* probes. Permission is granted if the current congestion window allows the
* transmission of new probes. It returns true if permission is granted and
* false if it is denied. */
bool FPNetworkControl::request_slots(size_t num_packets) {
int probes_outstanding = this->probes_sent - this->responses_recv - this->probes_timedout;
if (o.debugging > 3)
log_write(LOG_PLAIN, "[FPNetworkControl] Slot request for %u packets. ProbesOutstanding=%d cwnd=%f ssthresh=%f\n",
(unsigned int)num_packets, probes_outstanding, this->cc_cwnd, this->cc_ssthresh);
/* If we still have room for more outstanding probes, let the caller
* schedule transmissions. */
if ((probes_outstanding + num_packets) <= this->cc_cwnd) {
this->cc_update_sent(num_packets);
return true;
}
return false;
}
/* This method lets FPHosts register themselves in the network controller so
* the controller can call them back every time a packet they are interested
* in is captured.*/
int FPNetworkControl::register_caller(FPHost *newcaller) {
this->callers.push_back(newcaller);
return OP_SUCCESS;
}
/* This method lets FPHosts unregister themselves in the network controller so
* the controller does not call them back again. This is called by hosts that
* have already finished their OS detection. */
int FPNetworkControl::unregister_caller(FPHost *oldcaller) {
for (size_t i = 0; i < this->callers.size(); i++) {
if (this->callers[i] == oldcaller) {
this->callers.erase(this->callers.begin() + i);
return OP_SUCCESS;
}
}
return OP_FAILURE;
}
/* This method gets the controller ready for packet capture. Basically it
* obtains a pcap descriptor from nsock and sets an appropriate BPF filter. */
int FPNetworkControl::setup_sniffer(const char *iface, const char *bpf_filter) {
char pcapdev[128];
int rc;
#ifdef WIN32
/* Nmap normally uses device names obtained through dnet for interfaces, but
Pcap has its own naming system. So the conversion is done here */
if (!DnetName2PcapName(iface, pcapdev, sizeof(pcapdev))) {
/* Oh crap -- couldn't find the corresponding dev apparently. Let's just go
with what we have then ... */
Strncpy(pcapdev, iface, sizeof(pcapdev));
}
#else
Strncpy(pcapdev, iface, sizeof(pcapdev));
#endif
/* Obtain a pcap descriptor */
rc = nsock_pcap_open(this->nsp, this->pcap_nsi, pcapdev, 8192, 0, bpf_filter);
if (rc)
fatal("Error opening capture device %s\n", pcapdev);
/* Store the pcap NSI inside the pool so we can retrieve it inside a callback */
nsock_pool_set_udata(this->nsp, (void *)&(this->pcap_nsi));
return OP_SUCCESS;
}
/* This method makes the controller process pending events (like packet
* transmissions or packet captures). */
void FPNetworkControl::handle_events() {
nmap_adjust_loglevel(o.packetTrace());
nsock_loop(nsp, 50);
}
/* This method lets FPHosts to schedule the transmission of an OS detection
* probe. It takes an FPProbe pointer and the amount of milliseconds the
* controller should wait before injecting the probe into the wire. */
int FPNetworkControl::scheduleProbe(FPProbe *pkt, int in_msecs_time) {
nsock_timer_create(this->nsp, probe_transmission_handler_wrapper, in_msecs_time, (void*)pkt);
return OP_SUCCESS;
}
/* This is the handler for packet transmission. It is called by nsock whenever a timer expires,
* which means that a new packet needs to be transmitted. Note that this method is not
* called directly by Nsock but by the wrapper function probe_transmission_handler_wrapper().
* The reason for that is because C++ does not allow to use class methods as callback
* functions, so this is a small hack to make that happen. */
void FPNetworkControl::probe_transmission_handler(nsock_pool nsp, nsock_event nse, void *arg) {
assert(nsock_pool_get_udata(nsp) != NULL);
nsock_iod nsi_pcap = *((nsock_iod *)nsock_pool_get_udata(nsp));
enum nse_status status = nse_status(nse);
enum nse_type type = nse_type(nse);
FPProbe *myprobe = (FPProbe *)arg;
u8 *buf;
size_t len;
int result;
if (status == NSE_STATUS_SUCCESS) {
switch(type) {
/* Timer events mean that we need to send a packet. */
case NSE_TYPE_TIMER:
/* The first time a packet is sent, we schedule a pcap event. After that
* we don't have to worry since the response reception handler schedules
* a new capture event for each captured packet. */
if (!this->first_pcap_scheduled) {
this->pcap_ev_id = nsock_pcap_read_packet(nsp, nsi_pcap, response_reception_handler_wrapper, -1, NULL);
this->first_pcap_scheduled = true;
}
/* Send the packet*/
for (int decoy = 0; decoy < o.numdecoys; decoy++) {
result = myprobe->changeSourceAddress(&((struct sockaddr_in6 *)&o.decoys[decoy])->sin6_addr);
assert(result == OP_SUCCESS);
assert(myprobe->host != NULL);
buf = myprobe->getPacketBuffer(&len);
if (send_ip_packet(this->rawsd, myprobe->getEthernet(), myprobe->host->getTargetAddress(), buf, len) == -1) {
if (decoy == o.decoyturn) {
myprobe->setFailed();
this->cc_report_final_timeout();
myprobe->host->fail_one_probe();
gh_perror("Unable to send packet in %s", __func__);
}
}
if (decoy == o.decoyturn) {
myprobe->setTimeSent();
}
free(buf);
}
/* Reset the address to the original one if decoys were present and original Address wasn't last one */
if ( o.numdecoys != o.decoyturn+1 ) {
result = myprobe->changeSourceAddress(&((struct sockaddr_in6 *)&o.decoys[o.decoyturn])->sin6_addr);
assert(result == OP_SUCCESS);
}
break;
default:
fatal("Unexpected Nsock event in probe_transmission_handler()");
break;
} /* switch(type) */
} else if (status == NSE_STATUS_EOF) {
if (o.debugging)
log_write(LOG_PLAIN, "probe_transmission_handler(): EOF\n");
} else if (status == NSE_STATUS_ERROR || status == NSE_STATUS_PROXYERROR) {
if (o.debugging)
log_write(LOG_PLAIN, "probe_transmission_handler(): %s failed: %s\n", nse_type2str(type), strerror(socket_errno()));
} else if (status == NSE_STATUS_TIMEOUT) {
if (o.debugging)
log_write(LOG_PLAIN, "probe_transmission_handler(): %s timeout: %s\n", nse_type2str(type), strerror(socket_errno()));
} else if (status == NSE_STATUS_CANCELLED) {
if (o.debugging)
log_write(LOG_PLAIN, "probe_transmission_handler(): %s canceled: %s\n", nse_type2str(type), strerror(socket_errno()));
} else if (status == NSE_STATUS_KILL) {
if (o.debugging)
log_write(LOG_PLAIN, "probe_transmission_handler(): %s killed: %s\n", nse_type2str(type), strerror(socket_errno()));
} else {
if (o.debugging)
log_write(LOG_PLAIN, "probe_transmission_handler(): Unknown status code %d\n", status);
}
return;
}
/* This is the handler for packet capture. It is called by nsock whenever libpcap
* captures a packet from the network interface. This method basically captures
* the packet, extracts its source IP address and tries to find an FPHost that
* is targeting such address. If it does, it passes the packet to that FPHost
* via callback() so the FPHost can determine if the packet is actually the
* response to a FPProbe that it sent before. Note that this method is not
* called directly by Nsock but by the wrapper function
* response_reception_handler_wrapper(). See doc in probe_transmission_handler()
* for details. */
void FPNetworkControl::response_reception_handler(nsock_pool nsp, nsock_event nse, void *arg) {
nsock_iod nsi = nse_iod(nse);
enum nse_status status = nse_status(nse);
enum nse_type type = nse_type(nse);
const u8 *rcvd_pkt = NULL; /* Points to the captured packet */
size_t rcvd_pkt_len = 0; /* Length of the captured packet */
struct timeval pcaptime; /* Time the packet was captured */
struct sockaddr_storage sent_ss;
struct sockaddr_storage rcvd_ss;
struct sockaddr_in *rcvd_ss4 = (struct sockaddr_in *)&rcvd_ss;
struct sockaddr_in6 *rcvd_ss6 = (struct sockaddr_in6 *)&rcvd_ss;
memset(&rcvd_ss, 0, sizeof(struct sockaddr_storage));
IPv4Header ip4;
IPv6Header ip6;
int res = -1;
struct timeval tv;
gettimeofday(&tv, NULL);
if (status == NSE_STATUS_SUCCESS) {
switch(type) {
case NSE_TYPE_PCAP_READ:
/* Schedule a new pcap read operation */
this->pcap_ev_id = nsock_pcap_read_packet(nsp, nsi, response_reception_handler_wrapper, -1, NULL);
/* Get captured packet */
nse_readpcap(nse, NULL, NULL, &rcvd_pkt, &rcvd_pkt_len, NULL, &pcaptime);
/* Extract the packet's source address */
ip4.storeRecvData(rcvd_pkt, rcvd_pkt_len);
if (ip4.validate() != OP_FAILURE && ip4.getVersion() == 4) {
ip4.getSourceAddress(&(rcvd_ss4->sin_addr));
rcvd_ss4->sin_family = AF_INET;
} else {
ip6.storeRecvData(rcvd_pkt, rcvd_pkt_len);
if (ip6.validate() != OP_FAILURE && ip6.getVersion() == 6) {
ip6.getSourceAddress(&(rcvd_ss6->sin6_addr));
rcvd_ss6->sin6_family = AF_INET6;
} else {
/* If we get here it means that the received packet is not
* IPv4 or IPv6 so we just discard it returning. */
return;
}
}
/* Check if we have a caller that expects packets from this sender */
for (size_t i = 0; i < this->callers.size(); i++) {
/* Obtain the target address */
sent_ss = *this->callers[i]->getTargetAddress();
/* Check that the received packet is of the same address family */
if (sent_ss.ss_family != rcvd_ss.ss_family)
continue;
/* Check that the captured packet's source address matches the
* target address. If it matches, pass the received packet
* to the appropriate FPHost object through callback(). */
if (sockaddr_storage_equal(&rcvd_ss, &sent_ss)) {
if ((res = this->callers[i]->callback(rcvd_pkt, rcvd_pkt_len, &tv)) >= 0) {
/* If callback() returns >=0 it means that the packet we've just
* passed was successfully matched with a previous probe. Now
* update the count of received packets (so we can determine how
* many outstanding packets are out there). Note that we only do
* that if callback() returned >0 because 0 is a special case: a
* reply to a retransmitted timed probe that was already replied
* to in the past. We don't want to count replies to the same probe
* more than once, so that's why we only update when res > 0. */
if (res > 0)
this->cc_update_received();
/* When the callback returns more than 1 it means that the packet
* was sent more than once before being answered. This means that
* we experienced congestion (first transmission got dropped), so
* we update our CC parameters to deal with the congestion. */
if (res > 1) {
this->cc_report_drop();
}
}
return;
}
}
break;
default:
fatal("Unexpected Nsock event in response_reception_handler()");
break;
} /* switch(type) */
} else if (status == NSE_STATUS_EOF) {
if (o.debugging)
log_write(LOG_PLAIN, "response_reception_handler(): EOF\n");
} else if (status == NSE_STATUS_ERROR || status == NSE_STATUS_PROXYERROR) {
if (o.debugging)
log_write(LOG_PLAIN, "response_reception_handler(): %s failed: %s\n", nse_type2str(type), strerror(socket_errno()));
} else if (status == NSE_STATUS_TIMEOUT) {
if (o.debugging)
log_write(LOG_PLAIN, "response_reception_handler(): %s timeout: %s\n", nse_type2str(type), strerror(socket_errno()));
} else if (status == NSE_STATUS_CANCELLED) {
if (o.debugging)
log_write(LOG_PLAIN, "response_reception_handler(): %s canceled: %s\n", nse_type2str(type), strerror(socket_errno()));
} else if (status == NSE_STATUS_KILL) {
if (o.debugging)
log_write(LOG_PLAIN, "response_reception_handler(): %s killed: %s\n", nse_type2str(type), strerror(socket_errno()));
} else {
if (o.debugging)
log_write(LOG_PLAIN, "response_reception_handler(): Unknown status code %d\n", status);
}
return;
}
/******************************************************************************
* Implementation of class FPEngine. *
******************************************************************************/
FPEngine::FPEngine() {
this->osgroup_size = OSSCAN_GROUP_SIZE;
}
FPEngine::~FPEngine() {
}
/* Returns a suitable BPF filter for the OS detection. If less than 20 targets
* are passed, the filter contains an explicit list of target addresses. It
* looks similar to this:
*
* dst host fe80::250:56ff:fec0:1 and (src host fe80::20c:29ff:feb0:2316 or src host fe80::20c:29ff:fe9f:5bc2)
*
* When more than 20 targets are passed, a generic filter based on the source
* address is used. The returned filter looks something like:
*
* dst host fe80::250:56ff:fec0:1
*/
const char *FPEngine::bpf_filter(std::vector<Target *> &Targets) {
static char pcap_filter[2048];
/* 20 IPv6 addresses is max (46 byte addy + 14 (" or src host ")) * 20 == 1200 */
char dst_hosts[1220];
int filterlen = 0;
int len = 0;
unsigned int targetno;
memset(pcap_filter, 0, sizeof(pcap_filter));
/* If we have 20 or less targets, build a list of addresses so we can set
* an explicit BPF filter */
if (Targets.size() <= 20) {
for (targetno = 0; targetno < Targets.size(); targetno++) {
len = Snprintf(dst_hosts + filterlen,
sizeof(dst_hosts) - filterlen,
"%ssrc host %s", (targetno == 0)? "" : " or ",
Targets[targetno]->targetipstr());
if (len < 0 || len + filterlen >= (int) sizeof(dst_hosts))
fatal("ran out of space in dst_hosts");
filterlen += len;
}
if (len < 0 || len + filterlen >= (int) sizeof(dst_hosts))
fatal("ran out of space in dst_hosts");
len = Snprintf(pcap_filter, sizeof(pcap_filter), "dst host %s and (%s)",
Targets[0]->sourceipstr(), dst_hosts);
} else {
len = Snprintf(pcap_filter, sizeof(pcap_filter), "dst host %s", Targets[0]->sourceipstr());
}
if (len < 0 || len >= (int) sizeof(pcap_filter))
fatal("ran out of space in pcap filter");
return pcap_filter;
}
/******************************************************************************
* Implementation of class FPEngine6. *
******************************************************************************/
FPEngine6::FPEngine6() {
}
FPEngine6::~FPEngine6() {
}
/* Not all operating systems allow setting the flow label in outgoing packets;
notably all Unixes other than Linux when using raw sockets. This function
finds out whether the flow labels we set are likely really being sent.
Otherwise, the operating system is probably filling in 0. Compare to the
logic in send_ipv6_packet_eth_or_sd. */
static bool can_set_flow_label(const struct eth_nfo *eth) {
if (eth != NULL)
return true;
#if HAVE_IPV6_IPPROTO_RAW
return true;
#else
return false;
#endif
}
void FPHost6::fill_FPR(FingerPrintResultsIPv6 *FPR) {
unsigned int i;
FPR->begin_time = this->begin_time;
for (i = 0; i < sizeof(this->fp_responses) / sizeof(this->fp_responses[0]); i++) {
const FPResponse *resp;
resp = this->fp_responses[i];
if (resp != NULL) {
FPR->fp_responses[i] = new FPResponse(resp->probe_id, resp->buf, resp->len,
resp->senttime, resp->rcvdtime);
}
}
/* Were we actually able to set the flow label? */
FPR->flow_label = 0;
for (i = 0; i < sizeof(this->fp_probes) / sizeof(this->fp_probes[0]); i++) {
const FPProbe& probe = fp_probes[0];
if (probe.is_set()) {
if (can_set_flow_label(probe.getEthernet()))
FPR->flow_label = OSDETECT_FLOW_LABEL;
break;
}
}
/* Did we fail to send some probe? */
FPR->incomplete = this->incomplete_fp;
}
static IPv6Header *find_ipv6(const PacketElement *pe) {
while (pe != NULL && pe->protocol_id() != HEADER_TYPE_IPv6)
pe = pe->getNextElement();
return (IPv6Header *) pe;
}
static const TCPHeader *find_tcp(const PacketElement *pe) {
while (pe != NULL && pe->protocol_id() != HEADER_TYPE_TCP)
pe = pe->getNextElement();
return (TCPHeader *) pe;
}
static const ICMPv6Header *find_icmpv6(const PacketElement *pe) {
while (pe != NULL && pe->protocol_id() != HEADER_TYPE_ICMPv6)
pe = pe->getNextElement();
return (ICMPv6Header *) pe;
}
static double vectorize_plen(const PacketElement *pe) {
const IPv6Header *ipv6;
ipv6 = find_ipv6(pe);
if (ipv6 == NULL)
return -1;
else
return ipv6->getPayloadLength();
}
static double vectorize_tc(const PacketElement *pe) {
const IPv6Header *ipv6;
ipv6 = find_ipv6(pe);
if (ipv6 == NULL)
return -1;
else
return ipv6->getTrafficClass();
}
/* For reference, the [email protected] email thread which contains the explanations for the
* design decisions of this vectorization method:
* http://seclists.org/nmap-dev/2015/q1/218
*/
static int vectorize_hlim(const PacketElement *pe, int target_distance, enum dist_calc_method method) {
const IPv6Header *ipv6;
int hlim;
int er_lim;
ipv6 = find_ipv6(pe);
if (ipv6 == NULL)
return -1;
hlim = ipv6->getHopLimit();
if (method != DIST_METHOD_NONE) {
if (method == DIST_METHOD_TRACEROUTE || method == DIST_METHOD_ICMP) {
if (target_distance > 0)
hlim += target_distance - 1;
}
er_lim = 5;
} else
er_lim = 20;
if (32 - er_lim <= hlim && hlim <= 32+ 5 )
hlim = 32;
else if (64 - er_lim <= hlim && hlim <= 64+ 5 )
hlim = 64;
else if (128 - er_lim <= hlim && hlim <= 128+ 5 )
hlim = 128;
else if (255 - er_lim <= hlim && hlim <= 255+ 5 )
hlim = 255;
else
hlim = -1;
return hlim;
}
static double vectorize_isr(std::map<std::string, FPPacket>& resps) {
const char * const SEQ_PROBE_NAMES[] = {"S1", "S2", "S3", "S4", "S5", "S6"};
u32 seqs[NELEMS(SEQ_PROBE_NAMES)];
struct timeval times[NELEMS(SEQ_PROBE_NAMES)];
unsigned int i, j;
double sum, t;
j = 0;
for (i = 0; i < NELEMS(SEQ_PROBE_NAMES); i++) {
const char *probe_name;
const FPPacket *fp;
const TCPHeader *tcp;
std::map<std::string, FPPacket>::iterator it;
probe_name = SEQ_PROBE_NAMES[i];
it = resps.find(probe_name);
if (it == resps.end())
continue;
fp = &it->second;
tcp = find_tcp(fp->getPacket());
if (tcp == NULL)
continue;
seqs[j] = tcp->getSeq();
times[j] = fp->getTime();
j++;
}
if (j < 2)
return -1;
sum = 0.0;
for (i = 0; i < j - 1; i++)
sum += seqs[i + 1] - seqs[i];
t = TIMEVAL_FSEC_SUBTRACT(times[j - 1], times[0]);
return sum / t;
}
static int vectorize_icmpv6_type(const PacketElement *pe) {
const ICMPv6Header *icmpv6;
icmpv6 = find_icmpv6(pe);
if (icmpv6 == NULL)
return -1;
return icmpv6->getType();
}
static int vectorize_icmpv6_code(const PacketElement *pe) {
const ICMPv6Header *icmpv6;
icmpv6 = find_icmpv6(pe);
if (icmpv6 == NULL)
return -1;
return icmpv6->getCode();
}
static struct feature_node *vectorize(const FingerPrintResultsIPv6 *FPR) {
const char * const IPV6_PROBE_NAMES[] = {"S1", "S2", "S3", "S4", "S5", "S6", "IE1", "IE2", "NS", "U1", "TECN", "T2", "T3", "T4", "T5", "T6", "T7"};
const char * const TCP_PROBE_NAMES[] = {"S1", "S2", "S3", "S4", "S5", "S6", "TECN", "T2", "T3", "T4", "T5", "T6", "T7"};
const char * const ICMPV6_PROBE_NAMES[] = {"IE1", "IE2", "NS"};
unsigned int nr_feature, i, idx;
struct feature_node *features;
std::map<std::string, FPPacket> resps;
for (i = 0; i < NUM_FP_PROBES_IPv6; i++) {
PacketElement *pe;
if (FPR->fp_responses[i] == NULL)
continue;
pe = PacketParser::split(FPR->fp_responses[i]->buf, FPR->fp_responses[i]->len);
assert(pe != NULL);
resps[FPR->fp_responses[i]->probe_id].setPacket(pe);
resps[FPR->fp_responses[i]->probe_id].setTime(&FPR->fp_responses[i]->senttime);
}
nr_feature = get_nr_feature(&FPModel);
features = new feature_node[nr_feature + 1];
for (i = 0; i < nr_feature; i++) {
features[i].index = i + 1;
features[i].value = -1;
}
features[i].index = -1;
idx = 0;
for (i = 0; i < NELEMS(IPV6_PROBE_NAMES); i++) {
const char *probe_name;
probe_name = IPV6_PROBE_NAMES[i];
features[idx++].value = vectorize_plen(resps[probe_name].getPacket());
features[idx++].value = vectorize_tc(resps[probe_name].getPacket());
features[idx++].value = vectorize_hlim(resps[probe_name].getPacket(), FPR->distance, FPR->distance_calculation_method);
}
/* TCP features */
features[idx++].value = vectorize_isr(resps);
for (i = 0; i < NELEMS(TCP_PROBE_NAMES); i++) {
const char *probe_name;
const TCPHeader *tcp;
u16 flags;
u16 mask;
unsigned int j;
int mss;
int sackok;
int wscale;
probe_name = TCP_PROBE_NAMES[i];
mss = -1;
sackok = -1;
wscale = -1;
tcp = find_tcp(resps[probe_name].getPacket());
if (tcp == NULL) {
/* 49 TCP features. */
idx += 49;
continue;
}
features[idx++].value = tcp->getWindow();
flags = tcp->getFlags16();
for (mask = 0x001; mask <= 0x800; mask <<= 1)
features[idx++].value = (flags & mask) != 0;
for (j = 0; j < 16; j++) {
nping_tcp_opt_t opt;
opt = tcp->getOption(j);
if (opt.value == NULL)
break;
features[idx++].value = opt.type;
/* opt.len includes the two (type, len) bytes. */
if (opt.type == TCPOPT_MSS && opt.len == 4 && mss == -1)
mss = ntohs(*(u16 *) opt.value);
else if (opt.type == TCPOPT_SACKOK && opt.len == 2 && sackok == -1)
sackok = 1;
else if (opt.type == TCPOPT_WSCALE && opt.len == 3 && wscale == -1)
wscale = *(u8 *) opt.value;
}
for (; j < 16; j++)