예제 #1
0
 def get_event(self):
     fifo = Fifo()
     queue = Queue.Queue()
     thread = threading.Thread(target=fifo.pull(queue))
     thread.start()
     thread.join()
     result = queue.get()
     if result is not None:
         return result
     else:
         return None
예제 #2
0
 def get_event(self):
     fifo = Fifo()
     queue = Queue.Queue()
     thread = threading.Thread(target=fifo.pull(queue))
     thread.start()
     thread.join()
     result = queue.get()
     if result is not None:
         return result
     else:
         return None
예제 #3
0
    def set_event(self, *args):

        if args is None or len(args) < 1:
            print('Lack of arguments')

        params = args[0]
        data = dict()

        if System.Ahenk.is_running() is True:

            if len(params) > 1 and params[1] == 'clean':
                print('Ahenk stopping')
                System.Process.kill_by_pid(int(System.Ahenk.get_pid_number()))
                self.clean()
                return False

            elif len(params) == 5 and params[1] == 'login':
                print('{0} logging in'.format(str(params[2])))
                data['event'] = params[1]
                data['username'] = params[2]
                data['desktop'] = params[3]
                data['display'] = params[4]

            elif len(params) == 3 and params[1] == 'logout':
                print('{0} logging out'.format(str(params[2])))
                data['event'] = params[1]
                data['username'] = params[2]

            elif len(params) == 2 and params[1] == 'stop':
                data['event'] = 'stop'

            elif len(params) == 4 and params[1] == 'load' and params[2] == '-p':
                data['event'] = 'load'
                data['plugins'] = params[3]

            elif len(params) == 4 and params[1] == 'reload' and params[2] == '-p':
                data['event'] = 'reload'
                data['plugins'] = params[3]

            elif len(params) == 4 and params[1] == 'remove' and params[2] == '-p':
                data['event'] = 'remove'
                data['plugins'] = params[3]

            elif len(params) > 5 and params[1] == 'send':
                data['event'] = params[1]
                response = dict()
                response['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
                response['responseMessage'] = 'This content was sent via ahenk terminal command'

                if params[2] == '-t':
                    response['responseCode'] = MessageCode.TASK_PROCESSED.value
                    response['type'] = MessageType.TASK_STATUS.value
                    response['taskId'] = params[3]
                    if params[4] == '-m':
                        response['contentType'] = ContentType.APPLICATION_JSON.value
                        response['responseData'] = params[5]
                    elif params[4] == '-f':
                        if os.path.exists(str(params[5])):
                            response['contentType'] = self.get_relevant_type(str(params[5]))
                            response['responseData'] = Util.read_file(str(params[5]), 'rb')
                    else:
                        print(
                            'Wrong or missing parameter. Usage: send -t <task_id> -m|-f <message_content>|<file_path>')
                        return None

                    if len(params) > 6:
                        if params[6] == '-e':
                            response['responseCode'] = MessageCode.TASK_ERROR.value
                        elif params[6] == '-w':
                            response['responseCode'] = MessageCode.TASK_WARNING.value
                        elif params[6] == '-s':
                            response['responseCode'] = MessageCode.TASK_PROCESSED.value
                        else:
                            print(
                                'Wrong or missing parameter.(-e|-s|-w parameters are optional) Usage: send -t <task_id> -m|-f <message_content>|<file_path> -e|-s|-w')
                            return None

                elif len(params) > 7 and params[2] == '-p':
                    response['responseCode'] = MessageCode.POLICY_PROCESSED.value
                    response['type'] = MessageType.POLICY_STATUS.value
                    response['policyVersion'] = params[3]

                    if params[4] == '-c':
                        response['commandExecutionId'] = params[5]

                        if params[6] == '-m':
                            response['contentType'] = ContentType.APPLICATION_JSON.value
                            response['responseData'] = params[7]
                        elif params[6] == '-f':
                            if os.path.exists(str(params[7])):
                                response['contentType'] = self.get_relevant_type(str(params[7]))
                                response['responseData'] = Util.read_file(str(params[7]), 'rb')
                        else:
                            print(
                                'Wrong or missing parameter. Usage: send -p <policy_version> -c <command_execution_id> -m|-f <message_content>|<file_path>')
                            return None

                        if len(params) > 8:
                            if params[8] == '-e':
                                response['responseCode'] = MessageCode.POLICY_ERROR.value
                            elif params[8] == '-w':
                                response['responseCode'] = MessageCode.POLICY_WARNING.value
                            elif params[8] == '-s':
                                response['responseCode'] = MessageCode.POLICY_PROCESSED.value
                            else:
                                print(
                                    'Wrong or missing parameter.(-e|-s|-w parameters are optional) Usage: send -p <policy_version> -c <command_execution_id> -m|-f <message_content>|<file_path> -e|-s|-w')
                                return None

                    else:
                        print(
                            'Wrong or missing parameter. Usage: send -p <policy_version> -c <command_execution_id> -m|-f <message_content>|<file_path> -e|-s|-w')
                        return None

                resp = str(response).replace("\"{", "{")
                resp = resp.replace("}\"", "}")
                resp = resp.replace("'", "\"")
                data['message'] = json.loads(resp)
                # data['message'] = ast.literal_eval(str(response))

            else:
                print('Wrong or missing parameter. Usage : %s start|stop|restart|status|clean|send')
                return None
        else:

            if params[1] == 'clean':
                self.clean()

            else:
                print('Ahenk not running!')
                return None

        if len(data) > 0:
            fifo = Fifo()
            thread = threading.Thread(target=fifo.push(str(json.dumps(data)) + '\n'))
            thread.start()

        return True
예제 #4
0
    def set_event(self, *args):

        if args is None or len(args) < 1:
            print('Lack of arguments')

        params = args[0]
        data = dict()

        if System.Ahenk.is_running() is True:

            if len(params) > 1 and params[1] == 'clean':
                print('Ahenk stopping')
                System.Process.kill_by_pid(int(System.Ahenk.get_pid_number()))
                self.clean()
                return False

            elif len(params) > 4 and params[1] == 'login':
                print('{0} logging in'.format(str(params[2])))
                data['event'] = params[1]
                data['username'] = params[2]
                data['desktop'] = params[3]
                data['display'] = params[4]
                if len(params) == 6:
                    data['ip'] = params[5]

            elif len(params) == 3 and params[1] == 'logout':
                print('{0} logging out'.format(str(params[2])))
                data['event'] = params[1]
                data['username'] = params[2]

            elif len(params) == 4 and params[1] == 'logout':
                print('{0} logging out'.format(str(params[2])))
                data['event'] = params[1]
                data['username'] = params[2]
                data['ip'] = params[3]

            elif len(params) == 2 and params[1] == 'stop':
                data['event'] = 'stop'

            elif len(
                    params) == 4 and params[1] == 'load' and params[2] == '-p':
                data['event'] = 'load'
                data['plugins'] = params[3]

            elif len(params
                     ) == 4 and params[1] == 'reload' and params[2] == '-p':
                data['event'] = 'reload'
                data['plugins'] = params[3]

            elif len(params
                     ) == 4 and params[1] == 'remove' and params[2] == '-p':
                data['event'] = 'remove'
                data['plugins'] = params[3]

            elif len(params) > 5 and params[1] == 'send':
                data['event'] = params[1]
                response = dict()
                response['timestamp'] = str(
                    datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
                response[
                    'responseMessage'] = 'This content was sent via ahenk terminal command'

                if params[2] == '-t':
                    response['responseCode'] = MessageCode.TASK_PROCESSED.value
                    response['type'] = MessageType.TASK_STATUS.value
                    response['taskId'] = params[3]
                    if params[4] == '-m':
                        response[
                            'contentType'] = ContentType.APPLICATION_JSON.value
                        response['responseData'] = params[5]
                    elif params[4] == '-f':
                        if os.path.exists(str(params[5])):
                            response['contentType'] = self.get_relevant_type(
                                str(params[5]))
                            response['responseData'] = Util.read_file(
                                str(params[5]), 'rb')
                    else:
                        print(
                            'Wrong or missing parameter. Usage: send -t <task_id> -m|-f <message_content>|<file_path>'
                        )
                        return None

                    if len(params) > 6:
                        if params[6] == '-e':
                            response[
                                'responseCode'] = MessageCode.TASK_ERROR.value
                        elif params[6] == '-w':
                            response[
                                'responseCode'] = MessageCode.TASK_WARNING.value
                        elif params[6] == '-s':
                            response[
                                'responseCode'] = MessageCode.TASK_PROCESSED.value
                        else:
                            print(
                                'Wrong or missing parameter.(-e|-s|-w parameters are optional) Usage: send -t <task_id> -m|-f <message_content>|<file_path> -e|-s|-w'
                            )
                            return None

                elif len(params) > 7 and params[2] == '-p':
                    response[
                        'responseCode'] = MessageCode.POLICY_PROCESSED.value
                    response['type'] = MessageType.POLICY_STATUS.value
                    response['policyVersion'] = params[3]

                    if params[4] == '-c':
                        response['commandExecutionId'] = params[5]

                        if params[6] == '-m':
                            response[
                                'contentType'] = ContentType.APPLICATION_JSON.value
                            response['responseData'] = params[7]
                        elif params[6] == '-f':
                            if os.path.exists(str(params[7])):
                                response[
                                    'contentType'] = self.get_relevant_type(
                                        str(params[7]))
                                response['responseData'] = Util.read_file(
                                    str(params[7]), 'rb')
                        else:
                            print(
                                'Wrong or missing parameter. Usage: send -p <policy_version> -c <command_execution_id> -m|-f <message_content>|<file_path>'
                            )
                            return None

                        if len(params) > 8:
                            if params[8] == '-e':
                                response[
                                    'responseCode'] = MessageCode.POLICY_ERROR.value
                            elif params[8] == '-w':
                                response[
                                    'responseCode'] = MessageCode.POLICY_WARNING.value
                            elif params[8] == '-s':
                                response[
                                    'responseCode'] = MessageCode.POLICY_PROCESSED.value
                            else:
                                print(
                                    'Wrong or missing parameter.(-e|-s|-w parameters are optional) Usage: send -p <policy_version> -c <command_execution_id> -m|-f <message_content>|<file_path> -e|-s|-w'
                                )
                                return None

                    else:
                        print(
                            'Wrong or missing parameter. Usage: send -p <policy_version> -c <command_execution_id> -m|-f <message_content>|<file_path> -e|-s|-w'
                        )
                        return None

                resp = str(response).replace("\"{", "{")
                resp = resp.replace("}\"", "}")
                resp = resp.replace("'", "\"")
                data['message'] = json.loads(resp)
                # data['message'] = ast.literal_eval(str(response))

            else:
                print(
                    'Wrong or missing parameter. Usage : %s start|stop|restart|status|clean|send'
                )
                return None
        else:

            if params[1] == 'clean':
                self.clean()

            else:
                print('Ahenk not running!')
                return None

        if len(data) > 0:
            fifo = Fifo()
            thread = threading.Thread(
                target=fifo.push(str(json.dumps(data)) + '\n'))
            thread.start()

        return True