|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4 
|
Getting the process that's opening port x?
Hey guys, currently, I've written a port scanner, and need a little help. If I do a scan on 127.0.0.1 with the start port 10 and the endport 30, then it scans, and I found an open port! Is there anyway to determine what process is using this open port in C++? If you'd like, here is the source... Of what I have so far:
#include <cstdlib>
#include <iostream>
#include <winsock.h>
#include <conio.h> // including this for only getch()...
#include <windows.h>
#include "colors.h"
using namespace std;
static int __BACKGROUND = BLACK;
static int __FOREGROUND = LIGHTGRAY;
void textcolor(int color)
{
__FOREGROUND = color;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color + (__BACKGROUND << 4));
}
void textbackground(int color)
{
__BACKGROUND = color;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), __FOREGROUND + (color << 4));
}
int main()
{
SOCKET ehsocket;
char ip[50];
cout << "Enter an Ip address to scan: " << endl;
cin.getline(ip, 50);
char astartport[30];
cout << "Enter the starting port to scan: " << endl;
cin.getline(astartport, 30);
char aendport[30];
cout << "Enter the ending port to scan: " << endl;
cin.getline(aendport, 30);
int startport = atoi(astartport);
int endport = atoi(aendport);
WORD sockversion;
WSADATA wsadata;
sockversion = MAKEWORD(1, 1);
WSAStartup(sockversion, &wsadata);
cout << "Now scanning..." << endl << endl;
for (int n = startport; n <= endport; n++)
{
SOCKET ehsocket;
int ehconnect;
SOCKADDR_IN info;
info.sin_family = AF_INET;
info.sin_addr.s_addr = inet_addr(ip);
info.sin_port = htons(n);
ehsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ehconnect = connect(ehsocket, (SOCKADDR *)&info, sizeof(SOCKADDR_IN));
if (ehconnect != 0)
{
textcolor(RED);
cout << "Ip: " << ip << " Port: " << n << " Status: Closed" << endl;
}
else
{
textcolor(GREEN);
cout << "Ip: " << ip << " Port: " << n << " Status: Open" << endl;
}
}
textcolor(WHITE);
cout << endl << "Press any key to exit...";
getch();
closesocket(ehsocket);
WSACleanup();
} And... Here is "textcolor.h":
//textcolor.h
#ifndef TEXTCOLOR_H
#define TEXTCOLOR_H
void textcolor(int);
void textbackground(int);
typedef enum
{
BLACK,
BLUE,
GREEN,
CYAN,
RED,
MAGENTA,
BROWN,
LIGHTGRAY,
DARKGRAY,
LIGHTBLUE,
LIGHTGREEN,
LIGHTCYAN,
LIGHTRED,
LIGHTMAGENTA,
YELLOW,
WHITE
} COLORS;
#endif // TEXTCOLOR_H So, any idea on how I would do this...? Thanks! 
|