Skip to content

Commit fd87964

Browse files
authored
Merge PR #186 into Spicy continuation branch
Introduce Spicy parser framework with HTTP parser implementation
2 parents 1e53480 + 4001b4a commit fd87964

17 files changed

Lines changed: 1468 additions & 8 deletions

File tree

.github/workflows/workflow.yml

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,47 @@ jobs:
1010
checks:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- name: Dependencies
14-
run: sudo apt install libpcap-dev iptables
13+
- name: Basic Dependencies
14+
run: |
15+
sudo apt-get update
16+
sudo apt-get install -y libpcap-dev iptables zlib1g-dev build-essential
17+
18+
- name: Install spicyc
19+
run: |
20+
wget https://github.com/zeek/spicy/releases/download/v1.13.1/spicy_linux_ubuntu24.deb
21+
sudo dpkg --install spicy_linux_ubuntu24.deb
22+
sudo apt-get install -f -y # pulling in any missing deps
23+
rm spicy_linux_ubuntu24.deb
24+
25+
- name: Add Spicy CLI to PATH
26+
run: echo "/opt/spicy/bin" >> $GITHUB_PATH
27+
28+
- name: Install clang17
29+
run: |
30+
wget https://apt.llvm.org/llvm.sh
31+
chmod +x llvm.sh
32+
sudo ./llvm.sh 17
1533
1634
- name: Checkout
1735
uses: actions/checkout@v3
1836

1937
- name: Set up Go
2038
uses: actions/setup-go@v4
2139
with:
22-
go-version: "^1.21"
40+
go-version: "^1.23"
41+
42+
- name: Build Spicy generated files
43+
run: |
44+
make spicy
2345
2446
- name: Build
47+
env:
48+
CC: clang
49+
CXX: clang++
2550
run: go build -v ./...
2651

2752
- name: Test
53+
env:
54+
CC: clang
55+
CXX: clang++
2856
run: go test -v ./...

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ poc/
3737

3838
# Dev
3939
.vscode
40+
41+
# Spicy generated files
42+
protocols/spicy/*.cc
43+
protocols/spicy/parsers/*.h

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ upx:
2020
default: build
2121

2222
build:
23-
go build -ldflags=$(LDFLAGS) -o bin/server app/server.go
23+
CC=clang CXX=clang++ go build -ldflags=$(LDFLAGS) -o bin/server app/server.go
24+
25+
spicy:
26+
cd protocols/spicy && make
2427

2528
static:
2629
go build --ldflags '-extldflags "-static"' -o bin/server app/server.go

config/config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ producers:
2424

2525
conn_timeout: 45
2626
max_tcp_payload: 4096
27+
28+
spicy:
29+
enabled: true

glutton.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/mushorg/glutton/connection"
1919
"github.com/mushorg/glutton/producer"
2020
"github.com/mushorg/glutton/protocols"
21+
"github.com/mushorg/glutton/protocols/spicy"
2122
"github.com/mushorg/glutton/rules"
2223

2324
"github.com/google/uuid"
@@ -134,6 +135,13 @@ func (g *Glutton) Init() error {
134135
g.tcpProtocolHandlers = protocols.MapTCPProtocolHandlers(g.Logger, g)
135136
g.udpProtocolHandlers = protocols.MapUDPProtocolHandlers(g.Logger, g)
136137

138+
// Initializing Spicy parsers
139+
if viper.GetBool("spicy.enabled") {
140+
if err := spicy.Initialize(g.Logger); err != nil {
141+
return fmt.Errorf("failed to initialize Spicy: %w", err)
142+
}
143+
}
144+
137145
return nil
138146
}
139147

@@ -358,7 +366,12 @@ func (g *Glutton) Shutdown() {
358366
if err := flushTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "udp", uint32(g.Server.udpPort), uint32(viper.GetInt("ports.ssh"))); err != nil {
359367
g.Logger.Error("Failed to drop udp iptables", producer.ErrAttr(err))
360368
}
361-
369+
if viper.GetBool("spicy.enabled") {
370+
g.Logger.Info("Cleaning up and shutting down Spicy and HILTI runtimes")
371+
if err := spicy.Cleanup(); err != nil {
372+
g.Logger.Error("Failed to clean up Spicy and HILTI runtimes", producer.ErrAttr(err))
373+
}
374+
}
362375
g.Logger.Info("All done")
363376
}
364377

protocols/protocols.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010
"github.com/mushorg/glutton/connection"
1111
"github.com/mushorg/glutton/producer"
1212
"github.com/mushorg/glutton/protocols/interfaces"
13+
spicyHandlers "github.com/mushorg/glutton/protocols/spicy/handlers"
1314
"github.com/mushorg/glutton/protocols/tcp"
1415
"github.com/mushorg/glutton/protocols/udp"
16+
"github.com/spf13/viper"
1517
)
1618

1719
type TCPHandlerFunc func(ctx context.Context, conn net.Conn, md connection.Metadata) error
@@ -84,7 +86,11 @@ func MapTCPProtocolHandlers(log interfaces.Logger, h interfaces.Honeypot) map[st
8486
// poor mans check for HTTP request
8587
httpMap := map[string]bool{"GET ": true, "POST": true, "HEAD": true, "OPTI": true, "CONN": true}
8688
if _, ok := httpMap[strings.ToUpper(string(snip))]; ok {
87-
return tcp.HandleHTTP(ctx, bufConn, md, log, h)
89+
if viper.GetBool("spicy.enabled") {
90+
return spicyHandlers.HandleHTTP(ctx, bufConn, md, log, h)
91+
} else {
92+
return tcp.HandleHTTP(ctx, bufConn, md, log, h)
93+
}
8894
}
8995
// poor mans check for RDP header
9096
if bytes.Equal(snip, []byte{0x03, 0x00, 0x00, 0x2b}) {

protocols/spicy/Makefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
GRAMMARS := $(wildcard parsers/*.spicy)
2+
3+
GEN_CC := $(notdir $(GRAMMARS:.spicy=.cc))
4+
LINKER_CC := $(notdir $(patsubst parsers/%.spicy,spicy_linker_%.cc,$(GRAMMARS)))
5+
HEADERS := $(patsubst parsers/%.spicy,parsers/%.h,$(GRAMMARS))
6+
7+
SPICY_FLAGS := -g
8+
CXX ?= clang++
9+
CXXFLAGS += -I/opt/spicy/include -std=c++17 -fPIC -O3 -DNDEBUG -fvisibility=hidden -I$(CURDIR)/parsers
10+
11+
.SECONDARY: $(GEN_CC) $(LINKER_CC) $(HEADERS)
12+
13+
%.cc: parsers/%.spicy
14+
@echo "spicyc -c $< -> $@"
15+
spicyc -c $(SPICY_FLAGS) $< -o $@
16+
17+
parsers/%.h: parsers/%.spicy
18+
@echo "spicyc -P parsers/$* -o $@ $<"
19+
spicyc -P parsers/$* -o $@ $<
20+
21+
spicy_linker_%.cc: parsers/%.spicy %.cc
22+
@echo "spicyc -l $< -> $@"
23+
spicyc -l $(SPICY_FLAGS) $< -o $@
24+
25+
.PHONY: all
26+
all: $(GEN_CC) $(LINKER_CC) $(HEADERS)
27+
28+
.PHONY: clean
29+
clean:
30+
rm -f $(GEN_CC) $(LINKER_CC) $(HEADERS)

0 commit comments

Comments
 (0)