示例#1
0
 public function getFiles($dirname = '')
 {
     $arr_folder = array();
     $count = 0;
     $folder = UPLOAD_DIR;
     $folder .= DIRECTORY_SEPARATOR . $dirname;
     if (is_dir($folder)) {
         $dir = new RecursiveDirectoryIterator($folder);
         $iter = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);
         while ($iter->valid()) {
             if (!$iter->isDot()) {
                 if ($iter->getDepth() < $this->maxDepth) {
                     $item = array();
                     $item['id'] = $count;
                     $item['size'] = $iter->getSize();
                     $item['type'] = $iter->getType();
                     $item['ext'] = $iter->getExtension();
                     $item['pathname'] = $iter->getSubPathName();
                     $item['filename'] = $iter->getFilename();
                     $item['parent'] = '/' . $dirname;
                     $item['Exec'] = $iter->isExecutable();
                     $item['sub'] = '';
                     $count++;
                     $arr_folder[] = $item;
                 }
             }
             $iter->next();
         }
         return $arr_folder;
     }
     if (is_file($folder)) {
         $file = file_get_contents($folder);
         if (empty($file)) {
             return ' ';
         }
         return $file;
     }
 }
示例#2
0
文件: Abstract.php 项目: robeendey/ce
 public static function build($directory)
 {
     $list = array();
     $unique = array();
     // Build
     $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::KEY_AS_PATHNAME), RecursiveIteratorIterator::SELF_FIRST);
     while ($it->valid()) {
         $key = $it->key();
         // Make sure it's unique, Skip .svn files
         if (isset($unique[$key]) || stripos($key, '.svn') !== false) {
             $it->next();
             continue;
         }
         $unique[$key] = true;
         // Add
         $subpath = $it->getSubPathName();
         // Skip dot files, package files and .svn or CVS folders
         if (!$it->isDot() && substr(basename($subpath), 0, strrpos(basename($subpath), '.')) != 'package' && basename($subpath) != '.svn' && basename($subpath) != 'CVS') {
             $key = $it->key();
             //$list[$it->getSubPathName()] = array(
             $list[] = array('path' => self::fix_path($it->getSubPathName()), 'dir' => $it->isDir(), 'file' => $it->isFile(), 'perms' => substr(sprintf('%o', $it->getPerms()), -4), 'size' => $it->getSize(), 'sha1' => $it->isFile() ? sha1_file($key) : null);
         }
         $it->next();
     }
     ksort($list);
     return $list;
 }
示例#3
0
 public function build_files()
 {
     $id = 0;
     $partial_stop = 0;
     $maxSize = 0;
     $indexStep = 0;
     $maxInserts = 0;
     $files = array();
     if (isset($_POST['partialStop'])) {
         $partial_stop = (int) $_POST['partialStop'];
     }
     //Max Filesize check
     if (isset($_POST['maxSize'])) {
         $maxSize = (int) $_POST['maxSize'];
     }
     if (!$maxSize) {
         $maxSize = 5242880;
         //Default value.
     }
     //IndexStep check
     if (isset($_POST['indexStep'])) {
         $indexStep = (int) $_POST['indexStep'];
     }
     if (!$indexStep) {
         $indexStep = 3000;
     }
     //MaxInserts check
     if (isset($_POST['maxInserts'])) {
         $maxInserts = (int) $_POST['maxInserts'];
     }
     if (!$maxInserts) {
         $maxInserts = 300;
     }
     chdir(JPATH_ROOT);
     //If we don't have a partial stop count, then this is a new request.
     //We should therefore clear the database.
     if (!$partial_stop) {
         $db = JFactory::getDbo();
         $query = $db->getQuery(true);
         $db->setQuery("DELETE FROM " . $db->quoteName('#__jhackguard_scan_files'));
         $db->query();
     }
     $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('./'));
     /* And off we go into the loop */
     $sql_delimiter = 0;
     $total_count = 0;
     $stopped = 0;
     while ($it->valid()) {
         //We do no need ., .., or directories. Only files.
         if (!$it->isDot() and !$it->isDir()) {
             if ($it->getSize() > 0 and $it->getSize() < $maxSize) {
                 //We also do not need empty files or files bigger than 5MB.
                 $total_count++;
                 if ($partial_stop > 0 and $partial_stop > $total_count) {
                     $it->next();
                     continue;
                     //We don't want these items. We indexed them the last run.
                 }
                 $files[] = $it->getRealPath();
                 $sql_delimiter++;
                 if ($sql_delimiter > $maxInserts) {
                     //Perform insert.
                     $db = JFactory::getDbo();
                     $query = $db->getQuery(true);
                     $query->insert($db->quoteName('#__jhackguard_scan_files'));
                     $query->columns('fname');
                     foreach ($files as $path) {
                         $query->values($db->quote($path));
                     }
                     $db->setQuery($query);
                     $db->query();
                     //Reset sql_delimiter
                     $sql_delimiter = 0;
                     //Reset files array
                     $files = array();
                 }
                 if ($total_count == $partial_stop + $indexStep) {
                     $stopped = 1;
                     break;
                     //We have reached the 3k limit per run.
                 }
             }
         }
         $it->next();
     }
     //Did we miss to import the last batch of the files?
     if (count($files) > 0) {
         //Yup..
         $db = JFactory::getDbo();
         $query = $db->getQuery(true);
         $query->insert($db->quoteName('#__jhackguard_scan_files'));
         $query->columns('fname');
         foreach ($files as $path) {
             $query->values($db->quote($path));
         }
         $db->setQuery($query);
         $db->query();
         $files = array();
     }
     if ($stopped) {
         $partial_stop = $partial_stop + $indexStep;
         echo json_encode(array("success" => false, "partialStop" => $partial_stop, "partialRun" => true));
     } else {
         //Seems like we finished successfully. WOOHOO!
         //And the total count is...
         $db = JFactory::getDbo();
         $query = $db->getQuery(true);
         $query->select("COUNT(*) as total");
         $query->from($db->quoteName('#__jhackguard_scan_files'));
         $db->setQuery($query);
         $list = $db->loadColumn();
         echo json_encode(array("success" => true, "count" => $list[0]));
     }
 }
