(**kwargs)
Checks permissions on commands returned from inside files

    Loops through a provided list of dictionaries with a file name and
    commands found within. Checks to see if they are writeable or missing and
    living in a writable directory.

    Expected kwargs: file_paths, regex, message_text, message_box
Checks permissions on commands returned from inside files
Checks permissions on commands returned from inside files
def check_command_permission(**kwargs):
    """Checks permissions on commands returned from inside files

    Loops through a provided list of dictionaries with a file name and
    commands found within. Checks to see if they are writeable or missing and
    living in a writable directory.

    Expected kwargs: file_paths, regex, message_text, message_box
    """
    # Start an empty list for the return of the writable files/commands
    return_list = []

    for item in kwargs['commands']:
        return_dict = {}
        return_dict['text'] = []

        # The commands we have are long and may include parameters or even
        # multiple commands with pipes and ;. We try to split this all out
        # below.
        for command in item['text']:
            command = re.sub(r'[\'"]', '', command)
            command = re.split(r'[ ;\|]', command)

            # We now have a list of some commands and some parameters and
            # other garbage. Checking for os access will clean this up for us.
            # The lines below determine if we have write access to anything.
            # It also checks for the case where the target does not exist  but
            # the parent directory is writeable.
            for split_command in command:
                vuln = False

                # Ignore known not-vulnerable targets
                if split_command in NOT_VULN:
                    continue

                # Some systemd items will specicify a command with a path
                # relative to the calling item, particularly timer files.
                relative_path = os.path.dirname(item['file_name'])

                # First, check the obvious - is this a writable command?
                if os.access(split_command, os.W_OK):
                    vuln = True

                # What about if we assume it is a relative path?
                elif os.access(relative_path + '/' + split_command, os.W_OK):
                    vuln = True

                # Or maybe it doesn't exist at all, but is in a writeable
                # director?
                elif (os.access(os.path.dirname(split_command), os.W_OK)
                      and not os.path.exists(split_command)):
                    vuln = True

                # If so, pack it all up in a new dictionary which is used
                # below for output.
                if vuln:
                    return_dict['file_name'] = item['file_name']
                    return_dict['text'].append(split_command)
                    if return_dict not in return_list:
                        return_list.append(return_dict)

    if return_list and kwargs['message_text'] and kwargs['message_box']:
        # Print to console and log the interesting file names and content.
        tee("")
        tee(kwargs['message_text'], box=kwargs['message_box'])
        for item in return_list:
            tee("  {}:".format(item['file_name']))
            for text in item['text']:
                tee("    {}".format(text))
            tee("")
def check_command_permission ( * * kwargs ) : # Start an empty list for the return of the writable files/commands return_list = [ ] for item in kwargs [ 'commands' ] : return_dict = { } return_dict [ 'text' ] = [ ] # The commands we have are long and may include parameters or even # multiple commands with pipes and ;. We try to split this all out # below. for command in item [ 'text' ] : command = re . sub ( r'[\'"]' , '' , command ) command = re . split ( r'[ ;\|]' , command ) # We now have a list of some commands and some parameters and # other garbage. Checking for os access will clean this up for us. # The lines below determine if we have write access to anything. # It also checks for the case where the target does not exist  but # the parent directory is writeable. for split_command in command : vuln = False # Ignore known not-vulnerable targets if split_command in NOT_VULN : continue # Some systemd items will specicify a command with a path # relative to the calling item, particularly timer files. relative_path = os . path . dirname ( item [ 'file_name' ] ) # First, check the obvious - is this a writable command? if os . access ( split_command , os . W_OK ) : vuln = True # What about if we assume it is a relative path? elif os . access ( relative_path + '/' + split_command , os . W_OK ) : vuln = True # Or maybe it doesn't exist at all, but is in a writeable # director? elif ( os . access ( os . path . dirname ( split_command ) , os . W_OK ) and not os . path . exists ( split_command ) ) : vuln = True # If so, pack it all up in a new dictionary which is used # below for output. if vuln : return_dict [ 'file_name' ] = item [ 'file_name' ] return_dict [ 'text' ] . append ( split_command ) if return_dict not in return_list : return_list . append ( return_dict ) if return_list and kwargs [ 'message_text' ] and kwargs [ 'message_box' ] : # Print to console and log the interesting file names and content. tee ( "" ) tee ( kwargs [ 'message_text' ] , box = kwargs [ 'message_box' ] ) for item in return_list : tee ( "  {}:" . format ( item [ 'file_name' ] ) ) for text in item [ 'text' ] : tee ( "    {}" . format ( text ) ) tee ( "" )