Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checker env fix #28

Merged
merged 4 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions checker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ services:
ports:
- 3303:3303
environment:
- PHREAKING_GRPC_PASS=${PHREAKING_GRPC_PASS}
- PHREAKING_SIM_KEY=${PHREAKING_SIM_KEY}
- REDIS_PASS=asimplesentenceisbetter
env_file:
- .env
phreaking-db:
image: redis:7-alpine
restart: always
Expand Down
2 changes: 1 addition & 1 deletion checker/src/cmd/checker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
})

db := redis.NewClient(&redis.Options{
Addr: "phreaking_checker-phreaking-db-1:6379",
Addr: "phreaking-db:6379",
Password: string(os.Getenv("REDIS_PASS")),
DB: 0, // use default DB
})
Expand Down
23 changes: 10 additions & 13 deletions checker/src/pkg/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,19 @@ import (
"fmt"
"io"
"log"
"os"

"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/sha3"
)

var key = []byte(string(os.Getenv("PHREAKING_SIM_KEY")))

func ComputeHash(input []byte) (hash string) {
h := sha256.New()
h.Write(input)
bs := h.Sum(nil)
return string(bs)
}

func EncryptAES(input []byte) (res []byte) {
func EncryptAES(input []byte, key []byte) (res []byte) {
aesBlock, err := aes.NewCipher(key)
if err != nil {
fmt.Println(err)
Expand All @@ -47,7 +44,7 @@ func EncryptAES(input []byte) (res []byte) {
return buf
}

func DecryptAES(ct []byte) (res []byte) {
func DecryptAES(ct []byte, key []byte) (res []byte) {
aesBlock, err := aes.NewCipher(key)
if err != nil {
log.Fatalln(err)
Expand All @@ -65,40 +62,40 @@ func DecryptAES(ct []byte) (res []byte) {
return originalText
}

func CheckIntegrity(buf []byte, mac []byte) bool {
dec := string(DecryptAES(mac))
func CheckIntegrity(buf []byte, mac []byte, key []byte) bool {
dec := string(DecryptAES(mac, key))
hash := ComputeHash(buf)
return (dec == hash)
}

func IA0(msg []byte) (mac []byte) {
func IA0(msg []byte, key []byte) (mac []byte) {
h := sha256.New()
h.Write(msg)
return h.Sum(nil)
}

func IA1(msg []byte) (mac []byte) {
func IA1(msg []byte, key []byte) (mac []byte) {
hash := hmac.New(sha256.New, []byte(key))
hash.Write(msg)
return hash.Sum(nil)
}

func IA2(msg []byte) (mac []byte) {
func IA2(msg []byte, key []byte) (mac []byte) {
hash := hmac.New(sha512.New, []byte(key))
hash.Write(msg)
return hash.Sum(nil)
}

func IA3(msg []byte) (mac []byte) {
func IA3(msg []byte, key []byte) (mac []byte) {
hash := hmac.New(sha3.New256, []byte(key))
hash.Write(msg)
return hash.Sum(nil)
}

func IA4(msg []byte) (mac []byte) {
func IA4(msg []byte, key []byte) (mac []byte) {
hash, _ := blake2b.New256(key)
hash.Write(msg)
return hash.Sum(nil)
}

var IAalg = map[uint8]func([]byte) []byte{0: IA0, 1: IA1, 2: IA2, 3: IA3, 4: IA4}
var IAalg = map[uint8]func([]byte, []byte) []byte{0: IA0, 1: IA1, 2: IA2, 3: IA3, 4: IA4}
25 changes: 23 additions & 2 deletions checker/src/pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func (h *Handler) PutFlag(ctx context.Context, message *enochecker.TaskMessage)

c := pb.NewLocationClient(conn)

md := metadata.Pairs("auth", string(os.Getenv("PHREAKING_GRPC_PASS")))
grpcEnvVar := "PHREAKING_" + strconv.Itoa(int(message.TeamId)) + "_GRPC_PASS"

md := metadata.Pairs("auth", string(os.Getenv(grpcEnvVar)))
ctx_grpc := metadata.NewOutgoingContext(context.Background(), md)
_, err = c.UpdateLocation(ctx_grpc, &pb.Loc{Position: message.Flag})
if err != nil {
Expand Down Expand Up @@ -94,6 +96,11 @@ func (h *Handler) getFlagLocation(ctx context.Context, message *enochecker.TaskM
return err
}

defer func() {
coreConn.Close()
ueConn.Close()
}()

setup := ngap.NGSetupRequestMsg{GranId: 0, Tac: 0, Plmn: 0}
buf, _ := ngap.EncodeMsg(ngap.NGSetupRequest, &setup)

Expand Down Expand Up @@ -175,7 +182,11 @@ func (h *Handler) getFlagLocation(ctx context.Context, message *enochecker.TaskM
return err
}

dec := crypto.DecryptAES(reply[9:])
keyEnvVar := "PHREAKING_" + strconv.Itoa(int(message.TeamId)) + "_SIM_KEY"

key := []byte(string(os.Getenv(keyEnvVar)))

dec := crypto.DecryptAES(reply[9:], key)

var loc ngap.LocationUpdateMsg
err = ngap.DecodeMsg(dec, &loc)
Expand Down Expand Up @@ -225,6 +236,11 @@ func (h *Handler) Exploit(ctx context.Context, message *enochecker.TaskMessage)
return nil, err
}

defer func() {
coreConn.Close()
ueConn.Close()
}()

setup := ngap.NGSetupRequestMsg{GranId: 0, Tac: 0, Plmn: 0}
buf, _ := ngap.EncodeMsg(ngap.NGSetupRequest, &setup)

Expand Down Expand Up @@ -365,6 +381,11 @@ func (h *Handler) gnb(ctx context.Context, message *enochecker.TaskMessage, port
return err
}

defer func() {
coreConn.Close()
ueConn.Close()
}()

setup := ngap.NGSetupRequestMsg{GranId: 0, Tac: 0, Plmn: 0}
buf, _ := ngap.EncodeMsg(ngap.NGSetupRequest, &setup)

Expand Down
4 changes: 2 additions & 2 deletions checker/src/pkg/ngap/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func EncodeMsg[T any](t MsgType, msgPtr *T) ([]byte, error) {
return b.Bytes(), err
}

func EncodeEncMsg[T any](t MsgType, msgPtr *T) ([]byte, error) {
func EncodeEncMsg[T any](t MsgType, msgPtr *T, key []byte) ([]byte, error) {
var b bytes.Buffer
b.WriteByte(byte(t))
var be bytes.Buffer
e := gob.NewEncoder(&be)
err := e.Encode(&msgPtr)

tmp := be.Bytes()
tmp = crypto.EncryptAES(tmp)
tmp = crypto.EncryptAES(tmp, key)

b.Write(tmp)
return b.Bytes(), err
Expand Down