Image
 
linkedin_logo.png rss_logo.jpg
twitter_logo.png youtube_logo.jpg
Latest Additions
 
EH-Net Login
Welcome Guest.






Lost Password?
No account yet? Register
Who's Online
We have 53 guests and 3 members online
 
Free Business and Tech Magazines and eBooks

You are here: Home arrow Ethical Hacking Discussions and Related Certificationsarrow Programmingarrow [C ] Simple TCP Port Scanner Coded by Me
EH-Net
May 18, 2013, 04:31:24 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Go back to The Ethical Hacker Network Online Magazine Home Page
 
   Home   Help Calendar Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: [C ] Simple TCP Port Scanner Coded by Me  (Read 5824 times)
0 Members and 1 Guest are viewing this topic.
billy786
Newbie
*
Offline Offline

Posts: 5


View Profile WWW
« on: June 17, 2008, 08:24:23 PM »

This is a simple port scanner coded in c. It uses a simple socket and a for loop. The port scanner uses TCP Connect  to check whether the port is opened or closed.

This is for beginners who are trying to grasp simple sockets in C.

By the way this is for linux platform you can easily compile this on win32 using cygwin.

/* A TCP port scanner created by billy www.softhardware.co.uk*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <errno.h>


/* Main programs starts*/
int main(int argc, char **argv)
{
   int   sd;         //socket descriptor
   int    port;         //port number
   int   start;         //start port
   int    end;         //end port
   int    rval;         //socket descriptor for connect   
   char    responce[1024];      //to receive data
   char   *message="shell";       //data to send
   struct hostent *hostaddr;   //To be used for IPaddress
   struct sockaddr_in servaddr;   //socket structure
   
   if (argc < 4 )
   {
      printf("------Created By www.Softhardware.co.uk-----------\n");
      printf("--------------------------------------------------\n");
      printf("Usage: ./tscan <IPaddress> <Start Port> <End Port>\n");
      printf("--------------------------------------------------\n");
      return (EINVAL);
   }
   start = atoi(argv[2]);
   end   = atoi(argv[3]);
   for (port=start; port<=end; port++)
   {

         //portno is ascii to int second argument     

   sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //created the tcp socket
   if (sd == -1)
   {
     perror("Socket()\n");
     return (errno);
   }   

   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);
     
   //below connects to the specified ip in hostaddr
   
 

   rval = connect(sd, (struct sockaddr *) &servaddr, sizeof(servaddr));
   if (rval == -1)
   {
   printf("Port %d is closed\n", port);
   close(sd);
   }
   else
   printf("Port %d is open\n",port);
   
   close(sd);         //socket descriptor
   }
   
}
 
thanks
 Grin
Logged

shakuni
Jr. Member
**
Offline Offline

Posts: 80


View Profile
« Reply #1 on: June 18, 2008, 12:33:20 AM »

@billy786: Make it multithreaded.

@others:Read beej's guide to understand socket programming in C.
http://beej.us/guide/bgnet/output/html/multipage/index.html

-shakuni
Logged

There is no rule, law or tradition that apply universally... including this one.
Andrew Waite
Hero Member
*****
Offline Offline

Posts: 928



View Profile WWW
« Reply #2 on: June 18, 2008, 03:47:35 AM »

Billy786,

looks promising, might make a nice base for a project I've had in mind for a while. Thanks for sharing.

@billy786: Make it multithreaded.

I'd suggest this could be left as 'an excercise for the reader'. As Billy states this is good for beginners trying to grasp the basics, too many advanced features may just confuse matters (aka me Wink )
Logged

geekyone
Full Member
***
Offline Offline

Posts: 180



View Profile
« Reply #3 on: June 18, 2008, 04:03:23 PM »

@Billy786: Thanks for the post!

@shakuni: Thanks for the link!

Looks like great information for a beginning programmer like me.   Smiley
« Last Edit: June 18, 2008, 04:07:28 PM by geekyone » Logged

CISSP, CEH, GPEN, GCIH, GCFA
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
Joomla Bridge by JoomlaHacks.com
Valid XHTML 1.0! Valid CSS!
Page created in 0.07 seconds with 23 queries.
 
Exclusive Deal

sansfire13_245x90_cw90.jpg
SANSFIRE 2013
June 15 - 22

5% Off w/ Code: EHN_5

SANS Deals 4 EH-Netters
5% OFF Any SANS Course in Any Format!
Coupon Code: EHN_5 Including SANS Rocky Mountain 2013 & SANS Boston 2013
Polls
Compared to this year, 2013 will be:
 
Recent Forum Topics
EH-Net News Feeds
Latest Additions
 
         
Advertisement

© 2013 The Ethical Hacker Network
Joomla! is Free Software released under the GNU/GPL License.