-
Notifications
You must be signed in to change notification settings - Fork 30
/
Main.cpp
1735 lines (1421 loc) · 45.4 KB
/
Main.cpp
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
/*
Host Scanner
Copyright (C) 2016 RoliSoft <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Stdafx.h"
#include "Utils.h"
#include "Format.h"
#include "HostScanner.h"
#include "InternalScanner.h"
#include "NmapScanner.h"
#include "UdpScanner.h"
#include "PassiveScanner.h"
#include "ShodanScanner.h"
#include "CensysScanner.h"
#include "LooquerScanner.h"
#include "OperatingSystemIdentifier.h"
#include "BannerProcessor.h"
#include "VulnerabilityLookup.h"
#include "VendorLookupFactory.h"
#include <iostream>
#include <fstream>
#include <string>
#include <tuple>
#include <vector>
#include <set>
#include <mutex>
#include <iomanip>
#include <unordered_set>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/find.hpp>
#include <boost/core/ignore_unused.hpp>
#include <boost/regex.hpp>
#if HAVE_CURL
#include <curl/curl.h>
#endif
using namespace std;
using namespace boost;
namespace fs = boost::filesystem;
namespace po = boost::program_options;
/*!
* The logging severity threshold.
* Messages below this level will not be printed.
*/
int logging_level = MSG;
/*!
* Logs the specified message.
*
* Messages at warning or error level will be sent to the standard
* error, while messages below it will be sent to the standard
* output. Verbose and debug levels are only printed if the user
* requested such levels to be shown.
*
* \param level The message's severity level.
* \param msg The message's content.
* \param format Value indicating whether to enable formatting.
*/
void log(int level, const string& msg, bool format)
{
if (level < logging_level)
{
return;
}
static mutex mtx;
lock_guard<mutex> guard(mtx);
ostream* os;
switch (level)
{
case ERR:
os = &cerr;
*os << Format::Red << "[" << Format::Bold << "!" << Format::Normal << Format::Red << "] " << Format::Default;
break;
case WRN:
os = &cerr;
*os << Format::Yellow << "[" << Format::Bold << "!" << Format::Normal << Format::Yellow << "] " << Format::Default;
break;
case VRB:
os = &cout;
*os << Format::Green << "[" << Format::Bold << "-" << Format::Normal << Format::Green << "] " << Format::Default;
break;
case DBG:
os = &cout;
*os << Format::Green << "[" << Format::Bold << "." << Format::Normal << Format::Green << "] " << Format::Default;
break;
case INT:
os = &cout;
*os << Format::Green << "[ ] " << Format::Default;
break;
default:
os = &cout;
*os << Format::Green << "[" << Format::Bold << "*" << Format::Normal << Format::Green << "] " << Format::Default;
break;
}
if (!format)
{
*os << msg << endl;
return;
}
istringstream iss(msg);
string token;
auto seen = false;
for (auto i = 0; getline(iss, token, '$'); i++)
{
if (i == 0)
{
*os << token;
continue;
}
if (seen)
{
seen = false;
*os << Format::Normal << Format::Default;
}
else if (token.empty())
{
continue;
}
else
{
seen = true;
auto op = token.at(0);
switch (op)
{
case '!':
token = token.substr(1);
*os << Format::Bold;
break;
case 'r':
token = token.substr(1);
*os << Format::Red;
break;
case 'R':
token = token.substr(1);
*os << Format::Bold << Format::Red;
break;
case 'y':
token = token.substr(1);
*os << Format::Yellow;
break;
case 'Y':
token = token.substr(1);
*os << Format::Bold << Format::Yellow;
break;
case 'm':
token = token.substr(1);
*os << Format::Magenta;
break;
case 'M':
token = token.substr(1);
*os << Format::Bold << Format::Magenta;
break;
case 'g':
token = token.substr(1);
*os << Format::Green;
break;
case 'G':
token = token.substr(1);
*os << Format::Bold << Format::Green;
break;
case 'b':
token = token.substr(1);
*os << Format::Blue;
break;
case 'B':
token = token.substr(1);
*os << Format::Bold << Format::Blue;
break;
default:
seen = false;
break;
}
}
*os << token;
}
if (seen)
{
*os << Format::Normal << Format::Default;
}
*os << endl;
}
/*!
* Parses the specified argument value to the port arguments, and
* generates the corresponding list of ports to be used for scanning.
*
* \param portstr Specified value of the argument.
* \param isudp Value indicating whether the processed argument string
* was specified for the UDP port list.
* \param retval Reference to the return value. On error, the function will
* returns an empty set and sets this argument to `EXIT_FAILURE`.
*
* \return List of port numbers.
*/
set<unsigned short>* parse_ports(const string& portstr, int& retval, bool isudp = false)
{
auto ports = new set<unsigned short>();
if (portstr == "-")
{
log(VRB, "Scanning all 65535 " + string(isudp ? "UDP" : "TCP") + " ports.");
for (auto i = 0; i < 65535; i++)
{
ports->emplace(i);
}
return ports;
}
if (portstr.length() == 0 || iequals(portstr, "t") || iequals(portstr, "top"))
{
if (isudp)
{
auto entries = UdpScanner::GetPayloads();
for (auto entry : entries)
{
if (entry.first == 0)
{
// skip generic payload
continue;
}
ports->emplace(entry.first);
}
if (ports->size() == 0)
{
log(ERR, "Unable to generate port list based on known payloads.");
retval = EXIT_FAILURE;
}
else
{
log(VRB, "Scanning " + to_string(ports->size()) + " UDP ports with known payloads.");
}
}
else
{
log(VRB, "Scanning the top 100 TCP ports.");
ports->insert({
7 , 9 , 13 , 21 , 22, 23, 25, 26, 37, 53, 79, 80, 81, 88, 106, 110, 111, 113, 119, 135, 139, 143, 144, 179,
199 , 389, 427, 443, 445, 465, 513, 515, 543, 544, 548, 554, 587, 631, 646, 873, 990, 993, 995, 1025, 1026,
1027, 1028, 1029, 1110, 1433, 1720, 1723, 1755, 1900, 2000, 2001, 2049, 2121, 2717, 3000, 3128, 3306, 3389,
3986, 4899, 5000, 5009, 5051, 5060, 5101, 5190, 5357, 5432, 5631, 5666, 5800, 5900, 6000, 6001, 6646, 7070,
8000, 8008, 8009, 8080, 8081, 8443, 8888, 9100, 9999, 10000, 32768, 49152, 49153, 49154, 49155, 49156, 49157
});
}
return ports;
}
// read all listed ports
vector<string> portarr;
try
{
split(portarr, portstr, is_any_of(","), token_compress_on);
}
catch (boost::exception&)
{
log(ERR, "Unable to parse port list '" + portstr + "'.");
retval = EXIT_FAILURE;
return ports;
}
for (auto& s_port : portarr)
{
// check if range
if (s_port.find("-") != string::npos)
{
// check if unbounded
int a, b;
if (starts_with(s_port, "-"))
{
// from 1 to n
a = 1;
b = stoi(s_port.substr(1));
}
else if (ends_with(s_port, "-"))
{
// from n to 65535
a = stoi(s_port);
b = 65535;
}
else
{
// range specified
vector<string> s_range;
split(s_range, s_port, is_any_of("-"), token_compress_on);
if (s_range.size() < 2)
{
log(ERR, "Unable to parse '" + s_port + "' in port list.");
retval = EXIT_FAILURE;
return ports;
}
a = stoi(s_range[0]);
b = stoi(s_range[1]);
if (a > b)
{
swap(a, b);
}
}
if (a < 1 || a > 65535 || b < 1 || b > 65535)
{
log(ERR, "Port range '" + s_port + "' is invalid.");
retval = EXIT_FAILURE;
return ports;
}
for (int i = a; i <= b; i++)
{
ports->emplace(i);
}
}
else
{
int port = stoi(s_port);
if (port < 0 || port > 65535)
{
log(ERR, "Port '" + s_port + "' out of range.");
retval = EXIT_FAILURE;
return ports;
}
ports->emplace(port);
}
}
if (ports->size() == 0)
{
log(ERR, "Failed to parse ports from '" + portstr + "'.");
retval = EXIT_FAILURE;
}
else
{
log(VRB, "Scanning " + to_string(ports->size()) + " " + (isudp ? "UDP" : "TCP") + " ports.");
}
return ports;
}
/*!
* Parses the specified argument values to the host argument, and
* generates the corresponding list of hosts to be used for scanning.
*
* \param hoststrs Specified values of the argument.
* \param scanner Scanner object instance.
* \param retval Reference to the return value. On error, the function will
* returns an empty array and sets this argument to `EXIT_FAILURE`.
*
* \return List of hosts.
*/
Hosts* parse_hosts(const vector<string>& hoststrs, HostScanner* scanner, int& retval)
{
#if _MSC_VER
// even though it's used later on, MSVC is issuing an 'unreferenced formal parameter' warning for `scanner`
ignore_unused(scanner);
#endif
unordered_set<string> hostarr;
// merge targets specified via positional parameters and targets separated by comma
for (auto& hoststr : hoststrs)
{
if (hoststr.find(",") != string::npos)
{
vector<string> host;
split(host, hoststr, is_any_of(","));
hostarr.insert(host.begin(), host.end());
}
else
{
hostarr.emplace(hoststr);
}
}
auto hosts = new Hosts();
// iterate final target list
for (auto& s_target : hostarr)
{
if (s_target.find("/") != string::npos)
{
// CIDR
vector<string> s_cidr;
split(s_cidr, s_target, is_any_of("/"), token_compress_on);
if (s_cidr.size() != 2)
{
log(ERR, "CIDR notation '" + s_target + "' is invalid.");
retval = EXIT_FAILURE;
return hosts;
}
string addr = s_cidr[0];
int cidr = stoi(s_cidr[1]);
if (cidr < 0 || cidr > 32)
{
log(ERR, "CIDR notation '" + s_target + "' is out of range.");
retval = EXIT_FAILURE;
return hosts;
}
if (addr.find_first_not_of(".0123456789") != string::npos)
{
log(ERR, "CIDR notation is only available for IPv4 addresses, not '" + addr + "'.");
retval = EXIT_FAILURE;
return hosts;
}
log(VRB, "Scanning hosts in " + addr + "/" + to_string(cidr) + ".");
scanner->GenerateCidr(addr.c_str(), cidr, hosts);
}
else if (s_target.find("-") != string::npos)
{
// range
vector<string> s_range;
split(s_range, s_target, is_any_of("-"), token_compress_on);
if (s_range.size() != 2)
{
log(ERR, "Range notation '" + s_target + "' is invalid.");
retval = EXIT_FAILURE;
return hosts;
}
if (s_range[1].find("-") != string::npos)
{
log(ERR, "Only last octet can be a range in '" + s_target + "'.");
retval = EXIT_FAILURE;
return hosts;
}
auto lastsep = s_range[0].find_last_of(".");
if (lastsep == string::npos)
{
log(ERR, "Failed to find last octet in '" + s_target + "'.");
retval = EXIT_FAILURE;
return hosts;
}
if (s_range[0].find_first_not_of(".0123456789") != string::npos)
{
log(ERR, "Range notation is only available for IPv4 addresses, not '" + s_range[0] + "'.");
retval = EXIT_FAILURE;
return hosts;
}
string from = s_range[0];
string to = s_range[0].substr(0, lastsep) + "." + s_range[1];
if (s_range[1].find_first_not_of("0123456789") != string::npos)
{
log(ERR, "Range notation is only available for IPv4 addresses, not '" + to + "'.");
retval = EXIT_FAILURE;
return hosts;
}
log(VRB, "Scanning hosts " + from + " to " + to + ".");
scanner->GenerateRange(from.c_str(), to.c_str(), hosts);
}
else
{
// IP or host
if (s_target.find_first_not_of(".0123456789") == string::npos
|| s_target.find_first_not_of(":0123456789abcdef") == string::npos)
{
// IPv4 or v6
log(VRB, "Scanning host " + s_target + ".");
hosts->push_back(new Host(s_target.c_str()));
}
else
{
// hostname
struct addrinfo hint, *info = nullptr;
memset(&hint, 0, sizeof(struct addrinfo));
hint.ai_family = AF_UNSPEC; // allow both v4 and v6
getaddrinfo(s_target.c_str(), 0, &hint, &info);
if (info == nullptr)
{
log(ERR, "Failed to DNS resolve hostname '" + s_target + "'.");
retval = EXIT_FAILURE;
return hosts;
}
unordered_set<string> dnshosts;
auto infoit = info;
while (infoit != nullptr)
{
char infostr[INET6_ADDRSTRLEN];
switch (infoit->ai_family)
{
case AF_INET:
inet_ntop(AF_INET, &reinterpret_cast<struct sockaddr_in*>(infoit->ai_addr)->sin_addr, infostr, INET_ADDRSTRLEN);
break;
case AF_INET6:
inet_ntop(AF_INET6, &reinterpret_cast<struct sockaddr_in6*>(infoit->ai_addr)->sin6_addr, infostr, INET6_ADDRSTRLEN);
break;
default:
log(ERR, "Failed to DNS resolve hostname '" + s_target + "': unsupported address family of " + to_string(infoit->ai_family) + ".");
retval = EXIT_FAILURE;
freeaddrinfo(info);
return hosts;
}
dnshosts.emplace(string(infostr));
infoit = infoit->ai_next;
}
freeaddrinfo(info);
for (auto& dnshost : dnshosts)
{
log(VRB, "Scanning host " + s_target + " at " + dnshost + ".");
hosts->push_back(new Host(dnshost));
}
}
}
}
if (hosts->size() == 0)
{
log(ERR, "No targets to scan.");
retval = EXIT_FAILURE;
}
return hosts;
}
/*!
* Processes the arguments the user passed to the application when launching
* it, spawns the requested `HostScanner` type of instance, and builds the
* list of `Host` objects to be scanned based on the specified criteria.
*
* \param vm The variable map containing the process arguments.
*
* \return Return value to be used as an exit code.
*/
int scan(const po::variables_map& vm)
{
int retval = EXIT_SUCCESS;
string scannerstr, portstr, udportstr;
vector<string> hoststrs;
HostScanner *scanner = nullptr;
Hosts *hosts = nullptr;
set<unsigned short> *ports = nullptr, *udports = nullptr;
bool resolve, estimate;
unordered_map<string, vector<CveEntry>> cpevulns;
unordered_map<Host*, unordered_set<string>> hostpkgs;
unordered_map<Service*, unordered_set<string>> servpkgs;
unordered_map<Host*, pair<long, long>> hostdate;
unordered_map<char, int> stats;
string latexOut, latexContent, latexTitle, latexAbstract;
Services services;
// get scanner
if (vm.count("scanner") != 0)
{
try
{
scannerstr = vm["scanner"].as<string>();
trim(scannerstr);
to_lower(scannerstr);
}
catch (boost::exception&)
{
log(ERR, "Unable to parse scanner argument.");
retval = EXIT_FAILURE;
goto cleanup;
}
}
else
{
scannerstr = vm.count("passive") != 0 ? "shosys" : "internal";
}
if (scannerstr == "internal" || scannerstr.length() == 0)
{
scanner = new InternalScanner();
if (vm.count("delay") != 0)
{
auto delay = min(max(0, vm["delay"].as<int>()), 6);
auto delayms = 100ul;
switch (delay)
{
case 0: delayms = 300000; break; // 5m
case 1: delayms = 15000; break; // 15s
case 2: delayms = 400; break;
case 3: delayms = 100; break;
case 4: delayms = 10; break;
case 5: delayms = 5; break;
case 6: delayms = 0; break;
}
reinterpret_cast<InternalScanner*>(scanner)->SetOption(OPT_DELAY, &delayms);
}
}
else if (scannerstr == "nmap")
{
scanner = new NmapScanner();
if (vm.count("delay") != 0)
{
auto delay = min(max(0, vm["delay"].as<int>()), 5);
reinterpret_cast<NmapScanner*>(scanner)->SetOption(OPT_DELAY, &delay);
}
}
#if HAVE_CURL
else if (scannerstr == "shodan")
{
string key;
if (vm.count("shodan-key") != 0)
{
key = vm["shodan-key"].as<string>();
}
if (key.length() < 2)
{
log(ERR, "Shodan requires an API key via --shodan-key from https://account.shodan.io/");
retval = EXIT_FAILURE;
goto cleanup;
}
scanner = new ShodanScanner(key);
if (vm.count("shodan-uri") != 0)
{
reinterpret_cast<ShodanScanner*>(scanner)->SetEndpoint(vm["shodan-uri"].as<string>());
}
}
else if (scannerstr == "censys")
{
string auth;
if (vm.count("censys-key") != 0)
{
auth = vm["censys-key"].as<string>();
}
if (auth.length() < 2 || auth.find(":") == string::npos)
{
log(ERR, "Censys requires token in `uid:secret` format via --censys-key from https://censys.io/account");
retval = EXIT_FAILURE;
goto cleanup;
}
scanner = new CensysScanner(auth);
if (vm.count("censys-uri") != 0)
{
reinterpret_cast<CensysScanner*>(scanner)->SetEndpoint(vm["censys-uri"].as<string>());
}
}
else if (scannerstr == "looquer")
{
string key;
if (vm.count("looquer-key") != 0)
{
key = vm["looquer-key"].as<string>();
}
if (key.length() < 2)
{
log(ERR, "Mr Looquer requires an API key via --looquer-key from https://mrlooquer.com/manage");
retval = EXIT_FAILURE;
goto cleanup;
}
scanner = new LooquerScanner(key);
if (vm.count("looquer-uri") != 0)
{
reinterpret_cast<LooquerScanner*>(scanner)->SetEndpoint(vm["looquer-uri"].as<string>());
}
}
else if (scannerstr == "shosys" || scannerstr == "cendan")
{
string shodan_key, censys_auth, looquer_key;
if (vm.count("shodan-key") != 0)
{
shodan_key = vm["shodan-key"].as<string>();
}
if (shodan_key.length() < 2)
{
log(WRN, "Shodan requires an API key via --shodan-key from https://account.shodan.io/");
shodan_key.clear();
}
if (vm.count("censys-key") != 0)
{
censys_auth = vm["censys-key"].as<string>();
}
if (censys_auth.length() < 2 || censys_auth.find(":") == string::npos)
{
log(WRN, "Censys requires token in `uid:secret` format via --censys-key from https://censys.io/account");
censys_auth.clear();
}
if (vm.count("looquer-key") != 0)
{
looquer_key = vm["looquer-key"].as<string>();
}
if (looquer_key.length() < 2)
{
log(WRN, "Mr Looquer requires an API key via --looquer-key from https://mrlooquer.com/manage");
looquer_key.clear();
}
if (shodan_key.length() < 2 && censys_auth.length() < 2 && looquer_key.length() < 2)
{
log(ERR, "You need to specify at least one API key for this scanner.");
retval = EXIT_FAILURE;
goto cleanup;
}
scanner = new PassiveScanner(shodan_key, censys_auth, looquer_key);
if (vm.count("shodan-uri") != 0)
{
reinterpret_cast<PassiveScanner*>(scanner)->SetShodanEndpoint(vm["shodan-uri"].as<string>());
}
if (vm.count("censys-uri") != 0)
{
reinterpret_cast<PassiveScanner*>(scanner)->SetCensysEndpoint(vm["censys-uri"].as<string>());
}
if (vm.count("looquer-uri") != 0)
{
reinterpret_cast<PassiveScanner*>(scanner)->SetLooquerEndpoint(vm["looquer-uri"].as<string>());
}
}
#else
else if (scannerstr == "shodan" || scannerstr == "censys" || scannerstr == "looquer" || scannerstr == "shosys" || scannerstr == "cendan")
{
log(ERR, "Scanner '" + scannerstr + "' is not available as this version of the binary was compiled without libcurl.");
retval = EXIT_FAILURE;
goto cleanup;
}
#endif
else
{
log(ERR, "Scanner '" + scannerstr + "' is not supported.");
retval = EXIT_FAILURE;
goto cleanup;
}
// check if input file was provided
if (vm.count("input-file") != 0)
{
if (scannerstr != "nmap")
{
log(ERR, "Only the nmap scanner supports input files at this time.");
retval = EXIT_FAILURE;
goto cleanup;
}
auto fname = vm["input-file"].as<string>();
log(VRB, "Processing file '" + fs::path(fname).filename().string() + "'...");
ifstream fs(fname);
if (!fs.good())
{
log(ERR, "Failed to open the specified input file for reading.");
retval = EXIT_FAILURE;
goto cleanup;
}
stringstream buf;
buf << fs.rdbuf();
hosts = reinterpret_cast<NmapScanner*>(scanner)->Process(buf.str());
goto postScan;
}
// check passive
if (vm.count("passive") != 0 && !scanner->IsPassive())
{
log(ERR, "Scanner '" + scannerstr + "' is not passive.");
retval = EXIT_FAILURE;
goto cleanup;
}
// read ports
if (vm.count("port") != 0)
{
portstr = trim_copy(vm["port"].as<string>());
}
if (vm.count("udp-port") != 0)
{
udportstr = trim_copy(vm["udp-port"].as<string>());
}
if (portstr.length() != 0 || (portstr.length() == 0 && udportstr.length() == 0))
{
ports = parse_ports(portstr, retval, false);
if (retval == EXIT_FAILURE)
{
goto cleanup;
}
}
if (udportstr.length() != 0)
{
udports = parse_ports(udportstr, retval, true);
if (retval == EXIT_FAILURE)
{
goto cleanup;
}
}
// read targets
if (vm.count("target") != 0)
{
hoststrs = vm["target"].as<vector<string>>();
}
hosts = parse_hosts(hoststrs, scanner, retval);
if (retval == EXIT_FAILURE)
{
goto cleanup;
}
// create the services for the host objects
for (auto host : *hosts)
{
if (ports != nullptr)
{
host->AddServices(*ports, IPPROTO_TCP);
}
if (udports != nullptr)
{
host->AddServices(*udports, IPPROTO_UDP);
}
}
// start scan
log("Initiating scan against " + pluralize(hosts->size(), "host") + "...");
scanner->Scan(hosts);
postScan:
scanner->DumpResults(hosts);
if (vm.count("output-latex") != 0)
{
latexOut = vm["output-latex"].as<string>();
latexTitle = "Scan report for ";
for (auto hoststr : hoststrs)
{
latexTitle += hoststr + ", ";
}
latexTitle = latexTitle.substr(0, latexTitle.size() - 2);
}
// start OS detection
log("Initiating identification of " + pluralize(hosts->size(), "operating system") + "...");
for (auto host : *hosts)
{
auto res = OperatingSystemIdentifier::AutoProcess(host);
if (res)
{
log(MSG, "$!" + host->address + "$ is running $!cpe:/" + host->cpe[0] + "$");
}
for (auto service : *host->services)
{
if (service->banner.length() != 0)
{
services.push_back(service);
}
}
}
// start CPE detection
resolve = vm.count("resolve") != 0;
estimate = vm.count("estimate") != 0;
log("Initiating identification of " + pluralize(services.size(), "service banner") + "...");
for (auto service : services)
{
auto cpes = BannerProcessor::AutoProcess(service->banner, true);