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:
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:$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
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.Code:<?php
for($i = 1; $i < 1000; $i++) {
$fp = @fsockopen("www.example.com", $i, $errno, $errstr, 1);
if ($fp) {
echo "The port $i is open.\n";
fclose($fp);
}
}
?>
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.To make the script a little better, we will implement some error prevention.Code:<?php
for($i = $_SERVER['argv'][2]; $i < $_SERVER['argv'][3]; $i++) {
$fp = @fsockopen($_SERVER['argv'][1], $i, $errno, $errstr, 1);
if ($fp) {
echo "The port $i is open.\n";
fclose($fp);
}
}
?>Now by simply running this commandCode:<?php
$min = empty($_SERVER['argv'][2]) ? 1 : $_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, $errstr, 1);
if ($fp) {
echo "The port $i is open.\n";
fclose($fp);
}
}
?>You will sniff the ports between 1 and 100 on example.com.john@earth:~$ php sniffer.php example.com 1 100
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.
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(STDIN, 80);
echo "Min: ";
$min = fread(STDIN, 80);
echo "Max: ";
$max = fread(STDIN, 80);
Running this script produces the following output:Code:<?php
if(!defined("STDIN")) {
define("STDIN", fopen('php://stdin','r'));
}
echo "Host Name: ";
$host = fread(STDIN, 80);
echo "Min: ";
$min = fread(STDIN, 80);
echo "Max: ";
$max = fread(STDIN, 80);
$host = trim($host);
for($i = (int)$min; $i < (int)$max; $i++) {
$fp = @fsockopen($host, $i, $errno, $errstr, 1);
if ($fp) {
echo "The port $i is open.\n";
fclose($fp);
}
}
?>john@earth:~$ php sniffer.php
Host Name: localhost
Min: 1
Max: 100
The port 23 is open.
The port 80 is open.
Very nice tutorial! Were you able to figure out how to use sockets with GTK yet?
No, my LAMP is screwd up. I do intend on fixing it tonight, and playing with some sockets though.
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.
its very good tutorial for to use sockets.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks