Example #1
0
func TestValidFlag(t *testing.T) {

	priv, _ := vexillary.GenerateKey()
	flag, _ := vexillary.GenerateFlag(priv)

	// Check validation of valid flag
	valid, err := vexillary.ValidFlag(flag, priv.PublicKey)
	if !valid {
		log.Fatalln("Valid flag is invalid:", err)
	}

	// Check validation of invalid flag
	invalid_flag := "aaaaaaa6a0993562af00d027aff63e9502754018="
	valid, err = vexillary.ValidFlag(invalid_flag, priv.PublicKey)
	if valid {
		log.Fatalln("Invalid flag is valid:", err)
	}
}
Example #2
0
func handler(conn net.Conn, db *sql.DB, priv *rsa.PrivateKey,
	attackFlow chan scoreboard.Attack) {

	addr := conn.RemoteAddr().String()

	defer conn.Close()

	fmt.Fprint(conn, greetingMsg)

	flag, err := bufio.NewReader(conn).ReadString('\n')
	if err != nil {
		log.Println("Read error:", err)
	}

	flag = strings.Trim(flag, "\n")

	log.Printf("\tGet flag %s from %s", flag, addr)

	valid, err := vexillary.ValidFlag(flag, priv.PublicKey)
	if err != nil {
		log.Println("\tValidate flag failed:", err)
	}
	if !valid {
		fmt.Fprint(conn, invalidFlagMsg)
		return
	}

	exist, err := steward.FlagExist(db, flag)
	if err != nil {
		log.Println("\tExist flag check failed:", err)
		fmt.Fprint(conn, internalErrorMsg)
		return
	}
	if !exist {
		fmt.Fprint(conn, flagDoesNotExistMsg)
		return
	}

	flg, err := steward.GetFlagInfo(db, flag)
	if err != nil {
		log.Println("\tGet flag info failed:", err)
		fmt.Fprint(conn, internalErrorMsg)
		return
	}

	captured, err := steward.AlreadyCaptured(db, flg.ID)
	if err != nil {
		log.Println("\tAlready captured check failed:", err)
		fmt.Fprint(conn, internalErrorMsg)
		return
	}
	if captured {
		fmt.Fprint(conn, alreadyCapturedMsg)
		return
	}

	team, err := teamByAddr(db, addr)
	if err != nil {
		log.Println("\tGet team by ip failed:", err)
		fmt.Fprint(conn, invalidTeamMsg)
		return
	}

	if flg.TeamID == team.ID {
		log.Printf("\tTeam %s try to send their flag", team.Name)
		fmt.Fprint(conn, flagYoursMsg)
		return
	}

	round, err := steward.CurrentRound(db)

	if round.ID != flg.Round {
		log.Printf("\t%s try to send flag from past round", team.Name)
		fmt.Fprint(conn, flagExpiredMsg)
		return
	}

	roundEndTime := round.StartTime.Add(round.Len)

	if time.Now().After(roundEndTime) {
		log.Printf("\t%s try to send flag from finished round", team.Name)
		fmt.Fprint(conn, flagExpiredMsg)
		return
	}

	halfStatus := steward.Status{flg.Round, team.ID, flg.ServiceID,
		steward.StatusUnknown}
	state, err := steward.GetState(db, halfStatus)

	if state != steward.StatusUP {
		log.Printf("\t%s service not ok, cannot capture", team.Name)
		fmt.Fprint(conn, serviceNotUpMsg)
		return
	}

	err = steward.CaptureFlag(db, flg.ID, team.ID)
	if err != nil {
		log.Println("\tCapture flag failed:", err)
		fmt.Fprint(conn, internalErrorMsg)
		return
	}

	go func() {
		attack := scoreboard.Attack{
			Attacker:  team.ID,
			Victim:    flg.TeamID,
			Service:   flg.ServiceID,
			Timestamp: time.Now().Unix(),
		}

		select {
		case attackFlow <- attack:
		default:
			_ = <-attackFlow
			attackFlow <- attack
		}
	}()

	fmt.Fprint(conn, capturedMsg)
}