models

package
v0.2019.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2019 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package models contains the types for schema 'cyboard'.

Package models contains the types for schema 'cyboard'.

Package models contains the types for schema 'cyboard'.

Package models contains the types for schema 'cyboard'.

Package models contains the types for schema 'cyboard'.

Package models contains the types for schema 'cyboard'.

Index

Constants

View Source
const (
	// ExitStatusPass is the 'pass' ExitStatus.
	ExitStatusPass = ExitStatus(1)

	// ExitStatusFail is the 'fail' ExitStatus.
	ExitStatusFail = ExitStatus(2)

	// ExitStatusPartial is the 'partial' ExitStatus.
	ExitStatusPartial = ExitStatus(3)

	// ExitStatusTimeout is the 'timeout' ExitStatus.
	ExitStatusTimeout = ExitStatus(4)
)
View Source
const (
	// ValidFlag is for successful guesses of flags which were not previously submitted
	ValidFlag FlagState = 0
	// InvalidFlag is for bad guesses
	InvalidFlag = 1
	// AlreadyCaptured is for flags that were claimed by the team already
	AlreadyCaptured = 2
)
View Source
const (
	// TeamRoleUnspecified is an invalid TeamRole, likely bad user input.
	TeamRoleUnspecified = TeamRole(0)

	// TeamRoleAdmin is the 'admin' TeamRole.
	TeamRoleAdmin = TeamRole(1)

	// TeamRoleCtfCreator is the 'ctf_creator' TeamRole.
	TeamRoleCtfCreator = TeamRole(2)

	// TeamRoleBlueteam is the 'blueteam' TeamRole.
	TeamRoleBlueteam = TeamRole(3)
)

Variables

View Source
var (
	// DatabaseTables is a list of every table for the schema 'cyboard'
	DatabaseTables = []string{
		"challenge",
		"challenge_category",
		"challenge_file",
		"ctf_solve",
		"exit_status",
		"other_points",
		"service",
		"service_check",
		"team",
		"team_role",
	}
)

Functions

func EnableChallenge added in v0.2019.11

func EnableChallenge(db DB, flagID int) error

EnableChallenge updates a challenge, ensuring it is active for submission. Right now, this means it will definitely be visible on the CTF display page, ready to guess against.

func GetPublicChallengeDescription

func GetPublicChallengeDescription(db DB, flagID int) (string, error)

GetPublicChallengeDescription fetches a non-hidden challenge's description/body column. This field has a separate call due to how long it may get.

func LatestScoreChange

func LatestScoreChange(db DB) (time.Time, error)

LatestScoreChange retrieves the timestamp of the last event that changed any team's score. This is a lightweight way of checking if other pieces of info need updating. If the timestamp changes between calls, then that means other score or status related data should be queried for again.

func LatestServiceCheckRun

func LatestServiceCheckRun(db DB) (time.Time, error)

LatestServiceCheckRun retrieves the timestamp of the last run of the service monitor. See: `LatestScoreChange` in `scoring.go`. This delta check is specific to services.

Types

type BlueTeamStore

type BlueTeamStore struct {
	Name       string `json:"name"`        // name
	Hash       []byte `json:"-"`           // hash
	BlueteamIP int16  `json:"blueteam_ip"` // blueteam_ip
}

BlueTeamStore contains the fields used to insert new blue teams into the database.

type BlueTeamStoreSlice

type BlueTeamStoreSlice []BlueTeamStore

BlueTeamStoreSlice is a list of blue teams, ready to be batch inserted.

func (BlueTeamStoreSlice) Insert

func (teams BlueTeamStoreSlice) Insert(db TXer) error

Insert a batch of new blue teams into the database. Blue teams must have a unique name and ip from all other blueteams.

type BlueteamView

type BlueteamView struct {
	ID         int    `json:"id"`          // id
	Name       string `json:"name"`        // name
	BlueteamIP int16  `json:"blueteam_ip"` // blueteam_ip
}

BlueteamView has the Team fields needed by the service monitor.

func AllBlueteams

func AllBlueteams(db DB) ([]BlueteamView, error)

AllBlueteams fetches all non-disabled contestants from the database, along with their significant IP octet (the one octet that changes between teams, all the other octets are assumed to be the same).

type CTFProgress

