LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2008, 01:38 PM   #1
tnjones
LQ Newbie
 
Registered: Aug 2008
Posts: 27

Rep: Reputation: 15
How to write a port scanner in C++ for Linux


Hello,
I have this project where I have to implement a simple port scanner in C++ for Linux. The program should take as input a target IP address, beginning
port number, and ending port number.After recieving the above info, the program should display a list of open ports. This seems not too bad but I have NO IDEA where to begin to implement a port scanner. By any chance can anyone direct me in the right path! Any response/advice will be helpful.

Thanks in Advance
 
Old 10-03-2008, 01:49 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Sorry I do not think many people will answer that question here due what can be done with the information.
 
Old 10-03-2008, 02:38 PM   #3
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
The best known port scanner I can think of is nmap of course. Take a look into it and do a google search for "open source port scanner" or something like that. You might find something light enough to provide a starting point. Cheers
 
Old 10-04-2008, 10:12 PM   #4
jgallo
Member
 
Registered: Sep 2008
Posts: 39

Rep: Reputation: 15
this would be sweet

Linux Archive

Last edited by jgallo; 10-12-2008 at 02:00 AM.
 
Old 10-05-2008, 07:16 PM   #5
loperz7
Member
 
Registered: Sep 2008
Posts: 53

Rep: Reputation: 15
Read about sockets in linux?
A port scanner isn't a big threat unless it's something many people worked on and improved, since the most basic usually doesn't go very far:
check port #1, check port #2, check port #3 etc.
Most firewalls and security admins should catch that pretty easily.

Debian

Last edited by loperz7; 10-10-2008 at 05:29 AM.
 
Old 10-05-2008, 07:39 PM   #6
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
Simply "connect()" to a port; if it succeeds, then the port is open; if it fails, the reason for failure may give a clue why it failed (no listener, blocked by firewall, whatever).

On the networks I work on, if you ever tested such a thing you'd be in trouble; I don't know of anyone who's not checking for port scans.
 
Old 10-07-2008, 04:18 PM   #7
tnjones
LQ Newbie
 
Registered: Aug 2008
Posts: 27

Original Poster
Rep: Reputation: 15
Pinniped,
Thanks!! However, how do you check ports in C++ in Linux. I think if I knew how to even access ports I think I could do this. Any advice/example will be greatly appreciated.
Thanks in Advance!!
 
Old 10-07-2008, 04:39 PM   #8
dlinux
Member
 
Registered: Jan 2007
Location: Portugal
Distribution: Slackware 13.0
Posts: 52

Rep: Reputation: 15
tnjones:
man socket
man connect

You fill some sockaddr_in or sockaddr_in6 structs (for IPv6) with the port and address and pass it to connect,if it returns 0 then the port is open.

something like:
Code:
struct sockaddr_in in;
memset (&in, 0x00, sizeof (in));

in.sin_family = AF_INET;
in.sin_port = htons (PORTGOESHERE);
inet_pton (AF_INET, "xxx.xxx.xxx.xxx", &in.sin_addr, sizeof (struct in_addr));
 
Old 10-08-2008, 04:43 PM   #9
froguy
LQ Newbie
 
Registered: Sep 2003
Location: Ottawa, Canada
Distribution: Ubuntu
Posts: 17

Rep: Reputation: 0
you can thread each connect() attempt to make to scan faster.

The nmap manual also talks in detail about how it does SYN scanning.

As for this type of program being 'dangerous', I imagine nmap is a much better product that's distributed with most major Linux distributions. Besides, as has been stated, any half decently competent sys admin will be monitoring for port scans.
 
Old 10-08-2008, 04:48 PM   #10
tnjones
LQ Newbie
 
Registered: Aug 2008
Posts: 27

Original Poster
Rep: Reputation: 15
How to open multi sockets at once in C++

Hello,
I have this project where I have to implement a simple port scanner in C++ for Linux. The program should take as input a target IP address, beginning port number, and ending port number.After recieving the above info, the program should display a list of open ports. At this time I have it doing the above through a socket but it takes a very long time to search a wide range of ports. I was told that the run time would greatly decrease if I had several sockets open. However, I am confused about how to open several sockets at once. Any advice/input would greatly be appreciated.

