#define BOOST_BIND_GLOBAL_PLACEHOLDERS #include <iostream> #include <string> #include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/format.hpp> #include <boost/lexical_cast.hpp> #include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
#include <boost/bind.hpp> #include <boost/thread.hpp> #include <boost/function.hpp> #include <boost/thread/thread_guard.hpp>
using namespace std; using namespace boost; using boost::asio::ip::tcp; namespace opt = boost::program_options;
boost::mutex io_mutex;
void ShowOpt() { fprintf(stderr, "# # # \n" "# # # \n" "# # # ##### ###### ###### # ### # ## \n" "# # # # # # # # ## # # \n" "# # # #### # # # # # ### \n" "# ##### # # # # ## # # # \n" "##### # ##### # # #### # # # ## \n\n" ); }
class AsyncConnect { public: AsyncConnect(boost::asio::io_service& ios, tcp::socket &s) :io_service_(ios), timer_(ios), socket_(s) {}
bool aysnc_connect(const tcp::endpoint &ep, int million_seconds) { bool connect_success = false;
socket_.async_connect(ep, boost::bind(&AsyncConnect::connect_handle, this, _1, boost::ref(connect_success)));
timer_.expires_from_now(boost::posix_time::milliseconds(million_seconds)); bool timeout = false;
timer_.async_wait(boost::bind(&AsyncConnect::timer_handle, this, _1, boost::ref(timeout))); do { io_service_.run_one(); } while (!timeout && !connect_success); timer_.cancel(); return connect_success; }
private: void connect_handle(boost::system::error_code ec, bool &connect_success) { if (!ec) { connect_success = true; } }
void timer_handle(boost::system::error_code ec, bool &timeout) { if (!ec) { socket_.close(); timeout = true; } } boost::asio::io_service &io_service_; boost::asio::deadline_timer timer_; tcp::socket &socket_; };
bool PortScan(std::string address, int port, int timeout) { try { boost::asio::io_service io; tcp::socket socket(io); AsyncConnect acHandler(io, socket); tcp::endpoint ep(boost::asio::ip::address::from_string(address), port);
if (acHandler.aysnc_connect(ep, timeout)) { io.run(); return true; } else { return false; }
} catch (...) { return false; } }
bool CalculationAddress(std::string address, std::vector<std::string> &ref) { std::vector<std::string> vect; try { boost::split(vect, address, boost::is_any_of("/") || boost::is_any_of("."), boost::token_compress_on);
int start_count = lexical_cast<int>(vect[3]); int end_count = lexical_cast<int>(vect[4]);
if (end_count <= 255) { for (int x = start_count; x <= end_count; x++) { std::string this_address = boost::str(boost::format("%s.%s.%s.%s") % vect[0] % vect[1] % vect[2] % x); ref.push_back(this_address); } } else { return false; } } catch (...) { return false; } return true; }
bool CalculationPort(std::string port_string, std::vector<int> &ref) { std::vector<std::string> vect; try { boost::split(vect, port_string, boost::is_any_of(","), boost::token_compress_on);
for (int x = 0; x < vect.size(); x++) { ref.push_back(lexical_cast<int>(vect[x])); } return true; } catch (...) { return false; } return true; }
void MyThread(std::string address, int port) { bool is_open = PortScan(address, port, 1000);
boost::lock_guard<boost::mutex> global_mutex(io_mutex); if (is_open == true) { std::cout << "扫描地址: " << address << " 扫描端口: " << port << " 扫描状态: 开放" << std::endl; } else { std::cout << "扫描地址: " << address << " 扫描端口: " << port << " 扫描状态: 关闭" << std::endl; } }
void MyThreadB(std::string address, int port) { bool is_open = PortScan(address, port, 1000);
boost::lock_guard<boost::mutex> global_mutex(io_mutex); if (is_open == true) { std::cout << "扫描地址: " << address << " 扫描端口: " << port << " 扫描状态: 开放" << std::endl; } }
int main(int argc, char * argv[]) { opt::options_description des_cmd("\n Usage: LyShark 端口扫描器 Ver:1.1 \n\n Options"); des_cmd.add_options() ("address,a", opt::value<std::string>(), "指定扫描地址 192.168.1.1") ("c_address,c", opt::value<std::string>(), "设置扫描C地址段 192.168.1.1/24") ("set_port,s", opt::value<std::string>(), "设置扫描端口 80,443,135,139") ("type,t", opt::value<std::string>(), "对特定主机 扫描 1-65535 全端口") ("help,h", "帮助菜单");
opt::variables_map virtual_map; try { opt::store(opt::parse_command_line(argc, argv, des_cmd), virtual_map); } catch (...){ return 0; }
opt::notify(virtual_map);
if (virtual_map.empty()) { ShowOpt(); std::cout << des_cmd << std::endl; return 0; } else if (virtual_map.count("help") || virtual_map.count("h")) { ShowOpt(); std::cout << des_cmd << std::endl; return 0; } else if (virtual_map.count("address") && virtual_map.count("type")) { std::string address = virtual_map["address"].as<std::string>(); std::string type = virtual_map["type"].as<std::string>();
if (address.length() != 0 && type == "all") { boost::thread_group group; for (int x = 0; x < 65534; x++) { group.create_thread(boost::bind(MyThreadB, address, x)); _sleep(50); } group.join_all(); } }
else if (virtual_map.count("address") && virtual_map.count("set_port")) { std::string address = virtual_map["address"].as<std::string>(); std::string set_port = virtual_map["set_port"].as<std::string>();
std::vector<int> scan_port_list;
bool scan_ref = CalculationPort(set_port, scan_port_list); if (scan_ref == true) { boost::thread_group group; for (int x = 0; x < scan_port_list.size(); x++) { group.create_thread(boost::bind(MyThread, address, scan_port_list[x])); } group.join_all(); } }
else if (virtual_map.count("c_address") && virtual_map.count("set_port")) { std::string c_address = virtual_map["c_address"].as < std::string >(); std::string set_port = virtual_map["set_port"].as<std::string>();
std::vector<int> scan_port_list; bool scan_port_ref = CalculationPort(set_port, scan_port_list);
std::vector < std::string > scan_address_list;
bool scan_address_ref = CalculationAddress(c_address, scan_address_list);
if (scan_port_ref == true && scan_address_ref == true) { for (int x = 0; x < scan_address_list.size(); x++) { boost::thread_group group; for (int y = 0; y < scan_port_list.size(); y++) { group.create_thread(boost::bind(MyThreadB, scan_address_list[x], scan_port_list[y])); } group.join_all(); } } } else { std::cout << "参数错误" << std::endl; } return 0; }
|