type CTFProgress struct {
	Category string `json:"category"`
	Amount   int    `json:"count"`
	Max      int    `json:"max"`
}

CTFProgress holds a team's status for a ctf category. Represents e.g. `completed 4 out of 5 challenges in the reversing category`

func GetTeamCTFProgress

func GetTeamCTFProgress(db DB, teamID int) ([]CTFProgress, error)

GetTeamCTFProgress retrieves the given team's status in each ctf category, by counting the number of solved challenges out of the total amount of them.

type CapturedChallenge

type CapturedChallenge struct {
	Designer  string    `json:"designer"` // challenge.designer
	Category  string    `json:"category"` // challenge.category
	Name      string    `json:"name"`     // challenge.name
	Timestamp time.Time `json:"time"`     // ctf_solve.created_at
}

CapturedChallenge contains enough to identify a solved challenge.

type Challenge

type Challenge struct {
	ID       int     `json:"id"`       // id
	Name     string  `json:"name"`     // name
	Category string  `json:"category"` // category
	Designer string  `json:"designer"` // designer
	Flag     string  `json:"flag"`     // flag
	Total    float32 `json:"total"`    // total
	Body     string  `json:"body"`     // body
	Hidden   bool    `json:"hidden"`   // hidden

	CreatedAt  time.Time `json:"created_at"`  // created_at
	ModifiedAt time.Time `json:"modified_at"` // modified_at
}

Challenge represents a row from 'cyboard.challenge'.

func AllChallenges

func AllChallenges(db DB) ([]Challenge, error)

AllChallenges fetches all ctf challenges from the database, to be displayed to staff.

func ChallengeByFlag

func ChallengeByFlag(db DB, flag string) (*Challenge, error)

ChallengeByFlag retrieves a row from 'cyboard.challenge' as a Challenge.

func ChallengeByID

func ChallengeByID(db DB, id int) (*Challenge, error)

ChallengeByID retrieves a row from 'cyboard.challenge' as a Challenge.

func ChallengeByName

func ChallengeByName(db DB, name string) (*Challenge, error)

ChallengeByName retrieves a row from 'cyboard.challenge' as a Challenge.

func (*Challenge) Delete

func (c *Challenge) Delete(db DB) error

Delete deletes the Challenge from the database.

func (*Challenge) Insert

func (c *Challenge) Insert(db DB) error

Insert inserts the Challenge to the database.

func (*Challenge) Update

func (c *Challenge) Update(db DB) error

Update updates the Challenge in the database.

type ChallengeCaptureCount

type ChallengeCaptureCount struct {
	Designer  string     `json:"designer"`             // challenge.designer
	Category  string     `json:"category"`             // challenge.category
	Name      string     `json:"name"`                 // challenge.name
	Count     int        `json:"count"`                // --- (calculated column)
	FirstTeam *string    `json:"first_team,omitempty"` // team.name
	Timestamp *time.Time `json:"timestamp,omitempty"`  // ctf_solve.created_at
}

ChallengeCaptureCount holds the number of teams that have beaten a CTF challenge, and the first team w/ timestamp to solve it.

func ChallengeCapturesPerFlag

func ChallengeCapturesPerFlag(db DB) ([]ChallengeCaptureCount, error)

ChallengeCapturesPerFlag gets the number of times each flag was captured, sorted by designer, then category, then name. This includes flags that remain unsolved, which will have a Count of 0, and team/timestamp of nil.

type ChallengeGuess

type ChallengeGuess struct {
	Name     string  `json:"name"`     // name
	Category string  `json:"category"` // category
	Flag     string  `json:"flag"`     // flag
	Points   float32 `json:"points"`   // total
}

ChallengeGuess is a blueteam's attempt to captured a flag. Only the Flag field is required to be set. Leaving Name empty causes the guess to checked against all hidden flags. The Category, Name, and points are filled in on a successful guess.

type ChallengeSlice

type ChallengeSlice []Challenge

ChallengeSlice is an array of challenges, suitable to insert many of at once.

func (ChallengeSlice) Insert

func (cs ChallengeSlice) Insert(db TXer) error

Insert many ctf challenges into the database at once.

func (ChallengeSlice) Sum added in v0.2019.11

func (cs ChallengeSlice) Sum() float32

type ChallengeView

