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 transfer(self, approve=True): self.approved = now() self.processed = True if not approve: Events.info( 'Score Transfer from "%s" to "%s" of "%d" PTS was disapproved, returning "%d" PTS to "%s"!' % (self.source.path(), self.destination.path(), self.value, self.value, self.source.path())) result = new('TransferResult', save=False) result.value = self.value result.source = self.source result.destination = self.source result.save() return self.source.append(result) result = new('TransferResult', save=False) result.source = self.source result.value = self.value * -1 result.destination = self.destination result.save() Events.info( 'Score Transfer from "%s" to "%s" of "%d" PTS was approved, transfer "%d" PTS to "%s"!' % (self.source.path(), self.destination.path(), self.value, self.value, self.destination.path())) self.save() self.source.append(result) return self.destination.append(self)
def save(self, *args, **kwargs): if self.subclass is None: self.subclass = TEAM_SUBCLASS_SCORETEAM if self.token is None: self.token = new('Token', save=True) if self.stack is None: Team.save(self, *args, **kwargs) stack = new('Transaction', False) stack.source = self stack.destination = self stack.save() self.stack = stack del stack Team.save(self, *args, **kwargs)
def create_player(self): player = new('Player', False) player.name = self.name player.user = self.user player.member = self player.save() return player
def capture(self, team): if not self.__bool__(): General.warning( 'Team "%s" attempted to capture Flag "%s" which is not enabled!!' % (team.path(), self.path())) return None if self.stolen is not None: General.warning( 'Team "%s" attempted to capture Flag "%s" owned by "%s" which was already captured by "%s"!' % (team.path(), self.path(), self.team().path(), self.stolen.path())) return None self.stolen = team result = new('TransactionFlag', save=False) result.flag = self result.value = self.value result.destination = team result.source = self.team() result.save() team.append(result) self.team().append(result.reverse()) self.save() del result self.game().event('%s stole a Flag from %s!' % (self.stolen.get_name(), self.get_team().get_name())) Events.info('Flag "%s" owned by "%s" was captured by "%s"!' % (self.path(), self.team().path(), self.stolen.path())) return Flag.objects.get_next(team)
def save(self, *args, **kwargs): Model.save(self, *args, **kwargs) if self.goldteam is None: goldteam = new('Team', False) goldteam.game = self goldteam.name = 'Gold Team' goldteam.save() self.goldteam = goldteam Model.save(self, *args, **kwargs) if self.grayteam is None: grayteam = new('ScoreTeam', False) grayteam.game = self grayteam.name = 'Gray Team' grayteam.save() self.grayteam = grayteam Model.save(self, *args, **kwargs)
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 create_team(self): team = new('PlayingTeam', False) team.name = self.name team.logo = self.logo team.color = self.color team.membership = self team.save() return team
def register_token(self, days=TEAM_TOKEN_DAYS): token = new('Token', save=False) if isinstance(days, int) and days > 0: token.expires = (now() - timedelta(days=days)) token.save() self.registered.add(token) self.save() return token
def reverse(self): transaction = new(self.name(), save=False) transaction.when = self.when transaction.subclass = self.subclass transaction.value = self.score() * -1 transaction.destination = self.source transaction.source = self.destination transaction.save() return transaction
def new_job(self, monitor): if not monitor.game.__bool__(): return None teams = monitor.game.teams.all() for team_num in range(0, len(teams)): team = choice(teams).get_playingteam() if team is None or team.assets is None: continue Jobs.info( 'AssignedMonitor "%s": Selected Team "%s" for inspection.' % (monitor.get_name(), team.get_path())) hosts = team.assets.hosts.filter(enabled=True, scored__isnull=True) if len(hosts) == 0: Jobs.debug( 'AssignedMonitor "%s": Team "%s" has no valid hosts, skipping!' % (monitor.get_name(), team.get_path())) continue Jobs.debug( 'AssignedMonitor "%s": Team "%s" has "%d" valid hosts, beginning selection!' % (monitor.get_name(), team.get_path(), len(hosts))) for host_num in range(len(hosts)): host = choice(hosts) if host.jobs.filter(end__isnull=True).count() > 0: Jobs.debug( 'AssignedMonitor "%s": Skipping Host "%s" with open Jobs!' % (monitor.get_name(), host.get_path())) continue if monitor.selected.all().count() > 0 or not monitor.exclude: Job.debug( 'AssignedMonitor "%s": Has selection rules in place!' % (monitor.get_name())) host_selected = monitor.selected.filter(id=host.id).count() if host_selected > 0: if monitor.exclude: Jobs.debug( 'AssignedMonitor "%s": Skipping Host "%s" as it is excluded!' % (monitor.get_name(), host.get_path())) continue if not monitor.exclude and host_selected == 0: Jobs.debug( 'AssignedMonitor "%s": Skipping Host "%s" as it is not included!' % (monitor.get_name(), host.get_path())) continue del host_selected Jobs.info('AssignedMonitor "%s": Selected Host "%s"!' % (monitor.get_name(), host.get_path())) job = new('Job', False) job.monitor = monitor job.host = host job.save() return job return None
def score_job(self, total): Jobs.info('Job "%d": Finished Scoring for Host "%s"!' % (self.id, self.host.get_path())) transaction = new('Payment', False) transaction.value = total transaction.target = self.host.get_team() transaction.source = transaction.target.game.goldteam transaction.destination = transaction.target.game.grayteam transaction.save() transaction.destination.add_transaction(transaction) transaction.pay() self.end = now() self.save() return HttpResponse(status=202, content=JOB_MESSAGE_PASSED)
def process(self): payment = max( floor( float(self.get_score() * float( (100 - self.target.deduction) / 100))), 0) result = new('PaymentHealth', save=False) result.value = payment result.expected = self.score() result.source = self.destination result.destination = self.target result.save() self.target.append(result) Events.info( 'Payment from "%s" of "%d" via "%s" was issued to "%s" as "%d" (%d%% deduction).' % (self.source.path(), self.score(), self.destination.path(), self.target.path(), payment, self.target.deduction)) self.log() del payment return result
def save(self, *args, **kwargs): if self.token is None: self.token = new('Token') Model.save(self, *args, **kwargs)
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 new_game(self, name): game = new('Game', False) game.name = name.title() game.save() return game