def update(self): #if if self.end is None and self.scored is None: transaction = new('TransactionBeacon', False) transaction.beacon = self transaction.destination = self.owner transaction.source = self.get_host().get_team() transaction.value = int( self.get_game().get_setting('beacon_score')) transaction.save() transaction.destination.add_transaction(transaction) reverse = new('TransactionBeacon', False) reverse.beacon = self reverse.destination = self.owner reverse.source = self.get_host().get_team() reverse.value = transaction.value * -1 reverse.save() General.info( 'Scored a Beacon by "%s" on Host "%s" owned by "%s" for "%d" PTS!' % (self.owner.get_path(), self.get_host().get_path(), transaction.source.get_path(), transaction.value)) transaction.source.add_transaction(reverse) del reverse del transaction self.scored = now() self.save()
def start_game(self): self.status = GAME_RUNNING self.start = now() self.save() Events.info('Game "%s" was started!' % self.name) General.info('Game "%s" was started!' % self.name) self.event('Game %s has been started!' % self.name)
def get_port(self, team, port): try: number = int(port) if number <= 0: raise ValueError() except ValueError: General.error( 'Team "%s" attempted to open port "%s" which is not a valid integer!' % (team.get_path(), str(port))) return HttpResponseBadRequest(content=TEAM_MESSAGE_PORT_INVALID) try: self.get(port=number) except (ObjectDoesNotExist, MultipleObjectsReturned): pass else: return HttpResponse(status=418) openport = new('Port', False) openport.port = number openport.save() team.game.ports.add(openport) team.game.save() General.info('Team "%s" attempted to opened Beacon Port "%d"!' % (team.get_path(), number)) del openport return HttpResponse(status=201, content=TEAM_MESSAGE_PORT.format(port=port))
def expire(self): General.info( 'Beacon by "%s" on Host "%s" has expired after "%d" seconds!' % (self.owner.get_path(), self.get_host().get_path(), self.__len__())) Events.info( 'Beacon by "%s" on Host "%s" has expired after "%d" seconds!' % (self.owner.get_path(), self.get_host().get_path(), self.__len__())) self.end = now() self.save()
def job(request): client = ip(request) games, monitor, err = AssignedMonitor.objects.get_montiors( client, request.auth) if err is not None: return err if request.method == HTTP_GET: Authentication.debug( '[%s] AssignedMonitor "%s" connected to request a Job, assigned to "%d" running Games.' % (client, monitor.name, len(games))) General.info( '[%s] AssignedMonitor "%s" connected to request a Job, assigned to "%d" running Games.' % (client, monitor.name, len(games))) Jobs.debug( '[%s] AssignedMonitor "%s": Connected and will attempt to pick "%d" times for a valid Job.' % (client, monitor.name, len(games))) for game_num in range(0, len(games)): game = choice(games) Jobs.debug( '[%s] AssignedMonitor "%s": Selection round "%s" selected Game "%s".' % (client, monitor.name, game_num, game.game.get_name())) job = Job.objects.new_job(game) if job is not None: Jobs.info( '[%s] AssignedMonitor "%s": Job ID "%d" created for Host "%s"!' % (client, monitor.name, job.id, job.host.get_path())) return HttpResponse(status=201, content=dumps(job.get_json(), indent=4)) del game del games Jobs.debug( '[%s] AssignedMonitor "%s": Has no valid hosts to choose from!' % (client, monitor.name)) return HttpResponse(status=204, content=JOB_MESSAGE_NO_HOSTS) elif request.method == HTTP_POST: return Job.objects.get_job(monitor, request) return HttpResponseBadRequest(content=MESSAGE_INVALID_METHOD)
def get_beacon(self, team, token, address): try: target = IPv4Address(address) except ValueError: General.error( 'Team "%s" reported a Beacon for an invalid IP address "%s"!' % (team.get_path(), address)) return HttpResponseBadRequest(content=HOST_MESSAGE_INVALID_IP) General.info( 'Received a Beacon request by Team "%s" for address "%s"!' % (team.get_path(), address)) host = None ghost = False try: host = self.exclude(range__team=team).get(ip=address) except MultipleObjectsReturned: General.error( 'Team "%s" reported a Beacon for an invalid IP address "%s" that matches multiple Hosts!' % (team.get_path(), address)) return HttpResponseBadRequest(content=HOST_MESSAGE_INVALID_IP) except ObjectDoesNotExist: ghost = True try: host = get('BeaconHost').objects.exclude(range__team=team).get( ip=address) except (ObjectDoesNotExist, MultipleObjectsReturned): pass if host is None: General.info( 'Beacon request by Team "%s" for address "%s" does not match a known host, will attempt to match!' % (team.get_path(), address)) victim = None for match in team.get_game().teams.exclude(id=team.id): match_playing = match.get_playingteam() if match_playing is None or match_playing.assets is None: continue try: network = IPv4Network(match_playing.assets.subnet) except ValueError: General.warning( 'Team "%s" does not have a valid subnet entered for it\'s range "%s"!' % (match.get_path(), match_playing.assets.subnet)) continue else: if target in network: victim = match_playing break finally: del match_playing if victim is None: General.error( 'Beacon request by Team "%s" for address "%s" does not match a known host or Team subnet range!' % (team.get_path(), address)) return HttpResponseNotFound(content=HOST_MESSAGE_NO_HOST) General.debug( 'Creating BeaconHost due to Beacon request by Team "%s" for address "%s" that matches Team "%s" ' 'range!' % (team.get_path(), address, victim.get_path())) host = new('BeaconHost', False) host.ip = address host.range = victim.assets host.save() del victim del target General.debug('Received Beacon request by Team "%s" for Host "%s"!' % (team.get_path(), host.get_path())) if not host.get_game().__bool__(): General.error( 'Received Beacon request by Team "%s" for Host "%s" for a non-running Game!' % (team.get_path(), host.get_path())) return HttpResponseBadRequest(content=MESSAGE_GAME_NO_RUNNING) if host.get_game().id != team.get_game().id: General.error( 'Received Beacon request by Team "%s" for Host "%s" not in the same Game!' % (team.get_path(), host.get_path())) return HttpResponseBadRequest(content=HOST_MESSAGE_NO_HOST) try: beacon = host.beacons.get(end__isnull=True, owner=team) except MultipleObjectsReturned: General.warning( 'Received Beacon request by Team "%s" for Host "%s" attempting to add multiple Beacons!' % (team.get_path(), host.get_path())) return HttpResponseForbidden(content=HOST_MESSAGE_BEACON_EXISTS) except ObjectDoesNotExist: Events.info( 'Created a new Beacon on Host "%s" owned by Team "%s" from "%s"!' % (host.get_path(), host.get_team().get_path(), team.get_path())) General.info( 'Created a new Beacon on Host "%s" owned by Team "%s" from "%s"!' % (host.get_path(), host.get_team().get_path(), team.get_path())) team.get_game().event( '%s has compromised a Host on %s\'s network!' % (team.get_name(), host.get_team().get_name())) beacon = new('Beacon', False) beacon.owner = team if ghost: beacon.ghost = host else: beacon.host = host beacon.update = now() beacon.token = token beacon.save() return HttpResponse(status=201)
def get_team_json(self, request, field=TEAM_DEFAULT_FIELD, beacon=False, offensive=False): client = ip(request) try: json_str = request.body.decode('UTF-8') except UnicodeDecodeError: General.error( '[%s] Client attempted to submit an improperly encoded request!' & client) return None, None, None, HttpResponseBadRequest( content=MESSAGE_INVALID_ENCODING) try: json_data = loads(json_str) except JSONDecodeError: General.error( '[%s] Client attempted to submit an improperly JSON formatted request!' & client) return None, None, None, HttpResponseBadRequest( content=MESSAGE_INVALID_FORMAT) finally: del json_str if field not in json_data: General.error( '[%s] Data submitted by client is missing requested field "%s"!' & (client, field)) return None, None, None, HttpResponseBadRequest( content=TEAM_MESSAGE_MISSING_FIELD.format(field=field)) General.debug( '[%s] Client connected with token "%s" to request a Team.' % (client, str(request.auth.token.uid))) team, token = self.get_team_token(uuid=json_data[field], beacon=beacon) if team is None: General.info( '[%s] Client attempted to use value "%s" to request a non-existant Team!' % (client, json_data[field])) return None, None, None, HttpResponseBadRequest( content=TEAM_MESSAGE_NO_TEAM) General.debug( '[%s] Client connected and requested Team "%s" with Token "%s".' % (client, team.get_path(), json_data[field])) if not team.token.__bool__(): General.error( '[%s] Client attempted to use token "%s" that has expired!' % (client, str(team.token.uid))) return None, None, None, HttpResponseBadRequest( content=TEAM_MESSAGE_EXPIRED) if offensive: team = team.get_playingteam() if team is None or not team.offensive: General.error( '[%s] Client connected and requested Team "%s" with Token "%s", but Team is not marked Offensive!' % (client, team.get_path(), json_data[field])) return None, None, None, HttpResponseBadRequest( content=TEAM_MESSAGE_NOT_OFFENSIVE) if not team.get_game().__bool__(): General.error( '[%s] Client connected and requested Team "%s" that is not currently in a running Game!' % (client, team.get_path())) return HttpResponseBadRequest(content=MESSAGE_GAME_NO_RUNNING) return team, json_data, token, None