type ChallengeView struct {
	ID     int    `json:"id"`     // id
	Name   string `json:"name"`   // name
	Points int    `json:"points"` // points (rounded down to nearest int)

	Captured bool `json:"captured"` // Whether the viewing team has already got this flag
}

ChallengeView is a safe-for-public-display subset of fields over a CTF challenge.

type ChallengeViewGroup added in v0.2019.11

type ChallengeViewGroup struct {
	Category   string          `json:"category"`
	Challenges []ChallengeView `json:"challenges"`
}

ChallengeViewGroup wraps a set of ChallengeViews, by their category (crypto, web, etc.)

func AllPublicChallenges

func AllPublicChallenges(db DB, teamID int) ([]ChallengeViewGroup, error)

AllPublicChallenges fetches all non-hidden ctf challenges from the database, to be displayed to constestants.

type CtfSolve

type CtfSolve struct {
	CreatedAt   time.Time `json:"created_at"`   // created_at
	TeamID      int       `json:"team_id"`      // team_id
	ChallengeID int       `json:"challenge_id"` // challenge_id
}

CtfSolve represents a row from 'cyboard.ctf_solve'.

func (*CtfSolve) Insert

func (cs *CtfSolve) Insert(db DB) error

Insert a scored flag into the database. Congrats!

type CtfSolveResult added in v0.2019.11

type CtfSolveResult struct {
	Timestamp     time.Time `json:"timestamp"`      // ctf_solve.created_at
	TeamID        int       `json:"team_id"`        // team.id
	TeamName      string    `json:"team_name"`      // team.name
	ChallengeID   int       `json:"challenge_id"`   // challenge.id
	ChallengeName string    `json:"challenge_name"` // challenge.name
	Category      string    `json:"category"`       // challenge.category
	Points        float32   `json:"points"`         // challenge.total
}

CtfSolveResult represents a solved challenge, associated with the solving team, via the pivot table 'ctf_solve'

func ChallengeCapturesByTime added in v0.2019.11

func ChallengeCapturesByTime(db DB, cutoffTime time.Time) ([]CtfSolveResult, error)

ChallengeCapturesByTime fetches all `ctf_solve` rows with their ctf name/info and solving team's name & id, and orders them by time. A `cutoffTime` threshold will exclude any solves older than the date.

type DB

type DB interface {
	Exec(string, ...interface{}) (pgx.CommandTag, error)
	Query(string, ...interface{}) (*pgx.Rows, error)
	QueryRow(string, ...interface{}) *pgx.Row
	CopyFrom(tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int, error)
}

DB is the common interface for database operations that can be used with types from schema 'cyboard'.

type DBClient

type DBClient interface {
	DB
	TXer
}

DBClient is a postgres session/engine/client. This is the main interface for doing database related stuff. It can be used to query, insert, and start transactions.

type Deleter

type Deleter interface {
	Delete(DB) error
}

type ExitStatus

type ExitStatus uint16

ExitStatus is the 'exit_status' enum type from schema 'cyboard'.

func (ExitStatus) MarshalText

func (es ExitStatus) MarshalText() ([]byte, error)

MarshalText marshals ExitStatus into text.

func (*ExitStatus) Scan

func (es *ExitStatus) Scan(src interface{}) error

Scan satisfies the database/sql.Scanner interface for ExitStatus.

func (ExitStatus) String

func (es ExitStatus) String() string

String returns the string value of the ExitStatus.

func (*ExitStatus) UnmarshalText

func (es *ExitStatus) UnmarshalText(text []byte) error

UnmarshalText unmarshals ExitStatus from text.

func (ExitStatus) Value

func (es ExitStatus) Value() (driver.Value, error)

Value satisfies the sql/driver.Valuer interface for ExitStatus.

type FlagState

type FlagState int

FlagState represents the possibilities when user submits a flag guess

func CheckFlagSubmission

func CheckFlagSubmission(db TXer, ctx context.Context, team *Team, chal *ChallengeGuess) (FlagState, error)

CheckFlagSubmission will award the team with a captured flag if their flag string guess is correct. No points will be given on a repeat flag, or obviously if the flag submitted is simply wrong.

type Inserter

type Inserter interface {
	Insert(DB) error
}

type ManyInserter

