Skip to content

Commit db80447

Browse files
authored
Add support for TLS based HTTP connections. (#158)
* Add support for TLS based HTTP connections, With Unit Tests.
1 parent 48dd70d commit db80447

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

parser/configurations_parser.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ type BeelzebubServiceConfiguration struct {
6868
Description string `yaml:"description"`
6969
Banner string `yaml:"banner"`
7070
Plugin Plugin `yaml:"plugin"`
71+
TLSCertPath string `yaml:"tlsCertPath"`
72+
TLSKeyPath string `yaml:"tlsKeyPath"`
7173
}
7274

7375
// Command is the struct that contains the configurations of the commands

parser/configurations_parser_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ func mockReadfilebytesBeelzebubServiceConfiguration(filePath string) ([]byte, er
4949
apiVersion: "v1"
5050
protocol: "http"
5151
address: ":8080"
52+
tlsCertPath: "/tmp/cert.crt"
53+
tlsKeyPath: "/tmp/cert.key"
5254
commands:
5355
- regex: "wp-admin"
5456
handler: "login"
@@ -135,6 +137,8 @@ func TestReadConfigurationsServicesValid(t *testing.T) {
135137
assert.Equal(t, firstBeelzebubServiceConfiguration.Plugin.LLMModel, "llama3")
136138
assert.Equal(t, firstBeelzebubServiceConfiguration.Plugin.Host, "localhost:1563")
137139
assert.Equal(t, firstBeelzebubServiceConfiguration.Plugin.Prompt, "hello world")
140+
assert.Equal(t, firstBeelzebubServiceConfiguration.TLSCertPath, "/tmp/cert.crt")
141+
assert.Equal(t, firstBeelzebubServiceConfiguration.TLSKeyPath, "/tmp/cert.key")
138142
}
139143

140144
func TestGelAllFilesNameByDirName(t *testing.T) {

protocols/strategies/http.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ package strategies
22

33
import (
44
"fmt"
5-
"github.com/mariocandela/beelzebub/v3/parser"
6-
"github.com/mariocandela/beelzebub/v3/plugins"
7-
"github.com/mariocandela/beelzebub/v3/tracer"
85
"io"
96
"net"
107
"net/http"
118
"regexp"
129
"strings"
1310

1411
"github.com/google/uuid"
12+
"github.com/mariocandela/beelzebub/v3/parser"
13+
"github.com/mariocandela/beelzebub/v3/plugins"
14+
"github.com/mariocandela/beelzebub/v3/tracer"
1515
log "github.com/sirupsen/logrus"
1616
)
1717

@@ -67,13 +67,25 @@ func (httpStrategy HTTPStrategy) Init(beelzebubServiceConfiguration parser.Beelz
6767
}
6868

6969
setResponseHeaders(responseWriter, command.Headers, command.StatusCode)
70-
fmt.Fprintf(responseWriter, responseHTTPBody)
70+
fmt.Fprint(responseWriter, responseHTTPBody)
7171
break
7272
}
7373
}
7474
})
7575
go func() {
76-
err := http.ListenAndServe(httpStrategy.beelzebubServiceConfiguration.Address, serverMux)
76+
var err error
77+
// Launch a TLS supporting server if we are supplied a TLS Key and Certificate.
78+
// If relative paths are supplied, they are relative to the CWD of the binary.
79+
// The can be self-signed, only the client will validate this (or not).
80+
if httpStrategy.beelzebubServiceConfiguration.TLSKeyPath != "" && httpStrategy.beelzebubServiceConfiguration.TLSCertPath != "" {
81+
err = http.ListenAndServeTLS(
82+
httpStrategy.beelzebubServiceConfiguration.Address,
83+
httpStrategy.beelzebubServiceConfiguration.TLSCertPath,
84+
httpStrategy.beelzebubServiceConfiguration.TLSKeyPath,
85+
serverMux)
86+
} else {
87+
err = http.ListenAndServe(httpStrategy.beelzebubServiceConfiguration.Address, serverMux)
88+
}
7789
if err != nil {
7890
log.Errorf("Error during init HTTP Protocol: %s", err.Error())
7991
return
@@ -95,7 +107,7 @@ func traceRequest(request *http.Request, tr tracer.Tracer, HoneypotDescription s
95107
}
96108
host, port, _ := net.SplitHostPort(request.RemoteAddr)
97109

98-
tr.TraceEvent(tracer.Event{
110+
event := tracer.Event{
99111
Msg: "HTTP New request",
100112
RequestURI: request.RequestURI,
101113
Protocol: tracer.HTTP.String(),
@@ -111,7 +123,13 @@ func traceRequest(request *http.Request, tr tracer.Tracer, HoneypotDescription s
111123
SourcePort: port,
112124
ID: uuid.New().String(),
113125
Description: HoneypotDescription,
114-
})
126+
}
127+
// Capture the TLS details from the request, if provided.
128+
if request.TLS != nil {
129+
event.Msg = "HTTPS New Request"
130+
event.TLSServerName = request.TLS.ServerName
131+
}
132+
tr.TraceEvent(event)
115133
}
116134

117135
func mapHeaderToString(headers http.Header) string {

tracer/tracer.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
package tracer
33

44
import (
5-
log "github.com/sirupsen/logrus"
65
"sync"
76
"time"
87

8+
log "github.com/sirupsen/logrus"
9+
910
"github.com/prometheus/client_golang/prometheus"
1011
"github.com/prometheus/client_golang/prometheus/promauto"
1112
)
@@ -36,6 +37,7 @@ type Event struct {
3637
Description string
3738
SourceIp string
3839
SourcePort string
40+
TLSServerName string
3941
}
4042

4143
type (

0 commit comments

Comments
 (0)