2023-02-26 18:04:05 +01:00
|
|
|
package strategies
|
2022-05-08 20:49:53 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-10-08 17:45:31 +02:00
|
|
|
"github.com/mariocandela/beelzebub/v3/parser"
|
|
|
|
"github.com/mariocandela/beelzebub/v3/plugins"
|
|
|
|
"github.com/mariocandela/beelzebub/v3/tracer"
|
2023-04-14 22:35:11 +02:00
|
|
|
"regexp"
|
2023-10-08 17:45:31 +02:00
|
|
|
|
2023-04-14 22:35:11 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-05-08 20:49:53 +02:00
|
|
|
"github.com/gliderlabs/ssh"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
)
|
|
|
|
|
2023-06-01 00:15:21 +02:00
|
|
|
type SSHStrategy struct {
|
2022-05-08 20:49:53 +02:00
|
|
|
}
|
|
|
|
|
2023-06-01 00:15:21 +02:00
|
|
|
func (sshStrategy *SSHStrategy) Init(beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration, tr tracer.Tracer) error {
|
2022-05-08 20:49:53 +02:00
|
|
|
go func() {
|
|
|
|
server := &ssh.Server{
|
|
|
|
Addr: beelzebubServiceConfiguration.Address,
|
|
|
|
MaxTimeout: time.Duration(beelzebubServiceConfiguration.DeadlineTimeoutSeconds) * time.Second,
|
|
|
|
IdleTimeout: time.Duration(beelzebubServiceConfiguration.DeadlineTimeoutSeconds) * time.Second,
|
|
|
|
Version: beelzebubServiceConfiguration.ServerVersion,
|
|
|
|
Handler: func(sess ssh.Session) {
|
|
|
|
uuidSession := uuid.New()
|
2022-05-09 23:18:20 +02:00
|
|
|
|
|
|
|
tr.TraceEvent(tracer.Event{
|
2022-05-31 22:39:56 +02:00
|
|
|
Msg: "New SSH Session",
|
|
|
|
Protocol: tracer.SSH.String(),
|
|
|
|
RemoteAddr: sess.RemoteAddr().String(),
|
|
|
|
Status: tracer.Start.String(),
|
|
|
|
ID: uuidSession.String(),
|
|
|
|
Environ: strings.Join(sess.Environ(), ","),
|
|
|
|
User: sess.User(),
|
|
|
|
Description: beelzebubServiceConfiguration.Description,
|
2022-06-03 14:05:11 +02:00
|
|
|
Command: sess.RawCommand(),
|
2022-05-09 23:18:20 +02:00
|
|
|
})
|
|
|
|
|
2022-05-08 20:49:53 +02:00
|
|
|
term := terminal.NewTerminal(sess, buildPrompt(sess.User(), beelzebubServiceConfiguration.ServerName))
|
2024-06-23 10:55:06 +02:00
|
|
|
var histories []plugins.Message
|
2022-05-08 20:49:53 +02:00
|
|
|
for {
|
|
|
|
commandInput, err := term.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2023-04-14 22:35:11 +02:00
|
|
|
|
2022-05-08 20:49:53 +02:00
|
|
|
if commandInput == "exit" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for _, command := range beelzebubServiceConfiguration.Commands {
|
|
|
|
matched, err := regexp.MatchString(command.Regex, commandInput)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error regex: %s, %s", command.Regex, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if matched {
|
2022-12-16 23:02:16 +01:00
|
|
|
commandOutput := command.Handler
|
|
|
|
|
2024-07-21 20:11:18 +02:00
|
|
|
if command.Plugin == plugins.LLMPluginName {
|
2022-12-16 23:02:16 +01:00
|
|
|
|
2024-07-21 20:11:18 +02:00
|
|
|
llmModel, err := parser.FromString(beelzebubServiceConfiguration.Plugin.LLMModel)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error fromString: %s", err.Error())
|
|
|
|
commandOutput = "command not found"
|
|
|
|
}
|
|
|
|
|
|
|
|
llmHoneypot := plugins.LLMHoneypot{
|
|
|
|
Histories: histories,
|
|
|
|
OpenAIKey: beelzebubServiceConfiguration.Plugin.OpenAISecretKey,
|
|
|
|
Protocol: tracer.SSH,
|
|
|
|
Host: beelzebubServiceConfiguration.Plugin.Host,
|
|
|
|
Model: llmModel,
|
|
|
|
}
|
|
|
|
|
|
|
|
llmHoneypotInstance := plugins.InitLLMHoneypot(llmHoneypot)
|
|
|
|
|
|
|
|
if commandOutput, err = llmHoneypotInstance.ExecuteModel(commandInput); err != nil {
|
|
|
|
log.Errorf("Error ExecuteModel: %s, %s", commandInput, err.Error())
|
2022-12-16 23:02:16 +01:00
|
|
|
commandOutput = "command not found"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 16:00:31 +02:00
|
|
|
histories = append(histories, plugins.Message{Role: plugins.USER.String(), Content: commandInput})
|
|
|
|
histories = append(histories, plugins.Message{Role: plugins.ASSISTANT.String(), Content: commandOutput})
|
2022-12-16 23:02:16 +01:00
|
|
|
|
|
|
|
term.Write(append([]byte(commandOutput), '\n'))
|
2023-04-14 22:35:11 +02:00
|
|
|
|
|
|
|
tr.TraceEvent(tracer.Event{
|
|
|
|
Msg: "New SSH Terminal Session",
|
|
|
|
RemoteAddr: sess.RemoteAddr().String(),
|
|
|
|
Status: tracer.Interaction.String(),
|
|
|
|
Command: commandInput,
|
|
|
|
CommandOutput: commandOutput,
|
|
|
|
ID: uuidSession.String(),
|
|
|
|
Protocol: tracer.SSH.String(),
|
|
|
|
Description: beelzebubServiceConfiguration.Description,
|
|
|
|
})
|
2022-05-08 20:49:53 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-09 23:18:20 +02:00
|
|
|
tr.TraceEvent(tracer.Event{
|
|
|
|
Msg: "End SSH Session",
|
2022-05-17 23:32:00 +02:00
|
|
|
Status: tracer.End.String(),
|
2022-05-09 23:18:20 +02:00
|
|
|
ID: uuidSession.String(),
|
|
|
|
})
|
2022-05-08 20:49:53 +02:00
|
|
|
},
|
|
|
|
PasswordHandler: func(ctx ssh.Context, password string) bool {
|
2022-05-09 23:18:20 +02:00
|
|
|
tr.TraceEvent(tracer.Event{
|
2022-05-31 22:39:56 +02:00
|
|
|
Msg: "New SSH attempt",
|
|
|
|
Protocol: tracer.SSH.String(),
|
|
|
|
Status: tracer.Stateless.String(),
|
|
|
|
User: ctx.User(),
|
|
|
|
Password: password,
|
|
|
|
Client: ctx.ClientVersion(),
|
|
|
|
RemoteAddr: ctx.RemoteAddr().String(),
|
|
|
|
ID: uuid.New().String(),
|
|
|
|
Description: beelzebubServiceConfiguration.Description,
|
2022-05-09 23:18:20 +02:00
|
|
|
})
|
2022-05-08 20:49:53 +02:00
|
|
|
matched, err := regexp.MatchString(beelzebubServiceConfiguration.PasswordRegex, password)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error regex: %s, %s", beelzebubServiceConfiguration.PasswordRegex, err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return matched
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := server.ListenAndServe()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error during init SSH Protocol: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"port": beelzebubServiceConfiguration.Address,
|
|
|
|
"commands": len(beelzebubServiceConfiguration.Commands),
|
2023-10-09 01:16:53 +02:00
|
|
|
}).Infof("GetInstance service %s", beelzebubServiceConfiguration.Protocol)
|
2022-05-08 20:49:53 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildPrompt(user string, serverName string) string {
|
|
|
|
return fmt.Sprintf("%s@%s:~$ ", user, serverName)
|
|
|
|
}
|