feat(scanner): add subnet ping scan and TCP port scans for alive hosts
Introduce subnet scanning using CIDR prefix notation and perform TCP port scans on each discovered alive host. The change replaces the full port scan with a two-step process: first pinging the subnet to find active devices, then scanning their open TCP ports.
This commit is contained in:
+41
-18
@@ -1,20 +1,8 @@
|
||||
#include "cli_base.hpp"
|
||||
#include "local_ping_scanner.hpp"
|
||||
#include "logger.hpp"
|
||||
#include "port_scanner.hpp"
|
||||
#include "portscan_cli.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
static std::vector<uint16_t> allPorts()
|
||||
{
|
||||
std::vector<uint16_t> v;
|
||||
v.reserve(65535);
|
||||
for (uint32_t p = 1; p <= 65535; ++p) {
|
||||
v.push_back(static_cast<uint16_t>(p));
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
@@ -27,12 +15,47 @@ int main(int argc, char **argv)
|
||||
return 0;
|
||||
}
|
||||
|
||||
const std::string ip = cli.getIp();
|
||||
auto ports = allPorts();
|
||||
const std::string baseIp = cli.getBaseIp();
|
||||
|
||||
const int prefix = cli.getPrefix();
|
||||
if (prefix < 0 || prefix > 32) {
|
||||
Logger::fatal("Prefix must be in range 0..32\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
LocalPingScanner scanner(baseIp, prefix);
|
||||
Logger::info(std::format("Scanning subnet: {}/{}", baseIp, prefix));
|
||||
|
||||
Logger::info("Starting full port scan (1..65535)...");
|
||||
PortScanner scanner(ip, ports);
|
||||
scanner.run();
|
||||
Logger::info("Scan completed.");
|
||||
|
||||
const auto &alive = scanner.alive();
|
||||
if (alive.empty()) {
|
||||
Logger::info("No alive hosts found.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::cout << "Alive hosts:\n";
|
||||
for (const auto &ip : alive) {
|
||||
std::cout << " " << ip << "\n";
|
||||
}
|
||||
|
||||
// For each alive host, run full TCP port scan
|
||||
for (const auto &ip : alive) {
|
||||
std::cout << "Scanning ports on " << ip << " ...\n";
|
||||
|
||||
PortScanner ps(ip);
|
||||
ps.run();
|
||||
|
||||
const auto &open = ps.openPorts();
|
||||
if (open.empty()) {
|
||||
std::cout << " No open TCP ports found on " << ip << "\n";
|
||||
} else {
|
||||
std::cout << " Open ports on " << ip << ":\n";
|
||||
for (uint16_t port : open) {
|
||||
std::cout << " " << port << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user