type ManyInserter interface {
	Insert(TXer) error
}

type MonitorTeamService

type MonitorTeamService struct {
	Team struct {
		ID   int    // id
		Name string // name
		IP   int16  // blueteam_ip
	}
	Service struct {
		ID       int       // id
		Name     string    // name
		Script   string    // script
		Args     []string  // args
		StartsAt time.Time // starts_at
	}
}

func MonitorTeamsAndServices

func MonitorTeamsAndServices(db DBClient) ([]MonitorTeamService, error)

MonitorTeamsAndServices fetches every active service and blueteam from the database, at the same time. Returns an empty array for no rows, or an error if there is a problem fetching data from postgres.

type OtherPoints

type OtherPoints struct {
	CreatedAt time.Time `json:"created_at"` // created_at
	TeamID    int       `json:"team_id"`    // team_id
	Points    float32   `json:"points"`     // points
	Reason    string    `json:"reason"`     // reason
}

OtherPoints represents a row from 'cyboard.other_points'.

func (*OtherPoints) Insert

func (op *OtherPoints) Insert(db DB) error

Insert a bonus point award/deduction into the database.

type OtherPointsSlice

type OtherPointsSlice []OtherPoints

OtherPointsSlice is an array of bonus points, suitable to insert many of at once.

func (OtherPointsSlice) Insert

func (ops OtherPointsSlice) Insert(db TXer) error

Insert many bonus point scores into the database at once. The incoming slice should have the CreatedAt field set to the same value on each struct, allowing a batch of bonus points to 'come in' at exactly the same time.

type OtherPointsView added in v0.2019.11

type OtherPointsView struct {
	CreatedAt time.Time `json:"created_at"` // created_at
	Teams     []string  `json:"teams"`      // array of team names
	Points    float32   `json:"points"`     // points
	Reason    string    `json:"reason"`     // reason
}

OtherPointsView groups together a row of other_points by timestamp

func AllBonusPoints added in v0.2019.11

func AllBonusPoints(db DB) ([]OtherPointsView, error)

AllBonusPoints returns all the bonus point totals, grouped by timestamp.

type Service

type Service struct {
	ID          int      `json:"id"`           // id
	Name        string   `json:"name"`         // name
	Category    string   `json:"category"`     // category
	Description string   `json:"description"`  // description
	TotalPoints float32  `json:"total_points"` // total_points
	Points      *float32 `json:"points"`       // points
	Script      string   `json:"script"`       // script
	Args        []string `json:"args"`         // args
	Disabled    bool     `json:"disabled"`     // disabled

	StartsAt   time.Time `json:"starts_at"`   // starts_at
	CreatedAt  time.Time `json:"created_at"`  // created_at
	ModifiedAt time.Time `json:"modified_at"` // modified_at
}

Service represents a row from 'cyboard.service'.

func AllActiveServices

func AllActiveServices(db DB) ([]Service, error)

AllActiveServices retrieves all monitored services from 'cyboard.service'.

func AllServices

func AllServices(db DB) ([]Service, error)

AllServices retrieves all monitored services from 'cyboard.service'.

func ServiceByID

func ServiceByID(db DB, id int) (*Service, error)

ServiceByID retrieves a row from 'cyboard.service' as a Service.

func ServiceByName

func ServiceByName(db DB, name string) (*Service, error)

ServiceByName retrieves a row from 'cyboard.service' as a Service.

func (*Service) Delete

func (s *Service) Delete(db DB) error

Delete deletes the Service from the database.

func (*Service) Insert

func (s *Service) Insert(db DB) error

Insert inserts the Service to the database.

func (*Service) Update

func (s *Service) Update(db DB) error

Update updates the Service in the database.

type ServiceCheck

type ServiceCheck struct {
	CreatedAt time.Time  `json:"created_at"` // created_at
	TeamID    int        `json:"team_id"`    // team_id
	ServiceID int        `json:"service_id"` // service_id
	Status    ExitStatus `json:"status"`     // status
	ExitCode  int16      `json:"exit_code"`  // exit_code
}

ServiceCheck represents a row from 'cyboard.service_check'.

type ServiceCheckSlice

type ServiceCheckSlice []ServiceCheck

ServiceCheckSlice is an array of ServiceChecks, suitable to insert many of at once.

