Ejemplo n.º 1
0
def taskstatus(task_id):
    """
    Requests the status of a Celery task.
    :param task_id: Task's id to be requested.
    :return: Response object containing the status of the request
    """
    task = getTaskStatus(task_id)
    status = ""
    if task.state == 'PENDING':

        status = task.state
        body = {'state': task.state, 'current': 0, 'total': 1}
    elif task.state != 'FAILURE':

        status = "IN_PROGRESS"
        status = task.info.get('status', '')
        body = {
            'state': task.state,
            'current': task.info.get('current', 0),
            'total': task.info.get('total', 1),
        }
        if 'result' in task.info:
            body['result'] = task.info['result']
    else:
        # something went wrong in the background job
        body = {
            'state': task.state,
            'current': 1,
            'total': 1,
            'status': str(task.info),  # this is the exception raised
        }

    response = Response(True, "Task status", status, task_id)
    response.setBody(body)
    return jsonify(response.dictionary())
Ejemplo n.º 2
0
    def editOne(self, scenario_json):
        """
        Edits a current scenario with a JSON file
        :param scenario_json: JSON file with the new scenario
        :return: Response object containing the status of the request
        """
        response = Response()
        print(scenario_json)
        scenario_name = scenario_json["scenario_name"]
        if scenario_name in self.scenarios_dict:

            if "machines" in scenario_json:
                for machine in scenario_json["machines"]:

                    if scenario_json["machines"][machine]["uuid"] == "":
                        new_uuid = uuid.uuid4()
                        new_uuid = str(new_uuid).replace('-', '')
                        print("Unique id: ", new_uuid)
                        scenario_json['machines'][machine]['uuid'] = new_uuid

            scenario_json = Scenario(scenario_name).objectFromDictionary(
                scenario_json)

            self.scenarios_dict[scenario_name] = scenario_json
            #self._saveScenarioAsJSON(new_scenario)
            self.db_manager.editScenario(scenario_json.dictionary().copy())
            response.setResponse(True)
            response.setBody(self.scenarios_dict[scenario_name].dictionary())
        else:
            response.setReason('Scenario doesn\'t exist')
            response.setResponse(False)

            response.setBody(dict())
        return response.dictionary()
 def getAll(self):
     """
     Gets the available exploits
     :return: Response object containing the status of the request
     """
     # Variables
     exploits_dict = {
         "exploits":
         [self.exploits_dict[e].name for e in self.exploits_dict]
     }
     response = Response()
     response.setResponse(True)
     response.setBody(exploits_dict)
     return response.dictionary()
 def getOne(self, exploit_name):
     """
     Gets the scenario as a JSON file
     :param exploit_name: Exploit's name string
     :return: Response object containing the status of the request
     """
     response = Response()
     if exploit_name in self.exploits_dict:
         response.setResponse(True)
         response.setBody(self.exploits_dict[exploit_name].dictionary())
     else:
         response.setResponse(False)
         response.setReason('Exploit doesn\'t exist')
         response.setBody(dict())
     return response.dictionary()
Ejemplo n.º 5
0
 def getOne(self, scenario_name):
     """
     Gets the scenario as a JSON file
     :param scenario_name: String with the scenario name
     :return: Response object containing the status of the request
     """
     response = Response()
     if scenario_name in self.scenarios_dict:
         response.setResponse(True)
         response.setBody(self.scenarios_dict[scenario_name].dictionary())
     else:
         response.setResponse(False)
         response.setReason('Scenario doesn\'t exist')
         response.setBody(dict())
     return response.dictionary()
 def getOne(self, vulnerability_name):
     """
     Gets the scenario as a JSON file
     :param vulnerability_name: VUlnerability's name string
     :return: Response object containing the status of the request
     """
     response = Response()
     if vulnerability_name in self.vulnerabilities_dict:
         response.setResponse(True)
         response.setBody(
             self.vulnerabilities_dict[vulnerability_name].dictionary())
     else:
         response.setResponse(False)
         response.setReason('Vulnerability doesn\'t exist')
         response.setBody(dict())
     return response.dictionary()
 def getAll(self):
     """
     Gets the available exploits
     :return: Response object containing the status of the request
     """
     # Variables
     vulnerabilities_dict = {
         "vulnerabilities": [
             self.vulnerabilities_dict[v].name
             for v in self.vulnerabilities_dict
         ]
     }
     response = Response()
     response.setResponse(True)
     response.setBody(vulnerabilities_dict)
     return response.dictionary()
