Look at my Local Port Scanner - Need to Be Fixed Little Bit!

Oct 20, 2008 at 12:56am
First of all, i want to say Hi! Glad to join your great forum & glad to part of your members team!

The following program scans range of port of the local machine that program is running on. Hence, this is my first program so i do not really know if the algorithm is right or now...I only care how to solve this loop thing for now. First of all, prints the name of the host name....

My program compiles, the problem is currentPort reset itself to "startingPort" everytime a new loop starts...

If i take the bottom declarations out, my program return hostNameStatus as failed! Completely unrelated. Same problem happens if i take off "string tempPort & stringstream out", Weird! Isn't it!

See the code:

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
#include<iostream>
#include<string>
#include<sstream>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.h>
using namespace std;

//program description
void progDesc()
{
     cout<<"This a simple port scanner, scan range of\n";
     cout<<"ports on you IP address...\n"<<endl;

}

int main()
{
	int status;                  //receives the status of your pc address
	struct addrinfo hints;       //fill your host info - hints is input
	struct addrinfo *results;    //gets your host info - results is output
	int startingPort;            //stores the starting range of port number
	int endingPort;              //stores the ending rage of port number

	//tell user what program does
	progDesc();

	/**get your computer's name**/
	char pcName[40];       				  //holds your pc name
	size_t pcNameSize;     				  //holds size of the name
	int hostNameStatus; 				  //holds status of the function whether it succeeded or failed
	hostNameStatus = gethostname(pcName, pcNameSize); //gethostname grabs your pc name & pass it to pcName
	
	//make sure function host name returned successfully
	if(hostNameStatus == -1 )
	{
		cout<<"Error: could not locate your host name\n";
		return 1;
	}

	system("clear");

	//print machine name
	cout<<"Your Machine Name: ";
	cout<<pcName;
	cout<<endl;

	//ask port range from user
	cout<<"Enter Starting Port: ";
	cin>>startingPort;

	cout<<"Enter Ending Port: ";
	cin>>endingPort;

	cout<<"Start Checking: "<<endl;
	
	//set size of hints to zero
	memset(&hints, 0, sizeof hints);

	//fill some of your host address info
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_PASSIVE;

	/***convert a port from int to *char, so it can be passed into getaddrinfo()***/

	//convert startingPort to string. After that, convert the string into *char...
	string tempPort;
	stringstream out;
	out<<startingPort;
	tempPort = out.str();    //tempPort carries startingPort in string format

	//convert tempPort to *char - currentPort going to be passed into getaddrinfo()
	char currentPort[10];
	strcpy(currentPort, tempPort.c_str());

	/******************************************************************************/

	//check the status
	while(startingPort < endingPort)
	{
		//make sure getaddrinfo can locate your pc, more specific
		//details of your host address is returned to results...
		status = getaddrinfo(NULL, currentPort, &hints, &results);

		//make sure getaddrinfo had a success status
		if(status != 0)
		{
			fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
			return 2;
		}
	
		//create a socket.
		int socketfd;
		socketfd = socket(results->ai_family, results->ai_socktype, results->ai_protocol);

		if(socketfd == -1 )
		{
			cout<<"Error: failed to create a socket.\n";
			return 3;
		}

		//connect to your own IP address in every loop with 
		//a new port, connect() will associate your socket
		//with the "current port number & your local IP address".
		int connectStatus;
		connectStatus = connect(socketfd, results->ai_addr, results->ai_addrlen);
	
		if(connectStatus == -1 )
		{
			cout<<"Port "<<currentPort<<" is Closed or Blocked.\n";
		}else{
			cout<<"Port "<<currentPort<<" is OPEN.\n";
		}

		//close the created socket, because
		//a new one is created in every loop.
		close(socketfd);
	
		//move to the next port in the specified range
		startingPort++;

		//convert the incremented port to a string
		string tempPort;
		stringstream out;
		out<<startingPort;
		tempPort = out.str();
		char currentPort[10];
		strcpy(currentPort, tempPort.c_str());
	}
	return 0;
}


