Skip to content

Commit

Permalink
kern: use kprobe __sys_connect instead libc connect.
Browse files Browse the repository at this point in the history
Signed-off-by: CFC4N <cfc4n.cs@gmail.com>
  • Loading branch information
cfc4n committed May 31, 2024
1 parent 1564d61 commit 2f4ea9e
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 118 deletions.
1 change: 0 additions & 1 deletion cli/cmd/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func init() {
// opensslCmd.PersistentFlags().StringVar(&oc.Curlpath, "curl", "", "curl or wget file path, use to dectet openssl.so path, default:/usr/bin/curl. (Deprecated)")
opensslCmd.PersistentFlags().StringVar(&oc.Openssl, "libssl", "", "libssl.so file path, will automatically find it from curl default.")
opensslCmd.PersistentFlags().StringVar(&oc.CGroupPath, "cgroup_path", "/sys/fs/cgroup", "cgroup path, default: /sys/fs/cgroup.")
opensslCmd.PersistentFlags().StringVar(&oc.Pthread, "pthread", "", "libpthread.so file path, use to hook connect to capture socket FD.will automatically find it from curl.")
opensslCmd.PersistentFlags().StringVarP(&oc.Model, "model", "m", "text", "capture model, such as : text, pcap/pcapng, key/keylog")
opensslCmd.PersistentFlags().StringVarP(&oc.KeylogFile, "keylogfile", "k", "ecapture_openssl_key.og", "The file stores SSL/TLS keys, and eCapture captures these keys during encrypted traffic communication and saves them to the file.")
opensslCmd.PersistentFlags().StringVarP(&oc.PcapFile, "pcapfile", "w", "save.pcapng", "write the raw packets to file as pcapng format.")
Expand Down
1 change: 0 additions & 1 deletion user/config/config_openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ type OpensslConfig struct {
BaseConfig
// Curlpath string `json:"curlPath"` //curl的文件路径
Openssl string `json:"openssl"`
Pthread string `json:"pthread"` // /lib/x86_64-linux-gnu/libpthread.so.0
Model string `json:"model"` // eCapture Openssl capture model. text:pcap:keylog
PcapFile string `json:"pcapfile"` // pcapFile the raw packets to file rather than parsing and printing them out.
KeylogFile string `json:"keylog"` // Keylog The file stores SSL/TLS keys, and eCapture captures these keys during encrypted traffic communication and saves them to the file.
Expand Down
9 changes: 0 additions & 9 deletions user/config/config_openssl_androidgki.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,6 @@ func (oc *OpensslConfig) Check() error {
oc.Openssl = DefaultOpensslPath
}

if oc.Pthread != "" || len(strings.TrimSpace(oc.Pthread)) > 0 {
_, e := os.Stat(oc.Pthread)
if e != nil {
return e
}
} else {
oc.Pthread = DefaultLibcPath
}

if oc.Ifname == "" || len(strings.TrimSpace(oc.Ifname)) == 0 {
oc.Ifname = DefaultIfname
}
Expand Down
87 changes: 5 additions & 82 deletions user/config/config_openssl_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
package config

import (
"debug/elf"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -35,11 +33,6 @@ var (
"libssl.so.3", // ubuntu server 22.04
"libssl.so.1.1", // ubuntu server 21.04
}
connectSharedObjects = []string{
"libpthread.so.0", // ubuntu 21.04 server
"libc.so.6", // ubuntu 21.10 server
"libc.so", // Android
}
)

func (oc *OpensslConfig) checkOpenssl() error {
Expand Down Expand Up @@ -73,73 +66,9 @@ func (oc *OpensslConfig) checkOpenssl() error {
return nil
}

func (oc *OpensslConfig) checkConnect() error {

var funcName = ""
var found bool
var e error
for _, so := range connectSharedObjects {
var prefix string
var soLoadPaths = GetDynLibDirs()
for _, soPath := range soLoadPaths {

_, e = os.Stat(soPath)
if e != nil {
continue
}
prefix = soPath
break
}
if prefix == "" {
continue
}
oc.Pthread = filepath.Join(prefix, so)
_, e = os.Stat(oc.Pthread)
if e != nil {
// search all of connectSharedObjects
//return e
continue
}

_elf, e := elf.Open(oc.Pthread)
if e != nil {
//return e
continue
}

dynamicSymbols, err := _elf.DynamicSymbols()
if err != nil {
//return err
continue
}

//
for _, sym := range dynamicSymbols {
if sym.Name != "connect" {
continue
}
funcName = sym.Name
found = true
break
}

// if found
if found && funcName != "" {
break
}
}

//如果没找到,则报错。
if !found || funcName == "" {
oc.Pthread = ""
return errors.New(fmt.Sprintf("cant found 'connect' function to hook in files::%v", connectSharedObjects))
}
return nil
}

func (oc *OpensslConfig) Check() error {
oc.IsAndroid = false
var checkedOpenssl, checkedConnect bool
var checkedOpenssl bool
// 如果readline 配置,且存在,则直接返回。
if oc.Openssl != "" || len(strings.TrimSpace(oc.Openssl)) > 0 {
_, e := os.Stat(oc.Openssl)
Expand All @@ -154,21 +83,15 @@ func (oc *OpensslConfig) Check() error {
oc.Ifname = DefaultIfname
}

if checkedConnect && checkedOpenssl {
if checkedOpenssl {
return nil
}

if !checkedOpenssl {
e := oc.checkOpenssl()
if e != nil {
return e
}
e := oc.checkOpenssl()
if e != nil {
return e
}

if !checkedConnect {
// Optional check
_ = oc.checkConnect()
}
s, e := checkCgroupPath(oc.CGroupPath)
if e != nil {
return e
Expand Down
2 changes: 1 addition & 1 deletion user/event/event_openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (se *SSLDataEvent) String() string {

func (se *SSLDataEvent) Clone() IEventStruct {
event := new(SSLDataEvent)
event.eventType = EventTypeEventProcessor
event.eventType = EventTypeOutput //EventTypeEventProcessor
return event
}

Expand Down
25 changes: 1 addition & 24 deletions user/module/probe_openssl_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func (m *MOpenSSLProbe) setupManagersText() error {
var libPthread, binaryPath, sslVersion string
var binaryPath, sslVersion string
sslVersion = m.conf.(*config.OpensslConfig).SslVersion
sslVersion = strings.ToLower(sslVersion)
switch m.conf.(*config.OpensslConfig).ElfType {
Expand All @@ -35,12 +35,6 @@ func (m *MOpenSSLProbe) setupManagersText() error {
}
}

libPthread = m.conf.(*config.OpensslConfig).Pthread
if libPthread == "" {
//libPthread = "/lib/x86_64-linux-gnu/libpthread.so.0"
m.logger.Warn().Msg("libPthread path not found, IP info lost.")
}

_, err := os.Stat(binaryPath)
if err != nil {
return err
Expand Down Expand Up @@ -137,23 +131,6 @@ func (m *MOpenSSLProbe) setupManagersText() error {
},
}

// TODO disable
libPthread = ""
if libPthread != "" {
// detect libpthread.so path
_, err = os.Stat(libPthread)
if err == nil {
m.logger.Info().Str("libPthread", libPthread).Msg("libPthread path found")
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,

Expand Down

0 comments on commit 2f4ea9e

Please sign in to comment.