T05 · Unauthorized Access and Privilege Escalation
- Location
scripts/smartfolder.py:142- Finding
Recursive scans traverse hidden directories and read sensitive files
- Content
View full analysis
= min_size: files.append(path) if not files: return {"duplicates": {}, "total_files": 0} # Calculate hashes hash_map = defaultdict(list) total = len(files) print(f"Scanning {total} files...") for i, file_path in enumerate(files, 1): if i % 100 == 0: print(f"Progress: {i}/{total}") try: file_hash = self._file_hash(file_path) hash_map[file_hash].append(file_path) except Exception as e: print(f"Error hashing {file_path}: {e}", file=sys.stderr) ``` ```python def _file_hash(self, file_path: Path, chunk_size: int = 8192) -> str: """Calculate MD5 hash of file""" hash_md5 = hashlib.md5() with open(file_path, "rb") as f: for chunk in iter(lambda: f.read(chunk_size), b""): hash_md5.update(chunk) return hash_md5.hexdigest() ``` ```python # Collect all files for path in directory.rglob("*"): if path.is_file() and not path.name.startswith('.'): try: stat = path.stat() mtime = datetime.fromtimestamp(stat.st_mtime) if cutoff_time and mtime > cutoff_time: continue files.append({ "path": path, "size": stat.st_size, "modified": mtime }) total_size += stat.st_size except Exception: pass ``` ```python if result["duplicates"]: print("\n[DUPLICATE FILES]:") for hash_val, paths in result["duplicates"].items(): print(f"\n Group (hash: {hash_val[:16]}...):") for ...[truncated 2579 chars]- Remediation
View remediation
bool: relative = path.relative_to(root) return any(part.startswith(".") for part in relative.parts) ``` 2. Use `os.walk()` and modify `dirnames` in place so hidden directories are not traversed at all: ```python for current_root, dirnames, filenames in os.walk(directory): dirnames[:] = [name for name in dirnames if not name.startswith(".")] ``` 3. Exclude known sensitive directories such as `.ssh`, `.aws`, `.gnupg`, credential stores, browser profiles, and keychains by default. 4. Require explicit confirmation before recursively scanning broad locations such as a home directory or filesystem root. 5. Add an opt-in option for hidden files instead of scanning them by default. 6. Catch errors around both `path.is_file()` and `path.stat()` so unreadable or transient files do not terminate collection unexpectedly. 7. Minimize output involving sensitive paths. Consider relative paths, optional redaction, or a machine-readable output mode with clear privacy warnings. 8. Update documentation so the stated privacy guarantees exactly match the implemented behavior, and add tests covering files beneath hidden parent directories. ]]>
