Example #1
0
 async def get_api_jobs(status, agent_id, wait):
     query = {}
     if status:
         query['status'] = status
     agent = Agent.objects.with_id(agent_id)
     if not agent:
         raise web.HTTPForbidden
     agen = Agent.objects.with_id(agent.id)
     agen.modify(**{'check_in': datetime.now(timezone.utc), 'alive': True})
     query.update({'agent': agent.id})
     jobs = list(Job.objects(**query))
     return jobs
Example #2
0
    async def put_job_details(json, job):
        if type(job) is not Job:
            job = Job.objects(id=ObjectId(job['id']))[0]
        if 'result' in json['action']:
            # decode stdout from new rat
            try:
                temp = json['action']['result']
                if 'stdout' in temp:
                    core = base64.b64decode(temp['stdout'])
                    encoding = chardet.detect(core)['encoding']
                    if encoding is None:
                        logging.info('No encoding was found')
                        temp['stdout'] = ''
                    else:
                        temp['stdout'] = core.decode(encoding)
                job['action']['result'] = temp
            except TypeError:
                # Contains RAT pid number
                pass
        if 'error' in json['action']:
            job['action']['error'] = json['action']['error']
        if 'exception' in json['action']:
            job['action']['exception'] = json['action']['exception']
        job['status'] = json.get('status', job.status)

        if job['status'] == "failed" and 'error' in job['action'] and job[
                'action']['error'] == "no client":
            # Force update the clients list
            interface.get_clients(job.agent.host)
            # find the rat
            try:
                iv_name = job['action']["rats"]["args"][0]
                iv = Rat.objects(agent=job.agent, name=iv_name)
                iv.modify(**{'active': False})
            except KeyError:
                logging.warning("Could not find rat to remove for failed job")
        j = job.save()
        if job.status in ('success', 'failure'):
            Job.wakeup_job(job.id)
        return j