Hopefully, the algorithm is right...
Last edited on Oct 20, 2008 at 12:57am
Oct 20, 2008 at 6:58pm
Just a note. It looks as though you are actually making a connection to your target. This is definitely a bad approach as it's considered an "active" port-scan and readily detectable.

1
2
3
4
5
6
		string tempPort;
		stringstream out;
		out<<startingPort;
		tempPort = out.str();
		char currentPort[10];
		strcpy(currentPort, tempPort.c_str());


Wow. Try:
 
sprintf(currentPort, "%i", startingPort);



Oct 21, 2008 at 6:36am
I'm making this program for practicing only. Therefore, its very basic i do not concern much on how to make it undetectable yet. Note: i'm scanning on my PC. It does not scan port on remote hosts. Is the program correct - regardless of efficiency.

I will try sprintf(currentPort, "%i", startingPort); and let you know!

THANKS
Oct 21, 2008 at 8:35am
This method does not work sprintf(currentPort, "%i", startingPort);
I even tried fprintf(currentPort, "%i", startingPort);

Output appear as:
�TEST currentPort �

Anyhow, i will deal with it.
Oct 21, 2008 at 7:28pm
sprintf will write the value of startingPort into currentPort according to your format %i = integer. That should work fine.
Oct 21, 2008 at 8:18pm
I solved it!

I had to make pcName declared as char *pcName = new char[40];
not char pcName[40]; Things after that went fine!

Its odd, pcName was the cause of the problem even though it got to do
nothing with currentPort!

Thanks anyway. Done
Last edited on Oct 21, 2008 at 8:20pm
Oct 22, 2008 at 12:12am
Here is the code:

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
#include<iostream>
#include<string>
#include<sstream>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.h>
using namespace std;


struct addrinfo hints;      	//fill your host info - hints is input
struct addrinfo *results;	//gets your host info - results is output
char *pcName = new char[40];    //holds your pc name
string tempPort;                //holds ports temporarly
int startingPort;               //stores the starting range of port number
int status;                     //receives the status of your pc address
char currentPort[10];           //holds current port

//program description
void progDesc()
{
     cout<<"This is a simple port scanner, scan range of\n";
     cout<<"ports on your local machine...\n"<<endl;

}

//call host (local machine) name
void callHostName()
{
	size_t pcNameSize;     				  //holds size of the name
	int hostNameStatus; 				  //holds status of the function whether it succeeded or failed
	hostNameStatus = gethostname(pcName, pcNameSize); //gethostname grabs your pc name & pass it to pcName
	
	//make sure function host name returned successfully
	if(hostNameStatus == -1 )
	{
		cout<<"Error: could not locate your host name\n";
		exit(1);
	}

	//print machine name
	cout<<"Your Machine Name: ";
	cout<<pcName<<endl;
	cout<<endl;

	//deallocate memory
	//delete[] pcName; - i put it inside main()

}

//getaddrinfe locates your machine, more specific
//details of your host address is returned to results. 
void getAddrIn()
{

	status = getaddrinfo(pcName, currentPort, &hints, &results);

	if(status != 0)
	{
		fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
		exit(1);
	}
}

//get local IP which is usually version 4
void grabLocalIP()
{
	//carries your local IP
	char ipString[INET6_ADDRSTRLEN];
	struct addrinfo *p;
	
	cout<<"IP Address for "<<pcName<<" is of type ";

	//print all your local IP Addresses
	for(p = results; p != NULL; p = p->ai_next)
	{
		void *addr;
		string ipVer;
		if(p->ai_family == AF_INET)
		{
			struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
			addr = &(ipv4->sin_addr);
			ipVer = "IPv4";

		}else{
			struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
			addr = &(ipv6->sin6_addr);
			ipVer = "IPv6";
		}

		inet_ntop(p->ai_family, addr, ipString, sizeof ipString);
		cout<<""<<ipVer<<": ";
		printf("%s\n", ipString);
		cout<<endl;
	}	
}