Ejemplo n.º 8
0
 def getAll(self):
     """
     Gets the available scenarios
     :return: Response object containing the status of the request
     """
     # Variables
     scenarios_dict = {
         "scenarios": [
             self.scenarios_dict[s].scenario_name
             for s in self.scenarios_dict
         ]
     }
     response = Response()
     response.setResponse(True)
     response.setBody(scenarios_dict)
     return response.dictionary()
 def deleteOne(self, exploit_name):
     """
     Deletes an exploit from the database.
     :param exploit_name: Exploit's name string
     :return: Response object containing the status of the request
     """
     response = Response()
     if exploit_name in self.exploits_dict:
         deleted_exploit = self.exploits_dict.pop(exploit_name)
         self.db_manager.deleteExploit(exploit_name)
         response.setResponse(True)
         response.setBody(deleted_exploit.dictionary())
     else:
         response.setResponse(False)
         response.setReason('Exploit doesn\'t exist')
         response.setBody(dict())
     return response.dictionary()
Ejemplo n.º 10
0
 def deleteOne(self, scenario_name):
     """
     Deletes one scenario from the database.
     :param scenario_name: Scenario's name string
     :return: Response object containing the status of the request
     """
     response = Response()
     if scenario_name in self.scenarios_dict:
         deleted_scenario = self.scenarios_dict.pop(scenario_name)
         #self.file_manager.deleteScenariosFolder(scenario_name)
         self.db_manager.deleteScenario(scenario_name)
         response.setResponse(True)
         response.setBody(deleted_scenario.dictionary())
     else:
         response.setResponse(False)
         response.setReason('Scenario doesn\'t exist')
         response.setBody(dict())
     return response.dictionary()
 def deleteOne(self, vulnerability_name):
     """
     Deletes a vulnerability from the database.
     :param vulnerability_name: Vulnerability's name string
     :return: Response object containing the status of the request
     """
     response = Response()
     if vulnerability_name in self.vulnerabilities_dict:
         deleted_vulnerability = self.vulnerabilities_dict.pop(
             vulnerability_name)
         self.db_manager.deleteVulnerability(vulnerability_name)
         response.setResponse(True)
         response.setBody(deleted_vulnerability.dictionary())
     else:
         response.setResponse(False)
         response.setReason('Vulnerability doesn\'t exist')
         response.setBody(dict())
     return response.dictionary()
 def newEmpty(self, exploit_name):
     """
     Creates a new exploit which includes the folders and the exploit JSON file
     :param exploit_name: String with the exploit name
     :return: Response object containing the status of the request
     """
     response = Response()
     if exploit_name not in self.exploits_dict:
         exploit = Exploit(exploit_name)
         self.exploits_dict[exploit_name] = exploit
         self.db_manager.insertExploit(exploit.dictionary().copy())
         response.setResponse(True)
         response.setBody(exploit.dictionary())
     else:
         response.setResponse(False)
         response.setReason('Exploit already exist')
         response.setBody(dict())
     return response.dictionary()
 def newEmpty(self, vulnerability_name):
     """
     Creates a new vulnerability which includes the folders and the vulnerability JSON file
     :param vulnerability_name: String with the vulnerability name
     :return: Response object containing the status of the request
     """
     response = Response()
     if vulnerability_name not in self.vulnerabilities_dict:
         vulnerability = Vulnerability(vulnerability_name)
         self.vulnerabilities_dict[vulnerability_name] = vulnerability
         self.db_manager.insertVulnerability(
             vulnerability.dictionary().copy())
         response.setResponse(True)
         response.setBody(vulnerability.dictionary())
     else:
         response.setResponse(False)
         response.setReason('Vulnerability already exist')
         response.setBody(dict())
     return response.dictionary()
 def editOne(self, exploit_json):
     """
     Edits a current scenario with a JSON file
     :param exploit_json: JSON file with the exploit's data
     :return: Response object containing the status of the request
     """
     response = Response()
     print(exploit_json)
     exploit_name = exploit_json["name"]
     if exploit_name in self.exploits_dict:
         exploit_json = Exploit().objectFromDictionary(exploit_json)
         self.exploits_dict[exploit_name] = exploit_json
         self.db_manager.editExploit(exploit_json.dictionary().copy())
         response.setResponse(True)
         response.setBody(self.exploits_dict[exploit_name].dictionary())
     else:
         response.setReason('Exploit doesn\'t exist')
         response.setResponse(False)
         response.setBody(dict())
     return response.dictionary()
