-
Notifications
You must be signed in to change notification settings - Fork 838
/
probe_gotls_pcap.go
179 lines (153 loc) · 4.5 KB
/
probe_gotls_pcap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2022 CFC4N <cfc4n.cs@gmail.com>. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package module
import (
"errors"
"fmt"
"math"
"net"
"github.com/gojue/ecapture/user/config"
"github.com/gojue/ecapture/user/event"
"github.com/cilium/ebpf"
manager "github.com/gojue/ebpfmanager"
"golang.org/x/sys/unix"
)
const (
tcFuncNameIngress = "ingress_cls_func"
tcFuncNameEgress = "egress_cls_func"
)
func (g *GoTLSProbe) setupManagersPcap() error {
var ifname string
ifname = g.conf.(*config.GoTLSConfig).Ifname
g.ifName = ifname
interf, err := net.InterfaceByName(g.ifName)
if err != nil {
return err
}
// loopback devices are special, some tc probes should be skipped
isNetIfaceLo := interf.Flags&net.FlagLoopback == net.FlagLoopback
skipLoopback := true // TODO: detect loopback devices via aquasecrity/tracee/pkg/ebpf/probes/probe.go line 322
if isNetIfaceLo && skipLoopback {
return fmt.Errorf("%s\t%s is a loopback interface, skip it", g.Name(), g.ifName)
}
g.ifIdex = interf.Index
pcapFilter := g.conf.(*config.GoTLSConfig).PcapFilter
g.logger.Printf("%s\tHOOK type:golang elf, binrayPath:%s\n", g.Name(), g.path)
g.logger.Printf("%s\tPcapFilter: %s\n", g.Name(), pcapFilter)
g.logger.Printf("%s\tIfname: %s, Ifindex: %d\n", g.Name(), g.ifName, g.ifIdex)
g.logger.Printf("%s\tHook masterKey function: %s, Address:%x \n", g.Name(), config.GoTlsMasterSecretFunc, g.conf.(*config.GoTLSConfig).GoTlsMasterSecretAddr)
// create pcapng writer
netIfs, err := net.Interfaces()
if err != nil {
return err
}
err = g.createPcapng(netIfs)
if err != nil {
return err
}
// Serve pcapng writer to flush pcapng file
go func() {
g.ServePcap()
}()
var (
sec string
fn string
)
if g.isRegisterABI {
sec = "uprobe/gotls_mastersecret_register"
fn = "gotls_mastersecret_register"
} else {
sec = "uprobe/gotls_mastersecret_stack"
fn = "gotls_mastersecret_stack"
}
g.bpfManager = &manager.Manager{
Probes: []*manager.Probe{
{
Section: "classifier/egress",
EbpfFuncName: tcFuncNameEgress,
Ifname: g.ifName,
NetworkDirection: manager.Egress,
},
{
Section: "classifier/ingress",
EbpfFuncName: tcFuncNameIngress,
Ifname: g.ifName,
NetworkDirection: manager.Ingress,
},
// --------------------------------------------------
// gotls master secrets
{
Section: sec,
EbpfFuncName: fn,
AttachToFuncName: config.GoTlsMasterSecretFunc,
BinaryPath: g.path,
UID: "uprobe_gotls_master_secret",
UAddress: g.conf.(*config.GoTLSConfig).GoTlsMasterSecretAddr,
},
},
Maps: []*manager.Map{
{
Name: "mastersecret_go_events",
},
{
Name: "skb_events",
},
},
}
g.bpfManagerOptions = manager.Options{
DefaultKProbeMaxActive: 512,
VerifierOptions: ebpf.CollectionOptions{
Programs: ebpf.ProgramOptions{
LogSize: 2097152,
},
},
RLimit: &unix.Rlimit{
Cur: math.MaxUint64,
Max: math.MaxUint64,
},
}
if g.conf.EnableGlobalVar() {
// 填充 RewriteContants 对应map
g.bpfManagerOptions.ConstantEditors = g.constantEditor()
}
return nil
}
func (g *GoTLSProbe) initDecodeFunPcap() error {
// SkbEventsMap 与解码函数映射
SkbEventsMap, found, err := g.bpfManager.GetMap("skb_events")
if err != nil {
return err
}
if !found {
return errors.New("cant found map:skb_events")
}
g.eventMaps = append(g.eventMaps, SkbEventsMap)
sslEvent := &event.TcSkbEvent{}
// sslEvent.SetModule(g)
g.eventFuncMaps[SkbEventsMap] = sslEvent
// master secrets map at ebpf code
MasterkeyEventsMap, found, err := g.bpfManager.GetMap("mastersecret_go_events")
if err != nil {
return err
}
if !found {
return errors.New("cant found map:mastersecret_events")
}
g.eventMaps = append(g.eventMaps, MasterkeyEventsMap)
var masterkeyEvent event.IEventStruct
// goTLS Event struct
masterkeyEvent = &event.MasterSecretGotlsEvent{}
g.eventFuncMaps[MasterkeyEventsMap] = masterkeyEvent
return nil
}