Created
October 9, 2024 09:22
-
-
Save krisk0/564541ac97b8693921979ff03fa5b4bc to your computer and use it in GitHub Desktop.
Recursively check directories and file permissions. See comment inside file.
This file contains hidden or 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
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
''' | |
Visit all directories. | |
Print names of files that have group or other write permission (mask 022) | |
Print names of directories that contain executables (mask 111) and are | |
writable (mask 222) | |
''' | |
import os, pathlib, stat | |
g_writable_files,g_bad_dirs = set(),[] | |
g_dirs_with_executables = set() | |
g_executable = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH | |
g_writable_by_many = stat.S_IWGRP | stat.S_IWOTH | |
def visit_file(f): | |
p = os.stat(f)[stat.ST_MODE] | |
if p & g_executable: | |
g_dirs_with_executables.add(os.path.dirname(f)) | |
if p & g_writable_by_many: | |
g_writable_files.add(f) | |
def check_dirs_with_executables(): | |
for d in g_dirs_with_executables: | |
if d == '': | |
d = '.' | |
try: | |
p = os.stat(d)[stat.ST_MODE] | |
except: | |
print('not dir >%s<' % d) | |
raise | |
if p & g_writable_by_many: | |
g_bad_dirs.append(d) | |
def show_result(): | |
if g_bad_dirs: | |
g_bad_dirs.sort() | |
print('Плохие каталоги:') | |
for d in g_bad_dirs: | |
print(' %s' % d) | |
if g_writable_files: | |
ff = sorted(list(g_writable_files)) | |
print('Файлы с широкими правами на запись:') | |
for f in ff: | |
print(' %s' % f) | |
for g_x in pathlib.Path('.').rglob('*'): | |
if g_x.is_file(): | |
visit_file(str(g_x)) | |
check_dirs_with_executables() | |
show_result() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment