-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
194 lines (165 loc) · 3.95 KB
/
Source.cpp
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
//This project was developed for personal research and education purposes.
//Do not use this tool to scan networks or ports that you do not have permission to scan.
//Date Created: 14 Jun 2022
//Author: Kyle Noel
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <SFML/Network.hpp>
#define WINDOWS
using namespace std;
using namespace sf;
//Global var
const int PORT_LIMIT = 65535;
//Prototypes
void singleScan(string);
void rangeScan(string);
string portStatus(const string&, int);
void clearConsole();
int main()
{
int op1;
int op2;
char remote[16];
bool flag;
char cont;
const char header[62] = "-------------------- SIMPLE PORT SCANNER --------------------";
const char footer[62] = "-------------------- PROGRAM TERMINATED --------------------";
string scanHost;
const string LOCAL = "localhost";
do {
//Display header
cout << header << endl << endl;
//Local scan and remote scan menu
cout << "Would you like to perform a local scan or enter a remote IP?" << endl;
cout << "1. Scan localhost" << endl << "2. Enter an IP" << endl;
cout << ">> ";
cin >> op1;
while (op1 != 1 && op1 != 2)
{
cout << "Invalid response. Try again." << endl << ">> ";
cin >> op1;
}
switch (op1) {
case 1:
scanHost = LOCAL;
break;
case 2:
cout << endl << "Enter an IPv4 address (ex: 255.255.255.255) or a valid URL." << endl << ">> ";
cin.ignore();
cin.getline(remote, 16);
scanHost = remote;
break;
}
//Single scan and range scan menu
cout << endl;
cout << "Would you like to scan a single port or a range of ports?" << endl;
cout << "1. Single" << endl << "2. Range" << endl;
cout << ">> ";
cin >> op2;
while (op2 != 1 && op2 != 2)
{
cout << "Invalid response. Try again." << endl << ">> ";
cin >> op2;
}
cout << endl;
switch (op2) {
case 1:
singleScan(scanHost);
break;
case 2:
rangeScan(scanHost);
break;
}
//Restart option
cout << endl << "Would you like to perform another scan? Y/N" << endl << ">> ";
cin >> cont;
while (cont != 'Y' && cont != 'y' && cont != 'N' && cont != 'n')
{
cout << "Invalid response. Try again." << endl << ">> ";
cin >> cont;
}
if (cont == 'Y' || cont == 'y')
{
flag = true;
clearConsole();
}
else
flag = false;
} while (flag == true);
//Display footer
cout << endl << footer << endl;
return 0;
}
void singleScan(string host)
{
int portNum;
cout << "Enter a port number to scan." << endl << ">> ";
cin >> portNum;
while (portNum < 0 || portNum > PORT_LIMIT)
{
cout << "Invalid response. Enter a valid port number: ";
cin >> portNum;
}
//Displaying single scan result
cout << endl << "Scanning " << host << "..." << endl;
cout << "Port " << portNum << " is " << portStatus(host, portNum) << endl;
}
void rangeScan(string host)
{
int beg;
int end;
vector<int> range;
cout << "Enter the first port number in the scan range." << endl << ">> ";
cin >> beg;
while (beg < 0 || beg > PORT_LIMIT)
{
cout << "Invalid response. Try again." << endl << ">> ";
cin >> beg;
}
cout << endl << "Enter the last port number in the scan range." << endl << ">> ";
cin >> end;
while (end < 0 || end > PORT_LIMIT)
{
cout << "Invalid response. Try again." << endl << ">> ";
cin >> end;
}
//Err checking
if (beg > end)
swap(beg, end);
if (beg == end)
{
end = beg;
beg = 0; //displays ports 0-beg if beg and end are equal
}
for (; beg <= end; beg++)
{
range.push_back(beg);
}
//Displaying range scan results
cout << endl << "Scanning " << host << "..." << endl;
for (auto i = range.begin(); i != range.end(); ++i)
{
cout << "Port " << *i << setw(12) << portStatus(host, *i) << endl;
}
}
string portStatus(const string& ip, int port)
{
string status;
//Scanning ports
if (TcpSocket().connect(ip, port) == Socket::Done)
status = "open";
else
status = "closed";
return status;
}
void clearConsole()
{
#ifdef WINDOWS
system("cls");
#endif
#ifdef LINUX
system("clear")
#endif
}