+ Reply to Thread
Results 1 to 6 of 6

Thread: CLI Port Sniffer

  1. #1
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    CLI Port Sniffer

    Using fsockopen(), one of PHP's network functions, you have the ability to connect to a remove (or local) server. We will use this function to create a port sniffer, which will enable you to see what ports are open on any server.

    Simply enough, to connect to a server, on port 80, you will use the following code:
    Code:
    $fp fsockopen("www.example.com"80$errno$errstr30); 
    The first argument is the hostnam,e which can be a domain name or an ip address. The section argument is the port which we are connecting to. $errno holds the system level error number and $errstr is the error message (if an error occurred). The last argument is the connection timeout in seconds. To create the most simple port sniffer, we can just place the socket connection within a loop:
    Code:
    <?php
    for($i 1$i 1000$i++) {
        
    $fp = @fsockopen("www.example.com"$i$errno$errstr1);
        if (
    $fp) {
            echo 
    "The port $i is open.\n";
            
    fclose($fp);
        }
    }
    ?>
    This of course only checks the first 999 ports, and will fail if a connection was not established within one second. This can be ran via the command line, or in the browser. However, if it is ran in the browser, the script will need to finish executing prior to you seeing the results.

    We can make this script more command line friendly by either allowing arguments to be passed in, or prompting the user for the required values. We will first start with arguments.

    PHP defines the variable $_SERVER['argv'] for this reason. This variable is an array, so each argument (which are separated by a space) will be contained in the array. In relation to this variable, $_SERVER['argc'] returns the number of arguments supplied (which can be used for error checking). The script itself is considered an argument, so $_SERVER['argc'] will always be greater than or equal to one. In our script, three settings can varied, the minimum port number, the maximum port number, and the hostname. We will use these as arguments.
    Code:
    <?php
    for($i $_SERVER['argv'][2]; $i $_SERVER['argv'][3]; $i++) {
        
    $fp = @fsockopen($_SERVER['argv'][1], $i$errno$errstr1);
        if (
    $fp) {
            echo 
    "The port $i is open.\n";
            
    fclose($fp);
        }
    }
    ?>
    To make the script a little better, we will implement some error prevention.
    Code:
    <?php
    $min 
    = empty($_SERVER['argv'][2]) ? $_SERVER['argv'][2];
    $max = empty($_SERVER['argv'][3]) ? 1000 $_SERVER['argv'][3];
    $host = empty($_SERVER['argv'][1]) ? die("No host provided\n"
                                       : 
    $_SERVER['argv'];

    for(
    $i $min$i $max$i++) {
        
    $fp = @fsockopen($host$i$errno$errstr1);
        if (
    $fp) {
            echo 
    "The port $i is open.\n";
            
    fclose($fp);
        }
    }
    ?>
    Now by simply running this command
    john@earth:~$ php sniffer.php example.com 1 100
    You will sniff the ports between 1 and 100 on example.com.

    Lastly we can prompt users for values rather than requiring them to supply them as arguments. To do this, we use the constant STDIN. Most of the time it is defined, however, in the event it is not already defined, we will emulate it using fopen which can be seen in the first three lines of the code below. To grab content from the command line, we use fread.
    Code:
    <?php
    if(!defined("STDIN")) {
        
    define("STDIN"fopen('php://stdin','r'));
    }

    echo 
    "Host Name: ";
    $host fread(STDIN80);
    echo 
    "Min: ";
    $min fread(STDIN80);
    echo 
    "Max: ";
    $max fread(STDIN80);
    This code will prompt the user for the three necessary values. However, they are all strings. Since the ports need to be integers, we can simply cast them. Moreover, the strings main contain extra garbage (line feeds) so we need to trim() $host. Finally we are left with the following code:
    Code:
    <?php
    if(!defined("STDIN")) {
        
    define("STDIN"fopen('php://stdin','r'));
    }

    echo 
    "Host Name: ";
    $host fread(STDIN80);
    echo 
    "Min: ";
    $min fread(STDIN80);
    echo 
    "Max: ";
    $max fread(STDIN80);
    $host trim($host);

    for(
    $i = (int)$min$i < (int)$max$i++) {
        
    $fp = @fsockopen($host$i$errno$errstr1);
        if (
    $fp) {
            echo 
    "The port $i is open.\n";
            
    fclose($fp);
        }
    }
    ?>
    Running this script produces the following output:
    john@earth:~$ php sniffer.php
    Host Name: localhost
    Min: 1
    Max: 100
    The port 23 is open.
    The port 80 is open.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: CLI Port Sniffer

    Very nice tutorial! Were you able to figure out how to use sockets with GTK yet?

  4. #3
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: CLI Port Sniffer

    No, my LAMP is screwd up. I do intend on fixing it tonight, and playing with some sockets though.

  5. #4
    Jordan Guest

    Re: CLI Port Sniffer

    I haven't started reading the PHP-GTK+ (Pro) book I bought but once I do we should start a project together. I'm not sure what anyone would actually use since you need PHP and GTK installed before you can use it but it would be fun.

  6. #5
    Join Date
    Sep 2008
    Posts
    20
    Rep Power
    0

    Re: CLI Port Sniffer

    its very good tutorial for to use sockets.

  7. #6
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: CLI Port Sniffer

    Quote Originally Posted by adserverexpert View Post
    its very good tutorial for to use sockets.
    Thank you. If you have any questions regarding the code, feel free to ask.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. sniffer
    By prashant6388 in forum C# Programming
    Replies: 1
    Last Post: 12-04-2009, 03:25 AM
  2. packet sniffer
    By ged25 in forum C# Programming
    Replies: 6
    Last Post: 09-06-2009, 04:22 AM
  3. Sniffer Scripts
    By kavinpparker in forum Search Engine Optimization
    Replies: 1
    Last Post: 12-29-2007, 10:57 AM
  4. Packet Sniffer
    By DevilsCharm in forum Computer Software/OS
    Replies: 9
    Last Post: 12-30-2006, 09:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts