Go: update hook interface

This commit is contained in:
Ryan Hileman
2016-02-27 10:50:51 -08:00
parent aabcb95f01
commit 693719e732
4 changed files with 20 additions and 25 deletions

View File

@ -1,12 +1,16 @@
#include <unicorn/unicorn.h> #include <unicorn/unicorn.h>
#include "_cgo_export.h" #include "_cgo_export.h"
uc_err uc_hook_add_i1(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, uintptr_t user, int arg1) { uc_err uc_hook_add(uc_engine *uc, uc_hook *hh, int type, void *callback,
return uc_hook_add(handle, h2, type, callback, (void *)user, arg1); void *user_data, uint64_t begin, uint64_t end, ...);
uc_err uc_hook_add_wrap(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, uintptr_t user, uint64_t begin, uint64_t end) {
return uc_hook_add(handle, h2, type, callback, (void *)user, begin, end);
} }
uc_err uc_hook_add_u2(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, uintptr_t user, uint64_t arg1, uint64_t arg2) { uc_err uc_hook_add_insn(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, uintptr_t user, uint64_t begin, uint64_t end, int insn) {
return uc_hook_add(handle, h2, type, callback, (void *)user, arg1, arg2); return uc_hook_add(handle, h2, type, callback, (void *)user, begin, end, insn);
} }
void hookCode_cgo(uc_engine *handle, uint64_t addr, uint32_t size, uintptr_t user) { void hookCode_cgo(uc_engine *handle, uint64_t addr, uint32_t size, uintptr_t user) {

View File

@ -63,23 +63,21 @@ func hookX86Syscall(handle unsafe.Pointer, user unsafe.Pointer) {
hook.Callback.(func(Unicorn))(hook.Uc) hook.Callback.(func(Unicorn))(hook.Uc)
} }
func (u *uc) HookAdd(htype int, cb interface{}, extra ...uint64) (Hook, error) { func (u *uc) HookAdd(htype int, cb interface{}, begin, end uint64, extra ...int) (Hook, error) {
var callback unsafe.Pointer var callback unsafe.Pointer
var iarg1 C.int var insn C.int
var uarg1, uarg2 C.uint64_t var insnMode bool
rangeMode := false
switch htype { switch htype {
case HOOK_BLOCK, HOOK_CODE: case HOOK_BLOCK, HOOK_CODE:
rangeMode = true
callback = C.hookCode_cgo callback = C.hookCode_cgo
case HOOK_MEM_READ, HOOK_MEM_WRITE, HOOK_MEM_READ | HOOK_MEM_WRITE: case HOOK_MEM_READ, HOOK_MEM_WRITE, HOOK_MEM_READ | HOOK_MEM_WRITE:
rangeMode = true
callback = C.hookMemAccess_cgo callback = C.hookMemAccess_cgo
case HOOK_INTR: case HOOK_INTR:
callback = C.hookInterrupt_cgo callback = C.hookInterrupt_cgo
case HOOK_INSN: case HOOK_INSN:
iarg1 = C.int(extra[0]) insn = C.int(extra[0])
switch iarg1 { insnMode = true
switch insn {
case X86_INS_IN: case X86_INS_IN:
callback = C.hookX86In_cgo callback = C.hookX86In_cgo
case X86_INS_OUT: case X86_INS_OUT:
@ -93,7 +91,6 @@ func (u *uc) HookAdd(htype int, cb interface{}, extra ...uint64) (Hook, error) {
// special case for mask // special case for mask
if htype&(HOOK_MEM_READ_UNMAPPED|HOOK_MEM_WRITE_UNMAPPED|HOOK_MEM_FETCH_UNMAPPED| if htype&(HOOK_MEM_READ_UNMAPPED|HOOK_MEM_WRITE_UNMAPPED|HOOK_MEM_FETCH_UNMAPPED|
HOOK_MEM_READ_PROT|HOOK_MEM_WRITE_PROT|HOOK_MEM_FETCH_PROT) != 0 { HOOK_MEM_READ_PROT|HOOK_MEM_WRITE_PROT|HOOK_MEM_FETCH_PROT) != 0 {
rangeMode = true
callback = C.hookMemInvalid_cgo callback = C.hookMemInvalid_cgo
} else { } else {
return 0, errors.New("Unknown hook type.") return 0, errors.New("Unknown hook type.")
@ -102,16 +99,10 @@ func (u *uc) HookAdd(htype int, cb interface{}, extra ...uint64) (Hook, error) {
var h2 C.uc_hook var h2 C.uc_hook
data := &HookData{u, cb} data := &HookData{u, cb}
uptr := uintptr(unsafe.Pointer(data)) uptr := uintptr(unsafe.Pointer(data))
if rangeMode { if insnMode {
if len(extra) == 2 { C.uc_hook_add_wrap(u.handle, &h2, C.uc_hook_type(htype), callback, C.uintptr_t(uptr), C.uint64_t(begin), C.uint64_t(end))
uarg1 = C.uint64_t(extra[0])
uarg2 = C.uint64_t(extra[1])
} else { } else {
uarg1, uarg2 = 1, 0 C.uc_hook_add_insn(u.handle, &h2, C.uc_hook_type(htype), callback, C.uintptr_t(uptr), C.uint64_t(begin), C.uint64_t(end), insn)
}
C.uc_hook_add_u2(u.handle, &h2, C.uc_hook_type(htype), callback, C.uintptr_t(uptr), uarg1, uarg2)
} else {
C.uc_hook_add_i1(u.handle, &h2, C.uc_hook_type(htype), callback, C.uintptr_t(uptr), iarg1)
} }
hookDataMap[uptr] = data hookDataMap[uptr] = data
hookToUintptr[Hook(h2)] = uptr hookToUintptr[Hook(h2)] = uptr

View File

@ -1,5 +1,5 @@
uc_err uc_hook_add_i1(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, uintptr_t user, int arg1); uc_err uc_hook_add_wrap(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, uintptr_t user, uint64_t begin, uint64_t end);
uc_err uc_hook_add_u2(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, uintptr_t user, uint64_t arg1, uint64_t arg2); uc_err uc_hook_add_insn(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, uintptr_t user, uint64_t begin, uint64_t end, int insn);
void hookCode_cgo(uc_engine *handle, uint64_t addr, uint32_t size, uintptr_t user); void hookCode_cgo(uc_engine *handle, uint64_t addr, uint32_t size, uintptr_t user);
bool hookMemInvalid_cgo(uc_engine *handle, uc_mem_type type, uint64_t addr, int size, int64_t value, uintptr_t user); bool hookMemInvalid_cgo(uc_engine *handle, uc_mem_type type, uint64_t addr, int size, int64_t value, uintptr_t user);
void hookMemAccess_cgo(uc_engine *handle, uc_mem_type type, uint64_t addr, int size, int64_t value, uintptr_t user); void hookMemAccess_cgo(uc_engine *handle, uc_mem_type type, uint64_t addr, int size, int64_t value, uintptr_t user);

View File

@ -39,7 +39,7 @@ type Unicorn interface {
Start(begin, until uint64) error Start(begin, until uint64) error
StartWithOptions(begin, until uint64, options *UcOptions) error StartWithOptions(begin, until uint64, options *UcOptions) error
Stop() error Stop() error
HookAdd(htype int, cb interface{}, extra ...uint64) (Hook, error) HookAdd(htype int, cb interface{}, begin, end uint64, extra ...int) (Hook, error)
HookDel(hook Hook) error HookDel(hook Hook) error
Close() error Close() error
} }