Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 526b27deaf | |||
| 7c19123da9 | |||
| c9e1b0a149 | |||
| 0d96c21199 | |||
| 679d752177 | |||
| 9f88390b1a |
@@ -2,3 +2,4 @@
|
|||||||
.xmake
|
.xmake
|
||||||
build
|
build
|
||||||
compile_commands.json
|
compile_commands.json
|
||||||
|
ignore
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#include "portscan_cli.hpp"
|
||||||
|
#include <boost/program_options/value_semantic.hpp>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
PortScanCli::PortScanCli()
|
||||||
|
: CliBase("Port Scanner Options")
|
||||||
|
, ip_("")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void PortScanCli::setupOptions()
|
||||||
|
{
|
||||||
|
// clang-format off
|
||||||
|
desc_.add_options()
|
||||||
|
("help,h", "Show help")
|
||||||
|
("ip,H", withDefault(ip_, "<IP>"), "Local IPv4 to scan");
|
||||||
|
// clang-format on
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string PortScanCli::getIp() const
|
||||||
|
{
|
||||||
|
if (vm_["ip"].empty() == 0) {
|
||||||
|
return "127.0.0.1";
|
||||||
|
}
|
||||||
|
return vm_.at("ip").as<std::string>();
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "cli_base.hpp"
|
||||||
|
#include "option_utils.hpp"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class PortScanCli : public CliBase, protected OptionUtils
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
PortScanCli();
|
||||||
|
|
||||||
|
std::string getIp() const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void setupOptions() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string ip_;
|
||||||
|
};
|
||||||
@@ -1,27 +1,29 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
|
#include <boost/asio/ip/address.hpp>
|
||||||
#include <boost/program_options.hpp>
|
#include <boost/program_options.hpp>
|
||||||
#include <boost/system/error_code.hpp>
|
#include <boost/system/error_code.hpp>
|
||||||
|
|
||||||
// --- Error handling ---
|
// --- Error handling ---
|
||||||
using ErrorCode = boost::system::error_code;
|
using ErrorCode = boost::system::error_code;
|
||||||
|
|
||||||
// --- IO Context ---
|
// --- Asio types ---
|
||||||
using IoContext = boost::asio::io_context;
|
using IoContext = boost::asio::io_context;
|
||||||
|
using SteadyTimer = boost::asio::steady_timer;
|
||||||
|
|
||||||
// --- TCP ---
|
// --- Ip types ---
|
||||||
|
using IpAddress = boost::asio::ip::address;
|
||||||
|
|
||||||
|
// --- TCP types ---
|
||||||
using TcpSocket = boost::asio::ip::tcp::socket;
|
using TcpSocket = boost::asio::ip::tcp::socket;
|
||||||
using TcpAcceptor = boost::asio::ip::tcp::acceptor;
|
using TcpAcceptor = boost::asio::ip::tcp::acceptor;
|
||||||
using TcpEndpoint = boost::asio::ip::tcp::endpoint;
|
using TcpEndpoint = boost::asio::ip::tcp::endpoint;
|
||||||
|
|
||||||
// --- UDP (для DNS) ---
|
// --- UDP types ---
|
||||||
using UdpSocket = boost::asio::ip::udp::socket;
|
using UdpSocket = boost::asio::ip::udp::socket;
|
||||||
using UdpEndpoint = boost::asio::ip::udp::endpoint;
|
using UdpEndpoint = boost::asio::ip::udp::endpoint;
|
||||||
|
|
||||||
// --- Таймеры ---
|
|
||||||
using SteadyTimer = boost::asio::steady_timer;
|
|
||||||
|
|
||||||
// --- Program options ---
|
// --- Program options ---
|
||||||
using OptionsDescription = boost::program_options::options_description;
|
using OptionsDescription = boost::program_options::options_description;
|
||||||
using VariablesMap = boost::program_options::variables_map;
|
using VariablesMap = boost::program_options::variables_map;
|
||||||
|
|||||||
@@ -4,11 +4,17 @@
|
|||||||
|
|
||||||
class OptionUtils
|
class OptionUtils
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
template <typename T>
|
||||||
|
using OptionValue = boost::program_options::typed_value<T> *;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static OptionValue<T>
|
||||||
|
withDefault(const T &value, const char *name = nullptr);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
OptionUtils() = default;
|
OptionUtils() = default;
|
||||||
~OptionUtils() = default;
|
~OptionUtils() = default;
|
||||||
|
|
||||||
template <typename T> static auto withDefault(T value);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#include "option_utils.tpp"
|
#include "option_utils.tpp"
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "option_utils.hpp"
|
#include "option_utils.hpp"
|
||||||
|
|
||||||
template <typename T> auto OptionUtils::withDefault(T value)
|
template <typename T>
|
||||||
|
OptionUtils::OptionValue<T>
|
||||||
|
OptionUtils::withDefault(const T &value, const char *name)
|
||||||
{
|
{
|
||||||
return boost::program_options::value<T>()->default_value(value);
|
auto v = boost::program_options::value<T>()->default_value(value);
|
||||||
|
if (name) {
|
||||||
|
v->value_name(name); // Optional
|
||||||
|
}
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
class Server
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
virtual ~Server() = default;
|
|
||||||
virtual void start() = 0;
|
|
||||||
virtual void stop() = 0;
|
|
||||||
};
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
#include "session_base.hpp"
|
|
||||||
#include "logger.hpp"
|
|
||||||
#include <boost/asio/write.hpp>
|
|
||||||
|
|
||||||
SessionBase::SessionBase(TcpSocket socket)
|
|
||||||
: socket_(std::move(socket))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void SessionBase::start()
|
|
||||||
{
|
|
||||||
doRead();
|
|
||||||
}
|
|
||||||
|
|
||||||
void SessionBase::doRead()
|
|
||||||
{
|
|
||||||
using namespace boost::asio;
|
|
||||||
|
|
||||||
auto self = shared_from_this();
|
|
||||||
|
|
||||||
auto handler = [this, self](ErrorCode ec, std::size_t bytesTransferred) {
|
|
||||||
onRead(ec, bytesTransferred);
|
|
||||||
};
|
|
||||||
|
|
||||||
socket_.async_read_some(buffer(buffer_), handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SessionBase::onRead(ErrorCode ec, std::size_t bytesTransferred)
|
|
||||||
{
|
|
||||||
if (ec) {
|
|
||||||
Logger::error("Read error: " + ec.message());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string rawRequest(buffer_.data(), bytesTransferred);
|
|
||||||
Logger::info("Received request:\n" + rawRequest);
|
|
||||||
|
|
||||||
try {
|
|
||||||
handleRequest(rawRequest);
|
|
||||||
} catch (const std::exception &ex) {
|
|
||||||
Logger::error(std::string("Request handling failed: ") + ex.what());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SessionBase::doWrite(const std::string &response)
|
|
||||||
{
|
|
||||||
using namespace boost::asio;
|
|
||||||
|
|
||||||
auto self = shared_from_this();
|
|
||||||
auto onWriteHandler =
|
|
||||||
[this, self](ErrorCode ec, std::size_t /*bytesTransferred*/) {
|
|
||||||
if (ec) {
|
|
||||||
Logger::error("Write error: " + ec.message());
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorCode shutdownEc;
|
|
||||||
if (socket_.shutdown(TcpSocket::shutdown_both, shutdownEc)) {
|
|
||||||
Logger::warn("Shutdown error: " + shutdownEc.message());
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorCode closeEc;
|
|
||||||
if (socket_.close(closeEc)) {
|
|
||||||
Logger::warn("Close error: " + closeEc.message());
|
|
||||||
} else {
|
|
||||||
Logger::info("Connection closed");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
async_write(socket_, buffer(response), onWriteHandler);
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "aliases.hpp"
|
|
||||||
#include <array>
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class SessionBase : public std::enable_shared_from_this<SessionBase>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
explicit SessionBase(TcpSocket socket);
|
|
||||||
virtual ~SessionBase() = default;
|
|
||||||
|
|
||||||
void start();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual void handleRequest(const std::string_view rawRequest) = 0;
|
|
||||||
|
|
||||||
void doRead();
|
|
||||||
void onRead(ErrorCode ec, std::size_t bytesTransferred);
|
|
||||||
void doWrite(const std::string &response);
|
|
||||||
|
|
||||||
TcpSocket socket_;
|
|
||||||
std::array<char, 8192> buffer_;
|
|
||||||
};
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
#include "tcp_server_base.hpp"
|
|
||||||
#include "logger.hpp"
|
|
||||||
|
|
||||||
TcpServerBase::TcpServerBase(IoContext &io, unsigned short port)
|
|
||||||
: acceptor_(io, TcpEndpoint(boost::asio::ip::tcp::v4(), port))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void TcpServerBase::start()
|
|
||||||
{
|
|
||||||
auto doAccept = [this]() {
|
|
||||||
acceptor_.async_accept([this](ErrorCode ec, TcpSocket socket) {
|
|
||||||
if (!ec) {
|
|
||||||
Logger::info("New client connected");
|
|
||||||
createSession(std::move(socket));
|
|
||||||
} else {
|
|
||||||
Logger::error("Accept error: " + ec.message());
|
|
||||||
}
|
|
||||||
start(); // continue accepting
|
|
||||||
});
|
|
||||||
};
|
|
||||||
doAccept();
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "aliases.hpp"
|
|
||||||
|
|
||||||
class TcpServerBase
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
TcpServerBase(IoContext &io, unsigned short port);
|
|
||||||
virtual ~TcpServerBase() = default;
|
|
||||||
|
|
||||||
void start();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual void createSession(TcpSocket socket) = 0;
|
|
||||||
|
|
||||||
TcpAcceptor acceptor_;
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
#include "port_scanner.hpp"
|
||||||
|
#include "logger.hpp"
|
||||||
|
#include <boost/asio.hpp>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
PortScanner::PortScanner(
|
||||||
|
const std::string &ip, const std::vector<uint16_t> &ports
|
||||||
|
)
|
||||||
|
: ports_(ports)
|
||||||
|
{
|
||||||
|
ErrorCode ec;
|
||||||
|
addr_ = boost::asio::ip::make_address(ip, ec);
|
||||||
|
ip_ok_ = !ec;
|
||||||
|
|
||||||
|
if (!ip_ok_) {
|
||||||
|
Logger::error(std::string("PortScanner: invalid IP: ") + ip);
|
||||||
|
} else {
|
||||||
|
Logger::debug(std::string("PortScanner: using IP: ") + ip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PortScanner::setTimeoutMs(int ms)
|
||||||
|
{
|
||||||
|
if (ms > 0) {
|
||||||
|
timeout_ms_ = ms;
|
||||||
|
Logger::debug(
|
||||||
|
std::string("PortScanner: timeout set to ")
|
||||||
|
+ std::to_string(timeout_ms_) + " ms"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PortScanner::run()
|
||||||
|
{
|
||||||
|
if (!ip_ok_) {
|
||||||
|
Logger::error("PortScanner::run - aborting: ip not ok");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (uint16_t p : ports_) {
|
||||||
|
if (probe(p)) {
|
||||||
|
Logger::info(std::to_string(p));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PortScanner::probe(uint16_t port)
|
||||||
|
{
|
||||||
|
TcpSocket s(io_);
|
||||||
|
SteadyTimer t(io_);
|
||||||
|
auto ep = endpoint(port);
|
||||||
|
|
||||||
|
Logger::debug(
|
||||||
|
std::string("PortScanner::probe - probing port ") + std::to_string(port)
|
||||||
|
);
|
||||||
|
|
||||||
|
armTimer(t, s);
|
||||||
|
armConnect(s, ep);
|
||||||
|
runUntilDone();
|
||||||
|
cancelTimer(t);
|
||||||
|
drainLeftovers();
|
||||||
|
close(s);
|
||||||
|
|
||||||
|
if (ok_) {
|
||||||
|
Logger::debug(std::string("Port ") + std::to_string(port) + " is OPEN");
|
||||||
|
} else {
|
||||||
|
Logger::debug(
|
||||||
|
std::string("Port ") + std::to_string(port) + " is CLOSED/FILTERED"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ok_;
|
||||||
|
}
|
||||||
|
|
||||||
|
TcpEndpoint PortScanner::endpoint(uint16_t port) const
|
||||||
|
{
|
||||||
|
return TcpEndpoint(addr_, port);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PortScanner::armTimer(SteadyTimer &t, TcpSocket &s)
|
||||||
|
{
|
||||||
|
t.expires_after(std::chrono::milliseconds(timeout_ms_));
|
||||||
|
|
||||||
|
auto onTimeout = [&s](const ErrorCode &ec) {
|
||||||
|
if (!ec) {
|
||||||
|
Logger::debug("Timer expired -> cancelling socket");
|
||||||
|
s.cancel();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
t.async_wait(onTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PortScanner::armConnect(TcpSocket &s, const TcpEndpoint &ep)
|
||||||
|
{
|
||||||
|
ok_ = false;
|
||||||
|
done_ = false;
|
||||||
|
|
||||||
|
auto onConnect = [this](const ErrorCode &ec) {
|
||||||
|
if (!ec) {
|
||||||
|
ok_ = true;
|
||||||
|
Logger::debug("async_connect succeeded");
|
||||||
|
} else {
|
||||||
|
ok_ = false;
|
||||||
|
Logger::debug(std::string("async_connect failed: ") + ec.message());
|
||||||
|
}
|
||||||
|
done_ = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
s.async_connect(ep, onConnect);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PortScanner::runUntilDone()
|
||||||
|
{
|
||||||
|
io_.restart();
|
||||||
|
while (!done_ && io_.run_one()) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PortScanner::cancelTimer(SteadyTimer &t)
|
||||||
|
{
|
||||||
|
ErrorCode ec;
|
||||||
|
t.cancel();
|
||||||
|
if (ec) {
|
||||||
|
Logger::warn(std::string("cancelTimer: ") + ec.message());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PortScanner::drainLeftovers()
|
||||||
|
{
|
||||||
|
io_.restart();
|
||||||
|
while (io_.run_one()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PortScanner::close(TcpSocket &s)
|
||||||
|
{
|
||||||
|
ErrorCode ig;
|
||||||
|
if (s.close(ig)) {
|
||||||
|
Logger::warn(std::string("close socket failed: ") + ig.message());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "aliases.hpp"
|
||||||
|
#include <boost/asio.hpp>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class PortScanner
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
PortScanner(const std::string &ip, const std::vector<uint16_t> &ports);
|
||||||
|
|
||||||
|
void setTimeoutMs(int ms);
|
||||||
|
|
||||||
|
void run();
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool probe(uint16_t port);
|
||||||
|
TcpEndpoint endpoint(uint16_t port) const;
|
||||||
|
|
||||||
|
void armTimer(SteadyTimer &t, TcpSocket &s);
|
||||||
|
void armConnect(TcpSocket &s, const TcpEndpoint &ep);
|
||||||
|
void runUntilDone();
|
||||||
|
void cancelTimer(SteadyTimer &t);
|
||||||
|
void drainLeftovers();
|
||||||
|
void close(TcpSocket &s);
|
||||||
|
|
||||||
|
private:
|
||||||
|
IoContext io_;
|
||||||
|
IpAddress addr_;
|
||||||
|
std::vector<uint16_t> ports_;
|
||||||
|
|
||||||
|
int timeout_ms_{300};
|
||||||
|
bool ip_ok_{false};
|
||||||
|
bool ok_{false};
|
||||||
|
bool done_{false};
|
||||||
|
};
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
#include "cli_base.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)
|
||||||
|
{
|
||||||
|
Logger::init(true);
|
||||||
|
|
||||||
|
PortScanCli cli;
|
||||||
|
cli.parse(argc, argv);
|
||||||
|
if (cli.isHelp()) {
|
||||||
|
cli.printHelp();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string ip = cli.getIp();
|
||||||
|
auto ports = allPorts();
|
||||||
|
|
||||||
|
Logger::info("Starting full port scan (1..65535)...");
|
||||||
|
PortScanner scanner(ip, ports);
|
||||||
|
scanner.run();
|
||||||
|
Logger::info("Scan completed.");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -2,16 +2,18 @@ set_languages "c++23"
|
|||||||
|
|
||||||
add_rules("plugin.compile_commands.autoupdate", { outputdir = "." })
|
add_rules("plugin.compile_commands.autoupdate", { outputdir = "." })
|
||||||
|
|
||||||
add_requires("boost", {
|
add_requires("cmake::Boost", {
|
||||||
system = true, -- sudo pacman -S boost
|
alias = "boost",
|
||||||
|
system = true,
|
||||||
configs = {
|
configs = {
|
||||||
all = false,
|
components = { "program_options", "log" },
|
||||||
system = true, -- asio
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
target "app"
|
target "port_scanner"
|
||||||
|
do
|
||||||
set_kind "binary"
|
set_kind "binary"
|
||||||
add_files "src/*.cpp"
|
add_files("src/main.cpp", "src/cli/*.cpp", "src/common/*.cpp", "src/core/*.cpp")
|
||||||
add_links "boost_system"
|
add_includedirs("src", "src/cli", "src/common", "src/core", { public = true })
|
||||||
|
add_packages "boost"
|
||||||
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user