Skip to content

Commit d582f82

Browse files
committed
added ssl test
1 parent 8f0f27f commit d582f82

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed

.github/workflows/sandbox2.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Deploy to Sandbox
2+
run-name: "@${{ github.triggering_actor }}: ${{ github.ref_name }}: ${{ github.event_name }}"
3+
on:
4+
push:
5+
branches:
6+
- test-build
7+
defaults:
8+
run:
9+
shell: bash
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.ref_name }}
12+
cancel-in-progress: true
13+
permissions:
14+
packages: write
15+
contents: read
16+
pull-requests: read
17+
jobs:
18+
deploy-sandbox:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Checkout Code
22+
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
23+
with:
24+
fetch-depth: 2
25+
- name: Build Docker image
26+
run: |
27+
shopt -s nocasematch
28+
[[ ${GITHUB_REF_TYPE} == "tag" ]] &&
29+
VERSION=${GITHUB_REF_NAME} ||
30+
VERSION=$(git rev-parse --short "${COMMIT_SHA}")
31+
IMAGE_NAME=ghcr.io/firetail-io/kubernetes-sensor:tb-${VERSION}
32+
IMAGE_NAME=$(echo "$IMAGE_NAME" | tr '[:upper:]' '[:lower:]')
33+
echo "IMAGE_NAME=$IMAGE_NAME" >> $GITHUB_ENV
34+
env:
35+
# pull-requests don't use github.sha for some reason
36+
COMMIT_SHA: ${{github.event.pull_request.head.sha || github.sha}}
37+
- uses: docker/login-action@v3
38+
with:
39+
registry: ghcr.io
40+
username: ${{ github.actor }}
41+
password: ${{ secrets.GITHUB_TOKEN }}
42+
- name: Build Docker image
43+
run: |
44+
docker build --platform linux/amd64 -f build_setup/Dockerfile2 -t $IMAGE_NAME .
45+
docker push $IMAGE_NAME

build_setup/Dockerfile2

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM golang:1.24-bullseye
2+
WORKDIR /src
3+
RUN apt-get update && apt-get install -y --no-install-recommends libpcap-dev
4+
RUN apt-get install linux-headers-$(uname -r) libc6-dev
5+
COPY ./src2/go.* ./
6+
RUN go mod download
7+
COPY ./src2/ ./
8+
RUN go build -o /dist/main .
9+
RUN rm -rf /src/*
10+
RUN chmod +x /dist/main
11+
CMD ["/dist/main"]

src2/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/cilium/ebpf
2+
3+
go 1.18

src2/main.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
_ "embed"
6+
"encoding/binary"
7+
"fmt"
8+
"log"
9+
"os"
10+
11+
"github.com/cilium/ebpf"
12+
"github.com/cilium/ebpf/link"
13+
"github.com/cilium/ebpf/ringbuf"
14+
"golang.org/x/sys/unix"
15+
)
16+
17+
//go:embed ssl_read.o
18+
var bpfBytecode []byte
19+
20+
type sslEvent struct {
21+
PidTgid uint64
22+
SslPtr uint64
23+
Buffer uint64
24+
Num int32
25+
_ [4]byte // padding
26+
}
27+
28+
func main() {
29+
spec, err := ebpf.LoadCollectionSpecFromReader(bytes.NewReader(bpfBytecode))
30+
if err != nil {
31+
log.Fatalf("loading spec: %v", err)
32+
}
33+
34+
objs := struct {
35+
SslReadEnterV3 *ebpf.Program `ebpf:"ssl_read_enter_v3"`
36+
Events *ebpf.Map `ebpf:"events"`
37+
}{}
38+
39+
if err := spec.LoadAndAssign(&objs, nil); err != nil {
40+
log.Fatalf("loading objects: %v", err)
41+
}
42+
defer objs.SslReadEnterV3.Close()
43+
defer objs.Events.Close()
44+
45+
// Change this path based on your system's OpenSSL path
46+
libssl := "/usr/lib/x86_64-linux-gnu/libssl.so.1.1"
47+
48+
up, err := link.OpenExecutable(libssl)
49+
if err != nil {
50+
log.Fatalf("open executable: %v", err)
51+
}
52+
53+
// Attach uprobe to SSL_read
54+
uprober, err := up.Uprobe("SSL_read", objs.SslReadEnterV3, nil)
55+
if err != nil {
56+
log.Fatalf("attach uprobe: %v", err)
57+
}
58+
defer uprober.Close()
59+
60+
// Read events
61+
rd, err := ringbuf.NewReader(objs.Events)
62+
if err != nil {
63+
log.Fatalf("create ringbuf reader: %v", err)
64+
}
65+
defer rd.Close()
66+
67+
log.Println("Waiting for SSL_read calls...")
68+
69+
for {
70+
record, err := rd.Read()
71+
if err != nil {
72+
log.Fatalf("read ringbuf: %v", err)
73+
}
74+
75+
var evt sslEvent
76+
if err := binary.Read(bytes.NewBuffer(record.RawSample), binary.LittleEndian, &evt); err != nil {
77+
log.Printf("decode event: %v", err)
78+
continue
79+
}
80+
81+
pid := evt.PidTgid >> 32
82+
fmt.Printf("SSL_read: pid=%d ssl=0x%x buf=0x%x num=%d\n", pid, evt.SslPtr, evt.Buffer, evt.Num)
83+
}
84+
}

src2/ssl_read.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <linux/bpf.h>
2+
#include <bpf/bpf_helpers.h>
3+
4+
#define TLS_MASK 0x100000000ULL
5+
6+
struct {
7+
__uint(type, BPF_MAP_TYPE_RINGBUF);
8+
__uint(max_entries, 1 << 24);
9+
} events SEC(".maps");
10+
11+
struct ssl_event_t {
12+
__u64 pid_tgid;
13+
__u64 ssl_ptr;
14+
__u64 buffer;
15+
int num;
16+
};
17+
18+
SEC("uprobe/SSL_read")
19+
int BPF_UPROBE(ssl_read_enter_v3, void* ssl, void* buffer, int num) {
20+
struct ssl_event_t *event;
21+
event = bpf_ringbuf_reserve(&events, sizeof(*event), 0);
22+
if (!event) return 0;
23+
24+
event->pid_tgid = bpf_get_current_pid_tgid();
25+
event->ssl_ptr = (unsigned long)ssl;
26+
event->buffer = (unsigned long)buffer;
27+
event->num = num;
28+
29+
bpf_ringbuf_submit(event, 0);
30+
return 0;
31+
}
32+
33+
char LICENSE[] SEC("license") = "Dual BSD/GPL";

0 commit comments

Comments
 (0)