示例#4
0
try {
    if (!$it->isDot() and !$it->isDir()) {
        // First check the MD5 sum of the file.
        // Matched files will not be opened for reading to save time.
        // Only scan files bigger than 0 bytes and less than 2MB
        $fmd5 = md5_file($it->key());
        // Check if AppFocus has been defined and process the file first.
        if ($config->app_focused_run) {
            if (!$config->afo->hash_match($it->key(), $fmd5)) {
                $hits++;
                $infected[$it->getRealPath()] = array('explain' => '[modified_core_file]', 'score' => 100);
                $it->next();
                continue;
            }
        }
        if ($it->getSize() > 0 and $it->getSize() < 2048576) {
            if (in_array($fmd5, $false_positives)) {
                $it->next();
                continue;
            }
            if (in_array($fmd5, $md5s)) {
                //md5 hit
                $hits++;
                $infected[$it->getRealPath()] = array('explain' => '[md5sum_match]', 'score' => 100);
            } else {
                $s = new FileScanner();
                $s->scan($it);
                if ($s->score > 99) {
                    $infected[$it->getRealPath()] = array('score' => $s->score, 'explain' => $s->explain);
                    //Increase the hit rate by one.
                    $hits++;
 /**
  * run() method load specified directory
  *
  * @param array params
  * @return array
  */
 public function run(array $aParams)
 {
     // test of obligatory validated path
     if (isset($aParams['path']) && is_dir($aParams['path']) && (isset($aParams['pattern']) || isset($aParams['extension']))) {
         // init object
         $oDirRecIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($aParams['path']));
         // case of not recursive
         if (isset($aParams['recursive']) === false || isset($aParams['recursive']) === true && $aParams['recursive'] === false) {
             $oDirRecIterator->setMaxDepth(1);
         }
         // clear array
         $this->_aFiles = array();
         // rewind
         $this->rewind();
         $iCount = 0;
         // loop on object result
         while ($oDirRecIterator->valid()) {
             if ($oDirRecIterator->isDot() === false) {
                 // get file name
                 $sFileName = $oDirRecIterator->getFilename();
                 if (isset($aParams['pattern']) && preg_match($aParams['pattern'], $sFileName) || isset($aParams['extension']) && substr(strtolower($sFileName), strrpos($sFileName, '.') + 1) == $aParams['extension']) {
                     $this->_aFiles[$iCount]['path'] = $oDirRecIterator->key();
                     $this->_aFiles[$iCount]['filename'] = $sFileName;
                     // case of subpath
                     if (isset($aParams['subpath']) && $aParams['subpath']) {
                         $this->_aFiles[$iCount]['subpath'] = $oDirRecIterator->getSubPath();
                     }
                     // case of subpathname
                     if (isset($aParams['subpathname']) && $aParams['subpathname']) {
                         $this->_aFiles[$iCount]['subpathname'] = $oDirRecIterator->getSubPathname();
                     }
                     // case of size
                     if (isset($aParams['size']) && $aParams['size']) {
                         $this->_aFiles[$iCount]['size'] = $oDirRecIterator->getSize();
                     }
                     // case of type
                     if (isset($aParams['type']) && $aParams['type']) {
                         $this->_aFiles[$iCount]['type'] = $oDirRecIterator->getType();
                     }
                     // case of owner
                     if (isset($aParams['owner']) && $aParams['owner']) {
                         $this->_aFiles[$iCount]['owner'] = $oDirRecIterator->getOwner();
                     }
                     // case of group
                     if (isset($aParams['group']) && $aParams['group']) {
                         $this->_aFiles[$iCount]['group'] = $oDirRecIterator->getGroup();
                     }
                     // case of time
                     if (isset($aParams['time']) && $aParams['time']) {
                         $this->_aFiles[$iCount]['time'] = $oDirRecIterator->getCTime();
                     }
                     // case of verbose
                     if (isset($aParams['verbose']) && $aParams['verbose']) {
                         echo '[ ', isset($aParams['service']) ? $aParams['service'] : 'FILE', ' ] ', date("d-m-Y à H:i:s"), ' =>  matched file : ', $sFileName, "\n";
                     }
                     ++$iCount;
                 }
             }
             $oDirRecIterator->next();
         }
         // destruct object
         unset($oDirRecIterator);
         // return
         return $this->_aFiles;
     } else {
         // throw exception if specified directory is not declared
         throw new Exception('Specified path or extension or pattern are not declared or is not a valid path');
     }
 }