Skip to content

Commit e4bfe77

Browse files
author
Your Name
committed
支持追踪计算跳转地址偏移信息
1 parent f332d60 commit e4bfe77

7 files changed

Lines changed: 84 additions & 2 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ chmod +x /data/local/tmp/stackplz
6464
./stackplz --name com.sfx.ebpf stack --library libnative-lib.so --symbol _Z5func1v --unwindstack --regs
6565
```
6666

67+
通过`--reg`指定寄存器,对跳转目标地址进行偏移计算,再也不担心找不到跳哪儿去了
68+
69+
`--reg`选项需要搭配`--regs`或者`--unwindstack`使用,后续进行优化
70+
71+
```bash
72+
./stackplz --name com.xingin.xhs stack --library libtiny.so --offset 0x175248 --regs --reg x8
73+
```
74+
6775
![](./images/Snipaste_2022-11-13_14-11-03.png)
6876

6977
通过**指定包名和配置文件**进行批量hook

cli/cmd/stack.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func init() {
6666
stackCmd.PersistentFlags().StringVar(&stack_config.Library, "library", "/apex/com.android.runtime/lib64/bionic/libc.so", "full lib path")
6767
stackCmd.PersistentFlags().StringVar(&stack_config.Symbol, "symbol", "", "lib symbol")
6868
stackCmd.PersistentFlags().Uint64Var(&stack_config.Offset, "offset", 0, "lib hook offset")
69+
stackCmd.PersistentFlags().StringVar(&stack_config.RegName, "reg", "", "get the offset of reg")
6970
stackCmd.PersistentFlags().StringVar(&stack_config.Config, "config", "", "hook config file")
7071
rootCmd.AddCommand(stackCmd)
7172
}
@@ -118,6 +119,7 @@ func stackCommandFunc(command *cobra.Command, args []string) {
118119
SConfig: config.SConfig{
119120
UnwindStack: stack_config.UnwindStack,
120121
ShowRegs: stack_config.ShowRegs,
122+
RegName: stack_config.RegName,
121123
Uid: target_config.Uid,
122124
Pid: target_config.Pid,
123125
},

pkg/util/helper.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/binary"
66
"fmt"
7+
"io/ioutil"
78
"math/rand"
89
"os"
910
"strings"
@@ -88,3 +89,34 @@ func FindLib(library string, search_paths []string) (string, error) {
8889
}
8990
return library, nil
9091
}
92+
93+
func ParseReg(pid uint32, value uint64) (string, error) {
94+
info := "UNKNOWN"
95+
// 直接读取maps信息 计算value在什么地方 用于定位跳转目的地
96+
filename := fmt.Sprintf("/proc/%d/maps", pid)
97+
content, err := ioutil.ReadFile(filename)
98+
if err != nil {
99+
return info, fmt.Errorf("Error when opening file:%v", err)
100+
}
101+
var (
102+
seg_start uint64
103+
seg_end uint64
104+
permission string
105+
seg_offset uint64
106+
device string
107+
inode uint64
108+
seg_path string
109+
)
110+
for _, line := range strings.Split(string(content), "\n") {
111+
reader := strings.NewReader(line)
112+
n, err := fmt.Fscanf(reader, "%x-%x %s %x %s %d %s", &seg_start, &seg_end, &permission, &seg_offset, &device, &inode, &seg_path)
113+
if err == nil && n == 7 {
114+
if value >= seg_start && value < seg_end {
115+
offset := seg_offset + (value - seg_start)
116+
info = fmt.Sprintf("%s + 0x%x", seg_path, offset)
117+
break
118+
}
119+
}
120+
}
121+
return info, err
122+
}

user/config/config_stack.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type StackConfig struct {
66
Library string
77
Symbol string
88
Offset uint64
9+
RegName string
910
Config string
1011
}
1112

user/config/iconfig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type SConfig struct {
2222
Pid uint64
2323
UnwindStack bool
2424
ShowRegs bool
25+
RegName string
2526
Debug bool
2627
}
2728

user/event/event_stack.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"encoding/binary"
1010
"encoding/json"
1111
"fmt"
12+
"stackplz/pkg/util"
13+
"strconv"
14+
"strings"
1215
"unsafe"
1316
)
1417

@@ -23,6 +26,7 @@ type HookDataEvent struct {
2326
UnwindBuffer UnwindBuf
2427
UnwindStack bool
2528
ShowRegs bool
29+
RegName string
2630
UUID string
2731
}
2832

@@ -89,6 +93,39 @@ func (this *HookDataEvent) SetUUID(uuid string) {
8993
func (this *HookDataEvent) String() string {
9094
var s string
9195
s = fmt.Sprintf("[%s] PID:%d, Comm:%s, TID:%d", this.UUID, this.Pid, bytes.TrimSpace(bytes.Trim(this.Comm[:], "\x00")), this.Tid)
96+
if this.RegName != "" && (this.ShowRegs || this.UnwindStack) {
97+
// 如果设置了寄存器名字 那么尝试从获取到的寄存器数据中取值计算偏移
98+
// 当然前提是取了寄存器数据
99+
var tmp_regs [33]uint64
100+
if this.UnwindStack {
101+
tmp_regs = this.UnwindBuffer.Regs
102+
} else {
103+
tmp_regs = this.RegsBuffer.Regs
104+
}
105+
has_reg_value := false
106+
var regvalue uint64
107+
if strings.HasPrefix(this.RegName, "x") {
108+
parts := strings.SplitN(this.RegName, "x", 2)
109+
regno, _ := strconv.ParseUint(parts[1], 10, 32)
110+
if regno >= 0 && regno <= 29 {
111+
// 取到对应的寄存器值
112+
regvalue = tmp_regs[regno]
113+
has_reg_value = true
114+
}
115+
} else if this.RegName == "lr" {
116+
regvalue = tmp_regs[30]
117+
has_reg_value = true
118+
}
119+
if has_reg_value {
120+
// 读取maps 获取偏移信息
121+
info, err := util.ParseReg(this.Pid, regvalue)
122+
if err != nil {
123+
fmt.Printf("ParseReg for %s=0x%x failed", this.RegName, regvalue)
124+
} else {
125+
s += fmt.Sprintf(", Reg %s Info:\n%s", this.RegName, info)
126+
}
127+
}
128+
}
92129
if this.ShowRegs {
93130
var tmp_regs [33]uint64
94131
if this.UnwindStack {

user/module/probe_stack.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,9 @@ func (this *MStackProbe) Dispatcher(e event.IEventStruct) {
186186
// 事件类型指定为 EventTypeModuleData 直接使用当前方法处理
187187
// 如果需要多处联动收集信息 比如做统计之类的 那么使用 EventTypeEventProcessor 类型 并设计处理模式更合理
188188

189-
e.(*event.HookDataEvent).ShowRegs = this.probeConf.ShowRegs
190-
e.(*event.HookDataEvent).UnwindStack = this.probeConf.UnwindStack
189+
e.(*event.HookDataEvent).RegName = this.sconf.RegName
190+
e.(*event.HookDataEvent).ShowRegs = this.sconf.ShowRegs
191+
e.(*event.HookDataEvent).UnwindStack = this.sconf.UnwindStack
191192
this.logger.Println(e.(*event.HookDataEvent).String())
192193
}
193194

0 commit comments

Comments
 (0)