Skip to content

Commit

Permalink
wip - flagsubmit service
Browse files Browse the repository at this point in the history
WIP-2

WIP-2

WIP-2
  • Loading branch information
MayankMittal1 committed Dec 7, 2021
1 parent 7c76667 commit e148114
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ tmp/*
katana
test.go
vendor/*
*.log
*.log
6 changes: 5 additions & 1 deletion config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ manifests = [
"elasticsearch_statefulset.yaml",
"kibana.yaml",
"fluentd-daemonset.yaml",
"ingress.yml"
]

[services.api]
Expand Down Expand Up @@ -50,3 +49,8 @@ url = "mongodb://scar:scar@localhost:27017/?authSource=admin"
[admin]
username = "sdslabs"
password = "sdslabs"

[flag]
flaglength = 10
tickperiod = 300
submissionport = 4040
2 changes: 2 additions & 0 deletions configs/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ var (
TeamVmConfig = KatanaConfig.TeamVmConfig

MongoConfig = KatanaConfig.Mongo

FlagConfig = KatanaConfig.FlagConfig
)
7 changes: 7 additions & 0 deletions configs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ type MongoCfg struct {
URL string `toml:"url"`
}

type FlagCfg struct {
FlagLength uint `toml:"flaglength"`
TickPeriod uint `toml:"tickperiod"`
SubmissionServicePort string `toml:"submissionport"`
}

type KatanaCfg struct {
KubeHost string `toml:"kubehost"`
KubeNameSpace string `toml:"kubenamespace"`
Expand All @@ -62,4 +68,5 @@ type KatanaCfg struct {
Mongo MongoCfg `toml:"mongo"`
TeamVmConfig TeamChallengeConfig `toml:"teamvm"`
AdminConfig AdminCfg `toml:"admin"`
FlagConfig FlagCfg `toml:"flag"`
}
141 changes: 141 additions & 0 deletions services/flaghandlerservice/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package flaghandlerservice

import (
"fmt"
"log"
"net"
"strconv"
"strings"

"github.com/sdslabs/katana/configs"
"github.com/sdslabs/katana/lib/mongo"
"github.com/sdslabs/katana/lib/utils"
"github.com/sdslabs/katana/types"
)

type Team struct {
TeamName string
TeamID int
}

func server() {
ln, err := net.Listen(configs.FlagConfig.SubmissionServicePort, "tcp")
if err != nil {
log.Fatal("Failed to Start Flag Submission Service")
}
defer ln.Close()
log.Println("Flag Submission Service Started at port", configs.FlagConfig.SubmissionServicePort)
connectedTeam := Team{}

for {
conn, err := ln.Accept()
if err != nil {
fmt.Println(err)
if err := conn.Close(); err != nil {
log.Println("Failed to close", err)
}
continue
}

log.Println("Connected to", conn.RemoteAddr())
go handleConnection(conn, connectedTeam)
}
}

func handleConnection(conn net.Conn, connectedTeam Team) {
defer func() {
if err := conn.Close(); err != nil {
log.Println("Error Closing", err)
}
}()
writeToCient(conn, "Connected to Flag Submission Service\nInitiate your session by `init <teamName> <password>`\n")

for {
cmdLine := make([]byte, (1024 * 4))
n, err := conn.Read(cmdLine)

if n == 0 || err != nil {
log.Println("Connection Read err", err)
return
}

cmd, param, password := parseCommand(string(cmdLine[0:n]))

if cmd == "" {
writeToCient(conn, "Inavlid Command\n")
continue
}
switch cmd {
case "init":
if param == "" || password == "" {
writeToCient(conn, "Invalid Login Parameters\n")
continue
} else if (Team{}) != connectedTeam {
writeToCient(conn, "Team is already Logged in\n")
continue
} else {
if checkTeam(param) {
connectedTeam.TeamAddress = conn.RemoteAddr().String()
connectedTeam.TeamID = param
writeToCient(conn, "Team successfully connected,\n Enter flags to submit them\n")
continue
} else {
writeToCient(conn, "Invalid TeamID\n")
continue
}
}
case "exit":

default:
if status, points := submitFlag(cmd); status {
writeToCient(conn, "Submitted successfully, points:"+strconv.Itoa(points)+"\n")
} else {
writeToCient(conn, "Invalid Flag")
}
}
}
}

func parseCommand(cmdLine string) (cmd, param, password string) {
parts := strings.Split(cmdLine, " ")
if len(parts) == 3 {
cmd = strings.TrimSpace(parts[0])
param = strings.TrimSpace(parts[1])
password = strings.TrimSpace(parts[2])
return
}
if len(parts) == 2 {
cmd = strings.TrimSpace(parts[0])
param = strings.TrimSpace(parts[1])
password = ""
return
}
if len(parts) == 1 {
cmd = strings.TrimSpace(parts[0])
param = ""
password = ""
return
}
return "", "", ""
}

func checkTeam(teamName string, password string) (bool, types.CTFTeam) {
team := &types.CTFTeam{}
if team, err := mongo.FetchSingleTeam(teamName); err == nil {
if utils.CompareHashWithPassword(team.Password, password) {
return true, *team
}
}
return false, *team
}

func submitFlag(flag string) (bool, int) {
return true, 10
}

func writeToCient(conn net.Conn, message string) {
if _, err := conn.Write([]byte(message)); err != nil {
log.Println("failed to write", err)
return
}
}
1 change: 1 addition & 0 deletions services/sshproviderservice/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func createTeams() error {
Name: pod.Name,
PodName: pod.Name,
Password: hashed,
Score: 0,
}
fmt.Fprintf(credsFile, "Team: %d, Username: %s, Password: %s\n", i, team.Name, pwd)
teams = append(teams, team)
Expand Down
23 changes: 23 additions & 0 deletions types/mongo.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package types

import "time"

type AdminUser struct {
Username string `json:"username" bson:"username" binding:"required" `
Password string `json:"password" bson:"password" binding:"required"`
Expand All @@ -10,4 +12,25 @@ type CTFTeam struct {
Name string `json:"name" bson:"username" binding:"required"`
PodName string `json:"podname" bson:"podname" binding:"required"`
Password string `json:"password" bson:"password" binding:"required"`
Score int `json:"score" bson:"score" binding:"required"`
}

type Challenge struct {
ID int `json:"id" bson:"id" binding:"required"`
TeamID int `json:"teamid" bson:"teamid" binding:"required"`
Name string `json:"name" bson:"name" binding:"required"`
Points int `json:"points" bson:"points" binding:"required"`
}

type Flag struct {
Value string `json:"value" bson:"value" binding:"required"`
ChallengeID int `json:"challengeid" bson:"challengeid" binding:"required"`
TeamID int `json:"teamid" bson:"teamid" binding:"required"`
CreatedAt time.Time `json:"createtime" bson:"createtime" binding:"required"`
}

type Submission struct {
Submitter int `json:"submitter" bson:"submitter" binding:"required"`
ChallengeID int `json:"challengeid" bson:"challengeid" binding:"required"`
Flag string `json:"flag" bson:"flag" binding:"required"`
}

0 comments on commit e148114

Please sign in to comment.