Thursday, November 15, 2012

PHP script to search IP's and find matching URL's

For those who saw my Mobotix camera post, you probably have an idea of my love for security cameras and the like.
While trying to find more Mobotix cameras using Google, I quickly ran out of search results to check out. But I knew there had to be more out there that Google just isn't finding.

I decided there must be a simple script you could run that searches for URL's that match a particular search string (just like Google does).  But you could have it search a particular range of IP's, routinely checking one for the search string.
It wouldn't be pretty or efficient of course, but it would be simple and I could probably code it.

Let me preface this by saying, I'd never coded in PHP before, which might already be obvious.

So, my little PHP search script looks like this:

 You enter an IP address (minus the last 3 digits). This of course means that the search range is very limited.

Then you enter the IP range you want it to search. In the example above the IP's searched are:
216.172.100.1 through 216.172.100.80.

Then you enter a filename for the script to save its results to. If any valid IP's have a url that matches the search string, their header information is written to the file.

Like I said, it is very limited in what it can do, but not bad for my first PHP script, written on the fly. It's a starting place for anyone who wants to expand on it.

Here is the code. It's broken up into two files.

index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>IP Check</title>
</head>

<body>
 

<a href ="index.php">Home</a>

<p>IP Check
<p>
<p>
IP format XXX.XXX.XXX. (ex. 70.147.100.)
<p>
 X = lower number<p>
 max = upper limit <p>
 Filename to write to.<p><p><p>
 <form action="camcheck.php" method="post">

Enter IP: <input name="ip" size="12">
<p>
Enter X: <input name="x" size="3">
Enter Max: <input name="max" size="3">
<p>
 Filename: <input name="myfile" size="20">

<input type="submit" value="Start">
</form>

<p>
 <img src="magnify.jpg" alt="magnify"/>
<?php

?>
<p><p>
 </body>
</html>

The index file is basically just a bunch of html text boxes. The info captured is then used by the second file.

camcheck.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>IP Check</title>
</head>
<body>


<?php

$path = "/control/userimage.html";  // url search string
 
$myFile = $_POST['myfile'];
   
$fh = fopen($myFile, 'w') or die("Can't open file");  //open file

for ($x =(int)$_POST['x']; $x <= (int)$_POST['max']; $x++)
{
    $url= $_POST['ip'].strval($x);
    //echo $url;

    $valid = @fsockopen($url, 80, $errno, $errstr, 4);   //check url

 if (!$valid) {
     // Output Error Message, if you care
    // echo '<p>not valid<p>';
    //echo "$errstr ($errno)<br />\n";
    //$returnstring = $url."  Not Valid $errstr ($errno)> \n
    //$returnstring = "X = ".$x;
     //fwrite($fh,$returnstring);

       }
    else {
         // Output Success Message!
       fputs($valid, "\nHEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n"); //socket opened
        $headers = fread($valid, 4096);
       
     $returnstring = "\n".$url."  Valid\n";
     fwrite($fh,$returnstring);    //write valid IP to file
     fwrite($fh,$headers);         //write header info
     fwrite($fh,"\n");
     }
}
 echo "\n Program Complete. Results sent to ".$myFile;
fclose($fh);

?>
<p><p>
</body>
</html>

In the camcheck.php file, you'll see that the url search string is hard-coded into $path. It would probably be easy to make that another textbox, so you can change what url string to search for.

"/control/userimage.html" is a typical Mobotix web cam url. Which is what I created the script to find.

When you run the script, it saves the header results to a file on your webserver. 

Here are some example results:
 




216.172.104.49  Valid
HTTP/1.1 302 Found
Date: Thu, 15 Nov 2012 16:32:40 GMT
Server: Apache
Location: https://billing.dathorn.com/control/userimage.html
Connection: close
Content-Type: text/html; charset=iso-8859-1





216.172.104.50  Valid
HTTP/1.1 404 Not Found
Date: Thu, 15 Nov 2012 16:32:42 GMT
Server: LiteSpeed
Connection: close
Content-Type: text/html


216.172.104.55  Valid
HTTP/1.1 302 Found
Date: Thu, 15 Nov 2012 16:32:43 GMT
Server: LiteSpeed
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
Location: https://2/control/userimage.html


216.172.104.56  Valid
HTTP/1.1 404 Not Found
Date: Thu, 15 Nov 2012 16:32:43 GMT
Server: LiteSpeed
Connection: close
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: Thu, 15 Nov 2012 16:32:43 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
P3P: CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BUS IND PHY ONL UNI PUR COM NAV DEM STA"
Content-Type: text/html; charset=iso-8859-1
Set-Cookie: xid=788c930fc7639731a0609de479e12ae9; path=/
Set-Cookie: xid=788c930fc7639731a0609de479e12ae9; path=/; domain=www.comicjumps.com
Set-Cookie: xid=788c930fc7639731a0609de479e12ae9; path=/; domain=www.comicjumps.com
Set-Cookie: RefererCookie=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=www.comicjumps.com
Set-Cookie: store_language=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT
Set-Cookie: store_language=US; expires=Fri, 15-Nov-2013 16:32:43 GMT


I find it interesting just reading the different headers, and learning what they mean. You get an idea of what sites are located in certain IP's.
I haven't played with the script much, but I think it could be even more fun with some minor tweaks.

Right now it won't let me search more than 100 at a time before timing out. I haven't tried fixing that just yet.
Let me know if you have any hints/tips/fixes. I'd love to hear them.



No comments:

Post a Comment