//convert a port from int to *char, so it can be passed into getaddrinfo()
//call this function before the loop starts
void convertPortsToString()
{
	//convert startingPort to string. After that, convert the string into *char...
	//string tempPort;  make it static 
	stringstream out;
	out<<startingPort;
	tempPort = out.str();    //tempPort carries startingPort in string format

	//convert tempPort to *char - currentPort going to be passed into getaddrinfo()
	//char currentPort[10];  - i made it static
	strcpy(currentPort, tempPort.c_str());

}

//convert a port from int to *char, so it can be passed into getaddrinfo()
//call this function at the bottom of the loop
void convertIncrementedPortsToString()
{
	//you have to declare out in every loop, otherwise the 
	//incremented ports will concatenated with the previous ports.
	stringstream out;
	out<<startingPort;
	tempPort = out.str();    		//tempPort carries startingPort in string format
	strcpy(currentPort, tempPort.c_str());  //convert the string to *char

}

//Run the program
int main()
{
	int endingPort;                 //stores the ending rage of port number

	system("clear");

	//tell user what program does
	progDesc();

	//grab the name of the local machine (host)
	callHostName();
	
	//set size of hints to zero
	memset(&hints, 0, sizeof hints);

	//fill some of your host address info
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	//call getaddrinfo() to output to "results"
	//so you can grab the local IP from results
	getAddrIn();

	//grab your machine local IP
	grabLocalIP();

	//ask port range from user
	cout<<"Enter Starting Port: ";
	cin>>startingPort;

	cout<<"Enter Ending Port: ";
	cin>>endingPort;

	cout<<endl;

	cout<<"Start Checking: "<<endl;

	//convert a port from int to *char, so it can be passed into getaddrinfo()
	convertPortsToString();

	//check the status
	while(startingPort <= endingPort)
	{
		//call getaddrinfo()
		getAddrIn();

		//create a socket.
		int socketfd;
		socketfd = socket(results->ai_family, results->ai_socktype, results->ai_protocol);

		if(socketfd == -1 )
		{
			cout<<"Error: failed to create a socket.\n";
			return 2;
		}

		//connect to your own IP address in every loop with 
		//a new port, connect() will associate your socket
		//with the "current port number & your local IP address".
		int connectStatus;
		connectStatus = connect(socketfd, results->ai_addr, results->ai_addrlen);
	
		if(connectStatus == -1 )
		{
			cout<<"Port "<<currentPort<<" is Closed or Blocked.\n";
		}else{
			cout<<"Port "<<currentPort<<" is OPEN.\n";
		}

		close(socketfd);
	
		//move to the next port in the specified range
		startingPort++;

		/***convert the incremented port to *char***/
		convertIncrementedPortsToString();
	}

	//deallocate memory
	delete[] pcName;

	//free linkedlist of struct addrinfo *results 
	freeaddrinfo(results);

	return 0;
}


Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
This is a simple port scanner, scan range of
ports on your local machine...

Your Machine Name: Toshiba-Laptop

IP Address for Toshiba-Laptop is of type IPv4: 127.0.1.1

Enter Starting Port: 1
Enter Ending Port: 5

Start Checking: 
Port 1 is Closed or Blocked.
Port 2 is Closed or Blocked.
Port 3 is Closed or Blocked.
Port 4 is Closed or Blocked.
Port 5 is Closed or Blocked.


I'm just wondering why all ports show as Closed?!!! I tried to put ports from 1 to 3000, all closed! i think i have lots of ports open in my OS - i do not know - maybe!
I tested this program under linux ubuntu...

Any idea?!
Oct 22, 2008 at 12:50am
You shouldn't really have any ports open unless your running services.
Oct 22, 2008 at 3:57am
Yeah...the ports could also be seen closed/blocked unless they are actively sending/receiving data. Although I haven't really checked this, so it could be wrong.
Oct 22, 2008 at 6:56pm
Just a note. It looks as though you are actually making a connection to your target. This is definitely a bad approach as it's considered an "active" port-scan and readily detectable.
Topic archived. No new replies allowed.