Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

SmartLwei/goinlinehook

Open more actions menu
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

goinlinehook

goinlinehook 是go语言环境下的inline hook库。

特征

  • 可以调用原函数
  • 可以hook私有函数
  • 可以hook成员函数
  • 可以hook runtime函数

使用方法

  • 原函数
//go:noinline
func Add(a, b int) int {
	return a+b
}
  1. 编写代理函数
//go:noinline
func MyAdd(a, b int) int {
	var oldFunc func (int, int) int
	goinlinehook.GetOldFunc(Add, &oldFunc)
	return oldFunc(a, b) + 10
}
  1. hook目标函数
fmt.Println("result:", res)
hookItem, err := goinlinehook.NewAndHook(Add, MyAdd, &oldFunc)
if err != nil {
    fmt.Println(err.Error())
    return
}

这种方式默认是使用 push xx, ret的形式进行跳转的 也可以使用如下的形式:

trampoline[8191]
func Hook() {
    item := goinlinehook.NewHookItem(Add, MyAdd)
    ptr := (uintptr)(unsafe.Pointer(&trampoline[0]))
    item.TrampolineAddr = ptr
    err := item.Hook()
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    hook.GetOldFunc(mynewproc, &oldFunc)
}

这种方式由于在4字节跳转范围内,使用jmp xxx进行跳转,不占用栈空间。

About

golang inline hook

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%
Morty Proxy This is a proxified and sanitized view of the page, visit original site.