func (ServiceCheckSlice) Insert

func (sc ServiceCheckSlice) Insert(db DB) error

Insert a batch of service monitor results efficiently into the database.

type ServiceSlice added in v0.2019.11

type ServiceSlice []Service

func (ServiceSlice) Sum added in v0.2019.11

func (ss ServiceSlice) Sum() float32

type TXer

type TXer interface {
	Begin() (Tx, error)
	BeginEx(context.Context, *pgx.TxOptions) (Tx, error)
}

TXer can start an isolated transaction in the database.

type Team

type Team struct {
	ID         int      `json:"id"`          // id
	Name       string   `json:"name"`        // name
	RoleName   TeamRole `json:"role_name"`   // role_name
	Hash       []byte   `json:"-"`           // hash
	Disabled   bool     `json:"disabled"`    // disabled
	BlueteamIP *int16   `json:"blueteam_ip"` // blueteam_ip
}

Team represents a row from 'cyboard.team'.

func AllTeams

func AllTeams(db DB) ([]Team, error)

AllTeams fetches all teams (users) from the database. Used by the admin dashboard to view & modify all added users.

func TeamByID

func TeamByID(db DB, id int) (*Team, error)

TeamByID retrieves a row from 'cyboard.team' as a Team.

func TeamByName

func TeamByName(db DB, name string) (*Team, error)

TeamByName retrieves a row from 'cyboard.team' as a Team.

func (*Team) Delete

func (t *Team) Delete(db DB) error

Delete deletes the Team from the database.

func (*Team) Insert

func (t *Team) Insert(db DB) error

Insert inserts the Team to the database.

func (*Team) Update

func (t *Team) Update(db DB) error

Update updates the Team in the database. If the `Hash` field is not set, then Update will not attempt to change the team's password.

type TeamCapturedChallenges

type TeamCapturedChallenges struct {
	Team       string              `json:"team"` // team.name
	Challenges []CapturedChallenge `json:"challenges"`
}

TeamCapturedChallenges holds the flags a team has captured.

func ChallengeCapturesPerTeam

func ChallengeCapturesPerTeam(db DB) ([]TeamCapturedChallenges, error)

ChallengeCapturesPerTeam retrieves each team with the flags they've captured.

type TeamRole

type TeamRole uint16

TeamRole is the 'team_role' enum type from schema 'cyboard'.

func (TeamRole) MarshalText

func (tr TeamRole) MarshalText() ([]byte, error)

MarshalText marshals TeamRole into text.

func (*TeamRole) Scan

func (tr *TeamRole) Scan(src interface{}) error

Scan satisfies the database/sql.Scanner interface for TeamRole.

func (TeamRole) String

func (tr TeamRole) String() string

String returns the string value of the TeamRole.

func (*TeamRole) UnmarshalText

func (tr *TeamRole) UnmarshalText(text []byte) error

UnmarshalText unmarshals TeamRole from text.

func (TeamRole) Value

func (tr TeamRole) Value() (driver.Value, error)

Value satisfies the sql/driver.Valuer interface for TeamRole.

type TeamServiceStatusesView

type TeamServiceStatusesView struct {
	ServiceID   int          `json:"service_id"`
	ServiceName string       `json:"service_name"`
	Statuses    []ExitStatus `json:"statuses"`
}

TeamServiceStatusesView represents the current, calculated service's status (pass, fail, timeout), for all teams.

func TeamServiceStatuses

func TeamServiceStatuses(db DB) ([]TeamServiceStatusesView, error)

TeamServiceStatuses gets the current service status (pass, fail, timeout) for each team, for each service that has started and isn't disabled.

type TeamsScoresResponse

type TeamsScoresResponse struct {
	TeamID  int    `json:"team_id"`
	Name    string `json:"name"`
	Score   int    `json:"score"`
	Service int    `json:"service"`
	Ctf     int    `json:"ctf"`
	Other   int    `json:"other"`
}

func TeamsScores

func TeamsScores(db DB) ([]TeamsScoresResponse, error)

TeamsScores TODO

type Tx

type Tx = *pgx.Tx

Tx is a database connection that is in the middle of a transaction. Transactions can rollback all operations done during their lifetime, which helps maintain the database state.

type Updater

type Updater interface {
	Update(DB) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL