hinako

package module
v0.0.0-...-39cd4b7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2020 License: MIT Imports: 8 Imported by: 1

README

hinako

Windows API hooking (x86) with golang based on trampoline function.

Requirements

  • Windows OS
  • Golang i386 (not amd64)
    • Also implements amd64, but is unstable

Getting Started

Let's Hook MessageBoxW.

package main

import (
	"log"
	"syscall"
	"unsafe"

	"github.com/castaneai/hinako"
)

func main() {
	// Before hook
	// Call MessageBoxW
	target := syscall.NewLazyDLL("user32.dll").NewProc("MessageBoxW")
	if r, _, err := target.Call(0, wstrPtr("MessageBoxW"), wstrPtr("MessageBoxW"), 0); r == 0 && err != nil {
		log.Fatalf("failed to call MessageBoxW: %+v", err)
	}

	// API Hooking by hinako
	arch := &hinako.Arch386{}
	var originalMessageBoxW *syscall.Proc
	hook, err := hinako.NewHookByName(arch, "user32.dll", "MessageBoxW", func(hWnd syscall.Handle, lpText, lpCaption *uint16, uType uint) int {
		r, _, _ := originalMessageBoxW.Call(uintptr(hWnd), wstrPtr("Hooked!"), wstrPtr("Hooked!"), uintptr(uType))
		return int(r)
	})
	if err != nil {
		log.Fatalf("failed to hook MessageBoxW: %+v", err)
	}
	defer hook.Close()
	originalMessageBoxW = hook.OriginalProc

	// After hook
	// Call MessageBoxW
	if r, _, err := target.Call(0, wstrPtr("MessageBoxW"), wstrPtr("MessageBoxW"), 0); r == 0 && err != nil {
		log.Fatalf("failed to call hooked MessageBoxW: %+v", err)
	}
}

func wstrPtr(str string) uintptr {
	ptr, _ := syscall.UTF16PtrFromString(str)
	return uintptr(unsafe.Pointer(ptr))
}

Testing

$ go test ./...

Author

castaneai

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Arch

type Arch interface {
	DisassembleMode() int
	NearJumpSize() uint
	FarJumpSize() uint
	NewNearJumpAsm(from, to uintptr) []byte
	NewFarJumpAsm(from, to uintptr) []byte
}

func NewRuntimeArch

func NewRuntimeArch() (Arch, error)

NewRuntimeArch func

type Arch386

type Arch386 struct{}

func (*Arch386) DisassembleMode

func (a *Arch386) DisassembleMode() int

func (*Arch386) FarJumpSize

func (a *Arch386) FarJumpSize() uint

func (*Arch386) NearJumpSize

func (a *Arch386) NearJumpSize() uint

func (*Arch386) NewFarJumpAsm

func (a *Arch386) NewFarJumpAsm(from, to uintptr) []byte

func (*Arch386) NewNearJumpAsm

func (a *Arch386) NewNearJumpAsm(from, to uintptr) []byte

type ArchAMD64

type ArchAMD64 struct{}

func (*ArchAMD64) DisassembleMode

func (a *ArchAMD64) DisassembleMode() int

func (*ArchAMD64) FarJumpSize

func (a *ArchAMD64) FarJumpSize() uint

func (*ArchAMD64) NearJumpSize

func (a *ArchAMD64) NearJumpSize() uint

func (*ArchAMD64) NewFarJumpAsm

func (a *ArchAMD64) NewFarJumpAsm(from, to uintptr) []byte

func (*ArchAMD64) NewNearJumpAsm

func (a *ArchAMD64) NewNearJumpAsm(from, to uintptr) []byte

type Hook

type Hook struct {
	Arch         Arch
	OriginalProc *syscall.Proc
	HookFunc     interface{}
	// contains filtered or unexported fields
}

func NewHook

func NewHook(arch Arch, targetProc *syscall.Proc, hookFunc interface{}) (*Hook, error)

func NewHookByName

func NewHookByName(arch Arch, dllName, funcName string, hookFunc interface{}) (*Hook, error)

func (*Hook) Close

func (h *Hook) Close()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL