commit f8dcb9fe5b3359ecbe7684f92f27508051073857 Author: Joren Schipman Date: Tue Apr 30 16:27:44 2024 +0200 Initial Commit diff --git a/Connect.kt b/Connect.kt new file mode 100644 index 0000000..0b12a8b --- /dev/null +++ b/Connect.kt @@ -0,0 +1,90 @@ +package com.ti.mobpo.ui.util + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import java.io.BufferedReader +import java.io.InputStreamReader +import java.io.PrintWriter +import java.net.Socket +import java.security.SecureRandom +import javax.crypto.Cipher +import javax.crypto.spec.IvParameterSpec +import javax.crypto.spec.SecretKeySpec + +class Connect { + companion object { + fun decodeBase64(input: String): ByteArray { + return android.util.Base64.decode(input, android.util.Base64.DEFAULT) + } + + fun encodeBase64(input: ByteArray): String { + return android.util.Base64.encodeToString(input, android.util.Base64.DEFAULT) + } + + fun decrypt(algorithm: String, cipherText: String, key: SecretKeySpec, iv: IvParameterSpec): String { + val cipher = Cipher.getInstance(algorithm) + cipher.init(Cipher.DECRYPT_MODE, key, iv) + val plainText = cipher.doFinal(decodeBase64(cipherText)) + return String(plainText) + } + + fun encrypt(algorithm: String, inputText: String, key: SecretKeySpec, iv: IvParameterSpec): String { + val cipher = Cipher.getInstance(algorithm) + cipher.init(Cipher.ENCRYPT_MODE, key, iv) + val cipherText = cipher.doFinal(inputText.toByteArray()) + return encodeBase64(cipherText) + } + + @OptIn(ExperimentalStdlibApi::class) + suspend fun main() { + val host = "192.168.90.151" + val port = 8080 + + val secureRandom = SecureRandom() + val keyBytes = ByteArray(16) + secureRandom.nextBytes(keyBytes) + val key = SecretKeySpec(keyBytes, "AES") + + val ivBytes = ByteArray(16) + secureRandom.nextBytes(ivBytes) + val iv = IvParameterSpec(ivBytes) + + val algorithm = "AES/CBC/PKCS5Padding" + val inputText = "abcdefghigklmnopqrstuvwxyz0123456789" + + withContext(Dispatchers.IO) { + Socket(host, port).use { socket -> + val writer = PrintWriter(socket.getOutputStream(), true) + val reader = BufferedReader(InputStreamReader(socket.getInputStream())) + + val encodedKey = encodeBase64(keyBytes) + println(keyBytes.toHexString()) + writer.println(encodedKey) + + println(reader.readLine()) + + val encodedIV = encodeBase64(ivBytes) + println(ivBytes.toHexString()) + writer.println(encodedIV) + + println(reader.readLine()) + + val cipherText = encrypt(algorithm, inputText, key, iv) + println(cipherText) + writer.println(cipherText) + + writer.println("END_OF_COMMUNICATION") + + println(reader.readLine()) + + println("Client: Ready for next operation") + + writer.println("Ready for next operation") + + println(reader.readLine()) + } + } + } + } +} + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..af0b946 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module MalwareServer + +go 1.22.2 diff --git a/main.go b/main.go new file mode 100644 index 0000000..4b3754f --- /dev/null +++ b/main.go @@ -0,0 +1,131 @@ +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)] +} +