diff --git a/modecxr.go b/modecxr.go index 1fef249..a305516 100644 --- a/modecxr.go +++ b/modecxr.go @@ -1,6 +1,8 @@ package main import ( + "bytes" + "encoding/hex" "errors" "fmt" "log" @@ -65,3 +67,30 @@ func (sc syscon) receiveCXRCommand() (string, error) { } return "", errors.New("wrong response code") } + +func (sc syscon) virtualCommandCXRAuth() string { + res, err := sc.proccessCommand("AUTH1 " + auth) + resHex, _ := hex.DecodeString(res) + if err == nil && bytes.Equal(resHex[0:0x10], auth1ResponseHeader) { + fmt.Println("Right Auth1 response header") + data := decode(resHex[0x10:0x40]) + if bytes.Equal(data[0x8:0x10], zero[0x0:0x8]) && bytes.Equal(data[0x10:0x20], auth1Response) && bytes.Equal(data[0x20:0x30], zero) { + fmt.Println("Right Auth1 response body") + newData := append(data[0x8:0x10], data[0x0:0x8]...) + newData = append(newData, zero...) + newData = append(newData, zero...) + auth2Body := encode(newData) + authBody := append(auth2RequestHeader, auth2Body...) + com := fmt.Sprintf("AUTH2 %02X", authBody) + _, err := sc.proccessCommand(com) + if err == nil { + return "Auth successful" + } + } else { + fmt.Println("Wrong Auth1 response body") + } + } else { + fmt.Println("Wrong Auth1 response header") + } + return "Auth failed" +} diff --git a/modecxrf.go b/modecxrf.go index 7363dcc..fee9b84 100644 --- a/modecxrf.go +++ b/modecxrf.go @@ -1,6 +1,8 @@ package main import ( + "bytes" + "encoding/hex" "errors" "fmt" "log" @@ -31,3 +33,41 @@ func (sc syscon) receiveCXRFCommand() (string, error) { } return "", errors.New("wrong response") } + +func (sc syscon) virtualCommandCXRFAuth() string { + res, err := sc.proccessCommand("scopen") + if err == nil && res == "SC_READY" { + fmt.Printf("Successfully opened syscon\n%s\n", res) + res, err = sc.proccessCommand(auth) + if err == nil && len(res) == 128 { + fmt.Println("Right response length") + resHex, _ := hex.DecodeString(res) + if bytes.Equal(resHex[0:0x10], auth1ResponseHeader) { + fmt.Println("Right Auth1 response header") + data := decode(resHex[0x10:0x40]) + if bytes.Equal(data[0x8:0x10], zero[0x0:0x8]) && bytes.Equal(data[0x10:0x20], auth1Response) && bytes.Equal(data[0x20:0x30], zero) { + fmt.Println("Right Auth1 response body") + newData := append(data[0x8:0x10], data[0x0:0x8]...) + newData = append(newData, zero...) + newData = append(newData, zero...) + auth2Body := encode(newData) + authBody := append(auth2RequestHeader, auth2Body...) + com := fmt.Sprintf("%02X", authBody) + res, err := sc.proccessCommand(com) + if err == nil && strings.Contains(res, "SC_SUCCESS") { + return "Auth successful" + } + } else { + fmt.Println("Wrong Auth1 response body") + } + } else { + fmt.Println("Wrong Auth1 response header") + } + } else { + fmt.Println("Wrong response length") + } + } else { + fmt.Println("Error opening syscon") + } + return "Auth failed" +} diff --git a/virtualcommand.go b/virtualcommand.go index 63674ad..1df8311 100644 --- a/virtualcommand.go +++ b/virtualcommand.go @@ -1,7 +1,6 @@ package main import ( - "bytes" "crypto/aes" "crypto/cipher" "encoding/hex" @@ -167,67 +166,10 @@ func encode(plaintext []byte) []byte { func (sc syscon) virtualCommandAuth() string { switch sc.mode { case "cxrf": - res, err := sc.proccessCommand("scopen") - if err == nil && res == "SC_READY" { - fmt.Printf("Successfully opened syscon\n%s\n", res) - res, err = sc.proccessCommand(auth) - if err == nil && len(res) == 128 { - fmt.Println("Right response length") - resHex, _ := hex.DecodeString(res) - if bytes.Equal(resHex[0:0x10], auth1ResponseHeader) { - fmt.Println("Right Auth1 response header") - data := decode(resHex[0x10:0x40]) - if bytes.Equal(data[0x8:0x10], zero[0x0:0x8]) && bytes.Equal(data[0x10:0x20], auth1Response) && bytes.Equal(data[0x20:0x30], zero) { - fmt.Println("Right Auth1 response body") - newData := append(data[0x8:0x10], data[0x0:0x8]...) - newData = append(newData, zero...) - newData = append(newData, zero...) - auth2Body := encode(newData) - authBody := append(auth2RequestHeader, auth2Body...) - com := fmt.Sprintf("%02X", authBody) - res, err := sc.proccessCommand(com) - if err == nil && strings.Contains(res, "SC_SUCCESS") { - return "Auth successful" - } - } else { - fmt.Println("Wrong Auth1 response body") - } - } else { - fmt.Println("Wrong Auth1 response header") - } - } else { - fmt.Println("Wrong response length") - } - } else { - fmt.Println("Error opening syscon") - } + return sc.virtualCommandCXRFAuth() default: - res, err := sc.proccessCommand("AUTH1 " + auth) - resHex, _ := hex.DecodeString(res) - if err == nil && bytes.Equal(resHex[0:0x10], auth1ResponseHeader) { - fmt.Println("Right Auth1 response header") - data := decode(resHex[0x10:0x40]) - if bytes.Equal(data[0x8:0x10], zero[0x0:0x8]) && bytes.Equal(data[0x10:0x20], auth1Response) && bytes.Equal(data[0x20:0x30], zero) { - fmt.Println("Right Auth1 response body") - newData := append(data[0x8:0x10], data[0x0:0x8]...) - newData = append(newData, zero...) - newData = append(newData, zero...) - auth2Body := encode(newData) - authBody := append(auth2RequestHeader, auth2Body...) - com := fmt.Sprintf("AUTH2 %02X", authBody) - res, err := sc.proccessCommand(com) - fmt.Println(res) - if err == nil { - return "Auth successful" - } - } else { - fmt.Println("Wrong Auth1 response body") - } - } else { - fmt.Println("Wrong Auth1 response header") - } + return sc.virtualCommandCXRAuth() } - return "Auth failed" } func parseErrorCode(err string) string {