@@ -13,13 +13,15 @@ import (
13
13
"github.com/gliderlabs/ssh"
14
14
"github.com/google/uuid"
15
15
log "github.com/sirupsen/logrus"
16
- "golang.org/x/crypto/ssh/terminal "
16
+ "golang.org/x/term "
17
17
)
18
18
19
19
type SSHStrategy struct {
20
+ Sessions map [string ][]plugins.Message
20
21
}
21
22
22
23
func (sshStrategy * SSHStrategy ) Init (beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration , tr tracer.Tracer ) error {
24
+ sshStrategy .Sessions = make (map [string ][]plugins.Message )
23
25
go func () {
24
26
server := & ssh.Server {
25
27
Addr : beelzebubServiceConfiguration .Address ,
@@ -30,7 +32,9 @@ func (sshStrategy *SSHStrategy) Init(beelzebubServiceConfiguration parser.Beelze
30
32
uuidSession := uuid .New ()
31
33
32
34
host , port , _ := net .SplitHostPort (sess .RemoteAddr ().String ())
35
+ sessionKey := host + sess .User ()
33
36
37
+ // Inline SSH command
34
38
if sess .RawCommand () != "" {
35
39
for _ , command := range beelzebubServiceConfiguration .Commands {
36
40
matched , err := regexp .MatchString (command .Regex , sess .RawCommand ())
@@ -52,8 +56,14 @@ func (sshStrategy *SSHStrategy) Init(beelzebubServiceConfiguration parser.Beelze
52
56
llmProvider = plugins .OpenAI
53
57
}
54
58
59
+ histories := make ([]plugins.Message , 0 )
60
+
61
+ if sshStrategy .Sessions [sessionKey ] != nil {
62
+ histories = sshStrategy .Sessions [sessionKey ]
63
+ }
64
+
55
65
llmHoneypot := plugins.LLMHoneypot {
56
- Histories : make ([]plugins. Message , 0 ) ,
66
+ Histories : histories ,
57
67
OpenAIKey : beelzebubServiceConfiguration .Plugin .OpenAISecretKey ,
58
68
Protocol : tracer .SSH ,
59
69
Host : beelzebubServiceConfiguration .Plugin .Host ,
@@ -86,6 +96,10 @@ func (sshStrategy *SSHStrategy) Init(beelzebubServiceConfiguration parser.Beelze
86
96
Command : sess .RawCommand (),
87
97
CommandOutput : commandOutput ,
88
98
})
99
+ var histories []plugins.Message
100
+ histories = append (histories , plugins.Message {Role : plugins .USER .String (), Content : sess .RawCommand ()})
101
+ histories = append (histories , plugins.Message {Role : plugins .ASSISTANT .String (), Content : commandOutput })
102
+ sshStrategy .Sessions [sessionKey ] = histories
89
103
tr .TraceEvent (tracer.Event {
90
104
Msg : "End SSH Session" ,
91
105
Status : tracer .End .String (),
@@ -109,10 +123,14 @@ func (sshStrategy *SSHStrategy) Init(beelzebubServiceConfiguration parser.Beelze
109
123
Description : beelzebubServiceConfiguration .Description ,
110
124
})
111
125
112
- term := terminal .NewTerminal (sess , buildPrompt (sess .User (), beelzebubServiceConfiguration .ServerName ))
126
+ terminal := term .NewTerminal (sess , buildPrompt (sess .User (), beelzebubServiceConfiguration .ServerName ))
113
127
var histories []plugins.Message
128
+ if sshStrategy .Sessions [sessionKey ] != nil {
129
+ histories = sshStrategy .Sessions [sessionKey ]
130
+ }
131
+
114
132
for {
115
- commandInput , err := term .ReadLine ()
133
+ commandInput , err := terminal .ReadLine ()
116
134
if err != nil {
117
135
break
118
136
}
@@ -160,7 +178,7 @@ func (sshStrategy *SSHStrategy) Init(beelzebubServiceConfiguration parser.Beelze
160
178
histories = append (histories , plugins.Message {Role : plugins .USER .String (), Content : commandInput })
161
179
histories = append (histories , plugins.Message {Role : plugins .ASSISTANT .String (), Content : commandOutput })
162
180
163
- term .Write (append ([]byte (commandOutput ), '\n' ))
181
+ terminal .Write (append ([]byte (commandOutput ), '\n' ))
164
182
165
183
tr .TraceEvent (tracer.Event {
166
184
Msg : "New SSH Terminal Session" ,
@@ -178,6 +196,7 @@ func (sshStrategy *SSHStrategy) Init(beelzebubServiceConfiguration parser.Beelze
178
196
}
179
197
}
180
198
}
199
+ sshStrategy .Sessions [sessionKey ] = histories
181
200
tr .TraceEvent (tracer.Event {
182
201
Msg : "End SSH Session" ,
183
202
Status : tracer .End .String (),
0 commit comments