Thread: Port Scanner

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    under your bed....
    Posts
    24

    Port Scanner

    Hey hey,

    This is my first post, also i am new to c programming so if i make a mistake or if i've posted this in the wrong section or something please correct me ;P.

    So at tech today i was reading an online c manual and thought it would be pretty cool make to a half decent proggy.

    Here is what I wrote and i was wonder if someone could give me afew links or some explain source code to me on how port scanning works.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int checkPort()
    {
        // Loop scans port 3 times
    	int i = 3;
    	while (i > 0){
    		// Scan port; 
    		// record results in;
    		--i;
    	}
    }
    
    int main()
    {
    	// Inputs port number
    	int iPort;
    	cout << "Enter port you want to scan: ";
    	cin >> iPort;
    
    	// Scans the port
    	checkPort();
    
    	// Prints result
    	cout << "Port Result: " << endl;
    	cout << "1. " << endl;
    	cout << "2. " << endl;
    	cout << "3. " << endl;
    
    	system("Pause");
    	return 0;
    }
    What I need help with is how i would go about scanning a port in the while loop.

    Cheers in advance.

  2. #2
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Well... If you're new to C, maybe you jumped a little too far ahead.

    - If you're planning to scan a remote machine for open ports, you will need to send packets.
    To send packets you will need to learn network programming, sockets; I suggest you read this document first: beej.us/guide/bgnet/output/print/bgnet_A4.pdf

    - Now to the interesting part, the scanning itself.
    Nmap, in my opinion is the best scanner on the network, it's open source and applies different simple and complex methods to scan a remote machine, read about them here:
    http://insecure.org/nmap/nmap_doc.html

    Please note, that for 90% of the methods you will need to go a lot deeper, and be able to construct your own raw packets: Raw socket/network programming.

    - I would also suggest you learn about networking, how the whole thing works.
    The structure of the TCP/IP, how the packet travels from one machine to another.
    What kind of devices are there on the network, what do they do with the packet, etc.

    - And one last thing, get yourself a good sniffer, I suggest Ethereal.
    You will thank me once you start working...

    Goodluck.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  3. #3
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    it may intrest you to know that the code you posted is c++ and not c.

  4. #4
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by kryptkat
    it may intrest you to know that the code you posted is c++ and not c.
    details, details
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Location
    under your bed....
    Posts
    24
    Devil Panther thanks you alot for your reply. I have used WPE sniffer before due to my laziness in online games ^_^. ill read these link u gave to me and try to understand them .

  6. #6
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    To tell you the truth, the material is not hard... But without any knowledge in Networking it might be a little difficult.

    BTW, here is a link to Raw Socket/Network Programming:
    http://www.madchat.org//coding/c/c.r...raw_socket.txt

    Anyway, good luck.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  7. #7
    geek Whiteghost's Avatar
    Join Date
    Aug 2005
    Posts
    19
    hear a good book on network programming call: Unix network programming by W.Richard Stevens.

  8. #8
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    That's quite a strange piece of code as the inputted port number is never passed to the function responsible of scanning the port.
    Port scanning is pretty simple, connect to the remote adress at the specified port. Was the connection successful? If so then the port is open for business and you close the connection. Otherwise it's closed or an error occurred. To find out which check the error messages, a "Connection Refused" would mean that the port is closed.

  9. #9
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by OnionKnight
    That's quite a strange piece of code as the inputted port number is never passed to the function responsible of scanning the port.
    Port scanning is pretty simple, connect to the remote adress at the specified port. Was the connection successful? If so then the port is open for business and you close the connection. Otherwise it's closed or an error occurred. To find out which check the error messages, a "Connection Refused" would mean that the port is closed.
    This method is logged.
    plus it's slow like hell.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Devil Panther
    This method is logged.
    So? You are only using this as an exercise in network programming anyway, aren't you? Or perhaps to test your own server for security. In both cases, logging doesn't matter.


    RIGHT?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Quote Originally Posted by Devil Panther
    This method is logged.
    plus it's slow like hell.
    Maybe opening multiple sockets (in unblocking mode) and use select() to listen them is the fastest way. (if you consider the source of first version of nmap, this is the method used).

    And, in insecure.org you can find information about port scanning.

  12. #12
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by fnoyan
    Maybe opening multiple sockets (in unblocking mode) and use select() to listen them is the fastest way. (if you consider the source of first version of nmap, this is the method used).

    And, in insecure.org you can find information about port scanning.
    my friend, nmap went a long way since the fist version;
    And it's here to teach us more, as you said... and as I said a few posts back.

    As for the Logging, if you really wish to test your own logging, then you'll be doing a horrible job because if you really believe that the people who want to attack your poor network are going to scan you with traditional methods then you have another thing coming. ENJOY!
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A more advanced port scanner
    By fssp in forum C Programming
    Replies: 6
    Last Post: 03-23-2009, 01:14 AM
  2. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 01:58 PM
  3. My TCP Port Scanner in C
    By billy786 in forum Networking/Device Communication
    Replies: 5
    Last Post: 06-28-2008, 07:12 PM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  5. Basic port scanner code .. pls help ???
    By intruder in forum C Programming
    Replies: 18
    Last Post: 03-13-2003, 08:47 AM