Hack Forums
[PHP] Port Scanner By The Alchemist[PHP] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] Port Scanner By The Alchemist[PHP] (/showthread.php?tid=3158944)

Pages: Pages 1 2


Port Scanner By The Alchemist[PHP] - The Alchemist16 - Jan 7, 2013

Hello Hack Forums,
So, this is an online portscanner coded by me in PHP.

Port Scanners are programs used to check for any open ports in a server. It can be used by website owners to check whether any port in his/her server's is open or not. Or, it may be used by hackers for the same but with a different aim.:D

This port scanner made by me uses multiple(parallel) requests to check whether ports are open on a server or not using TCP scanning.
Since it uses multiple parallel requests, its very fast compared to other portscanners.

Save the file with a .php extension and upload it to your hosting account or your localhost and then, surf the php file.
Its important that your server supports PHP cURL.

Here's the code :
PHP Code

<?php 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
   <
title>Port Scanner</title>
 
 
   <
style type="text/css">
   
      
body
      
{
         
color: #ffffff;
         
text-shadow: 2px 2px #000000;
         
background-color: #282828;
         
font-family: Arial, Helvetica, sans-serif;
      }
     
      
pre
      
{
         
background-color: #353535;
         
border: solid 1px #505050;
      
}
     
      
input
      
{
         
font-family: Arial, Helvetica, sans-serif;
      }
     
      .
Button
      
{
         
padding: 5px 10px;
         
background: #303030;
         
border: solid #101010 1px;
         
color: #fff;
         
cursor: pointer;
         
font-weight: bold;
         
border-radius: 5px;
         -
moz-border-radius: 5px;
         -
webkit-border-radius: 5px;
         
text-shadow: 1px 1px #000;
      
}
     
      .
Input
      
{
         
border: solid #101010 1px;
         
color: white;
         
font-weight: bold;
         
padding: 3px;
         
background-color: #252525;
      
}
    </
style>
</
head>
<
body>
<
div align="center">
<
pre>
___________.__                 _____   .__           .__                      .__           __   
\__    ___
/|  |__    ____     /  _  \  |  |    ____  |  |__    ____    _____  |__|  _______/  |_ 
  
|    |   |  |  \ _/ __ \   /  /_\  \ |  |  _/ ___\ |  |  \ _/ __ \  /     \ |  | /  ___/\   __\
  |    |   |   
Y  \\  ___/  /    |    \|  |__\  \___ |   Y  \\  ___/ |  Y Y  \|  | \___ \  |  |  
  |
____|   |___|  / \___  > \____|__  /|____/ \___  >|___|  / \___  >|__|_|  /|__|/____  > |__|  
                \/      \/          \/            \/      \/      \/       \/          \/        
Port Scanner                                                                   www.HackForums.net
</pre>
<
form action="" method="POST">
Enter URL or IP : <input type="text" class="Input" name="target" value="<?php if(isset($_POST['target']))
{echo htmlentities(
$_POST['target']);} else { echo 'http://example.com';}?>" size="50" />
Enter lower limit : <input type="text" class="Input" name="lower" value="<?php if(isset($_POST['lower']))
{echo htmlentities(
$_POST['lower']);} else { echo 1;}?>" size="5" />
Enter upper limit : <input type="text" class="Input" name="upper" value="<?php if(isset($_POST['upper']))
{echo htmlentities(
$_POST['upper']);} else { echo 100;}?>" size="5" />
<
input type="submit" name="submit" class="Button" value="Port Scan" />
</
form>
<
br />
<
br />
<?
php
set_time_limit
(0);
if(isset(
$_POST['target']) && isset($_POST['upper']) && isset($_POST['lower']) 
    && isset(
$_POST['submit']) && is_numeric($_POST['upper']) && is_numeric($_POST['lower']) 
    && (
filter_var($_POST['target'],FILTER_VALIDATE_IP) || filter_var($_POST['target'],FILTER_VALIDATE_URL)))
{
      
$target = $_POST['target'];
    
$flag = 0;
    
$lower = $_POST['lower']; //set lower limit
    
$upper = $_POST['upper']; //set upper limit
    
if($lower <= 0)
    {
        
$lower = 1;
    }
    if(
$upper >= 65536)
    {
        
$upper = 65535;
    }
    
$numberof = ($upper - $lower) + 1; //number of parallel requests
    
$ar = range($lower,$upper); //putting all the ports in an array
    
for($i=0 ; $i < $numberof ; $i++)
    {
        
$ch[$i] = curl_init($target);
        
curl_setopt($ch[$i], CURLOPT_PORT, $ar[$i]);
        
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true);
    }
    
$mh = curl_multi_init();
    
// Thanks to PeopleUnderTheStairs for the idea of using multi cURL
    
for($i=0 ; $i < $numberof ; $i++)
    {
        
curl_multi_add_handle($mh,$ch[$i]);
    }
    