Ejemplo n.º 15
0
 def getSystemInfo(self):
     """
     Gets the system info.
     :return: Response object containing request info
     """
     cpu_count_logical = psutil.cpu_count(logical=True)
     cpu_count = psutil.cpu_count(logical=False)
     memory = psutil.virtual_memory()
     mem = getattr(memory, 'total')
     memory_bytes = int(mem)
     gigabytes = float(1024**3)
     total_ram = ceil(memory_bytes / gigabytes)
     info = {
         'cpu_count_logical': cpu_count,
         'cpu_count': cpu_count,
         'total_ram': total_ram
     }
     response = Response()
     response.setResponse(True)
     response.setBody(info)
     return response.dictionary()
Ejemplo n.º 16
0
    def newEmpty(self, scenario_name):
        """
        Creates a new scenario which includes the folders and the scenario JSON file
        :param scenario_name: String with the scenario name
        :return: Response object containing the status of the request
        """
        #Folder creation moved to FileManager
        response = Response()
        if scenario_name not in self.scenarios_dict:
            #self.file_manager.createScenarioFolders(scenario_name)
            scenario = Scenario(scenario_name)
            self.scenarios_dict[scenario_name] = scenario
            #self._saveScenarioAsJSON(scenario)
            self.db_manager.insertScenario(scenario.dictionary().copy())
            response.setResponse(True)
            response.setBody(scenario.dictionary())
        else:
            response.setResponse(False)
            response.setReason('Scenario already exist')
            response.setBody(dict())

        return response.dictionary()
 def editOne(self, vulnerability_json):
     """
     Edits a current scenario with a JSON file
     :param vulnerability_json: JSON file new vulnerability
     :return: Response object containing the status of the request
     """
     response = Response()
     print(vulnerability_json)
     vulnerability_name = vulnerability_json["name"]
     if vulnerability_name in self.vulnerabilities_dict:
         vulnerability_json = Vulnerability().objectFromDictionary(
             vulnerability_json)
         self.vulnerabilities_dict[vulnerability_name] = vulnerability_json
         self.db_manager.editVulnerability(
             vulnerability_json.dictionary().copy())
         response.setResponse(True)
         response.setBody(
             self.vulnerabilities_dict[vulnerability_name].dictionary())
     else:
         response.setReason('Vulnerability doesn\'t exist')
         response.setResponse(False)
         response.setBody(dict())
     return response.dictionary()
Ejemplo n.º 18
0
    def getAvailableBoxes(self):
        """
        Gets the available boxes in the Vagrant context
        :return: A list of string with the available boxes
        """
        # Variables
        response = Response()
        boxes = {}
        boxNum = 0
        boxlist = subprocess.check_output("vagrant box list", shell=True)
        boxlist = str(boxlist)
        boxlist = re.sub(r"(^[b']|'|\s(.*?)\\n)", " ", boxlist)
        boxlist = boxlist.split(" ")
        boxlist = filter(None, boxlist)

        print("Loading available Vanilla VMs")

        for boxName in boxlist:
            boxNum = boxNum + 1
            boxes[boxNum] = boxName
            print("[ " + str(boxNum) + " ]" + boxName)
        response.setResponse(True)
        response.setBody(boxes)
        return response.dictionary()