Jump to content

Source Code: Directory listings && Search!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
These are just some things I found useful in my time working with PHP. The first file just helps you get a listing of the directory with links, and to view the source code of anything. The second helps you search for certain file extensions like ".php" and ".phtml". Plenty of comments but if you guys have questions please feel free to ask.

<?PHP

function uplevel($dir) {

	$dir = explode('/', $dir);

	$ndir = "/";

	for($i =1; ; $i++) {

		if($i==(count($dir)-1)) { break; }

		$ndir .= $dir[$i].'/';

	}

	//just returns a link to the root and to go up a directory ".."

	return '<a href="?dir=/">Root</a> - <a href="?dir='.$ndir.'">^Dir</a>';

}

function dirlisting($dir) {

	//prints out a list of the direcotyr

	if(substr($dir, strlen($dir)-1, 1) != "/") { $dir .= '/'; }

	$files = scandir($dir);

	$echo = "<pre>Files is this Directory:\n";

	foreach($files as $file) {

		//prints with links =)

		$echo .= "\t<a href=\"?dir=".$dir.$file."\">$file</a>\n";

	}

	return $echo."</pre>";

}

function readcontents($file) {

	//if a file is selected then print its contents in a pretty textbox

	if (is_readable($file)) {

		$file = explode("\n", file_get_contents($file));

	} else {

		$file[] = "Permission denied";

	}

	//technically not read-only but save that for another tut

	$echo = "<pre><center>--Read-Only--</center>\n<textarea style='width:100%;height:700px;'>";

	foreach($file as $line) {

		//broke it up by line because just printing it out wouldnt be formatted

		$echo .= $line."\n";

	}

	return $echo."</textarea></pre>";

}

if($_GET['dir']=="") {

	//if directory isnt set go to default:

	$dir = '/';

} else {

	//else go here =)

	//notice: no filtering!

	$dir = $_GET['dir'];

}

//start $echo out

echo uplevel($dir);

if(is_dir($dir)) {

	//if its a directory go to this function

	echo dirlisting($dir);

} elseif(file_exists($dir)) {

	//files? no problem!

	echo readcontents($dir);

}

?>
<?PHP

function listallfiles($dir, $ext) {

	//check if we have permissions first

	if(is_readable($dir)) {

		//scandir in php puts all items into an array

		$files = scandir($dir);

		//loop through files

		foreach($files as $file) {

			//if its a directory use recursion

			if(is_dir($dir.$file)) {

				//make sure its not .. or .

				if(substr($file, -1, 1) != ".") {

					//recursive functions rule!

					$folders .= listallfiles($dir.$file."/", $ext);

				}

			} else {

				//if not a directory see if the last part of the file

				//has $ext

				$start = strlen($ext) - (strlen($ext)*2);

				$end = strlen($ext);

				if(substr($file, $start, $end) == $ext) {

					//if so were gonna add it to the variable

					$files .= $dir.$file."\n";

				}

			}

		}

		//lastly we print it all out

		return $folders.$files;

	}

}

$file = $_GET['q'];

$ext = $_GET['e'];

if(!is_dir($file)) {

	//default directory

    $file = "/";

}

if(strlen($ext)==0) {

	//default extension

	//may want to upgrade to enable more than one

	//example: .php .phtml

    $ext = ".php";

}

echo "\n<br /><br />";

echo '<form method="get"><input style="width:350px;" type="text" name="q" value="'.$file.'"><input style="width:50px;" type="text" name="e" value="'.$ext.'"><input type="submit" value="Search!"></form>';

echo "<pre>All {$ext} files is {$file}:\n";

//starts the function

echo listallfiles($file, $ext);

Disclaimer: All code is given as is. I accept no responsibility how you implement it any way or form. Please express caution when using any code given to you. Use at your own risk.

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I have to say, scandir is a very neat function. What project did you develop this for?

#3
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Originally when exploiting a vulnerability. Now I'm using it basically using it to get more familiar with the files on a certain server. It beats downloading them all and opening them one by one to view the source. lol

#4
amrosama

amrosama

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 8,674 posts
how can i protect myself from this? Posted via CodeCall Mobile

#5
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Physical access = complete access

Take away physical access - put your computer in a big 10ft thick box, chain it up, and throw it into the middle of the ocean.

:D

#6
Tamer

Tamer

    Newbie

  • Members
  • Pip
  • 1 posts
>> and throw it into the middle of the ocean
great =)