Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there any effective way to prevent a process from spawning children and revoke its permissions? I am seeking a robust approach to entirely block a process's ability to generate subprocesses and, if possible, restrict its permissions and terminate it along with its children. Do you have any suggestions or strategies that could be effective for this purpose?

What I have tried:

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()
Posted
Updated 14-Dec-23 7:07am

1 solution

Idea not answer
trying to block a proccess from creating you can either try killing all of its subproccesses repeatedly.
note: add a delay so the users computer doesn't have a bsod (bluescreen of death).
if that doesn't work or you still want some proccess/subproccesses left try:
Trying to find out stuff please wait when this answer is finished or maybe something stuff...
 
Share this answer
 
Comments
Arcadegame frame 14-Dec-23 13:36pm    
Actually my idea is to terminate the "memz" process using Python, without creating subprocesses, and you want code equivalent to the "taskkill /f /im memz.exe /t" command.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900