Các chiến lược ngăn chặn hiệu quả việc tạo quy trình con và thu hồi quyền (Python)

lập trình


Có cách nào hiệu quả để ngăn chặn quá trình sinh con và thu hồi quyền của nó không? Tôi đang tìm kiếm một cách tiếp cận mạnh mẽ để chặn hoàn toàn khả năng tạo ra các quy trình con của một quy trình và, nếu có thể, hạn chế các quyền của nó và chấm dứt nó cùng với các quy trình con của nó. Bạn có đề xuất hoặc chiến lược nào có thể hiệu quả cho mục đích này không?

Những gì tôi đã thử:

import psutil
import sys
import ctypes
import time<

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except Exception as e:
        print(f'Error checking admin privileges: {e}')
        return False

def run_as_admin():
    try:
        ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
    except Exception as e:
        print(f'Error restarting as administrator: {e}')
        sys.exit(1)

def block_child_creation(process_name):
    try:
        for process in psutil.process_iter(['pid', 'name']):
            if process.info['name'].lower() == process_name.lower():
                # Suspend the process
                process.suspend()
                print(f'The process with PID {process.pid} has been suspended.')

                # Terminate the process and its children
                for child in process.children(recursive=True):
                    child.kill()
                psutil.wait_procs(process.children(), timeout=5)
                process.kill()
                process.wait(5)
                print(f'The process with PID {process.pid} and its children have been terminated.')

                break  # No need to continue searching after blocking the process

        # Verificar novamente se o processo foi encerrado
        for process in psutil.process_iter(['pid', 'name']):
            if process.info['name'].lower() == process_name.lower():
                print(f'The process {process_name} is still running.')
            else:
                print(f'No process with the name {process_name}.')
                break
    except psutil.NoSuchProcess:
        print(f'No process with the name {process_name} found.')
    except Exception as e:
        print(f'An unexpected error occurred: {e}')

def main():
    try:
        if not is_admin():
            print("Restarting as administrator!")
            run_as_admin()

        global nome_processo
        nome_processo = input("Enter the name of the process you want to block and terminate with child creation: ")

        block_child_creation(nome_processo)

    except KeyboardInterrupt:
        print("\nTermination process interrupted by the user.")
    except Exception as e:
        print(f'An unexpected error occurred: {e}')

if __name__ == "__main__":
    main()

Giải pháp 1

Ý tưởng không trả lời

cố gắng chặn một tiến trình tạo ra, bạn có thể thử loại bỏ tất cả các tiến trình con của nó nhiều lần.
lưu ý: thêm độ trễ để máy tính của người dùng không có bsod (màn hình xanh chết chóc).

nếu cách đó không hiệu quả hoặc bạn vẫn muốn còn lại một số quy trình/quy trình con, hãy thử:
Đang cố gắng tìm hiểu nội dung, vui lòng đợi khi câu trả lời này kết thúc hoặc có thể có nội dung gì đó…

コメント

タイトルとURLをコピーしました