-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathssh-honeypot.go
More file actions
309 lines (263 loc) · 8.22 KB
/
Copy pathssh-honeypot.go
File metadata and controls
309 lines (263 loc) · 8.22 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
import (
"context"
"fmt"
"log"
"net"
"os"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/gliderlabs/ssh"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
gossh "golang.org/x/crypto/ssh"
)
var (
DeadlineTimeout = 30 * time.Second
IdleTimeout = 10 * time.Second
ipinfoIoToken = os.Getenv("IPINFOIO_TOKEN")
influxdbUrl = os.Getenv("INFLUXDB_URL")
influxdbToken = os.Getenv("INFLUXDB_TOKEN")
influxdbOrg = os.Getenv("INFLUXDB_ORG")
influxdbBucket = os.Getenv("INFLUXDB_BUCKET")
hostKeyPath = os.Getenv("HOST_KEY_PATH")
)
type IPInfo struct {
IP string `json:"ip"`
City string `json:"city"`
Region string `json:"region"`
Country string `json:"country"`
Latitude float64 `json:"latitute"`
Longitude float64 `json:"longitude"`
Org string `json:"org"`
Timezone string `json:"timezone"`
}
type SSHInfo struct {
User string
RemoteHost string
RemotePort string
LocalHost string
LocalPort string
ClientVersion string
Password string
Key string
Function string
Timestamp time.Time
}
func loadHostKey(hostKeyPath string) (ssh.Signer, error) {
keyBytes, err := os.ReadFile(hostKeyPath)
if err != nil {
return nil, err
}
signer, err := gossh.ParsePrivateKey(keyBytes)
if err != nil {
return nil, err
}
return signer, nil
}
func getIpInfo(host string, ctx context.Context, tracer trace.Tracer) (IPInfo, error) {
childCtx, span := tracer.Start(
ctx,
"getIpInfo")
defer span.End()
if ipinfoIoToken != "" {
tmp, err := getIpInfoIo(host, childCtx, tracer)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return IPInfo{}, err
}
span.AddEvent("Got IP info from ipinfo.io")
span.SetStatus(codes.Ok, fmt.Sprintf("Got IP info from ipinfo.io for '%s'", host))
return IPInfo{
IP: host,
City: tmp.City,
Region: tmp.Region,
Country: tmp.Country,
Latitude: tmp.Latitude,
Longitude: tmp.Longitude,
Org: tmp.Org,
Timezone: tmp.Timezone,
}, nil
} else {
tmp, err := getIpApi(host, childCtx, tracer)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return IPInfo{}, err
}
span.AddEvent("Got IP info from ip-api.com'")
span.SetStatus(codes.Ok, fmt.Sprintf("Got IP info from ip-api.com for '%s'", host))
return IPInfo{
IP: host,
City: tmp.City,
Region: tmp.Region,
Country: tmp.Country,
Latitude: tmp.Lat,
Longitude: tmp.Lon,
Org: tmp.Org,
Timezone: tmp.Timezone,
}, nil
}
}
func processRequest(writeAPI InfluxdbWriteAPI, sshContext ssh.Context, ctx context.Context, tracer trace.Tracer) error {
childCtx, span := tracer.Start(
ctx,
"processRequest")
defer span.End()
remote_host, remote_port, _ := net.SplitHostPort(sshContext.RemoteAddr().String())
local_host, local_port, _ := net.SplitHostPort(sshContext.LocalAddr().String())
sshInfo := SSHInfo{
User: sshContext.User(),
RemoteHost: remote_host,
RemotePort: remote_port,
LocalHost: local_host,
LocalPort: local_port,
ClientVersion: sshContext.ClientVersion(),
}
sshInfo.Timestamp = time.Now()
function := sshContext.Value("Function")
if function != nil {
sshInfo.Function = function.(string)
}
password := sshContext.Value("Password")
if password != nil {
sshInfo.Password = password.(string)
}
key := sshContext.Value("Key")
if key != nil {
sshInfo.Key = key.(string)
}
if (net.ParseIP(remote_host).IsPrivate() || net.ParseIP(remote_host).IsLoopback()) && os.Getenv("INFLUXDB_WRITE_PRIVATE_IPS") != "true" {
span.AddEvent("Request from private or loopback IP, or 'INFLUXDB_WRITE_PRIVATE_IPS' is set, skipping write to InfluxDB")
log.Printf("Request to '%s' from private or loopback IP: '%s', or 'INFLUXDB_WRITE_PRIVATE_IPS' is set to '%s', skipping write to InfluxDB", sshInfo.Function, remote_host, os.Getenv("INFLUXDB_WRITE_PRIVATE_IPS"))
sshContext.Done()
} else {
span.AddEvent("Request inccoming")
log.Printf("Request to '%s' from '%s'", sshInfo.Function, remote_host)
ipInfo, err := getIpInfo(sshInfo.RemoteHost, childCtx, tracer)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
log.Printf("Failed to get IP info: %v", err)
return err
}
if writeToInfluxDB(writeAPI, ipInfo, sshInfo, childCtx, tracer) != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
log.Printf("Failed to write to InfluxDB: %v", err)
return err
}
}
span.AddEvent("Request successfully processed")
span.SetStatus(codes.Ok, fmt.Sprintf("Request to '%s' from '%s' successfully processed", sshInfo.Function, remote_host))
return nil
}
func processRequestExponentialBackoff(writeAPI InfluxdbWriteAPI, sshContext ssh.Context, ctx context.Context, tracer trace.Tracer) error {
childCtx, span := tracer.Start(
ctx,
"processRequestExponentialBackoff")
defer span.End()
backoffSettings := backoff.NewExponentialBackOff()
backoffSettings.MaxElapsedTime = 30 * time.Minute
backoffContext := backoff.WithContext(backoffSettings, childCtx)
operation := func() error {
return processRequest(writeAPI, sshContext, backoffContext.Context(), tracer)
}
err := backoff.Retry(operation, backoffContext)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
log.Printf("Failed to process request: %v", err)
return err
}
span.AddEvent("Successfully processed request")
span.SetStatus(codes.Ok, "Successfully processed request")
log.Printf("Successfully processed request")
return nil
}
func main() {
shutdown := initTracer()
defer shutdown()
tracer := otel.Tracer("ssh-honeypot")
ctx := context.Background()
if influxdbUrl == "" {
log.Fatal("INFLUXDB_URL is not set")
}
if influxdbToken == "" {
log.Fatal("INFLUXDB_TOKEN is not set")
}
if influxdbOrg == "" {
log.Fatal("INFLUXDB_ORG is not set")
}
if influxdbBucket == "" {
log.Fatal("INFLUXDB_BUCKET is not set")
}
client := influxdb2.NewClient(influxdbUrl, influxdbToken)
defer client.Close()
writeAPI := InfluxdbWriteAPI{
WriteAPIBlocking: client.WriteAPIBlocking(influxdbOrg, influxdbBucket),
WriteAPI: client.WriteAPI(influxdbOrg, influxdbBucket),
}
defer writeAPI.WriteAPI.Flush()
ssh.Handle(func(s ssh.Session) {
s.Context().SetValue("Function", "session")
go processRequestExponentialBackoff(writeAPI, s.Context(), ctx, tracer)
log.Printf("Opened connection from '%s' to '%s@%s'", s.RemoteAddr().String(), s.User(), s.LocalAddr().String())
i := 0
for {
i += 1
log.Printf("Session active seconds: %d", i)
select {
case <-time.After(time.Second):
continue
case <-s.Context().Done():
log.Printf("Closed connection from '%s' to '%s@%s'", s.RemoteAddr().String(), s.User(), s.LocalAddr().String())
return
}
}
})
if hostKeyPath == "" {
hostKeyPath = "./host_key"
if _, err := os.Stat(hostKeyPath); os.IsNotExist(err) {
log.Printf("Generating host key...")
_, _, err := GenerateKey(hostKeyPath)
if err != nil {
log.Fatalf("Failed to generate host key: %v", err)
}
}
}
hostKey, err := loadHostKey(hostKeyPath)
if err != nil {
log.Fatalf("Failed to load host key: %v", err)
}
sshPort := os.Getenv("SSH_PORT")
if sshPort == "" {
sshPort = "2222"
}
log.Printf("Starting ssh server on port '%s'...", sshPort)
log.Printf("Connections will only last %s\n", DeadlineTimeout)
log.Printf("Timeout after %s of no activity\n", IdleTimeout)
server := &ssh.Server{
Addr: ":" + sshPort,
MaxTimeout: DeadlineTimeout,
IdleTimeout: IdleTimeout,
Version: "OpenSSH_7.4p1 Debian-10+deb9u7",
PublicKeyHandler: func(s ssh.Context, key ssh.PublicKey) bool {
s.SetValue("Function", "public_key")
s.SetValue("Key", string(gossh.MarshalAuthorizedKey(key)))
go processRequestExponentialBackoff(writeAPI, s, ctx, tracer)
return false
},
PasswordHandler: func(s ssh.Context, password string) bool {
s.SetValue("Function", "password")
s.SetValue("Password", password)
go processRequestExponentialBackoff(writeAPI, s, ctx, tracer)
return false
},
}
server.AddHostKey(hostKey)
log.Fatal(server.ListenAndServe())
}