-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
author mayankmittal-iitr <[email protected]> 1638563217 +0530 committer Mayank Mittal <[email protected]> 1642515212 +0530 Add flag submission service
- Loading branch information
1 parent
7c76667
commit e149cf5
Showing
17 changed files
with
510 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,4 @@ tmp/* | |
katana | ||
test.go | ||
vendor/* | ||
*.log | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"fmt" | ||
"golang.org/x/crypto/ssh" | ||
"strings" | ||
) | ||
|
||
func CheckPrivateKey(privateKey, publicKey string) bool { | ||
//check key | ||
pub, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publicKey)) | ||
if err != nil { | ||
fmt.Println("error in parsing public key") | ||
return false | ||
} | ||
|
||
private, err := ssh.ParsePrivateKey([]byte(privateKey)) | ||
if err != nil { | ||
fmt.Println("error in parsing private key") | ||
return false | ||
} | ||
|
||
return bytes.Equal(private.PublicKey().Marshal(), pub.Marshal()) | ||
} | ||
|
||
func GenerateSSHKeyPair() (string, string, error) { | ||
privateKey, err := rsa.GenerateKey(rand.Reader, 1024) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
// generate and write private key as PEM | ||
var privKeyBuf strings.Builder | ||
|
||
privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)} | ||
if err := pem.Encode(&privKeyBuf, privateKeyPEM); err != nil { | ||
return "", "", err | ||
} | ||
|
||
// generate and write public key | ||
pub, err := ssh.NewPublicKey(&privateKey.PublicKey) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
var pubKeyBuf strings.Builder | ||
pubKeyBuf.Write(ssh.MarshalAuthorizedKey(pub)) | ||
|
||
return pubKeyBuf.String(), privKeyBuf.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,48 @@ | ||
package flaghandlerservice | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/sdslabs/katana/lib/mongo" | ||
"github.com/sdslabs/katana/types" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
func checkSubmission(submission *types.Submission) bool { | ||
_, err := mongo.FetchSubmission(submission.SubmittedBy, submission.Flag) | ||
return err != nil | ||
} | ||
|
||
func submitFlag(value string, team types.CTFTeam) (bool, int) { | ||
flag, err := mongo.FetchFlag(value) | ||
var points int | ||
if err != nil { | ||
return false, 0 | ||
} | ||
if flag.TeamID == team.Index { | ||
return false, 0 | ||
} | ||
submission := &types.Submission{} | ||
submission.ChallengeID = flag.ChallengeID | ||
submission.Flag = flag.Value | ||
submission.SubmittedBy = team.Index | ||
submission.Time = time.Now() | ||
if !checkSubmission(submission) { | ||
return false, 0 | ||
} | ||
if res, err := mongo.FetchChallenge(flag.ChallengeID); err != nil { | ||
return false, 0 | ||
} else { | ||
team.Score = team.Score + res.Points | ||
points = res.Points | ||
} | ||
if err := mongo.UpdateOne(context.Background(), mongo.TeamsCollection, bson.M{"id": team.Index}, team, options.FindOneAndUpdate().SetUpsert(false)); err != nil { | ||
return false, 0 | ||
} | ||
if _, err := mongo.InsertOne(context.Background(), mongo.SubmissionsCollection, submission); err != nil { | ||
return false, 0 | ||
} | ||
return true, points | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package flaghandlerservice | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"sync" | ||
"time" | ||
|
||
"github.com/jasonlvhit/gocron" | ||
"github.com/sdslabs/katana/configs" | ||
"github.com/sdslabs/katana/lib/mongo" | ||
"github.com/sdslabs/katana/types" | ||
"go.mongodb.org/mongo-driver/bson" | ||
) | ||
|
||
func Ticker(wg sync.WaitGroup) { | ||
log.Println("Ticker") | ||
defer wg.Done() | ||
handler() | ||
gocron.Every(uint64(configs.FlagConfig.TickPeriod)).Second().Do(handler) | ||
<-gocron.Start() | ||
// gocron.Every(uint64(configs.FlagConfig.TickPeriod)).Second().Do(flaggetter) todo:trigger setter here | ||
// <-gocron.Start() | ||
} | ||
|
||
func handler() { | ||
log.Print("Handler triggered -> ", time.Now()) | ||
challenges := mongo.FetchChallenges(context.Background(), mongo.ChallengesCollection, bson.M{}) | ||
teams := mongo.FetchTeams(context.Background(), mongo.TeamsCollection, bson.M{}) | ||
time := time.Now() | ||
for _, team := range teams { | ||
for _, challenge := range challenges { | ||
go flagUpdater(challenge, team, time) | ||
} | ||
} | ||
} | ||
|
||
func flagUpdater(challenge types.Challenge, team types.CTFTeam, triggerTime time.Time) { | ||
flagValue := random(configs.FlagConfig.FlagLength) | ||
var flag = &types.Flag{} | ||
flag.Value = flagValue | ||
flag.ChallengeID = challenge.ID | ||
flag.CreatedAt = triggerTime | ||
flag.TeamID = team.Index | ||
if _, err := mongo.InsertOne(context.Background(), mongo.FlagsCollection, flag); err != nil { | ||
log.Println(err) | ||
} else { | ||
log.Println("Trigger setter") | ||
//todo : trigger flag setter here | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package flaghandlerservice | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"encoding/binary" | ||
) | ||
|
||
func Bytes(n int) []byte { | ||
b := make([]byte, n) | ||
_, err := rand.Read(b) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return b | ||
} | ||
|
||
var defLetters = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") | ||
|
||
func random(n int, letters ...string) string { | ||
var letterRunes []rune | ||
if len(letters) == 0 { | ||
letterRunes = defLetters | ||
} else { | ||
letterRunes = []rune(letters[0]) | ||
} | ||
|
||
var bb bytes.Buffer | ||
bb.Grow(n) | ||
l := uint32(len(letterRunes)) | ||
// on each loop, generate one random rune and append to output | ||
for i := 0; i < n; i++ { | ||
bb.WriteRune(letterRunes[binary.BigEndian.Uint32(Bytes(4))%l]) | ||
} | ||
return bb.String() | ||
} |
Oops, something went wrong.