-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnative_method.go
183 lines (166 loc) · 4.31 KB
/
native_method.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package emulator
import (
"fmt"
"github.com/pkg/errors"
bin "encoding/binary"
log "github.com/rs/zerolog/log"
uc "github.com/unicorn-engine/unicorn/bindings/go/unicorn"
)
func NativeWriteArgs(emu *Emulator, args ...interface{}) error {
var err error
amount := len(args)
if amount == 0 {
return nil
}
if amount >= 1 {
err = NativeWriteArgRegister(emu, uc.ARM_REG_R0, args[0])
if err != nil {
return errors.Wrap(err, "write reg r0 failed")
}
}
if amount >= 2 {
err = NativeWriteArgRegister(emu, uc.ARM_REG_R1, args[1])
if err != nil {
return errors.Wrap(err, "write reg r1 failed")
}
}
if amount >= 3 {
err = NativeWriteArgRegister(emu, uc.ARM_REG_R2, args[2])
if err != nil {
return errors.Wrap(err, "write reg r2 failed")
}
}
if amount >= 4 {
err = NativeWriteArgRegister(emu, uc.ARM_REG_R3, args[3])
if err != nil {
return errors.Wrap(err, "write reg r3 failed")
}
}
if amount >= 5 {
spStart, err := emu.Mu.RegRead(uc.ARM_REG_SP)
if err != nil {
log.Debug().Err(err).Msg("failed to read registry ARM_REG_SP writeArgs")
return err
}
spCurrent := spStart - STACK_OFFSET
spCurrent = spCurrent - (4 * (uint64(amount) - 4))
spEnd := spCurrent
for _, arg := range args[4:] {
ptr := NativeTranslateArg(emu, arg)
byt := make([]byte, 4)
bin.LittleEndian.PutUint32(byt, uint32(ptr))
err = emu.Mu.MemWrite(spCurrent, byt)
if err != nil {
log.Debug().Err(err).Msg("failed to write sp reg args val")
return errors.Wrap(err, "failed to write sp reg arg val")
}
spCurrent = spCurrent + 4
}
err = emu.Mu.RegWrite(uc.ARM_REG_SP, spEnd)
if err != nil {
log.Debug().Err(err).Msg("failed to write sp reg end address")
return errors.Wrap(err, "failed to write sp reg end address")
}
}
return nil
}
func NativeReadArgs(mu uc.Unicorn, argsCount int) []uint64 {
// init with 0
nativeArgs := make([]uint64, argsCount)
if argsCount >= 1 {
pt, err := mu.RegRead(uc.ARM_REG_R0)
if err != nil {
log.Debug().Err(err).Msg("failed to read reg r0")
}
nativeArgs[0] = pt
}
if argsCount >= 2 {
pt, err := mu.RegRead(uc.ARM_REG_R1)
if err != nil {
log.Debug().Err(err).Msg("failed to read reg r1")
}
nativeArgs[1] = pt
}
if argsCount >= 3 {
pt, err := mu.RegRead(uc.ARM_REG_R2)
if err != nil {
log.Debug().Err(err).Msg("failed to read reg r2")
}
nativeArgs[2] = pt
}
if argsCount >= 4 {
pt, err := mu.RegRead(uc.ARM_REG_R3)
if err != nil {
log.Debug().Err(err).Msg("failed to read reg r2")
}
nativeArgs[3] = pt
}
sp, err := mu.RegRead(uc.ARM_REG_SP)
if err != nil {
log.Debug().Err(err).Msg("failed to read reg SP for reading args")
return nativeArgs
}
sp = sp + STACK_OFFSET
if argsCount >= 5 {
var i uint64
for i = 0; i < uint64(argsCount - 4); i++ {
by, err := mu.MemRead(sp + (i * 4), 4)
if err != nil {
log.Debug().Uint64("addrSp", sp+(i*4)).Err(err).Msg("failed to read memory for reading arg")
continue
}
ptr := uint64(bin.LittleEndian.Uint32(by))
nativeArgs[4+int(i)] = ptr
}
}
return nativeArgs
}
func NativeTranslateArg(emu *Emulator, val interface{}) uint64 {
switch v := val.(type) {
case uint64:
return v
case int:
return uint64(v)
case uint32:
return uint64(v)
case uint16:
return uint64(v)
case []byte:
return emu.JavaVM.JniEnv.AddLocalReference(NewJObject(val.([]byte)))
case *javaClass:
return emu.JavaVM.JniEnv.AddLocalReference(NewJObject(val.(*javaClass)))
}
panic(fmt.Errorf("unable to write response '%T' %#+v", val, val))
return 0
}
func NativeWriteArgRegister(emu *Emulator, reg int, val interface{}) error {
ptr := NativeTranslateArg(emu, val)
return emu.Mu.RegWrite(reg, ptr)
}
type NativeMethodContext struct {
emu *Emulator
mu uc.Unicorn
}
func (nmc NativeMethodContext) Emu() *Emulator {
return nmc.emu
}
func (nmc NativeMethodContext) Mu() uc.Unicorn {
return nmc.mu
}
func (nmc NativeMethodContext) GetArgs(count int) []uint64 {
return NativeReadArgs(nmc.mu, count)
}
func (nmc NativeMethodContext) Return(res uint64) error {
return NativeWriteArgRegister(nmc.emu, uc.ARM_REG_R0, res)
}
func (nmc NativeMethodContext) Return2(low, high uint64) error {
err := NativeWriteArgRegister(nmc.emu, uc.ARM_REG_R0, low)
if err != nil {
return err
}
err = NativeWriteArgRegister(nmc.emu, uc.ARM_REG_R1, high)
if err != nil {
return err
}
return nil
}