-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
240 lines (196 loc) · 6.61 KB
/
Copy pathmain.cpp
File metadata and controls
240 lines (196 loc) · 6.61 KB
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
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <thread>
#include <chrono>
#include <string>
#include <vector>
#include <future>
#pragma comment(lib, "ws2_32.lib")
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable: 4996)
class NetworkUtils {
public:
static bool checkTCPConnect(const std::string& host, int port, int timeoutMs = 3000) {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cout << "WSAStartup failed\n";
return false;
}
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
std::cout << "Socket creation failed\n";
WSACleanup();
return false;
}
unsigned long timeout = timeoutMs;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout));
sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
if (inet_pton(AF_INET, host.c_str(), &serverAddr.sin_addr) != 1) {
hostent* remoteHost = gethostbyname(host.c_str());
if (remoteHost == NULL) {
std::cout << "Host resolution failed for: " << host << "\n";
closesocket(sock);
WSACleanup();
return false;
}
serverAddr.sin_addr = *(in_addr*)remoteHost->h_addr;
}
bool result = (connect(sock, (sockaddr*)&serverAddr, sizeof(serverAddr)) == 0);
closesocket(sock);
WSACleanup();
return result;
}
static void tcpPing(const std::string& host, int port = 80, int count = 4) {
std::cout << "TCP Ping " << host << " port " << port << ":\n";
int successCount = 0;
for (int i = 0; i < count; i++) {
auto start = std::chrono::steady_clock::now();
bool success = checkTCPConnect(host, port, 2000);
auto end = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
if (success) {
std::cout << "OK Answer from " << host << ": time=" << duration.count() << "ms\n";
successCount++;
}
else {
std::cout << "Timeout\n";
}
if (i < count - 1) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
std::cout << "\nStatistics TCP Ping for " << host << ":\n";
std::cout << " Packets: sent = " << count
<< ", received = " << successCount
<< ", lost = " << (count - successCount)
<< " (" << ((count - successCount) * 100 / count) << "% loss)\n";
}
};
std::string getServiceName(int port) {
switch (port) {
case 21: return "FTP";
case 22: return "SSH";
case 23: return "Telnet";
case 25: return "SMTP";
case 53: return "DNS";
case 80: return "HTTP";
case 110: return "POP3";
case 135: return "RPC";
case 139: return "NetBIOS";
case 143: return "IMAP";
case 443: return "HTTPS";
case 445: return "SMB";
case 993: return "IMAPS";
case 995: return "POP3S";
case 1723: return "PPTP";
case 3306: return "MySQL";
case 3389: return "RDP";
case 5900: return "VNC";
case 8080: return "HTTP-Alt";
default: return "Unknown";
}
}
void portScanner() {
std::string host;
std::cout << "Enter host/IP for scanning: ";
std::getline(std::cin, host);
std::cout << "Scanning main ports...\n";
int commonPorts[] = { 21, 22, 23, 25, 53, 80, 110, 135, 139, 143,
443, 445, 993, 995, 1723, 3306, 3389, 5900, 8080 };
std::vector<std::future<bool>> futures;
std::vector<int> openPorts;
for (int port : commonPorts) {
futures.push_back(std::async(std::launch::async, [host, port]() {
return NetworkUtils::checkTCPConnect(host, port, 1000);
}));
}
std::cout << "Scan results for " << host << ":\n";
std::cout << "============================================\n";
for (size_t i = 0; i < futures.size(); i++) {
if (futures[i].get()) {
std::string serviceName = getServiceName(commonPorts[i]);
std::cout << "OK Port " << commonPorts[i] << " (" << serviceName << ") - OPEN\n";
openPorts.push_back(commonPorts[i]);
}
}
std::cout << "============================================\n";
std::cout << "Open ports found: " << openPorts.size() << std::endl;
}
void tcpPingMenu() {
std::string host;
std::cout << "Enter host/IP for ping: ";
std::getline(std::cin, host);
int port;
std::cout << "Enter port for check (default 80): ";
std::string portInput;
std::getline(std::cin, portInput);
if (portInput.empty()) {
port = 80;
}
else {
port = std::stoi(portInput);
}
NetworkUtils::tcpPing(host, port);
}
void checkSpecificPort() {
std::string host;
int port;
std::cout << "Enter host/IP: ";
std::getline(std::cin, host);
std::cout << "Enter port: ";
std::cin >> port;
std::cin.ignore();
if (NetworkUtils::checkTCPConnect(host, port)) {
std::cout << "OK Port " << port << " open on host " << host << std::endl;
}
else {
std::cout << "FAIL Port " << port << " closed on host " << host << std::endl;
}
}
void showMenu() {
std::cout << "Network Utilities\n";
std::cout << "===================\n";
std::cout << "1. Port scanning\n";
std::cout << "2. Host ping (TCP)\n";
std::cout << "3. Check specific port\n";
std::cout << "4. Exit\n";
std::cout << "Choose option: ";
}
int main() {
setlocale(LC_ALL, "Russian");
std::cout << "=== Network Utilities ===\n";
std::cout << "Uses TCP connections\n\n";
int choice;
do {
showMenu();
std::cin >> choice;
std::cin.ignore();
switch (choice) {
case 1:
portScanner();
break;
case 2:
tcpPingMenu();
break;
case 3:
checkSpecificPort();
break;
case 4:
std::cout << "Exit...\n";
break;
default:
std::cout << "Wrong choice!\n";
}
if (choice != 4) {
std::cout << "\nPress Enter to continue...";
std::cin.get();
}
} while (choice != 4);
return 0;
}