From e1481146d7b142c8ff13ac6676e59b7384472b6e Mon Sep 17 00:00:00 2001 From: mayankmittal-iitr Date: Sat, 4 Dec 2021 01:56:57 +0530 Subject: [PATCH] wip - flagsubmit service WIP-2 WIP-2 WIP-2 --- .gitignore | 2 +- config.sample.toml | 6 +- configs/parser.go | 2 + configs/types.go | 7 ++ services/flaghandlerservice/server.go | 141 ++++++++++++++++++++++++++ services/sshproviderservice/helper.go | 1 + types/mongo.go | 23 +++++ 7 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 services/flaghandlerservice/server.go diff --git a/.gitignore b/.gitignore index 6e71ba44..7b97719d 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ tmp/* katana test.go vendor/* -*.log +*.log \ No newline at end of file diff --git a/config.sample.toml b/config.sample.toml index 80473a91..96112957 100644 --- a/config.sample.toml +++ b/config.sample.toml @@ -18,7 +18,6 @@ manifests = [ "elasticsearch_statefulset.yaml", "kibana.yaml", "fluentd-daemonset.yaml", - "ingress.yml" ] [services.api] @@ -50,3 +49,8 @@ url = "mongodb://scar:scar@localhost:27017/?authSource=admin" [admin] username = "sdslabs" password = "sdslabs" + +[flag] +flaglength = 10 +tickperiod = 300 +submissionport = 4040 \ No newline at end of file diff --git a/configs/parser.go b/configs/parser.go index 55b18d29..dcd71081 100644 --- a/configs/parser.go +++ b/configs/parser.go @@ -36,4 +36,6 @@ var ( TeamVmConfig = KatanaConfig.TeamVmConfig MongoConfig = KatanaConfig.Mongo + + FlagConfig = KatanaConfig.FlagConfig ) diff --git a/configs/types.go b/configs/types.go index 76876348..808378bd 100644 --- a/configs/types.go +++ b/configs/types.go @@ -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"` @@ -62,4 +68,5 @@ type KatanaCfg struct { Mongo MongoCfg `toml:"mongo"` TeamVmConfig TeamChallengeConfig `toml:"teamvm"` AdminConfig AdminCfg `toml:"admin"` + FlagConfig FlagCfg `toml:"flag"` } diff --git a/services/flaghandlerservice/server.go b/services/flaghandlerservice/server.go new file mode 100644 index 00000000..0b79d502 --- /dev/null +++ b/services/flaghandlerservice/server.go @@ -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 `\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 + } +} diff --git a/services/sshproviderservice/helper.go b/services/sshproviderservice/helper.go index d486b8cd..9de1cb98 100644 --- a/services/sshproviderservice/helper.go +++ b/services/sshproviderservice/helper.go @@ -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) diff --git a/types/mongo.go b/types/mongo.go index 6dc18e2d..560aebc7 100644 --- a/types/mongo.go +++ b/types/mongo.go @@ -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"` @@ -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"` }