first commit

This commit is contained in:
Mario
2022-05-08 20:49:53 +02:00
commit 7b7eeb3e15
21 changed files with 1314 additions and 0 deletions

View File

@ -0,0 +1,90 @@
package protocols
import (
"beelzebub/parser"
"fmt"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"regexp"
"strings"
)
type HypertextTransferProtocolStrategy struct {
serverMux *http.ServeMux
beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration
}
func (httpStrategy HypertextTransferProtocolStrategy) Init(beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration) error {
httpStrategy.beelzebubServiceConfiguration = beelzebubServiceConfiguration
httpStrategy.serverMux = http.NewServeMux()
httpStrategy.buildHandler()
go func() {
httpStrategy.listenAndServe()
}()
log.WithFields(log.Fields{
"port": beelzebubServiceConfiguration.Address,
"commands": len(beelzebubServiceConfiguration.Commands),
}).Infof("Init service %s", beelzebubServiceConfiguration.Protocol)
return nil
}
func (httpStrategy HypertextTransferProtocolStrategy) listenAndServe() {
err := http.ListenAndServe(httpStrategy.beelzebubServiceConfiguration.Address, httpStrategy.serverMux)
if err != nil {
log.Errorf("Error during init HTTP Protocol: %s", err.Error())
return
}
}
func (httpStrategy HypertextTransferProtocolStrategy) buildHandler() {
httpStrategy.serverMux.HandleFunc("/", func(responseWriter http.ResponseWriter, request *http.Request) {
traceRequest(request)
for _, command := range httpStrategy.beelzebubServiceConfiguration.Commands {
matched, err := regexp.MatchString(command.Regex, request.RequestURI)
if err != nil {
log.Errorf("Error regex: %s, %s", command.Regex, err.Error())
continue
}
if matched {
setResponseHeaders(responseWriter, command.Headers, command.StatusCode)
fmt.Fprintf(responseWriter, command.Handler)
break
}
}
})
}
func traceRequest(request *http.Request) {
bodyBytes, err := io.ReadAll(request.Body)
body := ""
if err == nil {
body = string(bodyBytes)
}
log.WithFields(log.Fields{
"requestURI": request.RequestURI,
"proto": request.Proto,
"method": request.Method,
"body": body,
"host": request.Host,
"userAgent": request.UserAgent(),
"cookies": request.Cookies(),
"ip": request.RemoteAddr,
"headers": request.Header,
"remoteAddr": request.RemoteAddr,
}).Info("New HTTP request")
}
func setResponseHeaders(responseWriter http.ResponseWriter, headers []string, statusCode int) {
responseWriter.WriteHeader(statusCode)
for _, headerStr := range headers {
keyValue := strings.Split(headerStr, ":")
if len(keyValue) > 1 {
responseWriter.Header().Add(keyValue[0], keyValue[1])
}
}
}

View File

@ -0,0 +1,19 @@
package protocols
import "beelzebub/parser"
type ProtocolManager struct {
strategy ServiceStrategy
}
func (pm *ProtocolManager) InitServiceManager() *ProtocolManager {
return &ProtocolManager{}
}
func (pm *ProtocolManager) SetProtocolStrategy(strategy ServiceStrategy) {
pm.strategy = strategy
}
func (pm *ProtocolManager) InitService(beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration) error {
return pm.strategy.Init(beelzebubServiceConfiguration)
}

View File

@ -0,0 +1,7 @@
package protocols
import "beelzebub/parser"
type ServiceStrategy interface {
Init(beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration) error
}

View File

@ -0,0 +1,113 @@
package protocols
import (
"beelzebub/parser"
"fmt"
"github.com/gliderlabs/ssh"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"
"regexp"
"time"
)
type SecureShellStrategy struct {
}
func (SSHStrategy *SecureShellStrategy) Init(beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration) error {
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()
traceSessionStart(sess, uuidSession)
term := terminal.NewTerminal(sess, buildPrompt(sess.User(), beelzebubServiceConfiguration.ServerName))
for {
commandInput, err := term.ReadLine()
if err != nil {
break
}
traceCommand(commandInput, uuidSession)
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 {
term.Write(append([]byte(command.Handler), '\n'))
break
}
}
}
traceSessionEnd(sess, uuidSession)
},
PasswordHandler: func(ctx ssh.Context, password string) bool {
traceAttempt(ctx, password)
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),
}).Infof("Init service %s", beelzebubServiceConfiguration.Protocol)
return nil
}
func buildPrompt(user string, serverName string) string {
return fmt.Sprintf("%s@%s:~$ ", user, serverName)
}
func traceAttempt(ctx ssh.Context, password string) {
log.WithFields(log.Fields{
"remoteAddr": ctx.RemoteAddr(),
"user": ctx.User(),
"password": password,
"client": ctx.ClientVersion(),
}).Info("New SSH attempt")
}
func traceSessionStart(sess ssh.Session, uuidSession uuid.UUID) {
log.WithFields(log.Fields{
"uuidSession": uuidSession,
"remoteAddr": sess.RemoteAddr(),
"command": sess.Command(),
"environ": sess.Environ(),
"user": sess.User(),
}).Info("New SSH Session")
}
func traceSessionEnd(sess ssh.Session, uuidSession uuid.UUID) {
log.WithFields(log.Fields{
"uuidSession": uuidSession,
"remoteAddr": sess.RemoteAddr(),
"command": sess.Command(),
"environ": sess.Environ(),
"user": sess.User(),
}).Info("End SSH Session")
}
func traceCommand(command string, uuidSession uuid.UUID) {
log.WithFields(log.Fields{
"uuidSession": uuidSession,
"command": command,
}).Info("New SSH Command")
}