Skip to content

Commit

Permalink
Add support for TLS based HTTP connections. (#158)
Browse files Browse the repository at this point in the history
* Add support for TLS based HTTP connections, With Unit Tests.
  • Loading branch information
bryannolen authored Feb 13, 2025
1 parent 48dd70d commit db80447
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
2 changes: 2 additions & 0 deletions parser/configurations_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type BeelzebubServiceConfiguration struct {
Description string `yaml:"description"`
Banner string `yaml:"banner"`
Plugin Plugin `yaml:"plugin"`
TLSCertPath string `yaml:"tlsCertPath"`
TLSKeyPath string `yaml:"tlsKeyPath"`
}

// Command is the struct that contains the configurations of the commands
Expand Down
4 changes: 4 additions & 0 deletions parser/configurations_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func mockReadfilebytesBeelzebubServiceConfiguration(filePath string) ([]byte, er
apiVersion: "v1"
protocol: "http"
address: ":8080"
tlsCertPath: "/tmp/cert.crt"
tlsKeyPath: "/tmp/cert.key"
commands:
- regex: "wp-admin"
handler: "login"
Expand Down Expand Up @@ -135,6 +137,8 @@ func TestReadConfigurationsServicesValid(t *testing.T) {
assert.Equal(t, firstBeelzebubServiceConfiguration.Plugin.LLMModel, "llama3")
assert.Equal(t, firstBeelzebubServiceConfiguration.Plugin.Host, "localhost:1563")
assert.Equal(t, firstBeelzebubServiceConfiguration.Plugin.Prompt, "hello world")
assert.Equal(t, firstBeelzebubServiceConfiguration.TLSCertPath, "/tmp/cert.crt")
assert.Equal(t, firstBeelzebubServiceConfiguration.TLSKeyPath, "/tmp/cert.key")
}

func TestGelAllFilesNameByDirName(t *testing.T) {
Expand Down
32 changes: 25 additions & 7 deletions protocols/strategies/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package strategies

import (
"fmt"
"github.com/mariocandela/beelzebub/v3/parser"
"github.com/mariocandela/beelzebub/v3/plugins"
"github.com/mariocandela/beelzebub/v3/tracer"
"io"
"net"
"net/http"
"regexp"
"strings"

"github.com/google/uuid"
"github.com/mariocandela/beelzebub/v3/parser"
"github.com/mariocandela/beelzebub/v3/plugins"
"github.com/mariocandela/beelzebub/v3/tracer"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -67,13 +67,25 @@ func (httpStrategy HTTPStrategy) Init(beelzebubServiceConfiguration parser.Beelz
}

setResponseHeaders(responseWriter, command.Headers, command.StatusCode)
fmt.Fprintf(responseWriter, responseHTTPBody)
fmt.Fprint(responseWriter, responseHTTPBody)
break
}
}
})
go func() {
err := http.ListenAndServe(httpStrategy.beelzebubServiceConfiguration.Address, serverMux)
var err error
// Launch a TLS supporting server if we are supplied a TLS Key and Certificate.
// If relative paths are supplied, they are relative to the CWD of the binary.
// The can be self-signed, only the client will validate this (or not).
if httpStrategy.beelzebubServiceConfiguration.TLSKeyPath != "" && httpStrategy.beelzebubServiceConfiguration.TLSCertPath != "" {
err = http.ListenAndServeTLS(
httpStrategy.beelzebubServiceConfiguration.Address,
httpStrategy.beelzebubServiceConfiguration.TLSCertPath,
httpStrategy.beelzebubServiceConfiguration.TLSKeyPath,
serverMux)
} else {
err = http.ListenAndServe(httpStrategy.beelzebubServiceConfiguration.Address, serverMux)
}
if err != nil {
log.Errorf("Error during init HTTP Protocol: %s", err.Error())
return
Expand All @@ -95,7 +107,7 @@ func traceRequest(request *http.Request, tr tracer.Tracer, HoneypotDescription s
}
host, port, _ := net.SplitHostPort(request.RemoteAddr)

tr.TraceEvent(tracer.Event{
event := tracer.Event{
Msg: "HTTP New request",
RequestURI: request.RequestURI,
Protocol: tracer.HTTP.String(),
Expand All @@ -111,7 +123,13 @@ func traceRequest(request *http.Request, tr tracer.Tracer, HoneypotDescription s
SourcePort: port,
ID: uuid.New().String(),
Description: HoneypotDescription,
})
}
// Capture the TLS details from the request, if provided.
if request.TLS != nil {
event.Msg = "HTTPS New Request"
event.TLSServerName = request.TLS.ServerName
}
tr.TraceEvent(event)
}

func mapHeaderToString(headers http.Header) string {
Expand Down
4 changes: 3 additions & 1 deletion tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
package tracer

import (
log "github.com/sirupsen/logrus"
"sync"
"time"

log "github.com/sirupsen/logrus"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
Expand Down Expand Up @@ -36,6 +37,7 @@ type Event struct {
Description string
SourceIp string
SourcePort string
TLSServerName string
}

type (
Expand Down

0 comments on commit db80447

Please sign in to comment.