-
Notifications
You must be signed in to change notification settings - Fork 838
/
probe_openssl_text.go
219 lines (198 loc) · 5.64 KB
/
probe_openssl_text.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package module
import (
"errors"
"github.com/cilium/ebpf"
manager "github.com/gojue/ebpfmanager"
"github.com/gojue/ecapture/user/config"
"github.com/gojue/ecapture/user/event"
"golang.org/x/sys/unix"
"math"
"os"
"path"
"strings"
)
func (m *MOpenSSLProbe) setupManagersText() error {
var libPthread, binaryPath, sslVersion string
sslVersion = m.conf.(*config.OpensslConfig).SslVersion
sslVersion = strings.ToLower(sslVersion)
switch m.conf.(*config.OpensslConfig).ElfType {
//case config.ElfTypeBin:
// binaryPath = m.conf.(*config.OpensslConfig).Curlpath
case config.ElfTypeSo:
binaryPath = m.conf.(*config.OpensslConfig).Openssl
err := m.getSslBpfFile(binaryPath, sslVersion)
if err != nil {
return err
}
default:
//如果没找到
binaryPath = path.Join(defaultSoPath, "libssl.so.1.1")
err := m.getSslBpfFile(binaryPath, sslVersion)
if err != nil {
return err
}
}
libPthread = m.conf.(*config.OpensslConfig).Pthread
if libPthread == "" {
//libPthread = "/lib/x86_64-linux-gnu/libpthread.so.0"
m.logger.Printf("%s\tlibPthread path not found, IP info lost.\n", m.Name())
}
_, err := os.Stat(binaryPath)
if err != nil {
return err
}
m.logger.Printf("%s\tHOOK type:%d, binrayPath:%s\n", m.Name(), m.conf.(*config.OpensslConfig).ElfType, binaryPath)
m.logger.Printf("%s\tHook masterKey function:%s\n", m.Name(), m.masterHookFuncs)
m.bpfManager = &manager.Manager{
Probes: []*manager.Probe{
{
Section: "uprobe/SSL_write",
EbpfFuncName: "probe_entry_SSL_write",
AttachToFuncName: "SSL_write",
BinaryPath: binaryPath,
},
{
Section: "uretprobe/SSL_write",
EbpfFuncName: "probe_ret_SSL_write",
AttachToFuncName: "SSL_write",
BinaryPath: binaryPath,
},
{
Section: "uprobe/SSL_read",
EbpfFuncName: "probe_entry_SSL_read",
AttachToFuncName: "SSL_read",
BinaryPath: binaryPath,
},
{
Section: "uretprobe/SSL_read",
EbpfFuncName: "probe_ret_SSL_read",
AttachToFuncName: "SSL_read",
BinaryPath: binaryPath,
},
// --------------------------------------------------
//{
// Section: "uprobe/connect",
// EbpfFuncName: "probe_connect",
// AttachToFuncName: "connect",
// BinaryPath: libPthread,
//},
// --------------------------------------------------
// openssl masterkey
/*{
Section: "uprobe/SSL_write_key",
EbpfFuncName: "probe_ssl_master_key",
AttachToFuncName: m.masterHookFuncs,
BinaryPath: binaryPath,
UID: "uprobe_ssl_master_key",
},*/
// ------------------- SSL_set_fd hook-------------------------------------
{
Section: "uprobe/SSL_set_fd",
EbpfFuncName: "probe_SSL_set_fd",
AttachToFuncName: "SSL_set_fd",
BinaryPath: binaryPath,
UID: "uprobe_ssl_set_fd",
},
{
Section: "uprobe/SSL_set_rfd",
EbpfFuncName: "probe_SSL_set_fd",
AttachToFuncName: "SSL_set_rfd",
BinaryPath: binaryPath,
UID: "uprobe_ssl_set_rfd",
},
{
Section: "uprobe/SSL_set_wfd",
EbpfFuncName: "probe_SSL_set_fd",
AttachToFuncName: "SSL_set_wfd",
BinaryPath: binaryPath,
UID: "uprobe_ssl_set_wfd",
},
},
Maps: []*manager.Map{
{
Name: "tls_events",
},
{
Name: "connect_events",
},
//{
// Name: "mastersecret_events",
//},
},
}
if libPthread != "" {
// detect libpthread.so path
_, err = os.Stat(libPthread)
if err == nil {
m.logger.Printf("%s\tlibPthread:%s\n", m.Name(), libPthread)
m.bpfManager.Probes = append(m.bpfManager.Probes, &manager.Probe{
Section: "uprobe/connect",
EbpfFuncName: "probe_connect",
AttachToFuncName: "connect",
BinaryPath: libPthread,
UID: "uprobe_connect",
})
}
}
m.bpfManagerOptions = manager.Options{
DefaultKProbeMaxActive: 512,
VerifierOptions: ebpf.CollectionOptions{
Programs: ebpf.ProgramOptions{
LogSize: 2097152,
},
},
RLimit: &unix.Rlimit{
Cur: math.MaxUint64,
Max: math.MaxUint64,
},
}
if m.conf.EnableGlobalVar() {
// 填充 RewriteContants 对应map
m.bpfManagerOptions.ConstantEditors = m.constantEditor()
} else {
m.logger.Printf("%s\tYour kernel version is less than 5.2, the following parameters will be ignored:[target_pid, target_uid, target_port]\n", m.Name())
}
return nil
}
func (m *MOpenSSLProbe) initDecodeFunText() error {
//SSLDumpEventsMap 与解码函数映射
SSLDumpEventsMap, found, err := m.bpfManager.GetMap("tls_events")
if err != nil {
return err
}
if !found {
return errors.New("cant found map:tls_events")
}
m.eventMaps = append(m.eventMaps, SSLDumpEventsMap)
sslEvent := &event.SSLDataEvent{}
m.eventFuncMaps[SSLDumpEventsMap] = sslEvent
ConnEventsMap, found, err := m.bpfManager.GetMap("connect_events")
if err != nil {
return err
}
if !found {
return errors.New("cant found map:connect_events")
}
m.eventMaps = append(m.eventMaps, ConnEventsMap)
connEvent := &event.ConnDataEvent{}
m.eventFuncMaps[ConnEventsMap] = connEvent
/*
MasterkeyEventsMap, found, err := m.bpfManager.GetMap("mastersecret_events")
if err != nil {
return err
}
if !found {
return errors.New("cant found map:mastersecret_events")
}
m.eventMaps = append(m.eventMaps, MasterkeyEventsMap)
var masterkeyEvent event.IEventStruct
if m.isBoringSSL {
masterkeyEvent = &event.MasterSecretBSSLEvent{}
} else {
masterkeyEvent = &event.MasterSecretEvent{}
}
//masterkeyEvent.SetModule(m)
m.eventFuncMaps[MasterkeyEventsMap] = masterkeyEvent
*/
return nil
}