mirror of
https://github.com/Vita3K/unicorn.git
synced 2024-11-27 07:10:45 +00:00
Merge branch 'master' into mem_map_ex_cse
This commit is contained in:
commit
eab6167241
2
.gitignore
vendored
2
.gitignore
vendored
@ -86,4 +86,4 @@ regress/sigill2
|
||||
regress/block_test
|
||||
regress/map_write
|
||||
regress/ro_mem_test
|
||||
|
||||
regress/nr_mem_test
|
||||
|
@ -15,6 +15,7 @@ SAMPLE_X86 = $(TMPDIR)/sample_x86
|
||||
|
||||
all:
|
||||
cd python && $(MAKE) gen_const
|
||||
cd go && $(MAKE) gen_const
|
||||
|
||||
samples: expected python
|
||||
|
||||
|
@ -5,7 +5,7 @@ import sys, re
|
||||
|
||||
INCL_DIR = '../include/unicorn/'
|
||||
|
||||
include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h', 'sparc.h', 'm68k.h' ]
|
||||
include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h', 'sparc.h', 'm68k.h', 'unicorn.h' ]
|
||||
|
||||
template = {
|
||||
'python': {
|
||||
@ -20,13 +20,14 @@ template = {
|
||||
'x86.h': 'x86',
|
||||
'sparc.h': 'sparc',
|
||||
'm68k.h': 'm68k',
|
||||
'unicorn.h': 'unicorn',
|
||||
'comment_open': '#',
|
||||
'comment_close': '',
|
||||
},
|
||||
'go': {
|
||||
'header': "package unicorn\n// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.go]\nconst (",
|
||||
'header': "package unicorn\n// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.go]\nconst (\n",
|
||||
'footer': ")",
|
||||
'line_format': '%s = %s\n',
|
||||
'line_format': '\t%s = %s\n',
|
||||
'out_file': './go/unicorn/%s_const.go',
|
||||
# prefixes for constant filenames of all archs - case sensitive
|
||||
'arm.h': 'arm',
|
||||
@ -35,6 +36,7 @@ template = {
|
||||
'x86.h': 'x86',
|
||||
'sparc.h': 'sparc',
|
||||
'm68k.h': 'm68k',
|
||||
'unicorn.h': 'unicorn',
|
||||
'comment_open': '//',
|
||||
'comment_close': '',
|
||||
},
|
||||
@ -50,9 +52,11 @@ def gen(lang):
|
||||
prefix = templ[target]
|
||||
outfile = open(templ['out_file'] %(prefix), 'w')
|
||||
outfile.write(templ['header'] % (prefix))
|
||||
|
||||
if target == 'unicorn.h':
|
||||
prefix = ''
|
||||
lines = open(INCL_DIR + target).readlines()
|
||||
|
||||
previous = {}
|
||||
count = 0
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
@ -65,17 +69,21 @@ def gen(lang):
|
||||
if line == '' or line.startswith('//'):
|
||||
continue
|
||||
|
||||
if not line.startswith("UC_" + prefix.upper()):
|
||||
continue
|
||||
|
||||
tmp = line.strip().split(',')
|
||||
for t in tmp:
|
||||
t = t.strip()
|
||||
if not t or t.startswith('//'): continue
|
||||
f = re.split('\s+', t)
|
||||
|
||||
# parse #define UC_TARGET (num)
|
||||
define = False
|
||||
if f[0] == '#define' and len(f) >= 3 and f[2].isdigit():
|
||||
define = True
|
||||
f.pop(0)
|
||||
f.insert(1, '=')
|
||||
|
||||
if f[0].startswith("UC_" + prefix.upper()):
|
||||
if len(f) > 1 and f[1] not in '//=':
|
||||
if len(f) > 1 and f[1] not in ('//', '='):
|
||||
print("Error: Unable to convert %s" % f)
|
||||
continue
|
||||
elif len(f) > 1 and f[1] == '=':
|
||||
@ -84,29 +92,31 @@ def gen(lang):
|
||||
rhs = str(count)
|
||||
count += 1
|
||||
|
||||
try:
|
||||
lhs = f[0].strip()
|
||||
# evaluate bitshifts in constants e.g. "UC_X86 = 1 << 1"
|
||||
match = re.match(r'(?P<rhs>\s*\d+\s*<<\s*\d+\s*)', rhs)
|
||||
if match:
|
||||
rhs = eval(match.group(1))
|
||||
else:
|
||||
# evaluate references to other constants e.g. "UC_ARM_REG_X = UC_ARM_REG_SP"
|
||||
match = re.match(r'^([^\d]\w+)$', rhs)
|
||||
if match:
|
||||
rhs = previous[match.group(1)]
|
||||
|
||||
count = int(rhs) + 1
|
||||
if (count == 1):
|
||||
outfile.write("\n")
|
||||
except ValueError:
|
||||
if lang == 'ocaml':
|
||||
# ocaml uses lsl for '<<', lor for '|'
|
||||
rhs = rhs.replace('<<', ' lsl ')
|
||||
rhs = rhs.replace('|', ' lor ')
|
||||
# ocaml variable has _ as prefix
|
||||
if rhs[0].isalpha():
|
||||
rhs = '_' + rhs
|
||||
|
||||
outfile.write(templ['line_format'] %(f[0].strip(), rhs))
|
||||
outfile.write(templ['line_format'] % (lhs, rhs))
|
||||
previous[lhs] = rhs
|
||||
|
||||
outfile.write(templ['footer'])
|
||||
outfile.close()
|
||||
|
||||
def main():
|
||||
try:
|
||||
lang = sys.argv[1]
|
||||
if not lang in template:
|
||||
raise RuntimeError("Unsupported binding %s" % lang)
|
||||
gen(sys.argv[1])
|
||||
except:
|
||||
raise RuntimeError("Unsupported binding %s" % sys.argv[1])
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
|
@ -2,9 +2,13 @@
|
||||
|
||||
.PHONY: gen_const test
|
||||
|
||||
all:
|
||||
$(MAKE) gen_const
|
||||
cd unicorn && go build
|
||||
$(MAKE) test
|
||||
|
||||
gen_const:
|
||||
cd .. && python const_generator.py go
|
||||
cd unicorn && go build
|
||||
|
||||
test:
|
||||
cd unicorn && go test
|
||||
|
@ -1,6 +1,7 @@
|
||||
package unicorn
|
||||
// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [arm64_const.go]
|
||||
const (
|
||||
|
||||
// ARM64 registers
|
||||
|
||||
UC_ARM64_REG_INVALID = 0
|
||||
@ -269,8 +270,8 @@ UC_ARM64_REG_PC = 260
|
||||
UC_ARM64_REG_ENDING = 261
|
||||
|
||||
// alias registers
|
||||
UC_ARM64_REG_IP1 = UC_ARM64_REG_X16
|
||||
UC_ARM64_REG_IP0 = UC_ARM64_REG_X17
|
||||
UC_ARM64_REG_FP = UC_ARM64_REG_X29
|
||||
UC_ARM64_REG_LR = UC_ARM64_REG_X30
|
||||
UC_ARM64_REG_IP1 = 215
|
||||
UC_ARM64_REG_IP0 = 216
|
||||
UC_ARM64_REG_FP = 1
|
||||
UC_ARM64_REG_LR = 2
|
||||
)
|
@ -1,6 +1,7 @@
|
||||
package unicorn
|
||||
// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [arm_const.go]
|
||||
const (
|
||||
|
||||
// ARM registers
|
||||
|
||||
UC_ARM_REG_INVALID = 0
|
||||
@ -117,11 +118,11 @@ UC_ARM_REG_S31 = 110
|
||||
UC_ARM_REG_ENDING = 111
|
||||
|
||||
// alias registers
|
||||
UC_ARM_REG_R13 = UC_ARM_REG_SP
|
||||
UC_ARM_REG_R14 = UC_ARM_REG_LR
|
||||
UC_ARM_REG_R15 = UC_ARM_REG_PC
|
||||
UC_ARM_REG_SB = UC_ARM_REG_R9
|
||||
UC_ARM_REG_SL = UC_ARM_REG_R10
|
||||
UC_ARM_REG_FP = UC_ARM_REG_R11
|
||||
UC_ARM_REG_IP = UC_ARM_REG_R12
|
||||
UC_ARM_REG_R13 = 12
|
||||
UC_ARM_REG_R14 = 10
|
||||
UC_ARM_REG_R15 = 11
|
||||
UC_ARM_REG_SB = 75
|
||||
UC_ARM_REG_SL = 76
|
||||
UC_ARM_REG_FP = 77
|
||||
UC_ARM_REG_IP = 78
|
||||
)
|
@ -1,22 +0,0 @@
|
||||
package unicorn
|
||||
|
||||
// #include <unicorn/unicorn.h>
|
||||
import "C"
|
||||
|
||||
// TODO: update const script to generate these?
|
||||
const (
|
||||
UC_HOOK_BLOCK = C.UC_HOOK_BLOCK
|
||||
UC_HOOK_CODE = C.UC_HOOK_CODE
|
||||
UC_HOOK_MEM_INVALID = C.UC_HOOK_MEM_INVALID
|
||||
UC_HOOK_MEM_READ = C.UC_HOOK_MEM_READ
|
||||
UC_HOOK_MEM_WRITE = C.UC_HOOK_MEM_WRITE
|
||||
UC_HOOK_MEM_READ_WRITE = C.UC_HOOK_MEM_READ_WRITE
|
||||
UC_HOOK_INSN = C.UC_HOOK_INSN
|
||||
|
||||
UC_ARCH_X86 = C.UC_ARCH_X86
|
||||
UC_MODE_32 = C.UC_MODE_32
|
||||
UC_MODE_64 = C.UC_MODE_64
|
||||
|
||||
UC_ERR_MEM_READ = C.UC_ERR_MEM_READ
|
||||
UC_ERR_MEM_WRITE = C.UC_ERR_MEM_WRITE
|
||||
)
|
@ -1,6 +1,7 @@
|
||||
package unicorn
|
||||
// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [m68k_const.go]
|
||||
const (
|
||||
|
||||
// M68K registers
|
||||
|
||||
UC_M68K_REG_INVALID = 0
|
||||
|
@ -1,6 +1,7 @@
|
||||
package unicorn
|
||||
// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [mips_const.go]
|
||||
const (
|
||||
|
||||
// MIPS registers
|
||||
|
||||
UC_MIPS_REG_INVALID = 0
|
||||
@ -153,45 +154,45 @@ UC_MIPS_REG_MPL0 = 134
|
||||
UC_MIPS_REG_MPL1 = 135
|
||||
UC_MIPS_REG_MPL2 = 136
|
||||
UC_MIPS_REG_ENDING = 137
|
||||
UC_MIPS_REG_ZERO = UC_MIPS_REG_0
|
||||
UC_MIPS_REG_AT = UC_MIPS_REG_1
|
||||
UC_MIPS_REG_V0 = UC_MIPS_REG_2
|
||||
UC_MIPS_REG_V1 = UC_MIPS_REG_3
|
||||
UC_MIPS_REG_A0 = UC_MIPS_REG_4
|
||||
UC_MIPS_REG_A1 = UC_MIPS_REG_5
|
||||
UC_MIPS_REG_A2 = UC_MIPS_REG_6
|
||||
UC_MIPS_REG_A3 = UC_MIPS_REG_7
|
||||
UC_MIPS_REG_T0 = UC_MIPS_REG_8
|
||||
UC_MIPS_REG_T1 = UC_MIPS_REG_9
|
||||
UC_MIPS_REG_T2 = UC_MIPS_REG_10
|
||||
UC_MIPS_REG_T3 = UC_MIPS_REG_11
|
||||
UC_MIPS_REG_T4 = UC_MIPS_REG_12
|
||||
UC_MIPS_REG_T5 = UC_MIPS_REG_13
|
||||
UC_MIPS_REG_T6 = UC_MIPS_REG_14
|
||||
UC_MIPS_REG_T7 = UC_MIPS_REG_15
|
||||
UC_MIPS_REG_S0 = UC_MIPS_REG_16
|
||||
UC_MIPS_REG_S1 = UC_MIPS_REG_17
|
||||
UC_MIPS_REG_S2 = UC_MIPS_REG_18
|
||||
UC_MIPS_REG_S3 = UC_MIPS_REG_19
|
||||
UC_MIPS_REG_S4 = UC_MIPS_REG_20
|
||||
UC_MIPS_REG_S5 = UC_MIPS_REG_21
|
||||
UC_MIPS_REG_S6 = UC_MIPS_REG_22
|
||||
UC_MIPS_REG_S7 = UC_MIPS_REG_23
|
||||
UC_MIPS_REG_T8 = UC_MIPS_REG_24
|
||||
UC_MIPS_REG_T9 = UC_MIPS_REG_25
|
||||
UC_MIPS_REG_K0 = UC_MIPS_REG_26
|
||||
UC_MIPS_REG_K1 = UC_MIPS_REG_27
|
||||
UC_MIPS_REG_GP = UC_MIPS_REG_28
|
||||
UC_MIPS_REG_SP = UC_MIPS_REG_29
|
||||
UC_MIPS_REG_FP = UC_MIPS_REG_30
|
||||
UC_MIPS_REG_S8 = UC_MIPS_REG_30
|
||||
UC_MIPS_REG_RA = UC_MIPS_REG_31
|
||||
UC_MIPS_REG_HI0 = UC_MIPS_REG_AC0
|
||||
UC_MIPS_REG_HI1 = UC_MIPS_REG_AC1
|
||||
UC_MIPS_REG_HI2 = UC_MIPS_REG_AC2
|
||||
UC_MIPS_REG_HI3 = UC_MIPS_REG_AC3
|
||||
UC_MIPS_REG_LO0 = UC_MIPS_REG_HI0
|
||||
UC_MIPS_REG_LO1 = UC_MIPS_REG_HI1
|
||||
UC_MIPS_REG_LO2 = UC_MIPS_REG_HI2
|
||||
UC_MIPS_REG_LO3 = UC_MIPS_REG_HI3
|
||||
UC_MIPS_REG_ZERO = 2
|
||||
UC_MIPS_REG_AT = 3
|
||||
UC_MIPS_REG_V0 = 4
|
||||
UC_MIPS_REG_V1 = 5
|
||||
UC_MIPS_REG_A0 = 6
|
||||
UC_MIPS_REG_A1 = 7
|
||||
UC_MIPS_REG_A2 = 8
|
||||
UC_MIPS_REG_A3 = 9
|
||||
UC_MIPS_REG_T0 = 10
|
||||
UC_MIPS_REG_T1 = 11
|
||||
UC_MIPS_REG_T2 = 12
|
||||
UC_MIPS_REG_T3 = 13
|
||||
UC_MIPS_REG_T4 = 14
|
||||
UC_MIPS_REG_T5 = 15
|
||||
UC_MIPS_REG_T6 = 16
|
||||
UC_MIPS_REG_T7 = 17
|
||||
UC_MIPS_REG_S0 = 18
|
||||
UC_MIPS_REG_S1 = 19
|
||||
UC_MIPS_REG_S2 = 20
|
||||
UC_MIPS_REG_S3 = 21
|
||||
UC_MIPS_REG_S4 = 22
|
||||
UC_MIPS_REG_S5 = 23
|
||||
UC_MIPS_REG_S6 = 24
|
||||
UC_MIPS_REG_S7 = 25
|
||||
UC_MIPS_REG_T8 = 26
|
||||
UC_MIPS_REG_T9 = 27
|
||||
UC_MIPS_REG_K0 = 28
|
||||
UC_MIPS_REG_K1 = 29
|
||||
UC_MIPS_REG_GP = 30
|
||||
UC_MIPS_REG_SP = 31
|
||||
UC_MIPS_REG_FP = 32
|
||||
UC_MIPS_REG_S8 = 32
|
||||
UC_MIPS_REG_RA = 33
|
||||
UC_MIPS_REG_HI0 = 45
|
||||
UC_MIPS_REG_HI1 = 46
|
||||
UC_MIPS_REG_HI2 = 47
|
||||
UC_MIPS_REG_HI3 = 48
|
||||
UC_MIPS_REG_LO0 = 45
|
||||
UC_MIPS_REG_LO1 = 46
|
||||
UC_MIPS_REG_LO2 = 47
|
||||
UC_MIPS_REG_LO3 = 48
|
||||
)
|
@ -1,6 +1,7 @@
|
||||
package unicorn
|
||||
// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [sparc_const.go]
|
||||
const (
|
||||
|
||||
// SPARC registers
|
||||
|
||||
UC_SPARC_REG_INVALID = 0
|
||||
@ -93,6 +94,6 @@ UC_SPARC_REG_Y = 86
|
||||
UC_SPARC_REG_XCC = 87
|
||||
UC_SPARC_REG_PC = 88
|
||||
UC_SPARC_REG_ENDING = 89
|
||||
UC_SPARC_REG_O6 = UC_SPARC_REG_SP
|
||||
UC_SPARC_REG_I6 = UC_SPARC_REG_FP
|
||||
UC_SPARC_REG_O6 = 85
|
||||
UC_SPARC_REG_I6 = 53
|
||||
)
|
@ -17,7 +17,7 @@ func (u UcError) Error() string {
|
||||
}
|
||||
|
||||
func errReturn(err C.uc_err) error {
|
||||
if err != C.UC_ERR_OK {
|
||||
if err != UC_ERR_OK {
|
||||
return UcError(err)
|
||||
}
|
||||
return nil
|
||||
@ -36,10 +36,10 @@ func NewUc(arch, mode int) (*Uc, error) {
|
||||
var major, minor C.uint
|
||||
C.uc_version(&major, &minor)
|
||||
if major != C.UC_API_MAJOR || minor != C.UC_API_MINOR {
|
||||
return nil, UcError(C.UC_ERR_VERSION)
|
||||
return nil, UcError(UC_ERR_VERSION)
|
||||
}
|
||||
var handle C.uch
|
||||
if ucerr := C.uc_open(C.uc_arch(arch), C.uc_mode(mode), &handle); ucerr != C.UC_ERR_OK {
|
||||
if ucerr := C.uc_open(C.uc_arch(arch), C.uc_mode(mode), &handle); ucerr != UC_ERR_OK {
|
||||
return nil, UcError(ucerr)
|
||||
}
|
||||
uc := &Uc{handle, arch, mode}
|
||||
|
66
bindings/go/unicorn/unicorn_const.go
Normal file
66
bindings/go/unicorn/unicorn_const.go
Normal file
@ -0,0 +1,66 @@
|
||||
package unicorn
|
||||
// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [unicorn_const.go]
|
||||
const (
|
||||
|
||||
UC_API_MAJOR = 0
|
||||
UC_API_MINOR = 9
|
||||
UC_SECOND_SCALE = 1000000
|
||||
UC_MILISECOND_SCALE = 1000
|
||||
UC_ARCH_ARM = 1
|
||||
UC_ARCH_ARM64 = 2
|
||||
UC_ARCH_MIPS = 3
|
||||
UC_ARCH_X86 = 4
|
||||
UC_ARCH_PPC = 5
|
||||
UC_ARCH_SPARC = 6
|
||||
UC_ARCH_M68K = 7
|
||||
UC_ARCH_MAX = 8
|
||||
|
||||
UC_MODE_LITTLE_ENDIAN = 0
|
||||
|
||||
UC_MODE_ARM = 0
|
||||
UC_MODE_16 = 2
|
||||
UC_MODE_32 = 4
|
||||
UC_MODE_64 = 8
|
||||
UC_MODE_THUMB = 16
|
||||
UC_MODE_MCLASS = 32
|
||||
UC_MODE_V8 = 64
|
||||
UC_MODE_MICRO = 16
|
||||
UC_MODE_MIPS3 = 32
|
||||
UC_MODE_MIPS32R6 = 64
|
||||
UC_MODE_V9 = 16
|
||||
UC_MODE_QPX = 16
|
||||
UC_MODE_BIG_ENDIAN = 2147483648
|
||||
UC_MODE_MIPS32 = 4
|
||||
UC_MODE_MIPS64 = 8
|
||||
|
||||
UC_ERR_OK = 0
|
||||
UC_ERR_OOM = 1
|
||||
UC_ERR_ARCH = 2
|
||||
UC_ERR_HANDLE = 3
|
||||
UC_ERR_UCH = 4
|
||||
UC_ERR_MODE = 5
|
||||
UC_ERR_VERSION = 6
|
||||
UC_ERR_MEM_READ = 7
|
||||
UC_ERR_MEM_WRITE = 8
|
||||
UC_ERR_CODE_INVALID = 9
|
||||
UC_ERR_HOOK = 10
|
||||
UC_ERR_INSN_INVALID = 11
|
||||
UC_ERR_MAP = 12
|
||||
UC_ERR_MEM_WRITE_NW = 13
|
||||
UC_ERR_MEM_READ_NR = 14
|
||||
UC_MEM_READ = 16
|
||||
UC_MEM_WRITE = 17
|
||||
UC_MEM_READ_WRITE = 18
|
||||
UC_MEM_WRITE_NW = 19
|
||||
UC_MEM_READ_NR = 20
|
||||
UC_HOOK_INTR = 32
|
||||
UC_HOOK_INSN = 33
|
||||
UC_HOOK_CODE = 34
|
||||
UC_HOOK_BLOCK = 35
|
||||
UC_HOOK_MEM_INVALID = 36
|
||||
UC_HOOK_MEM_READ = 37
|
||||
UC_HOOK_MEM_WRITE = 38
|
||||
UC_HOOK_MEM_READ_WRITE = 39
|
||||
UC_PROT_READ = 1
|
||||
UC_PROT_WRITE = 2
|
||||
)
|
@ -1,6 +1,7 @@
|
||||
package unicorn
|
||||
// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [x86_const.go]
|
||||
const (
|
||||
|
||||
// X86 registers
|
||||
|
||||
UC_X86_REG_INVALID = 0
|
||||
|
15
bindings/python/sample_all.sh
Executable file
15
bindings/python/sample_all.sh
Executable file
@ -0,0 +1,15 @@
|
||||
#!/bin/sh
|
||||
|
||||
./sample_x86.py
|
||||
echo "=========================="
|
||||
./shellcode.py
|
||||
echo "=========================="
|
||||
./sample_arm.py
|
||||
echo "=========================="
|
||||
./sample_arm64.py
|
||||
echo "=========================="
|
||||
./sample_mips.py
|
||||
echo "=========================="
|
||||
./sample_sparc.py
|
||||
echo "=========================="
|
||||
./sample_m68k.py
|
@ -1,464 +1,4 @@
|
||||
# Unicorn Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
|
||||
import sys
|
||||
_python2 = sys.version_info[0] < 3
|
||||
if _python2:
|
||||
range = xrange
|
||||
from . import arm_const, arm64_const, mips_const, sparc_const, m68k_const, x86_const
|
||||
|
||||
__all__ = [
|
||||
'Uc',
|
||||
|
||||
'uc_version',
|
||||
'uc_arch_supported',
|
||||
'version_bind',
|
||||
'debug',
|
||||
|
||||
'UC_API_MAJOR',
|
||||
'UC_API_MINOR',
|
||||
|
||||
'UC_ARCH_ARM',
|
||||
'UC_ARCH_ARM64',
|
||||
'UC_ARCH_MIPS',
|
||||
'UC_ARCH_X86',
|
||||
'UC_ARCH_SPARC',
|
||||
'UC_ARCH_M68K',
|
||||
'UC_ARCH_ALL',
|
||||
|
||||
'UC_MODE_LITTLE_ENDIAN',
|
||||
'UC_MODE_BIG_ENDIAN',
|
||||
'UC_MODE_16',
|
||||
'UC_MODE_32',
|
||||
'UC_MODE_64',
|
||||
'UC_MODE_ARM',
|
||||
'UC_MODE_THUMB',
|
||||
'UC_MODE_MCLASS',
|
||||
'UC_MODE_MICRO',
|
||||
'UC_MODE_MIPS3',
|
||||
'UC_MODE_MIPS32R6',
|
||||
'UC_MODE_MIPSGP64',
|
||||
'UC_MODE_V8',
|
||||
'UC_MODE_V9',
|
||||
'UC_MODE_MIPS32',
|
||||
'UC_MODE_MIPS64',
|
||||
|
||||
'UC_ERR_OK',
|
||||
'UC_ERR_OOM',
|
||||
'UC_ERR_ARCH',
|
||||
'UC_ERR_HANDLE',
|
||||
'UC_ERR_UCH',
|
||||
'UC_ERR_MODE',
|
||||
'UC_ERR_VERSION',
|
||||
'UC_ERR_MEM_READ',
|
||||
'UC_ERR_MEM_WRITE',
|
||||
'UC_ERR_CODE_INVALID',
|
||||
'UC_ERR_HOOK',
|
||||
'UC_ERR_INSN_INVALID',
|
||||
'UC_ERR_MAP',
|
||||
|
||||
'UC_HOOK_INTR',
|
||||
'UC_HOOK_INSN',
|
||||
'UC_HOOK_CODE',
|
||||
'UC_HOOK_BLOCK',
|
||||
'UC_HOOK_MEM_INVALID',
|
||||
'UC_HOOK_MEM_READ',
|
||||
'UC_HOOK_MEM_WRITE',
|
||||
'UC_HOOK_MEM_READ_WRITE',
|
||||
|
||||
'UC_MEM_READ',
|
||||
'UC_MEM_WRITE',
|
||||
'UC_MEM_READ_WRITE',
|
||||
|
||||
'UC_SECOND_SCALE',
|
||||
'UC_MILISECOND_SCALE',
|
||||
|
||||
'UcError',
|
||||
]
|
||||
|
||||
# Unicorn C interface
|
||||
|
||||
# API version
|
||||
UC_API_MAJOR = 0
|
||||
UC_API_MINOR = 9
|
||||
|
||||
# Architectures
|
||||
UC_ARCH_ARM = 1
|
||||
UC_ARCH_ARM64 = 2
|
||||
UC_ARCH_MIPS = 3
|
||||
UC_ARCH_X86 = 4
|
||||
UC_ARCH_PPC = 5
|
||||
UC_ARCH_SPARC = 6
|
||||
UC_ARCH_M68K = 7
|
||||
UC_ARCH_MAX = 8
|
||||
UC_ARCH_ALL = 0xFFFF
|
||||
|
||||
# Hardware modes
|
||||
UC_MODE_LITTLE_ENDIAN = 0 # little-endian mode (default mode)
|
||||
UC_MODE_ARM = 0 # ARM mode
|
||||
UC_MODE_16 = (1 << 1) # 16-bit mode (for X86)
|
||||
UC_MODE_32 = (1 << 2) # 32-bit mode (for X86)
|
||||
UC_MODE_64 = (1 << 3) # 64-bit mode (for X86, PPC)
|
||||
UC_MODE_THUMB = (1 << 4) # ARM's Thumb mode, including Thumb-2
|
||||
UC_MODE_MCLASS = (1 << 5) # ARM's Cortex-M series
|
||||
UC_MODE_V8 = (1 << 6) # ARMv8 A32 encodings for ARM
|
||||
UC_MODE_MICRO = (1 << 4) # MicroMips mode (MIPS architecture)
|
||||
UC_MODE_MIPS3 = (1 << 5) # Mips III ISA
|
||||
UC_MODE_MIPS32R6 = (1 << 6) # Mips32r6 ISA
|
||||
UC_MODE_MIPSGP64 = (1 << 7) # General Purpose Registers are 64-bit wide (MIPS arch)
|
||||
UC_MODE_V9 = (1 << 4) # Sparc V9 mode (for Sparc)
|
||||
UC_MODE_BIG_ENDIAN = (1 << 31) # big-endian mode
|
||||
UC_MODE_MIPS32 = UC_MODE_32 # Mips32 ISA
|
||||
UC_MODE_MIPS64 = UC_MODE_64 # Mips64 ISA
|
||||
|
||||
|
||||
# Unicorn error type
|
||||
UC_ERR_OK = 0 # No error: everything was fine
|
||||
UC_ERR_OOM = 1 # Out-Of-Memory error: uc_open(), uc_emulate()
|
||||
UC_ERR_ARCH = 2 # Unsupported architecture: uc_open()
|
||||
UC_ERR_HANDLE = 3 # Invalid handle
|
||||
UC_ERR_UCH = 4 # Invalid handle (uch)
|
||||
UC_ERR_MODE = 5 # Invalid/unsupported mode: uc_open()
|
||||
UC_ERR_VERSION = 6 # Unsupported version (bindings)
|
||||
UC_ERR_MEM_READ = 7 # Quit emulation due to invalid memory READ: uc_emu_start()
|
||||
UC_ERR_MEM_WRITE = 8 # Quit emulation due to invalid memory WRITE: uc_emu_start()
|
||||
UC_ERR_CODE_INVALID = 9 # Quit emulation due to invalid code address: uc_emu_start()
|
||||
UC_ERR_HOOK = 10 # Invalid hook type: uc_hook_add()
|
||||
UC_ERR_INSN_INVALID = 11 # Invalid instruction
|
||||
UC_ERR_MAP = 12 # Invalid memory mapping
|
||||
|
||||
|
||||
# All type of hooks for uc_hook_add() API.
|
||||
UC_HOOK_INTR = 32 # Hook all interrupt events
|
||||
UC_HOOK_INSN = 33 # Hook a particular instruction
|
||||
UC_HOOK_CODE = 34 # Hook a range of code
|
||||
UC_HOOK_BLOCK = 35 # Hook basic blocks
|
||||
UC_HOOK_MEM_INVALID = 36 # Hook for all invalid memory access events
|
||||
UC_HOOK_MEM_READ = 37 # Hook all memory read events.
|
||||
UC_HOOK_MEM_WRITE = 38 # Hook all memory write events.
|
||||
UC_HOOK_MEM_READ_WRITE = 39 # Hook all memory accesses (either READ or WRITE).
|
||||
|
||||
|
||||
# All type of memory accesses for UC_HOOK_MEM_*
|
||||
UC_MEM_READ = 16 # Memory is read from
|
||||
UC_MEM_WRITE = 17 # Memory is written to
|
||||
UC_MEM_READ_WRITE = 18 # Memory is accessed (either READ or WRITE)
|
||||
|
||||
|
||||
# Time scales to calculate timeout on microsecond unit
|
||||
# This is for Uc.emu_start()
|
||||
UC_SECOND_SCALE = 1000000 # 1 second = 1000,000 microseconds
|
||||
UC_MILISECOND_SCALE = 1000 # 1 milisecond = 1000 nanoseconds
|
||||
|
||||
|
||||
import ctypes, ctypes.util, sys
|
||||
from os.path import split, join, dirname
|
||||
import distutils.sysconfig
|
||||
|
||||
|
||||
import inspect
|
||||
if not hasattr(sys.modules[__name__], '__file__'):
|
||||
__file__ = inspect.getfile(inspect.currentframe())
|
||||
|
||||
_lib_path = split(__file__)[0]
|
||||
_all_libs = ['unicorn.dll', 'libunicorn.so', 'libunicorn.dylib']
|
||||
_found = False
|
||||
|
||||
for _lib in _all_libs:
|
||||
try:
|
||||
_lib_file = join(_lib_path, _lib)
|
||||
# print "Trying to load:", _lib_file
|
||||
_uc = ctypes.cdll.LoadLibrary(_lib_file)
|
||||
_found = True
|
||||
break
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
if _found == False:
|
||||
# try loading from default paths
|
||||
for _lib in _all_libs:
|
||||
try:
|
||||
_uc = ctypes.cdll.LoadLibrary(_lib)
|
||||
_found = True
|
||||
break
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
if _found == False:
|
||||
# last try: loading from python lib directory
|
||||
_lib_path = distutils.sysconfig.get_python_lib()
|
||||
for _lib in _all_libs:
|
||||
try:
|
||||
_lib_file = join(_lib_path, 'unicorn', _lib)
|
||||
# print "Trying to load:", _lib_file
|
||||
_uc = ctypes.cdll.LoadLibrary(_lib_file)
|
||||
_found = True
|
||||
break
|
||||
except OSError:
|
||||
pass
|
||||
if _found == False:
|
||||
raise ImportError("ERROR: fail to load the dynamic library.")
|
||||
|
||||
|
||||
# setup all the function prototype
|
||||
def _setup_prototype(lib, fname, restype, *argtypes):
|
||||
getattr(lib, fname).restype = restype
|
||||
getattr(lib, fname).argtypes = argtypes
|
||||
|
||||
_setup_prototype(_uc, "uc_version", ctypes.c_int, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
|
||||
_setup_prototype(_uc, "uc_arch_supported", ctypes.c_bool, ctypes.c_int)
|
||||
_setup_prototype(_uc, "uc_open", ctypes.c_int, ctypes.c_uint, ctypes.c_uint, ctypes.POINTER(ctypes.c_size_t))
|
||||
_setup_prototype(_uc, "uc_close", ctypes.c_int, ctypes.POINTER(ctypes.c_size_t))
|
||||
_setup_prototype(_uc, "uc_strerror", ctypes.c_char_p, ctypes.c_int)
|
||||
_setup_prototype(_uc, "uc_errno", ctypes.c_int, ctypes.c_size_t)
|
||||
_setup_prototype(_uc, "uc_reg_read", ctypes.c_int, ctypes.c_size_t, ctypes.c_int, ctypes.c_void_p)
|
||||
_setup_prototype(_uc, "uc_reg_write", ctypes.c_int, ctypes.c_size_t, ctypes.c_int, ctypes.c_void_p)
|
||||
_setup_prototype(_uc, "uc_mem_read", ctypes.c_int, ctypes.c_size_t, ctypes.c_uint64, ctypes.POINTER(ctypes.c_char), ctypes.c_size_t)
|
||||
_setup_prototype(_uc, "uc_mem_write", ctypes.c_int, ctypes.c_size_t, ctypes.c_uint64, ctypes.POINTER(ctypes.c_char), ctypes.c_size_t)
|
||||
_setup_prototype(_uc, "uc_emu_start", ctypes.c_int, ctypes.c_size_t, ctypes.c_uint64, ctypes.c_uint64, ctypes.c_uint64, ctypes.c_size_t)
|
||||
_setup_prototype(_uc, "uc_emu_stop", ctypes.c_int, ctypes.c_size_t)
|
||||
_setup_prototype(_uc, "uc_hook_del", ctypes.c_int, ctypes.c_size_t, ctypes.POINTER(ctypes.c_size_t))
|
||||
_setup_prototype(_uc, "uc_mem_map", ctypes.c_int, ctypes.c_size_t, ctypes.c_uint64, ctypes.c_size_t)
|
||||
|
||||
# uc_hook_add is special due to variable number of arguments
|
||||
_uc.uc_hook_add = getattr(_uc, "uc_hook_add")
|
||||
_uc.uc_hook_add.restype = ctypes.c_int
|
||||
|
||||
UC_HOOK_CODE_CB = ctypes.CFUNCTYPE(None, ctypes.c_size_t, ctypes.c_uint64, ctypes.c_size_t, ctypes.c_void_p)
|
||||
UC_HOOK_MEM_INVALID_CB = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_size_t, ctypes.c_int, \
|
||||
ctypes.c_uint64, ctypes.c_int, ctypes.c_int64, ctypes.c_void_p)
|
||||
UC_HOOK_MEM_ACCESS_CB = ctypes.CFUNCTYPE(None, ctypes.c_size_t, ctypes.c_int, \
|
||||
ctypes.c_uint64, ctypes.c_int, ctypes.c_int64, ctypes.c_void_p)
|
||||
UC_HOOK_INTR_CB = ctypes.CFUNCTYPE(None, ctypes.c_size_t, ctypes.c_uint32, \
|
||||
ctypes.c_void_p)
|
||||
UC_HOOK_INSN_IN_CB = ctypes.CFUNCTYPE(ctypes.c_uint32, ctypes.c_size_t, ctypes.c_uint32, \
|
||||
ctypes.c_int, ctypes.c_void_p)
|
||||
UC_HOOK_INSN_OUT_CB = ctypes.CFUNCTYPE(None, ctypes.c_size_t, ctypes.c_uint32, \
|
||||
ctypes.c_int, ctypes.c_uint32, ctypes.c_void_p)
|
||||
UC_HOOK_INSN_SYSCALL_CB = ctypes.CFUNCTYPE(None, ctypes.c_size_t, ctypes.c_void_p)
|
||||
|
||||
|
||||
# access to error code via @errno of UcError
|
||||
class UcError(Exception):
|
||||
def __init__(self, errno):
|
||||
self.errno = errno
|
||||
|
||||
def __str__(self):
|
||||
return _uc.uc_strerror(self.errno)
|
||||
|
||||
|
||||
# return the core's version
|
||||
def uc_version():
|
||||
major = ctypes.c_int()
|
||||
minor = ctypes.c_int()
|
||||
combined = _uc.uc_version(ctypes.byref(major), ctypes.byref(minor))
|
||||
return (major.value, minor.value, combined)
|
||||
|
||||
|
||||
# return the binding's version
|
||||
def version_bind():
|
||||
return (UC_API_MAJOR, UC_API_MINOR, (UC_API_MAJOR << 8) + UC_API_MINOR)
|
||||
|
||||
|
||||
# check to see if this engine supports a particular arch
|
||||
def uc_arch_supported(query):
|
||||
return _uc.uc_arch_supported(query)
|
||||
|
||||
|
||||
class Uc(object):
|
||||
def __init__(self, arch, mode):
|
||||
# verify version compatibility with the core before doing anything
|
||||
(major, minor, _combined) = uc_version()
|
||||
if major != UC_API_MAJOR or minor != UC_API_MINOR:
|
||||
self._uch = None
|
||||
# our binding version is different from the core's API version
|
||||
raise UcError(UC_ERR_VERSION)
|
||||
|
||||
self._arch, self._mode = arch, mode
|
||||
self._uch = ctypes.c_size_t()
|
||||
status = _uc.uc_open(arch, mode, ctypes.byref(self._uch))
|
||||
if status != UC_ERR_OK:
|
||||
self._uch = None
|
||||
raise UcError(status)
|
||||
# internal mapping table to save callback & userdata
|
||||
self._callbacks = {}
|
||||
self._callback_count = 0
|
||||
|
||||
|
||||
# destructor to be called automatically when object is destroyed.
|
||||
def __del__(self):
|
||||
if self._uch:
|
||||
try:
|
||||
status = _uc.uc_close(ctypes.byref(self._uch))
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
except: # _uc might be pulled from under our feet
|
||||
pass
|
||||
|
||||
|
||||
# emulate from @begin, and stop when reaching address @until
|
||||
def emu_start(self, begin, until, timeout=0, count=0):
|
||||
status = _uc.uc_emu_start(self._uch, begin, until, timeout, count)
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
|
||||
|
||||
# stop emulation
|
||||
def emu_stop(self):
|
||||
status = _uc.uc_emu_stop(self._uch)
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
|
||||
|
||||
# return the value of a register
|
||||
def reg_read(self, reg_id):
|
||||
# read to 64bit number to be safe
|
||||
reg = ctypes.c_int64(0)
|
||||
status = _uc.uc_reg_read(self._uch, reg_id, ctypes.byref(reg))
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
return reg.value
|
||||
|
||||
|
||||
# write to a register
|
||||
def reg_write(self, reg_id, value):
|
||||
# convert to 64bit number to be safe
|
||||
reg = ctypes.c_int64(value)
|
||||
status = _uc.uc_reg_write(self._uch, reg_id, ctypes.byref(reg))
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
|
||||
|
||||
# read data from memory
|
||||
def mem_read(self, address, size):
|
||||
data = ctypes.create_string_buffer(size)
|
||||
status = _uc.uc_mem_read(self._uch, address, data, size)
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
return bytearray(data)
|
||||
|
||||
|
||||
# write to memory
|
||||
def mem_write(self, address, data):
|
||||
status = _uc.uc_mem_write(self._uch, address, data, len(data))
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
|
||||
|
||||
# map a range of memory
|
||||
def mem_map(self, address, size):
|
||||
status = _uc.uc_mem_map(self._uch, address, size)
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
|
||||
|
||||
def _hookcode_cb(self, handle, address, size, user_data):
|
||||
# call user's callback with self object
|
||||
(cb, data) = self._callbacks[user_data]
|
||||
cb(self, address, size, data)
|
||||
|
||||
|
||||
def _hook_mem_invalid_cb(self, handle, access, address, size, value, user_data):
|
||||
# call user's callback with self object
|
||||
(cb, data) = self._callbacks[user_data]
|
||||
return cb(self, access, address, size, value, data)
|
||||
|
||||
|
||||
def _hook_mem_access_cb(self, handle, access, address, size, value, user_data):
|
||||
# call user's callback with self object
|
||||
(cb, data) = self._callbacks[user_data]
|
||||
cb(self, access, address, size, value, data)
|
||||
|
||||
|
||||
def _hook_intr_cb(self, handle, intno, user_data):
|
||||
# call user's callback with self object
|
||||
(cb, data) = self._callbacks[user_data]
|
||||
cb(self, intno, data)
|
||||
|
||||
|
||||
def _hook_insn_in_cb(self, handle, port, size, user_data):
|
||||
# call user's callback with self object
|
||||
(cb, data) = self._callbacks[user_data]
|
||||
return cb(self, port, size, data)
|
||||
|
||||
|
||||
def _hook_insn_out_cb(self, handle, port, size, value, user_data):
|
||||
# call user's callback with self object
|
||||
(cb, data) = self._callbacks[user_data]
|
||||
cb(self, port, size, value, data)
|
||||
|
||||
|
||||
def _hook_insn_syscall_cb(self, handle, user_data):
|
||||
# call user's callback with self object
|
||||
(cb, data) = self._callbacks[user_data]
|
||||
cb(self, data)
|
||||
|
||||
|
||||
# add a hook
|
||||
def hook_add(self, htype, callback, user_data=None, arg1=1, arg2=0):
|
||||
_h2 = ctypes.c_size_t()
|
||||
|
||||
# save callback & user_data
|
||||
self._callback_count += 1
|
||||
self._callbacks[self._callback_count] = (callback, user_data)
|
||||
|
||||
if htype in (UC_HOOK_BLOCK, UC_HOOK_CODE):
|
||||
begin = ctypes.c_uint64(arg1)
|
||||
end = ctypes.c_uint64(arg2)
|
||||
# set callback with wrapper, so it can be called
|
||||
# with this object as param
|
||||
cb = ctypes.cast(UC_HOOK_CODE_CB(self._hookcode_cb), UC_HOOK_CODE_CB)
|
||||
status = _uc.uc_hook_add(self._uch, ctypes.byref(_h2), htype, cb, \
|
||||
ctypes.cast(self._callback_count, ctypes.c_void_p), begin, end)
|
||||
elif htype == UC_HOOK_MEM_INVALID:
|
||||
cb = ctypes.cast(UC_HOOK_MEM_INVALID_CB(self._hook_mem_invalid_cb), UC_HOOK_MEM_INVALID_CB)
|
||||
status = _uc.uc_hook_add(self._uch, ctypes.byref(_h2), htype, \
|
||||
cb, ctypes.cast(self._callback_count, ctypes.c_void_p))
|
||||
elif htype in (UC_HOOK_MEM_READ, UC_HOOK_MEM_WRITE, UC_HOOK_MEM_READ_WRITE):
|
||||
cb = ctypes.cast(UC_HOOK_MEM_ACCESS_CB(self._hook_mem_access_cb), UC_HOOK_MEM_ACCESS_CB)
|
||||
status = _uc.uc_hook_add(self._uch, ctypes.byref(_h2), htype, \
|
||||
cb, ctypes.cast(self._callback_count, ctypes.c_void_p))
|
||||
elif htype == UC_HOOK_INSN:
|
||||
insn = ctypes.c_int(arg1)
|
||||
if arg1 == x86_const.UC_X86_INS_IN: # IN instruction
|
||||
cb = ctypes.cast(UC_HOOK_INSN_IN_CB(self._hook_insn_in_cb), UC_HOOK_INSN_IN_CB)
|
||||
if arg1 == x86_const.UC_X86_INS_OUT: # OUT instruction
|
||||
cb = ctypes.cast(UC_HOOK_INSN_OUT_CB(self._hook_insn_out_cb), UC_HOOK_INSN_OUT_CB)
|
||||
if arg1 in (x86_const.UC_X86_INS_SYSCALL, x86_const.UC_X86_INS_SYSENTER): # SYSCALL/SYSENTER instruction
|
||||
cb = ctypes.cast(UC_HOOK_INSN_SYSCALL_CB(self._hook_insn_syscall_cb), UC_HOOK_INSN_SYSCALL_CB)
|
||||
status = _uc.uc_hook_add(self._uch, ctypes.byref(_h2), htype, \
|
||||
cb, ctypes.cast(self._callback_count, ctypes.c_void_p), insn)
|
||||
elif htype == UC_HOOK_INTR:
|
||||
cb = ctypes.cast(UC_HOOK_INTR_CB(self._hook_intr_cb), UC_HOOK_INTR_CB)
|
||||
status = _uc.uc_hook_add(self._uch, ctypes.byref(_h2), htype, \
|
||||
cb, ctypes.cast(self._callback_count, ctypes.c_void_p))
|
||||
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
|
||||
return _h2.value
|
||||
|
||||
|
||||
# delete a hook
|
||||
def hook_del(self, h):
|
||||
_h = ctypes.c_size_t(h)
|
||||
status = _uc.uc_hook_del(self._uch, ctypes.byref(_h))
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
h = 0
|
||||
|
||||
|
||||
# print out debugging info
|
||||
def debug():
|
||||
archs = { "arm": UC_ARCH_ARM, "arm64": UC_ARCH_ARM64, \
|
||||
"mips": UC_ARCH_MIPS, "sparc": UC_ARCH_SPARC, \
|
||||
"m68k": UC_ARCH_M68K, "x86": UC_ARCH_X86 }
|
||||
|
||||
all_archs = ""
|
||||
keys = archs.keys()
|
||||
keys.sort()
|
||||
for k in keys:
|
||||
if uc_arch_supported(archs[k]):
|
||||
all_archs += "-%s" % k
|
||||
|
||||
(major, minor, _combined) = uc_version()
|
||||
|
||||
return "python-%s-c%u.%u-b%u.%u" % (all_archs, major, minor, UC_API_MAJOR, UC_API_MINOR)
|
||||
from unicorn_const import *
|
||||
from unicorn import Uc, uc_version, uc_arch_supported, version_bind, debug, UcError
|
||||
|
@ -268,7 +268,7 @@ UC_ARM64_REG_PC = 260
|
||||
UC_ARM64_REG_ENDING = 261
|
||||
|
||||
# alias registers
|
||||
UC_ARM64_REG_IP1 = UC_ARM64_REG_X16
|
||||
UC_ARM64_REG_IP0 = UC_ARM64_REG_X17
|
||||
UC_ARM64_REG_FP = UC_ARM64_REG_X29
|
||||
UC_ARM64_REG_LR = UC_ARM64_REG_X30
|
||||
UC_ARM64_REG_IP1 = 215
|
||||
UC_ARM64_REG_IP0 = 216
|
||||
UC_ARM64_REG_FP = 1
|
||||
UC_ARM64_REG_LR = 2
|
||||
|
@ -116,10 +116,10 @@ UC_ARM_REG_S31 = 110
|
||||
UC_ARM_REG_ENDING = 111
|
||||
|
||||
# alias registers
|
||||
UC_ARM_REG_R13 = UC_ARM_REG_SP
|
||||
UC_ARM_REG_R14 = UC_ARM_REG_LR
|
||||
UC_ARM_REG_R15 = UC_ARM_REG_PC
|
||||
UC_ARM_REG_SB = UC_ARM_REG_R9
|
||||
UC_ARM_REG_SL = UC_ARM_REG_R10
|
||||
UC_ARM_REG_FP = UC_ARM_REG_R11
|
||||
UC_ARM_REG_IP = UC_ARM_REG_R12
|
||||
UC_ARM_REG_R13 = 12
|
||||
UC_ARM_REG_R14 = 10
|
||||
UC_ARM_REG_R15 = 11
|
||||
UC_ARM_REG_SB = 75
|
||||
UC_ARM_REG_SL = 76
|
||||
UC_ARM_REG_FP = 77
|
||||
UC_ARM_REG_IP = 78
|
||||
|
@ -152,44 +152,44 @@ UC_MIPS_REG_MPL0 = 134
|
||||
UC_MIPS_REG_MPL1 = 135
|
||||
UC_MIPS_REG_MPL2 = 136
|
||||
UC_MIPS_REG_ENDING = 137
|
||||
UC_MIPS_REG_ZERO = UC_MIPS_REG_0
|
||||
UC_MIPS_REG_AT = UC_MIPS_REG_1
|
||||
UC_MIPS_REG_V0 = UC_MIPS_REG_2
|
||||
UC_MIPS_REG_V1 = UC_MIPS_REG_3
|
||||
UC_MIPS_REG_A0 = UC_MIPS_REG_4
|
||||
UC_MIPS_REG_A1 = UC_MIPS_REG_5
|
||||
UC_MIPS_REG_A2 = UC_MIPS_REG_6
|
||||
UC_MIPS_REG_A3 = UC_MIPS_REG_7
|
||||
UC_MIPS_REG_T0 = UC_MIPS_REG_8
|
||||
UC_MIPS_REG_T1 = UC_MIPS_REG_9
|
||||
UC_MIPS_REG_T2 = UC_MIPS_REG_10
|
||||
UC_MIPS_REG_T3 = UC_MIPS_REG_11
|
||||
UC_MIPS_REG_T4 = UC_MIPS_REG_12
|
||||
UC_MIPS_REG_T5 = UC_MIPS_REG_13
|
||||
UC_MIPS_REG_T6 = UC_MIPS_REG_14
|
||||
UC_MIPS_REG_T7 = UC_MIPS_REG_15
|
||||
UC_MIPS_REG_S0 = UC_MIPS_REG_16
|
||||
UC_MIPS_REG_S1 = UC_MIPS_REG_17
|
||||
UC_MIPS_REG_S2 = UC_MIPS_REG_18
|
||||
UC_MIPS_REG_S3 = UC_MIPS_REG_19
|
||||
UC_MIPS_REG_S4 = UC_MIPS_REG_20
|
||||
UC_MIPS_REG_S5 = UC_MIPS_REG_21
|
||||
UC_MIPS_REG_S6 = UC_MIPS_REG_22
|
||||
UC_MIPS_REG_S7 = UC_MIPS_REG_23
|
||||
UC_MIPS_REG_T8 = UC_MIPS_REG_24
|
||||
UC_MIPS_REG_T9 = UC_MIPS_REG_25
|
||||
UC_MIPS_REG_K0 = UC_MIPS_REG_26
|
||||
UC_MIPS_REG_K1 = UC_MIPS_REG_27
|
||||
UC_MIPS_REG_GP = UC_MIPS_REG_28
|
||||
UC_MIPS_REG_SP = UC_MIPS_REG_29
|
||||
UC_MIPS_REG_FP = UC_MIPS_REG_30
|
||||
UC_MIPS_REG_S8 = UC_MIPS_REG_30
|
||||
UC_MIPS_REG_RA = UC_MIPS_REG_31
|
||||
UC_MIPS_REG_HI0 = UC_MIPS_REG_AC0
|
||||
UC_MIPS_REG_HI1 = UC_MIPS_REG_AC1
|
||||
UC_MIPS_REG_HI2 = UC_MIPS_REG_AC2
|
||||
UC_MIPS_REG_HI3 = UC_MIPS_REG_AC3
|
||||
UC_MIPS_REG_LO0 = UC_MIPS_REG_HI0
|
||||
UC_MIPS_REG_LO1 = UC_MIPS_REG_HI1
|
||||
UC_MIPS_REG_LO2 = UC_MIPS_REG_HI2
|
||||
UC_MIPS_REG_LO3 = UC_MIPS_REG_HI3
|
||||
UC_MIPS_REG_ZERO = 2
|
||||
UC_MIPS_REG_AT = 3
|
||||
UC_MIPS_REG_V0 = 4
|
||||
UC_MIPS_REG_V1 = 5
|
||||
UC_MIPS_REG_A0 = 6
|
||||
UC_MIPS_REG_A1 = 7
|
||||
UC_MIPS_REG_A2 = 8
|
||||
UC_MIPS_REG_A3 = 9
|
||||
UC_MIPS_REG_T0 = 10
|
||||
UC_MIPS_REG_T1 = 11
|
||||
UC_MIPS_REG_T2 = 12
|
||||
UC_MIPS_REG_T3 = 13
|
||||
UC_MIPS_REG_T4 = 14
|
||||
UC_MIPS_REG_T5 = 15
|
||||
UC_MIPS_REG_T6 = 16
|
||||
UC_MIPS_REG_T7 = 17
|
||||
UC_MIPS_REG_S0 = 18
|
||||
UC_MIPS_REG_S1 = 19
|
||||
UC_MIPS_REG_S2 = 20
|
||||
UC_MIPS_REG_S3 = 21
|
||||
UC_MIPS_REG_S4 = 22
|
||||
UC_MIPS_REG_S5 = 23
|
||||
UC_MIPS_REG_S6 = 24
|
||||
UC_MIPS_REG_S7 = 25
|
||||
UC_MIPS_REG_T8 = 26
|
||||
UC_MIPS_REG_T9 = 27
|
||||
UC_MIPS_REG_K0 = 28
|
||||
UC_MIPS_REG_K1 = 29
|
||||
UC_MIPS_REG_GP = 30
|
||||
UC_MIPS_REG_SP = 31
|
||||
UC_MIPS_REG_FP = 32
|
||||
UC_MIPS_REG_S8 = 32
|
||||
UC_MIPS_REG_RA = 33
|
||||
UC_MIPS_REG_HI0 = 45
|
||||
UC_MIPS_REG_HI1 = 46
|
||||
UC_MIPS_REG_HI2 = 47
|
||||
UC_MIPS_REG_HI3 = 48
|
||||
UC_MIPS_REG_LO0 = 45
|
||||
UC_MIPS_REG_LO1 = 46
|
||||
UC_MIPS_REG_LO2 = 47
|
||||
UC_MIPS_REG_LO3 = 48
|
||||
|
@ -92,5 +92,5 @@ UC_SPARC_REG_Y = 86
|
||||
UC_SPARC_REG_XCC = 87
|
||||
UC_SPARC_REG_PC = 88
|
||||
UC_SPARC_REG_ENDING = 89
|
||||
UC_SPARC_REG_O6 = UC_SPARC_REG_SP
|
||||
UC_SPARC_REG_I6 = UC_SPARC_REG_FP
|
||||
UC_SPARC_REG_O6 = 85
|
||||
UC_SPARC_REG_I6 = 53
|
||||
|
321
bindings/python/unicorn/unicorn.py
Normal file
321
bindings/python/unicorn/unicorn.py
Normal file
@ -0,0 +1,321 @@
|
||||
# Unicorn Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
|
||||
import sys
|
||||
_python2 = sys.version_info[0] < 3
|
||||
if _python2:
|
||||
range = xrange
|
||||
from . import arm_const, arm64_const, mips_const, sparc_const, m68k_const, x86_const
|
||||
from unicorn_const import *
|
||||
|
||||
import ctypes, ctypes.util, sys
|
||||
from os.path import split, join, dirname
|
||||
import distutils.sysconfig
|
||||
|
||||
|
||||
import inspect
|
||||
if not hasattr(sys.modules[__name__], '__file__'):
|
||||
__file__ = inspect.getfile(inspect.currentframe())
|
||||
|
||||
_lib_path = split(__file__)[0]
|
||||
_all_libs = ['unicorn.dll', 'libunicorn.so', 'libunicorn.dylib']
|
||||
_found = False
|
||||
|
||||
for _lib in _all_libs:
|
||||
try:
|
||||
_lib_file = join(_lib_path, _lib)
|
||||
# print "Trying to load:", _lib_file
|
||||
_uc = ctypes.cdll.LoadLibrary(_lib_file)
|
||||
_found = True
|
||||
break
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
if _found == False:
|
||||
# try loading from default paths
|
||||
for _lib in _all_libs:
|
||||
try:
|
||||
_uc = ctypes.cdll.LoadLibrary(_lib)
|
||||
_found = True
|
||||
break
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
if _found == False:
|
||||
# last try: loading from python lib directory
|
||||
_lib_path = distutils.sysconfig.get_python_lib()
|
||||
for _lib in _all_libs:
|
||||
try:
|
||||
_lib_file = join(_lib_path, 'unicorn', _lib)
|
||||
# print "Trying to load:", _lib_file
|
||||
_uc = ctypes.cdll.LoadLibrary(_lib_file)
|
||||
_found = True
|
||||
break
|
||||
except OSError:
|
||||
pass
|
||||
if _found == False:
|
||||
raise ImportError("ERROR: fail to load the dynamic library.")
|
||||
|
||||
|
||||
# setup all the function prototype
|
||||
def _setup_prototype(lib, fname, restype, *argtypes):
|
||||
getattr(lib, fname).restype = restype
|
||||
getattr(lib, fname).argtypes = argtypes
|
||||
|
||||
_setup_prototype(_uc, "uc_version", ctypes.c_int, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
|
||||
_setup_prototype(_uc, "uc_arch_supported", ctypes.c_bool, ctypes.c_int)
|
||||
_setup_prototype(_uc, "uc_open", ctypes.c_int, ctypes.c_uint, ctypes.c_uint, ctypes.POINTER(ctypes.c_size_t))
|
||||
_setup_prototype(_uc, "uc_close", ctypes.c_int, ctypes.POINTER(ctypes.c_size_t))
|
||||
_setup_prototype(_uc, "uc_strerror", ctypes.c_char_p, ctypes.c_int)
|
||||
_setup_prototype(_uc, "uc_errno", ctypes.c_int, ctypes.c_size_t)
|
||||
_setup_prototype(_uc, "uc_reg_read", ctypes.c_int, ctypes.c_size_t, ctypes.c_int, ctypes.c_void_p)
|
||||
_setup_prototype(_uc, "uc_reg_write", ctypes.c_int, ctypes.c_size_t, ctypes.c_int, ctypes.c_void_p)
|
||||
_setup_prototype(_uc, "uc_mem_read", ctypes.c_int, ctypes.c_size_t, ctypes.c_uint64, ctypes.POINTER(ctypes.c_char), ctypes.c_size_t)
|
||||
_setup_prototype(_uc, "uc_mem_write", ctypes.c_int, ctypes.c_size_t, ctypes.c_uint64, ctypes.POINTER(ctypes.c_char), ctypes.c_size_t)
|
||||
_setup_prototype(_uc, "uc_emu_start", ctypes.c_int, ctypes.c_size_t, ctypes.c_uint64, ctypes.c_uint64, ctypes.c_uint64, ctypes.c_size_t)
|
||||
_setup_prototype(_uc, "uc_emu_stop", ctypes.c_int, ctypes.c_size_t)
|
||||
_setup_prototype(_uc, "uc_hook_del", ctypes.c_int, ctypes.c_size_t, ctypes.POINTER(ctypes.c_size_t))
|
||||
_setup_prototype(_uc, "uc_mem_map", ctypes.c_int, ctypes.c_size_t, ctypes.c_uint64, ctypes.c_size_t)
|
||||
|
||||
# uc_hook_add is special due to variable number of arguments
|
||||
_uc.uc_hook_add = getattr(_uc, "uc_hook_add")
|
||||
_uc.uc_hook_add.restype = ctypes.c_int
|
||||
|
||||
UC_HOOK_CODE_CB = ctypes.CFUNCTYPE(None, ctypes.c_size_t, ctypes.c_uint64, ctypes.c_size_t, ctypes.c_void_p)
|
||||
UC_HOOK_MEM_INVALID_CB = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_size_t, ctypes.c_int, \
|
||||
ctypes.c_uint64, ctypes.c_int, ctypes.c_int64, ctypes.c_void_p)
|
||||
UC_HOOK_MEM_ACCESS_CB = ctypes.CFUNCTYPE(None, ctypes.c_size_t, ctypes.c_int, \
|
||||
ctypes.c_uint64, ctypes.c_int, ctypes.c_int64, ctypes.c_void_p)
|
||||
UC_HOOK_INTR_CB = ctypes.CFUNCTYPE(None, ctypes.c_size_t, ctypes.c_uint32, \
|
||||
ctypes.c_void_p)
|
||||
UC_HOOK_INSN_IN_CB = ctypes.CFUNCTYPE(ctypes.c_uint32, ctypes.c_size_t, ctypes.c_uint32, \
|
||||
ctypes.c_int, ctypes.c_void_p)
|
||||
UC_HOOK_INSN_OUT_CB = ctypes.CFUNCTYPE(None, ctypes.c_size_t, ctypes.c_uint32, \
|
||||
ctypes.c_int, ctypes.c_uint32, ctypes.c_void_p)
|
||||
UC_HOOK_INSN_SYSCALL_CB = ctypes.CFUNCTYPE(None, ctypes.c_size_t, ctypes.c_void_p)
|
||||
|
||||
|
||||
# access to error code via @errno of UcError
|
||||
class UcError(Exception):
|
||||
def __init__(self, errno):
|
||||
self.errno = errno
|
||||
|
||||
def __str__(self):
|
||||
return _uc.uc_strerror(self.errno)
|
||||
|
||||
|
||||
# return the core's version
|
||||
def uc_version():
|
||||
major = ctypes.c_int()
|
||||
minor = ctypes.c_int()
|
||||
combined = _uc.uc_version(ctypes.byref(major), ctypes.byref(minor))
|
||||
return (major.value, minor.value, combined)
|
||||
|
||||
|
||||
# return the binding's version
|
||||
def version_bind():
|
||||
return (UC_API_MAJOR, UC_API_MINOR, (UC_API_MAJOR << 8) + UC_API_MINOR)
|
||||
|
||||
|
||||
# check to see if this engine supports a particular arch
|
||||
def uc_arch_supported(query):
|
||||
return _uc.uc_arch_supported(query)
|
||||
|
||||
|
||||
class Uc(object):
|
||||
def __init__(self, arch, mode):
|
||||
# verify version compatibility with the core before doing anything
|
||||
(major, minor, _combined) = uc_version()
|
||||
if major != UC_API_MAJOR or minor != UC_API_MINOR:
|
||||
self._uch = None
|
||||
# our binding version is different from the core's API version
|
||||
raise UcError(UC_ERR_VERSION)
|
||||
|
||||
self._arch, self._mode = arch, mode
|
||||
self._uch = ctypes.c_size_t()
|
||||
status = _uc.uc_open(arch, mode, ctypes.byref(self._uch))
|
||||
if status != UC_ERR_OK:
|
||||
self._uch = None
|
||||
raise UcError(status)
|
||||
# internal mapping table to save callback & userdata
|
||||
self._callbacks = {}
|
||||
self._callback_count = 0
|
||||
|
||||
|
||||
# destructor to be called automatically when object is destroyed.
|
||||
def __del__(self):
|
||||
if self._uch:
|
||||
try:
|
||||
status = _uc.uc_close(ctypes.byref(self._uch))
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
except: # _uc might be pulled from under our feet
|
||||
pass
|
||||
|
||||
|
||||
# emulate from @begin, and stop when reaching address @until
|
||||
def emu_start(self, begin, until, timeout=0, count=0):
|
||||
status = _uc.uc_emu_start(self._uch, begin, until, timeout, count)
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
|
||||
|
||||
# stop emulation
|
||||
def emu_stop(self):
|
||||
status = _uc.uc_emu_stop(self._uch)
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
|
||||
|
||||
# return the value of a register
|
||||
def reg_read(self, reg_id):
|
||||
# read to 64bit number to be safe
|
||||
reg = ctypes.c_int64(0)
|
||||
status = _uc.uc_reg_read(self._uch, reg_id, ctypes.byref(reg))
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
return reg.value
|
||||
|
||||
|
||||
# write to a register
|
||||
def reg_write(self, reg_id, value):
|
||||
# convert to 64bit number to be safe
|
||||
reg = ctypes.c_int64(value)
|
||||
status = _uc.uc_reg_write(self._uch, reg_id, ctypes.byref(reg))
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
|
||||
|
||||
# read data from memory
|
||||
def mem_read(self, address, size):
|
||||
data = ctypes.create_string_buffer(size)
|
||||
status = _uc.uc_mem_read(self._uch, address, data, size)
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
return bytearray(data)
|
||||
|
||||
|
||||
# write to memory
|
||||
def mem_write(self, address, data):
|
||||
status = _uc.uc_mem_write(self._uch, address, data, len(data))
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
|
||||
|
||||
# map a range of memory
|
||||
def mem_map(self, address, size):
|
||||
status = _uc.uc_mem_map(self._uch, address, size)
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
|
||||
|
||||
def _hookcode_cb(self, handle, address, size, user_data):
|
||||
# call user's callback with self object
|
||||
(cb, data) = self._callbacks[user_data]
|
||||
cb(self, address, size, data)
|
||||
|
||||
|
||||
def _hook_mem_invalid_cb(self, handle, access, address, size, value, user_data):
|
||||
# call user's callback with self object
|
||||
(cb, data) = self._callbacks[user_data]
|
||||
return cb(self, access, address, size, value, data)
|
||||
|
||||
|
||||
def _hook_mem_access_cb(self, handle, access, address, size, value, user_data):
|
||||
# call user's callback with self object
|
||||
(cb, data) = self._callbacks[user_data]
|
||||
cb(self, access, address, size, value, data)
|
||||
|
||||
|
||||
def _hook_intr_cb(self, handle, intno, user_data):
|
||||
# call user's callback with self object
|
||||
(cb, data) = self._callbacks[user_data]
|
||||
cb(self, intno, data)
|
||||
|
||||
|
||||
def _hook_insn_in_cb(self, handle, port, size, user_data):
|
||||
# call user's callback with self object
|
||||
(cb, data) = self._callbacks[user_data]
|
||||
return cb(self, port, size, data)
|
||||
|
||||
|
||||
def _hook_insn_out_cb(self, handle, port, size, value, user_data):
|
||||
# call user's callback with self object
|
||||
(cb, data) = self._callbacks[user_data]
|
||||
cb(self, port, size, value, data)
|
||||
|
||||
|
||||
def _hook_insn_syscall_cb(self, handle, user_data):
|
||||
# call user's callback with self object
|
||||
(cb, data) = self._callbacks[user_data]
|
||||
cb(self, data)
|
||||
|
||||
|
||||
# add a hook
|
||||
def hook_add(self, htype, callback, user_data=None, arg1=1, arg2=0):
|
||||
_h2 = ctypes.c_size_t()
|
||||
|
||||
# save callback & user_data
|
||||
self._callback_count += 1
|
||||
self._callbacks[self._callback_count] = (callback, user_data)
|
||||
|
||||
if htype in (UC_HOOK_BLOCK, UC_HOOK_CODE):
|
||||
begin = ctypes.c_uint64(arg1)
|
||||
end = ctypes.c_uint64(arg2)
|
||||
# set callback with wrapper, so it can be called
|
||||
# with this object as param
|
||||
cb = ctypes.cast(UC_HOOK_CODE_CB(self._hookcode_cb), UC_HOOK_CODE_CB)
|
||||
status = _uc.uc_hook_add(self._uch, ctypes.byref(_h2), htype, cb, \
|
||||
ctypes.cast(self._callback_count, ctypes.c_void_p), begin, end)
|
||||
elif htype == UC_HOOK_MEM_INVALID:
|
||||
cb = ctypes.cast(UC_HOOK_MEM_INVALID_CB(self._hook_mem_invalid_cb), UC_HOOK_MEM_INVALID_CB)
|
||||
status = _uc.uc_hook_add(self._uch, ctypes.byref(_h2), htype, \
|
||||
cb, ctypes.cast(self._callback_count, ctypes.c_void_p))
|
||||
elif htype in (UC_HOOK_MEM_READ, UC_HOOK_MEM_WRITE, UC_HOOK_MEM_READ_WRITE):
|
||||
cb = ctypes.cast(UC_HOOK_MEM_ACCESS_CB(self._hook_mem_access_cb), UC_HOOK_MEM_ACCESS_CB)
|
||||
status = _uc.uc_hook_add(self._uch, ctypes.byref(_h2), htype, \
|
||||
cb, ctypes.cast(self._callback_count, ctypes.c_void_p))
|
||||
elif htype == UC_HOOK_INSN:
|
||||
insn = ctypes.c_int(arg1)
|
||||
if arg1 == x86_const.UC_X86_INS_IN: # IN instruction
|
||||
cb = ctypes.cast(UC_HOOK_INSN_IN_CB(self._hook_insn_in_cb), UC_HOOK_INSN_IN_CB)
|
||||
if arg1 == x86_const.UC_X86_INS_OUT: # OUT instruction
|
||||
cb = ctypes.cast(UC_HOOK_INSN_OUT_CB(self._hook_insn_out_cb), UC_HOOK_INSN_OUT_CB)
|
||||
if arg1 in (x86_const.UC_X86_INS_SYSCALL, x86_const.UC_X86_INS_SYSENTER): # SYSCALL/SYSENTER instruction
|
||||
cb = ctypes.cast(UC_HOOK_INSN_SYSCALL_CB(self._hook_insn_syscall_cb), UC_HOOK_INSN_SYSCALL_CB)
|
||||
status = _uc.uc_hook_add(self._uch, ctypes.byref(_h2), htype, \
|
||||
cb, ctypes.cast(self._callback_count, ctypes.c_void_p), insn)
|
||||
elif htype == UC_HOOK_INTR:
|
||||
cb = ctypes.cast(UC_HOOK_INTR_CB(self._hook_intr_cb), UC_HOOK_INTR_CB)
|
||||
status = _uc.uc_hook_add(self._uch, ctypes.byref(_h2), htype, \
|
||||
cb, ctypes.cast(self._callback_count, ctypes.c_void_p))
|
||||
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
|
||||
return _h2.value
|
||||
|
||||
|
||||
# delete a hook
|
||||
def hook_del(self, h):
|
||||
_h = ctypes.c_size_t(h)
|
||||
status = _uc.uc_hook_del(self._uch, ctypes.byref(_h))
|
||||
if status != UC_ERR_OK:
|
||||
raise UcError(status)
|
||||
h = 0
|
||||
|
||||
|
||||
# print out debugging info
|
||||
def debug():
|
||||
archs = { "arm": UC_ARCH_ARM, "arm64": UC_ARCH_ARM64, \
|
||||
"mips": UC_ARCH_MIPS, "sparc": UC_ARCH_SPARC, \
|
||||
"m68k": UC_ARCH_M68K, "x86": UC_ARCH_X86 }
|
||||
|
||||
all_archs = ""
|
||||
keys = archs.keys()
|
||||
keys.sort()
|
||||
for k in keys:
|
||||
if uc_arch_supported(archs[k]):
|
||||
all_archs += "-%s" % k
|
||||
|
||||
(major, minor, _combined) = uc_version()
|
||||
|
||||
return "python-%s-c%u.%u-b%u.%u" % (all_archs, major, minor, UC_API_MAJOR, UC_API_MINOR)
|
63
bindings/python/unicorn/unicorn_const.py
Normal file
63
bindings/python/unicorn/unicorn_const.py
Normal file
@ -0,0 +1,63 @@
|
||||
# For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [unicorn_const.py]
|
||||
|
||||
UC_API_MAJOR = 0
|
||||
UC_API_MINOR = 9
|
||||
UC_SECOND_SCALE = 1000000
|
||||
UC_MILISECOND_SCALE = 1000
|
||||
UC_ARCH_ARM = 1
|
||||
UC_ARCH_ARM64 = 2
|
||||
UC_ARCH_MIPS = 3
|
||||
UC_ARCH_X86 = 4
|
||||
UC_ARCH_PPC = 5
|
||||
UC_ARCH_SPARC = 6
|
||||
UC_ARCH_M68K = 7
|
||||
UC_ARCH_MAX = 8
|
||||
|
||||
UC_MODE_LITTLE_ENDIAN = 0
|
||||
|
||||
UC_MODE_ARM = 0
|
||||
UC_MODE_16 = 2
|
||||
UC_MODE_32 = 4
|
||||
UC_MODE_64 = 8
|
||||
UC_MODE_THUMB = 16
|
||||
UC_MODE_MCLASS = 32
|
||||
UC_MODE_V8 = 64
|
||||
UC_MODE_MICRO = 16
|
||||
UC_MODE_MIPS3 = 32
|
||||
UC_MODE_MIPS32R6 = 64
|
||||
UC_MODE_V9 = 16
|
||||
UC_MODE_QPX = 16
|
||||
UC_MODE_BIG_ENDIAN = 2147483648
|
||||
UC_MODE_MIPS32 = 4
|
||||
UC_MODE_MIPS64 = 8
|
||||
|
||||
UC_ERR_OK = 0
|
||||
UC_ERR_OOM = 1
|
||||
UC_ERR_ARCH = 2
|
||||
UC_ERR_HANDLE = 3
|
||||
UC_ERR_UCH = 4
|
||||
UC_ERR_MODE = 5
|
||||
UC_ERR_VERSION = 6
|
||||
UC_ERR_MEM_READ = 7
|
||||
UC_ERR_MEM_WRITE = 8
|
||||
UC_ERR_CODE_INVALID = 9
|
||||
UC_ERR_HOOK = 10
|
||||
UC_ERR_INSN_INVALID = 11
|
||||
UC_ERR_MAP = 12
|
||||
UC_ERR_MEM_WRITE_NW = 13
|
||||
UC_ERR_MEM_READ_NR = 14
|
||||
UC_MEM_READ = 16
|
||||
UC_MEM_WRITE = 17
|
||||
UC_MEM_READ_WRITE = 18
|
||||
UC_MEM_WRITE_NW = 19
|
||||
UC_MEM_READ_NR = 20
|
||||
UC_HOOK_INTR = 32
|
||||
UC_HOOK_INSN = 33
|
||||
UC_HOOK_CODE = 34
|
||||
UC_HOOK_BLOCK = 35
|
||||
UC_HOOK_MEM_INVALID = 36
|
||||
UC_HOOK_MEM_READ = 37
|
||||
UC_HOOK_MEM_WRITE = 38
|
||||
UC_HOOK_MEM_READ_WRITE = 39
|
||||
UC_PROT_READ = 1
|
||||
UC_PROT_WRITE = 2
|
0
include/uc_priv.h
Executable file → Normal file
0
include/uc_priv.h
Executable file → Normal file
0
include/unicorn/unicorn.h
Executable file → Normal file
0
include/unicorn/unicorn.h
Executable file → Normal file
0
qemu/softmmu_template.h
Executable file → Normal file
0
qemu/softmmu_template.h
Executable file → Normal file
13
regress/emu_stop_segfault.py
Executable file
13
regress/emu_stop_segfault.py
Executable file
@ -0,0 +1,13 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
"""See https://github.com/unicorn-engine/unicorn/issues/65"""
|
||||
|
||||
import unicorn
|
||||
ADDR = 0x10101000
|
||||
mu = unicorn.Uc(unicorn.UC_ARCH_X86, unicorn.UC_MODE_32)
|
||||
mu.mem_map(ADDR, 1024 * 4)
|
||||
mu.mem_write(ADDR, b'\x41')
|
||||
mu.emu_start(ADDR, ADDR + 1, count=1)
|
||||
# The following should not trigger a null pointer dereference
|
||||
mu.emu_stop()
|
||||
|
@ -17,6 +17,8 @@ if test -e $DIR/sample_x86; then
|
||||
$DIR/sample_x86 -64
|
||||
echo "=========================="
|
||||
$DIR/sample_x86 -16
|
||||
echo "=========================="
|
||||
$DIR/shellcode -32
|
||||
fi
|
||||
if test -e $DIR/sample_arm; then
|
||||
echo "=========================="
|
||||
|
@ -26,6 +26,7 @@
|
||||
//#define X86_CODE64 "\x41\xBC\x3B\xB0\x28\x2A\x49\x0F\xC9\x90\x4D\x0F\xAD\xCF\x49\x87\xFD\x90\x48\x81\xD2\x8A\xCE\x77\x35\x48\xF7\xD9"
|
||||
#define X86_CODE64 "\x41\xBC\x3B\xB0\x28\x2A\x49\x0F\xC9\x90\x4D\x0F\xAD\xCF\x49\x87\xFD\x90\x48\x81\xD2\x8A\xCE\x77\x35\x48\xF7\xD9\x4D\x29\xF4\x49\x81\xC9\xF6\x8A\xC6\x53\x4D\x87\xED\x48\x0F\xAD\xD2\x49\xF7\xD4\x48\xF7\xE1\x4D\x19\xC5\x4D\x89\xC5\x48\xF7\xD6\x41\xB8\x4F\x8D\x6B\x59\x4D\x87\xD0\x68\x6A\x1E\x09\x3C\x59"
|
||||
#define X86_CODE16 "\x00\x00" // add byte ptr [bx + si], al
|
||||
#define X86_CODE64_SYSCALL "\x0f\x05" // SYSCALL
|
||||
|
||||
// memory address where emulation starts
|
||||
#define ADDRESS 0x1000000
|
||||
@ -152,6 +153,19 @@ static void hook_out(uch handle, uint32_t port, int size, uint32_t value, void *
|
||||
printf("--- register value = 0x%x\n", tmp);
|
||||
}
|
||||
|
||||
// callback for SYSCALL instruction (X86).
|
||||
static void hook_syscall(uch handle, void *user_data)
|
||||
{
|
||||
uint64_t rax;
|
||||
|
||||
uc_reg_read(handle, UC_X86_REG_RAX, &rax);
|
||||
if (rax == 0x100) {
|
||||
rax = 0x200;
|
||||
uc_reg_write(handle, UC_X86_REG_RAX, &rax);
|
||||
} else
|
||||
printf("ERROR: was not expecting rax=0x%"PRIx64 " in syscall\n", rax);
|
||||
}
|
||||
|
||||
static void test_i386(void)
|
||||
{
|
||||
uch handle;
|
||||
@ -673,6 +687,57 @@ static void test_x86_64(void)
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
static void test_x86_64_syscall(void)
|
||||
{
|
||||
uch handle;
|
||||
uch trace1;
|
||||
uc_err err;
|
||||
|
||||
int64_t rax = 0x100;
|
||||
|
||||
printf("===================================\n");
|
||||
printf("Emulate x86_64 code with 'syscall' instruction\n");
|
||||
|
||||
// Initialize emulator in X86-64bit mode
|
||||
err = uc_open(UC_ARCH_X86, UC_MODE_64, &handle);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
if (uc_mem_write(handle, ADDRESS, (uint8_t *)X86_CODE64_SYSCALL, sizeof(X86_CODE64_SYSCALL) - 1)) {
|
||||
printf("Failed to write emulation code to memory, quit!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// hook interrupts for syscall
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_INSN, hook_syscall, NULL, UC_X86_INS_SYSCALL);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, UC_X86_REG_RAX, &rax);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(X86_CODE64_SYSCALL) - 1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned %u: %s\n",
|
||||
err, uc_strerror(err));
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(handle, UC_X86_REG_RAX, &rax);
|
||||
|
||||
printf(">>> RAX = 0x%" PRIx64 "\n", rax);
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
static void test_x86_16(void)
|
||||
{
|
||||
uch handle;
|
||||
@ -741,6 +806,7 @@ int main(int argc, char **argv, char **envp)
|
||||
|
||||
if (!strcmp(argv[1], "-64")) {
|
||||
test_x86_64();
|
||||
test_x86_64_syscall();
|
||||
}
|
||||
|
||||
if (!strcmp(argv[1], "-16")) {
|
||||
|
4
uc.c
Executable file → Normal file
4
uc.c
Executable file → Normal file
@ -429,6 +429,7 @@ uc_err uc_emu_start(uch handle, uint64_t begin, uint64_t until, uint64_t timeout
|
||||
uc->stop_request = false;
|
||||
uc->invalid_error = UC_ERR_OK;
|
||||
uc->block_full = false;
|
||||
uc->emulation_done = false;
|
||||
|
||||
switch(uc->arch) {
|
||||
default:
|
||||
@ -507,6 +508,9 @@ uc_err uc_emu_stop(uch handle)
|
||||
// invalid handle
|
||||
return UC_ERR_UCH;
|
||||
|
||||
if (uc->emulation_done)
|
||||
return UC_ERR_OK;
|
||||
|
||||
uc->stop_request = true;
|
||||
// exit the current TB
|
||||
cpu_exit(uc->current_cpu);
|
||||
|
Loading…
Reference in New Issue
Block a user