$running = NULL;
    do
    {
        
curl_multi_exec($mh,$running);
    }while(
$running);
    for(
$i = 0 ; $i < $numberof ; $i++)
    {
        if(!
curl_error($ch[$i]))
        {         
            
$flag = 1;
            echo 
'<span style="color: #F00;">Port '.$ar[$i].' is open</span><br />';
        }
        
curl_multi_remove_handle($mh,$ch[$i]);
        
curl_close($ch[$i]);   
    }
    
curl_multi_close($mh); //All the curl handlers and multi curl handlers are closed
    
if($flag == 0)
    {
        echo 
'<span style="color: #F00;">No open ports found between '.$lower.' and '.$upper.'</span>';
    }
}
?>
</div>
</body>
</html> 

Thanks to PeopleUnderTheStairs for giving me the idea of using multi curl handler.

PLEASE GIVE FEEDBACK!!


* - CarrotMilk - Jan 7, 2013

Just to let you know, the isset() function supports multiple parameters so you don't need a bunch of &&s in your conditionals.


* - The Alchemist16 - Jan 7, 2013

^^^ I never knew that.(how could I not know that??? DAMN)
Thanks for notifying. DAMN, how could I miss that out??!!!


* - CarrotMilk - Jan 7, 2013

Don't worry haha, a lot of people don't :')


* - Novalty - Jan 7, 2013

I just wish that empty() accepted multiple arguments :(


* - badbrains - Jan 7, 2013

(Jan 7, 2013 05:27 PM)Novalty Wrote:  I just wish that empty() accepted multiple arguments :(

PHP Code

<?php
    
function isEmpty() {
        foreach(
func_get_args() as $arg) {
            if(empty(
$arg)) {
                return 
true;
            }
        }

        return 
false;
    }

    
$e = isEmpty('a', 'a', 1);
    
var_dump($e);
?>



* - The Alchemist16 - Jan 7, 2013

^^^ I've gotta do some research on that function func_get_args()
Will be helpful in the future.


* - CarrotMilk - Jan 8, 2013

PUTS YOU STOLE MY FUNCTION FU.

But instead of empty(), you should use strlen(). It'll return 0 for an empty param (not false), but it'll return 1 for a string '0' or 0 whereas empty() would return true.


* - badbrains - Jan 8, 2013

(Jan 8, 2013 08:45 AM)CarrotMilk Wrote:  PUTS YOU STOLE MY FUNCTION FU.

But instead of empty(), you should use strlen(). It'll return 0 for an empty param (not false), but it'll return 1 for a string '0' or 0 whereas empty() would return true.

uhh, I never stole it, I've been using that function for a long time now lol.


* - CarrotMilk - Jan 8, 2013

(Jan 8, 2013 08:50 AM)PeopleUnderTheStairs Wrote:  uhh, I never stole it, I've been using that function for a long time now lol.

https://hackforums.net/showthread.php?tid=2423972&pid=21919546#pid21919546

madder than a giraffe on steroids


* - badbrains - Jan 8, 2013

(Jan 8, 2013 08:52 AM)CarrotMilk Wrote:  https://hackforums.net/showthread.php?tid=2423972&pid=21919546#pid21919546

madder than a giraffe on steroids

>implying i use inline code blocks
yah ur gay boi

neway im goin to school nao


* - BlackMotion - Jan 8, 2013

thanks for source i downloaded it to my archive


* - Zoidberg - Jan 8, 2013

Great work, I don't even know about this stuff.
Next I'd try making this update realtime with AJAX. IE: scan 1 port, wait for a response, once a response is gotten from the ajax request, write it to the page and move to the next port.


^intentional hr right here, this is not a low quality double post

Oh also if you're using cURL i'd add something in the beginning that will make the script die if cURL is not loaded.
PHP Code

<?php 
if  (!in_array('curl', get_loaded_extensions())) {
  die(
"<h2>You need to enable php_curl!</h2>");
    } 



* - badbrains - Jan 8, 2013

(Jan 8, 2013 01:49 PM)Zoidberg Wrote:  Great work, I don't even know about this stuff.
Next I'd try making this update realtime with AJAX. IE: scan 1 port, wait for a response, once a response is gotten from the ajax request, write it to the page and move to the next port.


^intentional hr right here, this is not a low quality double post

Oh also if you're using cURL i'd add something in the beginning that will make the script die if cURL is not loaded.
PHP Code

<?php 
if  (!in_array('curl', get_loaded_extensions())) {
  die(
"<h2>You need to enable php_curl!</h2>");
    } 

Why use in_array()? Why not this:

PHP Code

<?php 
if(!extension_loaded('curl')) {
throw new 
Exception('no curl :(');
} 



* - Zoidberg - Jan 8, 2013

(Jan 8, 2013 01:57 PM)PeopleUnderTheStairs Wrote:  Why use in_array()? Why not this:

PHP Code

<?php 
if(!extension_loaded('curl')) {
throw new 
Exception('no curl :(');
} 

Oh, yeah, forgot about that. Either works I guess.