-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathport_scanner.cpp
More file actions
211 lines (171 loc) · 5.87 KB
/
port_scanner.cpp
File metadata and controls
211 lines (171 loc) · 5.87 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
/*
* Port Scanner Implementation
* Handles TCP port scanning for specific services
*/
#include "port_scanner.h"
PortScanner::PortScanner() {
scanResults.reserve(MAX_DEVICES * TARGET_PORTS.size());
}
PortScanner::~PortScanner() {
scanResults.clear();
}
void PortScanner::begin() {
Serial.println("Initializing Port Scanner...");
scanResults.clear();
#if DEBUG_PORT_SCAN
Serial.println("Port Scanner initialized successfully");
#endif
}
bool PortScanner::testPort(IPAddress target, int port) {
if (!isValidPort(port)) {
#if DEBUG_PORT_SCAN
Serial.printf("Invalid port number: %d\n", port);
#endif
return false;
}
unsigned long responseTime = 0;
bool isOpen = tcpConnect(target, port, responseTime);
// Add result to cache
addResult(target, port, isOpen, responseTime);
#if DEBUG_PORT_SCAN
Serial.printf("Port scan: %s:%d - %s (Response: %lu ms)\n",
target.toString().c_str(),
port,
isOpen ? "OPEN" : "CLOSED",
responseTime);
#endif
return isOpen;
}
std::vector<PortScanResult> PortScanner::scanPorts(IPAddress target, const std::vector<int>& ports) {
std::vector<PortScanResult> results;
#if DEBUG_PORT_SCAN
Serial.printf("Scanning %d ports on %s\n", ports.size(), target.toString().c_str());
#endif
for (int port : ports) {
unsigned long responseTime = 0;
bool isOpen = tcpConnect(target, port, responseTime);
PortScanResult result;
result.target = target;
result.port = port;
result.isOpen = isOpen;
result.responseTime = responseTime;
result.serviceName = getServiceName(port);
results.push_back(result);
addResult(target, port, isOpen, responseTime);
// Small delay between port scans
delay(50);
yield();
}
return results;
}
std::vector<PortScanResult> PortScanner::getLastResults() {
return scanResults;
}
void PortScanner::clearResults() {
scanResults.clear();
}
bool PortScanner::tcpConnect(IPAddress target, int port, unsigned long& responseTime) {
WiFiClient client;
// Set timeout for connection attempt
client.setTimeout(PORT_TIMEOUT);
unsigned long startTime = millis();
bool connected = false;
// Attempt connection with retry logic
for (int attempt = 0; attempt < MAX_RETRY_ATTEMPTS; attempt++) {
connected = client.connect(target, port);
if (connected) {
break;
}
if (attempt < MAX_RETRY_ATTEMPTS - 1) {
delay(RETRY_DELAY);
}
}
responseTime = millis() - startTime;
if (connected) {
// Send a minimal request for specific protocols
if (port == 80) {
client.print("HEAD / HTTP/1.1\r\nHost: ");
client.print(target.toString());
client.print("\r\nConnection: close\r\n\r\n");
} else if (port == 443) {
// For HTTPS, just the connection attempt is enough
// as SSL handshake would require more complex implementation
} else if (port == 502) {
// MODBUS TCP - send a simple query
uint8_t modbusQuery[] = {0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x00, 0x00, 0x01};
client.write(modbusQuery, sizeof(modbusQuery));
} else if (port == 47808) {
// BACnet - send a simple who-is request
uint8_t bacnetQuery[] = {0x81, 0x0B, 0x00, 0x0C, 0x01, 0x20, 0xFF, 0xFF, 0x00, 0xFF, 0x10, 0x08};
client.write(bacnetQuery, sizeof(bacnetQuery));
}
// Wait for response (brief)
delay(100);
// Check if we got any response
bool hasResponse = client.available() > 0;
client.stop();
return true;
}
return false;
}
bool PortScanner::synScan(IPAddress target, int port) {
// Simplified SYN scan - not implementing raw sockets
// Fall back to TCP connect
unsigned long responseTime;
return tcpConnect(target, port, responseTime);
}
bool PortScanner::isCommonPort(int port) {
// Check if port is in our target list
for (int targetPort : TARGET_PORTS) {
if (port == targetPort) {
return true;
}
}
return false;
}
String PortScanner::getServiceName(int port) {
switch (port) {
case 80: return "HTTP";
case 443: return "HTTPS";
case 502: return "MODBUS TCP";
case 47808: return "BACnet";
case 21: return "FTP";
case 22: return "SSH";
case 23: return "Telnet";
case 25: return "SMTP";
case 53: return "DNS";
case 110: return "POP3";
case 143: return "IMAP";
case 993: return "IMAPS";
case 995: return "POP3S";
case 1883: return "MQTT";
case 8080: return "HTTP-Alt";
case 8443: return "HTTPS-Alt";
default: return "Unknown";
}
}
bool PortScanner::isValidPort(int port) {
return (port > 0 && port <= 65535);
}
void PortScanner::addResult(IPAddress target, int port, bool isOpen, unsigned long responseTime) {
// Check if we already have this result
for (auto& result : scanResults) {
if (result.target == target && result.port == port) {
result.isOpen = isOpen;
result.responseTime = responseTime;
return;
}
}
// Add new result
PortScanResult result;
result.target = target;
result.port = port;
result.isOpen = isOpen;
result.responseTime = responseTime;
result.serviceName = getServiceName(port);
scanResults.push_back(result);
// Limit cache size
if (scanResults.size() > MAX_DEVICES * TARGET_PORTS.size()) {
scanResults.erase(scanResults.begin());
}
}