|
5 | 5 | #include <regex> |
6 | 6 | #include <string> |
7 | 7 |
|
| 8 | +#include "../include/Common.h" |
8 | 9 | #include "../include/Exporter.h" |
9 | 10 | #include "../include/Printer.h" |
10 | 11 | #include "../include/Scanner.h" |
11 | 12 |
|
12 | | -#define START_PORT 1 |
13 | | -#define END_PORT 65535 |
14 | | -#define RED "\033[31m" |
15 | | -#define BLUE "\033[36m" |
16 | | -#define PINK "\033[33m" |
17 | | -#define NC "\033[0m" |
18 | | - |
19 | 13 | using namespace std; |
20 | 14 |
|
21 | 15 | #define THREAD_NUM 200 |
22 | 16 |
|
23 | | -void print_usage() |
24 | | -{ |
25 | | - std::cout << PINK << "Usage: xpscan <IP> [options]\n" |
26 | | - << NC |
27 | | - << BLUE << "Options:\n" |
28 | | - << " --help Print this message\n" |
29 | | - << " --json Export results to [IP].json\n" |
30 | | - << " --txt Export results to [IP].txt\n" |
31 | | - << " --full Scan all 65535 ports\n" |
32 | | - << NC; |
| 17 | +void print_usage() { |
| 18 | + std::cout << PINK << "Usage: xpscan <IP> [options]\n" |
| 19 | + << NC << BLUE << "Options:\n" |
| 20 | + << " --help Print this message\n" |
| 21 | + << " --json Export results to [IP].json\n" |
| 22 | + << " --txt Export results to [IP].txt\n" |
| 23 | + << " --full Scan all 65535 ports\n" |
| 24 | + << NC; |
33 | 25 | } |
34 | 26 |
|
35 | | -int main(int argc, char* argv[]) |
36 | | -{ |
37 | | - // check if at least one arg is passed |
38 | | - if (argc < 2 || std::strcmp(argv[1], "--help") == 0) |
39 | | - { |
40 | | - print_usage(); |
41 | | - return 1; |
42 | | - } |
43 | | - |
44 | | - // Validate the ip |
45 | | - std::regex ip_regex(R"(^(\d{1,3}\.){3}\d{1,3}$)"); |
46 | | - if (!std::regex_match(argv[1], ip_regex)) |
47 | | - { |
48 | | - std::cerr << RED << "[x] Invalid IP address format." << NC << endl; |
49 | | - return 1; |
50 | | - } |
51 | | - |
52 | | - // Instatiate user input struct |
53 | | - struct UserInput input{}; |
54 | | - |
55 | | - // Parse args |
56 | | - std::vector<std::string> args(argv + 2, argv + argc); |
57 | | - |
58 | | - // Set IP attribute to the second arg passed |
59 | | - input.ip = argv[1]; |
60 | | - |
61 | | - // Parse Flags |
62 | | - bool exportJson = (find(args.begin(), args.end(), "--json") != args.end()); |
63 | | - bool exportTxt = (find(args.begin(), args.end(), "--txt") != args.end()); |
64 | | - bool fullScan = (find(args.begin(), args.end(), "--full") != args.end()); |
65 | | - |
66 | | - Printer::printHeader(); |
67 | | - |
68 | | - // Check for fullscan flag |
69 | | - if (fullScan) |
70 | | - { |
71 | | - input.start_port = START_PORT; |
72 | | - input.end_port = END_PORT; |
73 | | - } |
74 | | - else |
75 | | - { |
76 | | - std::cout << BLUE << "Enter start port:" << NC; |
77 | | - std::cin >> input.start_port; |
78 | | - std::cout << BLUE << "Enter end port:" << NC; |
79 | | - std::cin >> input.end_port; |
80 | | - } |
81 | | - |
82 | | - // Instatiate the scanner class with params |
83 | | - Scanner scanner(input.ip, THREAD_NUM); |
84 | | - |
85 | | - // Record start time |
86 | | - auto start = std::chrono::steady_clock::now(); |
87 | | - |
88 | | - // Proceed with the scanning logic |
89 | | - scanner.scanRange(input.start_port, input.end_port); |
90 | | - |
91 | | - // Record end time |
92 | | - auto end = std::chrono::steady_clock::now(); |
93 | | - |
94 | | - // Calculate elapsed time |
95 | | - std::chrono::duration<double> diff = end - start; |
96 | | - std::cout << "\n" |
97 | | - << BLUE << "[!] Scan finished in " << diff.count() << "s" << NC << std::endl; |
98 | | - |
99 | | - // Check for export options and proceed with it |
100 | | - if (exportJson) |
101 | | - Exporter::saveToJson(input.ip, scanner.getResults()); |
102 | | - if (exportTxt) |
103 | | - Exporter::saveToText(input.ip, scanner.getResults()); |
104 | | - |
105 | | - if (!exportJson && !exportTxt) |
106 | | - { |
107 | | - std::cout << PINK << "[!] No export flags set. Use --json or --txt to save results." << NC << std::endl; |
108 | | - } |
109 | | - |
110 | | - return 0; |
| 27 | +int main(int argc, char *argv[]) { |
| 28 | + // check if at least one arg is passed |
| 29 | + if (argc < 2 || std::strcmp(argv[1], "--help") == 0) { |
| 30 | + print_usage(); |
| 31 | + return 1; |
| 32 | + } |
| 33 | + |
| 34 | + // Validate the ip |
| 35 | + std::regex ip_regex(R"(^(\d{1,3}\.){3}\d{1,3}$)"); |
| 36 | + if (!std::regex_match(argv[1], ip_regex)) { |
| 37 | + std::cerr << RED << "[x] Invalid IP address format." << NC << endl; |
| 38 | + return 1; |
| 39 | + } |
| 40 | + |
| 41 | + // Instatiate user input struct |
| 42 | + struct UserInput input{}; |
| 43 | + |
| 44 | + // Parse args |
| 45 | + std::vector<std::string> args(argv + 2, argv + argc); |
| 46 | + |
| 47 | + // Set IP attribute to the second arg passed |
| 48 | + input.ip = argv[1]; |
| 49 | + |
| 50 | + // Parse Flags |
| 51 | + bool exportJson = (find(args.begin(), args.end(), "--json") != args.end()); |
| 52 | + bool exportTxt = (find(args.begin(), args.end(), "--txt") != args.end()); |
| 53 | + bool fullScan = (find(args.begin(), args.end(), "--full") != args.end()); |
| 54 | + |
| 55 | + Printer::printHeader(); |
| 56 | + |
| 57 | + // Check for fullscan flag |
| 58 | + if (fullScan) { |
| 59 | + input.start_port = START_PORT; |
| 60 | + input.end_port = END_PORT; |
| 61 | + } else { |
| 62 | + std::cout << BLUE << "Enter start port:" << NC; |
| 63 | + std::cin >> input.start_port; |
| 64 | + std::cout << BLUE << "Enter end port:" << NC; |
| 65 | + std::cin >> input.end_port; |
| 66 | + } |
| 67 | + |
| 68 | + // Instatiate the scanner class with params |
| 69 | + Scanner scanner(input.ip, THREAD_NUM); |
| 70 | + |
| 71 | + // Record start time |
| 72 | + auto start = std::chrono::steady_clock::now(); |
| 73 | + |
| 74 | + // Proceed with the scanning logic |
| 75 | + scanner.scanRange(input.start_port, input.end_port); |
| 76 | + |
| 77 | + // Record end time |
| 78 | + auto end = std::chrono::steady_clock::now(); |
| 79 | + |
| 80 | + // Calculate elapsed time |
| 81 | + std::chrono::duration<double> diff = end - start; |
| 82 | + std::cout << "\n" |
| 83 | + << BLUE << "[!] Scan finished in " << diff.count() << "s" << NC |
| 84 | + << std::endl; |
| 85 | + |
| 86 | + Exporter exporter; // Uses default config path because i extracted the config file path as a parameter |
| 87 | + if (exportJson) exporter.saveToJson(input.ip, scanner.getResults()); |
| 88 | + if (exportTxt) exporter.saveToText(input.ip, scanner.getResults()); |
| 89 | + |
| 90 | + if (!exportJson && !exportTxt) { |
| 91 | + std::cout << PINK |
| 92 | + << "[!] No export flags set. Use --json or --txt to save results." |
| 93 | + << NC << std::endl; |
| 94 | + } |
| 95 | + |
| 96 | + return 0; |
111 | 97 | } |
0 commit comments