Thanks in Advance
 
Old 10-08-2008, 04:54 PM   #11
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
You could do it with select()
http://www.manpagez.com/man/2/select/
http://www.lowtek.com/sockets/select.html

Cheers
 
Old 10-09-2008, 10:07 PM   #12
froguy
LQ Newbie
 
Registered: Sep 2003
Location: Ottawa, Canada
Distribution: Ubuntu
Posts: 17

Rep: Reputation: 0
Quote:
Originally Posted by tnjones View Post
Hello,
I have this project where I have to implement a simple port scanner in C++ for Linux. The program should take as input a target IP address, beginning port number, and ending port number.After recieving the above info, the program should display a list of open ports. At this time I have it doing the above through a socket but it takes a very long time to search a wide range of ports. I was told that the run time would greatly decrease if I had several sockets open. However, I am confused about how to open several sockets at once. Any advice/input would greatly be appreciated.

Thanks in Advance
Threading. Start up a thread for each socket.

I've used pthread.. but it's a C library, so it won't play well with your object oriented design. zthread (http://zthread.sourceforge.net/) was another library I found out about later, no personal experience with it though.
 
Old 10-10-2008, 02:43 AM   #13
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
Quote:
Originally Posted by froguy View Post
Threading. Start up a thread for each socket.

I've used pthread.. but it's a C library, so it won't play well with your object oriented design. zthread (http://zthread.sourceforge.net/) was another library I found out about later, no personal experience with it though.
Threads will not help at all on a uniprocessor and even on a multiprocessor machine the benefits of threading a port scanner are dubious. The best way to scan faster is to open a lot more ports at once, which means calling socket() and connect() many times in one pass. Managing so many open sockets at once is slightly more complex with a single thread than with multiple threads, but it does not have the overhead (context switching) of the multithread approach. Using a single thread does force you to think of sensible ways to manage the many sockets and their states, while the code looks slightly neater with the multithread approach but also forces you to use IPC mechanisms.

"pthread" will work with C++ just as it works with C.
 
Old 10-12-2008, 10:11 AM   #14
tnjones
LQ Newbie
 
Registered: Aug 2008
Posts: 27

Original Poster
Rep: Reputation: 15
pinniped,
Thanks! By any chance can you give me an example on how to go about doing what you stated(The best way to scan faster is to open a lot more ports at once, which means calling socket() and connect() many times in one pass.). This is my first time working with sockets and don't fully understand. Below is what I have so far, it just opens one socket and see if it is open or not:
Code:
for (port=start; port<=end; port++)
   {

soc = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //created the tcp socket



    fcntl(soc,F_SETFL, O_NONBLOCK);
    memset( &servaddr, 0, sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_port = htons(port); //set the portno


  
   hostaddr = gethostbyname( argv[1] ); //get the ip 1st argument
  
   memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
      
  
   conres = connect(sd, (struct sockaddr*)&servaddr, sizeof(servaddr));
   if (conres == -1)
   {
   printf("Port %d is closed\n", port);
       close(soc);  
    
   }
   else
   printf("Port %d is open\n",port);
  
   close(soc);  
  }
  
    return 0;
}
Any help will be a great help!!!
 
Old 10-13-2008, 04:27 AM   #15
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
If you want examples, have a look at the source for existing port scanners. There is no simple 1-page example for this. You might even look at a network library like RakNet to see how numerous sockets are handled.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to write a vulnerability scanner scanner with Perl? nsfocus Programming 5 05-20-2008 03:23 AM
how to read and write from parallel port in linux in kernal user joecole10 Linux - Software 1 03-15-2008 02:02 PM
writing linux port scanner ilnli Programming 5 07-19-2007 07:39 PM
best port scanner To scan open port in a network tanveer Linux - Security 8 01-21-2007 08:19 PM
Port Scanner tfrye Linux - Security 1 04-18-2001 11:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration