Last active
December 11, 2017 02:16
-
-
Save ghabxph/140e08165a07f3fd98aea804f37cd62d to your computer and use it in GitHub Desktop.
File Searcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('DS', DIRECTORY_SEPARATOR); | |
class FileSearcher | |
{ | |
private $sRootPath; | |
private $aPhpFiles = []; | |
public function __construct($sRootPath) | |
{ | |
$this->sRootPath = $sRootPath; | |
} | |
public function scan($sPath = null) | |
{ | |
$sPath = is_null($sPath) ? $this->sRootPath : $sPath; | |
$aFiles = scandir($sPath); | |
array_shift($aFiles); | |
array_shift($aFiles); | |
foreach ($aFiles as $sFile) { | |
$sFile = $sPath . DS . $sFile; | |
if (is_dir($sFile)) { | |
$this->scan($sFile); | |
} else if (is_file($sFile) && pathinfo($sFile, PATHINFO_EXTENSION) === 'php') { | |
$this->aPhpFiles[] = $sFile; | |
} | |
} | |
} | |
public function findString($aString) | |
{ | |
$aFound = []; | |
foreach ($this->aPhpFiles as $sPhpFile) { | |
echo 'Scanning: ' . $sPhpFile . PHP_EOL; | |
$aLines = file($sPhpFile); | |
$iLine = 0; | |
foreach ($aLines as $sLine) { | |
$iLine++; | |
foreach($aString as $sString) { | |
$iPos = strpos($sLine, $sString); | |
if ($iPos !== false) { | |
$iPos++; | |
$aFound[] = "$sPhpFile:($iLine,$iPos):" . trim($sLine); | |
} | |
} | |
} | |
} | |
return $aFound; | |
} | |
private function getFiles() | |
{ | |
return $this->aPhpFiles; | |
} | |
} | |
$sRootPath = getcwd(); | |
$oFileSearcher = new FileSearcher($sRootPath); | |
echo "Traversing $sRootPath directory..."; | |
$oFileSearcher->scan(); | |
$aFound = $oFileSearcher->findString(['eval(', 'eval (', 'system(', 'system (']); | |
$sFound = implode(PHP_EOL, $aFound); | |
echo 'Writing report in scan-report.txt'; | |
file_put_contents('C:\Users\devpc\Desktop\scan-report.txt', $sFound); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment