import
This commit is contained in:
170
samples/Makefile
Normal file
170
samples/Makefile
Normal file
@@ -0,0 +1,170 @@
|
||||
# Unicorn Engine
|
||||
# By Nguyen Anh Quynh <aquynh@gmail.com>, 2015
|
||||
|
||||
include ../config.mk
|
||||
|
||||
LIBNAME = unicorn
|
||||
|
||||
# Find GLIB
|
||||
ifndef GLIB
|
||||
GLIB = `pkg-config --libs glib-2.0`
|
||||
endif
|
||||
|
||||
UNICORN_DEP_LIBS_STATIC += -lpthread -lm $(GLIB)
|
||||
|
||||
# Verbose output?
|
||||
V ?= 0
|
||||
|
||||
INCDIR = ../include
|
||||
ifndef BUILDDIR
|
||||
SAMPLEDIR = .
|
||||
OBJDIR = .
|
||||
LIBDIR = ..
|
||||
else
|
||||
SAMPLEDIR = $(BUILDDIR)/samples
|
||||
OBJDIR = $(BUILDDIR)/obj/samples
|
||||
LIBDIR = $(BUILDDIR)
|
||||
endif
|
||||
|
||||
CFLAGS += -Wall -I$(INCDIR)
|
||||
LDFLAGS += -L$(LIBDIR) -l$(LIBNAME)
|
||||
LDFLAGS_STATIC += $(UNICORN_DEP_LIBS_STATIC)
|
||||
|
||||
ifeq ($(CROSS),)
|
||||
CC ?= cc
|
||||
LDFLAGS += -lm $(GLIB)
|
||||
else
|
||||
CC = $(CROSS)gcc
|
||||
endif
|
||||
|
||||
|
||||
#CFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch))
|
||||
#LDFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch))
|
||||
|
||||
BIN_EXT =
|
||||
AR_EXT = a
|
||||
|
||||
# Cygwin?
|
||||
IS_CYGWIN := $(shell $(CC) -dumpmachine | grep -i cygwin | wc -l)
|
||||
ifeq ($(IS_CYGWIN),1)
|
||||
CFLAGS := $(CFLAGS:-fPIC=)
|
||||
BIN_EXT = .exe
|
||||
AR_EXT = lib
|
||||
else
|
||||
# mingw?
|
||||
IS_MINGW := $(shell $(CC) --version | grep -i mingw | wc -l)
|
||||
ifeq ($(IS_MINGW),1)
|
||||
CFLAGS := $(CFLAGS:-fPIC=)
|
||||
BIN_EXT = .exe
|
||||
AR_EXT = lib
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(UNICORN_STATIC),yes)
|
||||
ifeq ($(IS_MINGW),1)
|
||||
ARCHIVE = $(LIBDIR)/$(LIBNAME).$(AR_EXT)
|
||||
else ifeq ($(IS_CYGWIN),1)
|
||||
ARCHIVE = $(LIBDIR)/$(LIBNAME).$(AR_EXT)
|
||||
else
|
||||
ARCHIVE = $(LIBDIR)/lib$(LIBNAME).$(AR_EXT)
|
||||
#ARCHIVE_X86 = $(LIBDIR)/lib$(LIBNAME)_x86.$(AR_EXT)
|
||||
#ARCHIVE_ARM = $(LIBDIR)/lib$(LIBNAME)_arm.$(AR_EXT)
|
||||
#ARCHIVE_ARM64 = $(LIBDIR)/lib$(LIBNAME)_arm64.$(AR_EXT)
|
||||
endif
|
||||
endif
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
UNICORN_ARCHS := $(shell if [ -a ../config.log ]; then cat ../config.log;\
|
||||
else printf "$(UNICORN_ARCHS)"; fi)
|
||||
|
||||
SOURCES =
|
||||
ifneq (,$(findstring arm,$(UNICORN_ARCHS)))
|
||||
SOURCES += sample_arm.c
|
||||
endif
|
||||
ifneq (,$(findstring aarch64,$(UNICORN_ARCHS)))
|
||||
SOURCES += sample_arm64.c
|
||||
endif
|
||||
ifneq (,$(findstring mips,$(UNICORN_ARCHS)))
|
||||
SOURCES += sample_mips.c
|
||||
endif
|
||||
ifneq (,$(findstring ppc,$(UNICORN_ARCHS)))
|
||||
#SOURCES += sample_ppc.c
|
||||
endif
|
||||
ifneq (,$(findstring sparc,$(UNICORN_ARCHS)))
|
||||
SOURCES += sample_sparc.c
|
||||
endif
|
||||
ifneq (,$(findstring x86,$(UNICORN_ARCHS)))
|
||||
SOURCES += sample_x86.c
|
||||
SOURCES += shellcode.c
|
||||
endif
|
||||
ifneq (,$(findstring m68k,$(UNICORN_ARCHS)))
|
||||
SOURCES += sample_m68k.c
|
||||
endif
|
||||
|
||||
OBJS = $(addprefix $(OBJDIR)/,$(SOURCES:.c=.o))
|
||||
OBJS_ELF = $(addprefix $(OBJDIR)/,$(SOURCES:.c=))
|
||||
BINARY = $(addprefix $(SAMPLEDIR)/,$(SOURCES:.c=$(BIN_EXT)))
|
||||
|
||||
all: $(BINARY)
|
||||
|
||||
clean:
|
||||
rm -rf *.o $(OBJS_ELF) $(BINARY) $(SAMPLEDIR)/*.exe $(SAMPLEDIR)/*.static $(OBJDIR)/lib$(LIBNAME)* $(OBJDIR)/$(LIBNAME)*
|
||||
rm -rf libunicorn*.so libunicorn*.lib libunicorn*.dylib unicorn*.dll unicorn*.lib
|
||||
rm -rf sample_x86 sample_arm sample_arm64 sample_mips sample_sparc sample_ppc sample_m68k shellcode
|
||||
|
||||
$(BINARY): $(OBJS)
|
||||
|
||||
$(SAMPLEDIR)/%$(BIN_EXT): $(OBJDIR)/%.o
|
||||
@mkdir -p $(@D)
|
||||
ifeq ($(V),0)
|
||||
ifeq ($(UNICORN_SHARED),yes)
|
||||
$(call log,LINK,$(notdir $@))
|
||||
@$(link-dynamic)
|
||||
endif
|
||||
ifeq ($(UNICORN_STATIC),yes)
|
||||
ifneq ($(IS_MINGW),1)
|
||||
$(call log,LINK,$(notdir $(call staticname,$@)))
|
||||
@$(link-static)
|
||||
endif
|
||||
endif
|
||||
else
|
||||
ifeq ($(UNICORN_SHARED),yes)
|
||||
$(link-dynamic)
|
||||
endif
|
||||
ifeq ($(UNICORN_STATIC),yes)
|
||||
ifneq ($(IS_MINGW),1)
|
||||
$(link-static)
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
@mkdir -p $(@D)
|
||||
ifeq ($(V),0)
|
||||
$(call log,CC,$(@:$(OBJDIR)/%=%))
|
||||
@$(compile)
|
||||
else
|
||||
$(compile)
|
||||
endif
|
||||
|
||||
|
||||
define link-dynamic
|
||||
$(CC) $< $(LDFLAGS) -o $@
|
||||
endef
|
||||
|
||||
|
||||
define link-static
|
||||
$(CC) $< $(ARCHIVE) $(LDFLAGS_STATIC) -o $(call staticname,$@)
|
||||
endef
|
||||
|
||||
|
||||
staticname = $(subst $(BIN_EXT),,$(1)).static$(BIN_EXT)
|
||||
|
||||
define log
|
||||
@printf " %-7s %s\n" "$(1)" "$(2)"
|
||||
endef
|
||||
|
||||
define compile
|
||||
${CC} ${CFLAGS} -c $< -o $@
|
||||
endef
|
||||
38
samples/sample_all.sh
Executable file
38
samples/sample_all.sh
Executable file
@@ -0,0 +1,38 @@
|
||||
#!/bin/sh
|
||||
|
||||
[ -z "${UNAME}" ] && UNAME=$(uname)
|
||||
|
||||
DIR=`dirname $0`
|
||||
|
||||
if [ "$UNAME" = Darwin ]; then
|
||||
export DYLD_LIBRARY_PATH=.
|
||||
else
|
||||
export LD_LIBRARY_PATH=.
|
||||
fi
|
||||
|
||||
if test -e $DIR/sample_x86; then
|
||||
echo "=========================="
|
||||
$DIR/sample_x86 -32
|
||||
echo "=========================="
|
||||
$DIR/sample_x86 -64
|
||||
fi
|
||||
if test -e $DIR/sample_arm; then
|
||||
echo "=========================="
|
||||
$DIR/sample_arm
|
||||
fi
|
||||
if test -e $DIR/sample_arm64; then
|
||||
echo "=========================="
|
||||
$DIR/sample_arm64
|
||||
fi
|
||||
if test -e $DIR/sample_mips; then
|
||||
echo "=========================="
|
||||
$DIR/sample_mips
|
||||
fi
|
||||
if test -e $DIR/sample_sparc; then
|
||||
echo "=========================="
|
||||
$DIR/sample_sparc
|
||||
fi
|
||||
if test -e $DIR/sample_m68k; then
|
||||
echo "=========================="
|
||||
$DIR/sample_m68k
|
||||
fi
|
||||
140
samples/sample_arm.c
Normal file
140
samples/sample_arm.c
Normal file
@@ -0,0 +1,140 @@
|
||||
/* Unicorn Emulator Engine */
|
||||
/* By Nguyen Anh Quynh, 2015 */
|
||||
|
||||
/* Sample code to demonstrate how to emulate ARM code */
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <unicorn/unicorn.h>
|
||||
|
||||
|
||||
// code to be emulated
|
||||
#define ARM_CODE "\x37\x00\xa0\xe3\x03\x10\x42\xe0" // mov r0, #0x37; sub r1, r2, r3
|
||||
#define THUMB_CODE "\x83\xb0" // sub sp, #0xc
|
||||
|
||||
// memory address where emulation starts
|
||||
#define ADDRESS 0x10000
|
||||
|
||||
static void hook_block(uch handle, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing basic block at 0x%"PRIx64 ", block size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void hook_code(uch handle, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void test_arm(void)
|
||||
{
|
||||
uch handle;
|
||||
uc_err err;
|
||||
uch trace1, trace2;
|
||||
|
||||
int r0 = 0x1234; // R0 register
|
||||
int r2 = 0x6789; // R1 register
|
||||
int r3 = 0x3333; // R2 register
|
||||
int r1; // R1 register
|
||||
|
||||
printf("Emulate ARM code\n");
|
||||
|
||||
// Initialize emulator in ARM mode
|
||||
err = uc_open(UC_ARCH_ARM, UC_MODE_ARM, &handle);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(handle, ADDRESS, (uint8_t *)ARM_CODE, sizeof(ARM_CODE) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, ARM_REG_R0, &r0);
|
||||
uc_reg_write(handle, ARM_REG_R2, &r2);
|
||||
uc_reg_write(handle, ARM_REG_R3, &r3);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// tracing one instruction at ADDRESS with customized callback
|
||||
uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)ADDRESS, (uint64_t)ADDRESS);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(ARM_CODE) -1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(handle, ARM_REG_R0, &r0);
|
||||
uc_reg_read(handle, ARM_REG_R1, &r1);
|
||||
printf(">>> R0 = 0x%x\n", r0);
|
||||
printf(">>> R1 = 0x%x\n", r1);
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
static void test_thumb(void)
|
||||
{
|
||||
uch handle;
|
||||
uc_err err;
|
||||
uch trace1, trace2;
|
||||
|
||||
int sp = 0x1234; // R0 register
|
||||
|
||||
printf("Emulate THUMB code\n");
|
||||
|
||||
// Initialize emulator in ARM mode
|
||||
err = uc_open(UC_ARCH_ARM, UC_MODE_THUMB, &handle);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(handle, ADDRESS, (uint8_t *)THUMB_CODE, sizeof(THUMB_CODE) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, ARM_REG_SP, &sp);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// tracing one instruction at ADDRESS with customized callback
|
||||
uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)ADDRESS, (uint64_t)ADDRESS);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(THUMB_CODE) -1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(handle, ARM_REG_SP, &sp);
|
||||
printf(">>> SP = 0x%x\n", sp);
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
test_arm();
|
||||
printf("==========================\n");
|
||||
test_thumb();
|
||||
|
||||
return 0;
|
||||
}
|
||||
85
samples/sample_arm64.c
Normal file
85
samples/sample_arm64.c
Normal file
@@ -0,0 +1,85 @@
|
||||
/* Unicorn Emulator Engine */
|
||||
/* By Nguyen Anh Quynh, 2015 */
|
||||
|
||||
/* Sample code to demonstrate how to emulate ARM64 code */
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <unicorn/unicorn.h>
|
||||
|
||||
|
||||
// code to be emulated
|
||||
#define ARM_CODE "\xab\x01\x0f\x8b" // add x11, x13, x15
|
||||
|
||||
// memory address where emulation starts
|
||||
#define ADDRESS 0x10000
|
||||
|
||||
static void hook_block(uch handle, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing basic block at 0x%"PRIx64 ", block size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void hook_code(uch handle, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void test_arm64(void)
|
||||
{
|
||||
uch handle;
|
||||
uc_err err;
|
||||
uch trace1, trace2;
|
||||
|
||||
int64_t x11 = 0x1234; // X11 register
|
||||
int64_t x13 = 0x6789; // X13 register
|
||||
int64_t x15 = 0x3333; // X15 register
|
||||
|
||||
printf("Emulate ARM64 code\n");
|
||||
|
||||
// Initialize emulator in ARM mode
|
||||
err = uc_open(UC_ARCH_ARM64, UC_MODE_ARM, &handle);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(handle, ADDRESS, (uint8_t *)ARM_CODE, sizeof(ARM_CODE) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, ARM64_REG_X11, &x11);
|
||||
uc_reg_write(handle, ARM64_REG_X13, &x13);
|
||||
uc_reg_write(handle, ARM64_REG_X15, &x15);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// tracing one instruction at ADDRESS with customized callback
|
||||
uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)ADDRESS, (uint64_t)ADDRESS);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(ARM_CODE) -1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(handle, ARM64_REG_X11, &x11);
|
||||
printf(">>> X11 = 0x%" PRIx64 "\n", x11);
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
test_arm64();
|
||||
|
||||
return 0;
|
||||
}
|
||||
145
samples/sample_m68k.c
Normal file
145
samples/sample_m68k.c
Normal file
@@ -0,0 +1,145 @@
|
||||
/* Unicorn Emulator Engine */
|
||||
/* By Loi Anh Tuan, 2015 */
|
||||
|
||||
/* Sample code to demonstrate how to emulate m68k code */
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <unicorn/unicorn.h>
|
||||
|
||||
// code to be emulated
|
||||
#define M68K_CODE "\x76\xed" // movq #-19, %d3
|
||||
|
||||
// memory address where emulation starts
|
||||
#define ADDRESS 0x10000
|
||||
|
||||
static void hook_block(uch handle, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing basic block at 0x%"PRIx64 ", block size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void hook_code(uch handle, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void test_m68k(void)
|
||||
{
|
||||
uch handle;
|
||||
uch trace1, trace2;
|
||||
uc_err err;
|
||||
|
||||
int d0 = 0x0000; // d0 data register
|
||||
int d1 = 0x0000; // d1 data register
|
||||
int d2 = 0x0000; // d2 data register
|
||||
int d3 = 0x0000; // d3 data register
|
||||
int d4 = 0x0000; // d4 data register
|
||||
int d5 = 0x0000; // d5 data register
|
||||
int d6 = 0x0000; // d6 data register
|
||||
int d7 = 0x0000; // d7 data register
|
||||
|
||||
int a0 = 0x0000; // a0 address register
|
||||
int a1 = 0x0000; // a1 address register
|
||||
int a2 = 0x0000; // a2 address register
|
||||
int a3 = 0x0000; // a3 address register
|
||||
int a4 = 0x0000; // a4 address register
|
||||
int a5 = 0x0000; // a5 address register
|
||||
int a6 = 0x0000; // a6 address register
|
||||
int a7 = 0x0000; // a6 address register
|
||||
|
||||
int pc = 0x0000; // program counter
|
||||
int sr = 0x0000; // status register
|
||||
|
||||
printf("Emulate M68K code\n");
|
||||
|
||||
// Initialize emulator in M68K mode
|
||||
err = uc_open(UC_ARCH_M68K, UC_MODE_BIG_ENDIAN, &handle);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(handle, ADDRESS, (uint8_t *)M68K_CODE, sizeof(M68K_CODE) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, M68K_REG_D0, &d0);
|
||||
uc_reg_write(handle, M68K_REG_D1, &d1);
|
||||
uc_reg_write(handle, M68K_REG_D2, &d2);
|
||||
uc_reg_write(handle, M68K_REG_D3, &d3);
|
||||
uc_reg_write(handle, M68K_REG_D4, &d4);
|
||||
uc_reg_write(handle, M68K_REG_D5, &d5);
|
||||
uc_reg_write(handle, M68K_REG_D6, &d6);
|
||||
uc_reg_write(handle, M68K_REG_D7, &d7);
|
||||
|
||||
uc_reg_write(handle, M68K_REG_A0, &a0);
|
||||
uc_reg_write(handle, M68K_REG_A1, &a1);
|
||||
uc_reg_write(handle, M68K_REG_A2, &a2);
|
||||
uc_reg_write(handle, M68K_REG_A3, &a3);
|
||||
uc_reg_write(handle, M68K_REG_A4, &a4);
|
||||
uc_reg_write(handle, M68K_REG_A5, &a5);
|
||||
uc_reg_write(handle, M68K_REG_A6, &a6);
|
||||
uc_reg_write(handle, M68K_REG_A7, &a7);
|
||||
|
||||
uc_reg_write(handle, M68K_REG_PC, &pc);
|
||||
uc_reg_write(handle, M68K_REG_SR, &sr);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// tracing all instruction
|
||||
uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(M68K_CODE)-1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(handle, M68K_REG_D0, &d0);
|
||||
uc_reg_read(handle, M68K_REG_D1, &d1);
|
||||
uc_reg_read(handle, M68K_REG_D2, &d2);
|
||||
uc_reg_read(handle, M68K_REG_D3, &d3);
|
||||
uc_reg_read(handle, M68K_REG_D4, &d4);
|
||||
uc_reg_read(handle, M68K_REG_D5, &d5);
|
||||
uc_reg_read(handle, M68K_REG_D6, &d6);
|
||||
uc_reg_read(handle, M68K_REG_D7, &d7);
|
||||
|
||||
uc_reg_read(handle, M68K_REG_A0, &a0);
|
||||
uc_reg_read(handle, M68K_REG_A1, &a1);
|
||||
uc_reg_read(handle, M68K_REG_A2, &a2);
|
||||
uc_reg_read(handle, M68K_REG_A3, &a3);
|
||||
uc_reg_read(handle, M68K_REG_A4, &a4);
|
||||
uc_reg_read(handle, M68K_REG_A5, &a5);
|
||||
uc_reg_read(handle, M68K_REG_A6, &a6);
|
||||
uc_reg_read(handle, M68K_REG_A7, &a7);
|
||||
|
||||
uc_reg_read(handle, M68K_REG_PC, &pc);
|
||||
uc_reg_read(handle, M68K_REG_SR, &sr);
|
||||
|
||||
printf(">>> A0 = 0x%x\t\t>>> D0 = 0x%x\n", a0, d0);
|
||||
printf(">>> A1 = 0x%x\t\t>>> D1 = 0x%x\n", a1, d1);
|
||||
printf(">>> A2 = 0x%x\t\t>>> D2 = 0x%x\n", a2, d2);
|
||||
printf(">>> A3 = 0x%x\t\t>>> D3 = 0x%x\n", a3, d3);
|
||||
printf(">>> A4 = 0x%x\t\t>>> D4 = 0x%x\n", a4, d4);
|
||||
printf(">>> A5 = 0x%x\t\t>>> D5 = 0x%x\n", a5, d5);
|
||||
printf(">>> A6 = 0x%x\t\t>>> D6 = 0x%x\n", a6, d6);
|
||||
printf(">>> A7 = 0x%x\t\t>>> D7 = 0x%x\n", a7, d7);
|
||||
printf(">>> PC = 0x%x\n", pc);
|
||||
printf(">>> SR = 0x%x\n", sr);
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
test_m68k();
|
||||
return 0;
|
||||
}
|
||||
133
samples/sample_mips.c
Normal file
133
samples/sample_mips.c
Normal file
@@ -0,0 +1,133 @@
|
||||
/* Unicorn Emulator Engine */
|
||||
/* By Nguyen Anh Quynh, 2015 */
|
||||
|
||||
/* Sample code to demonstrate how to emulate Mips code (big endian) */
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <unicorn/unicorn.h>
|
||||
|
||||
|
||||
// code to be emulated
|
||||
#define MIPS_CODE_EB "\x34\x21\x34\x56" // ori $at, $at, 0x3456;
|
||||
#define MIPS_CODE_EL "\x56\x34\x21\x34" // ori $at, $at, 0x3456;
|
||||
|
||||
// memory address where emulation starts
|
||||
#define ADDRESS 0x10000
|
||||
|
||||
static void hook_block(uch handle, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing basic block at 0x%"PRIx64 ", block size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void hook_code(uch handle, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void test_mips_eb(void)
|
||||
{
|
||||
uch handle;
|
||||
uc_err err;
|
||||
uch trace1, trace2;
|
||||
|
||||
int r1 = 0x6789; // R1 register
|
||||
|
||||
printf("Emulate MIPS code (big-endian)\n");
|
||||
|
||||
// Initialize emulator in MIPS mode
|
||||
err = uc_open(UC_ARCH_MIPS, UC_MODE_MIPS32 + UC_MODE_BIG_ENDIAN, &handle);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(handle, ADDRESS, (uint8_t *)MIPS_CODE_EB, sizeof(MIPS_CODE_EB) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, MIPS_REG_1, &r1);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// tracing one instruction at ADDRESS with customized callback
|
||||
uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)ADDRESS, (uint64_t)ADDRESS);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(MIPS_CODE_EB) - 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, MIPS_REG_1, &r1);
|
||||
printf(">>> R1 = 0x%x\n", r1);
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
static void test_mips_el(void)
|
||||
{
|
||||
uch handle;
|
||||
uc_err err;
|
||||
uch trace1, trace2;
|
||||
|
||||
int r1 = 0x6789; // R1 register
|
||||
|
||||
printf("===========================\n");
|
||||
printf("Emulate MIPS code (little-endian)\n");
|
||||
|
||||
// Initialize emulator in MIPS mode
|
||||
err = uc_open(UC_ARCH_MIPS, UC_MODE_MIPS32, &handle);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(handle, ADDRESS, (uint8_t *)MIPS_CODE_EL, sizeof(MIPS_CODE_EL) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, MIPS_REG_1, &r1);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// tracing one instruction at ADDRESS with customized callback
|
||||
uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)ADDRESS, (uint64_t)ADDRESS);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(MIPS_CODE_EL) - 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, MIPS_REG_1, &r1);
|
||||
printf(">>> R1 = 0x%x\n", r1);
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
test_mips_eb();
|
||||
test_mips_el();
|
||||
|
||||
return 0;
|
||||
}
|
||||
87
samples/sample_sparc.c
Normal file
87
samples/sample_sparc.c
Normal file
@@ -0,0 +1,87 @@
|
||||
/* Unicorn Emulator Engine */
|
||||
/* By Nguyen Anh Quynh, 2015 */
|
||||
|
||||
/* Sample code to demonstrate how to emulate Sparc code */
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <unicorn/unicorn.h>
|
||||
|
||||
|
||||
// code to be emulated
|
||||
#define SPARC_CODE "\x86\x00\x40\x02" // add %g1, %g2, %g3;
|
||||
//#define SPARC_CODE "\xbb\x70\x00\x00" // illegal code
|
||||
|
||||
// memory address where emulation starts
|
||||
#define ADDRESS 0x10000
|
||||
|
||||
static void hook_block(uch handle, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing basic block at 0x%"PRIx64 ", block size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void hook_code(uch handle, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void test_sparc(void)
|
||||
{
|
||||
uch handle;
|
||||
uc_err err;
|
||||
uch trace1, trace2;
|
||||
|
||||
int g1 = 0x1230; // G1 register
|
||||
int g2 = 0x6789; // G2 register
|
||||
int g3 = 0x5555; // G3 register
|
||||
|
||||
printf("Emulate SPARC code\n");
|
||||
|
||||
// Initialize emulator in Sparc mode
|
||||
err = uc_open(UC_ARCH_SPARC, UC_MODE_BIG_ENDIAN, &handle);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(handle, ADDRESS, (uint8_t *)SPARC_CODE, sizeof(SPARC_CODE) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, SPARC_REG_G1, &g1);
|
||||
uc_reg_write(handle, SPARC_REG_G2, &g2);
|
||||
uc_reg_write(handle, SPARC_REG_G3, &g3);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// tracing one instruction at ADDRESS with customized callback
|
||||
uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)ADDRESS, (uint64_t)ADDRESS);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(SPARC_CODE) -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, SPARC_REG_G3, &g3);
|
||||
printf(">>> G3 = 0x%x\n", g3);
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
test_sparc();
|
||||
|
||||
return 0;
|
||||
}
|
||||
704
samples/sample_x86.c
Normal file
704
samples/sample_x86.c
Normal file
@@ -0,0 +1,704 @@
|
||||
/* Unicorn Emulator Engine */
|
||||
/* By Nguyen Anh Quynh & Dang Hoang Vu, 2015 */
|
||||
|
||||
/* Sample code to demonstrate how to emulate X86 code */
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <unicorn/unicorn.h>
|
||||
|
||||
|
||||
// code to be emulated
|
||||
#define X86_CODE32 "\x41\x4a" // INC ecx; DEC dex
|
||||
#define X86_CODE32_JUMP "\xeb\x02\x90\x90\x90\x90\x90\x90" // jmp 4; nop; nop; nop; nop; nop; nop
|
||||
// #define X86_CODE32_SELF "\xeb\x1c\x5a\x89\xd6\x8b\x02\x66\x3d\xca\x7d\x75\x06\x66\x05\x03\x03\x89\x02\xfe\xc2\x3d\x41\x41\x41\x41\x75\xe9\xff\xe6\xe8\xdf\xff\xff\xff\x31\xd2\x6a\x0b\x58\x99\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x52\x53\x89\xe1\xca\x7d\x41\x41\x41\x41"
|
||||
//#define X86_CODE32 "\x51\x51\x51\x51" // PUSH ecx;
|
||||
#define X86_CODE32_LOOP "\x41\x4a\xeb\xfe" // INC ecx; DEC dex; JMP self-loop
|
||||
#define X86_CODE32_MEM_WRITE "\x89\x0D\xAA\xAA\xAA\xAA\x41\x4a" // mov [0xaaaaaaaa], ecx; INC ecx; dec edx
|
||||
#define X86_CODE32_MEM_READ "\x8B\x0D\xAA\xAA\xAA\xAA\x41\x4a" // mov ecx,[0xaaaaaaaa]; INC ecx; DEC dex
|
||||
|
||||
#define X86_CODE32_JMP_INVALID "\xe9\xe9\xee\xee\xee\x41\x4a" // JMP outside; INC ecx; DEC dex
|
||||
#define X86_CODE32_INOUT "\x41\xE4\x3F\x4a\xE6\x46\x43" // INC ecx; IN AL, 0x3f; DEC edx; OUT 0x46, AL; INC ebx
|
||||
|
||||
//#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" // <== still crash
|
||||
//#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" //\xc3"
|
||||
|
||||
// memory address where emulation starts
|
||||
#define ADDRESS 0x1000000
|
||||
|
||||
// callback for tracing basic blocks
|
||||
static void hook_block(uch handle, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing basic block at 0x%"PRIx64 ", block size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
// callback for tracing instruction
|
||||
static void hook_code(uch handle, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
int eflags;
|
||||
printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size);
|
||||
|
||||
uc_reg_read(handle, X86_REG_EFLAGS, &eflags);
|
||||
printf(">>> --- EFLAGS is 0x%x\n", eflags);
|
||||
|
||||
// Uncomment below code to stop the emulation using uc_emu_stop()
|
||||
// if (address == 0x1000009)
|
||||
// uc_emu_stop(handle);
|
||||
}
|
||||
|
||||
// callback for tracing instruction
|
||||
static void hook_code64(uch handle, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
uint64_t rip;
|
||||
|
||||
uc_reg_read(handle, X86_REG_RIP, &rip);
|
||||
printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size);
|
||||
printf(">>> RIP is 0x%"PRIx64 "\n", rip);
|
||||
|
||||
// Uncomment below code to stop the emulation using uc_emu_stop()
|
||||
// if (address == 0x1000009)
|
||||
// uc_emu_stop(handle);
|
||||
}
|
||||
|
||||
// callback for tracing memory access (READ or WRITE)
|
||||
static bool hook_mem_invalid(uch handle, uc_mem_type type,
|
||||
uint64_t address, int size, int64_t value, void *user_data)
|
||||
{
|
||||
switch(type) {
|
||||
default:
|
||||
// return false to indicate we want to stop emulation
|
||||
return false;
|
||||
case UC_MEM_WRITE:
|
||||
printf(">>> Missing memory is being WRITE at 0x%"PRIx64 ", data size = %u, data value = 0x%"PRIx64 "\n",
|
||||
address, size, value);
|
||||
// map this memory in with 2MB in size
|
||||
uc_mem_map(handle, 0xaaaa0000, 2 * 1024*1024);
|
||||
// return true to indicate we want to continue
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static void hook_mem64(uch handle, uc_mem_type type,
|
||||
uint64_t address, int size, int64_t value, void *user_data)
|
||||
{
|
||||
switch(type) {
|
||||
default: break;
|
||||
case UC_MEM_READ:
|
||||
printf(">>> Memory is being READ at 0x%"PRIx64 ", data size = %u\n",
|
||||
address, size);
|
||||
break;
|
||||
case UC_MEM_WRITE:
|
||||
printf(">>> Memory is being WRITE at 0x%"PRIx64 ", data size = %u, data value = 0x%"PRIx64 "\n",
|
||||
address, size, value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// callback for IN instruction (X86).
|
||||
// this returns the data read from the port
|
||||
static uint32_t hook_in(uch handle, uint32_t port, int size, void *user_data)
|
||||
{
|
||||
uint32_t eip;
|
||||
|
||||
uc_reg_read(handle, X86_REG_EIP, &eip);
|
||||
|
||||
printf("--- reading from port 0x%x, size: %u, address: 0x%x\n", port, size, eip);
|
||||
|
||||
switch(size) {
|
||||
default:
|
||||
return 0; // should never reach this
|
||||
case 1:
|
||||
// read 1 byte to AL
|
||||
return 0xf1;
|
||||
case 2:
|
||||
// read 2 byte to AX
|
||||
return 0xf2;
|
||||
break;
|
||||
case 4:
|
||||
// read 4 byte to EAX
|
||||
return 0xf4;
|
||||
}
|
||||
}
|
||||
|
||||
// callback for OUT instruction (X86).
|
||||
static void hook_out(uch handle, uint32_t port, int size, uint32_t value, void *user_data)
|
||||
{
|
||||
uint32_t tmp;
|
||||
uint32_t eip;
|
||||
|
||||
uc_reg_read(handle, X86_REG_EIP, &eip);
|
||||
|
||||
printf("--- writing to port 0x%x, size: %u, value: 0x%x, address: 0x%x\n", port, size, value, eip);
|
||||
|
||||
// confirm that value is indeed the value of AL/AX/EAX
|
||||
switch(size) {
|
||||
default:
|
||||
return; // should never reach this
|
||||
case 1:
|
||||
uc_reg_read(handle, X86_REG_AL, &tmp);
|
||||
break;
|
||||
case 2:
|
||||
uc_reg_read(handle, X86_REG_AX, &tmp);
|
||||
break;
|
||||
case 4:
|
||||
uc_reg_read(handle, X86_REG_EAX, &tmp);
|
||||
break;
|
||||
}
|
||||
|
||||
printf("--- register value = 0x%x\n", tmp);
|
||||
}
|
||||
|
||||
static void test_i386(void)
|
||||
{
|
||||
uch handle;
|
||||
uc_err err;
|
||||
uint32_t tmp;
|
||||
uch trace1, trace2;
|
||||
|
||||
int r_ecx = 0x1234; // ECX register
|
||||
int r_edx = 0x7890; // EDX register
|
||||
|
||||
printf("Emulate i386 code\n");
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
err = uc_open(UC_ARCH_X86, UC_MODE_32, &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_CODE32, sizeof(X86_CODE32) - 1)) {
|
||||
printf("Failed to write emulation code to memory, quit!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, X86_REG_ECX, &r_ecx);
|
||||
uc_reg_write(handle, X86_REG_EDX, &r_edx);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// tracing all instruction by having @begin > @end
|
||||
uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// emulate machine code in infinite time
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(X86_CODE32) - 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, X86_REG_ECX, &r_ecx);
|
||||
uc_reg_read(handle, X86_REG_EDX, &r_edx);
|
||||
printf(">>> ECX = 0x%x\n", r_ecx);
|
||||
printf(">>> EDX = 0x%x\n", r_edx);
|
||||
|
||||
// read from memory
|
||||
if (!uc_mem_read(handle, ADDRESS, (uint8_t *)&tmp, 4))
|
||||
printf(">>> Read 4 bytes from [0x%x] = 0x%x\n", ADDRESS, tmp);
|
||||
else
|
||||
printf(">>> Failed to read 4 bytes from [0x%x]\n", ADDRESS);
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
static void test_i386_jump(void)
|
||||
{
|
||||
uch handle;
|
||||
uc_err err;
|
||||
uch trace1, trace2;
|
||||
|
||||
printf("===================================\n");
|
||||
printf("Emulate i386 code with jump\n");
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
err = uc_open(UC_ARCH_X86, UC_MODE_32, &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_CODE32_JUMP,
|
||||
sizeof(X86_CODE32_JUMP) - 1)) {
|
||||
printf("Failed to write emulation code to memory, quit!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// tracing 1 basic block with customized callback
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)ADDRESS, (uint64_t)ADDRESS);
|
||||
|
||||
// tracing 1 instruction at ADDRESS
|
||||
uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)ADDRESS, (uint64_t)ADDRESS);
|
||||
|
||||
// emulate machine code in infinite time
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(X86_CODE32_JUMP) - 1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned %u: %s\n",
|
||||
err, uc_strerror(err));
|
||||
}
|
||||
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
// emulate code that loop forever
|
||||
static void test_i386_loop(void)
|
||||
{
|
||||
uch handle;
|
||||
uc_err err;
|
||||
|
||||
int r_ecx = 0x1234; // ECX register
|
||||
int r_edx = 0x7890; // EDX register
|
||||
|
||||
printf("===================================\n");
|
||||
printf("Emulate i386 code that loop forever\n");
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
err = uc_open(UC_ARCH_X86, UC_MODE_32, &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_CODE32_LOOP, sizeof(X86_CODE32_LOOP) - 1)) {
|
||||
printf("Failed to write emulation code to memory, quit!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, X86_REG_ECX, &r_ecx);
|
||||
uc_reg_write(handle, X86_REG_EDX, &r_edx);
|
||||
|
||||
// emulate machine code in 2 seconds, so we can quit even
|
||||
// if the code loops
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(X86_CODE32_LOOP) - 1, 2 * UC_SECOND_SCALE, 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, X86_REG_ECX, &r_ecx);
|
||||
uc_reg_read(handle, X86_REG_EDX, &r_edx);
|
||||
printf(">>> ECX = 0x%x\n", r_ecx);
|
||||
printf(">>> EDX = 0x%x\n", r_edx);
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
// emulate code that read invalid memory
|
||||
static void test_i386_invalid_mem_read(void)
|
||||
{
|
||||
uch handle;
|
||||
uc_err err;
|
||||
uch trace1, trace2;
|
||||
|
||||
int r_ecx = 0x1234; // ECX register
|
||||
int r_edx = 0x7890; // EDX register
|
||||
|
||||
printf("===================================\n");
|
||||
printf("Emulate i386 code that read from invalid memory\n");
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
err = uc_open(UC_ARCH_X86, UC_MODE_32, &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_CODE32_MEM_READ, sizeof(X86_CODE32_MEM_READ) - 1)) {
|
||||
printf("Failed to write emulation code to memory, quit!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, X86_REG_ECX, &r_ecx);
|
||||
uc_reg_write(handle, X86_REG_EDX, &r_edx);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// tracing all instruction by having @begin > @end
|
||||
uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// emulate machine code in infinite time
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(X86_CODE32_MEM_READ) - 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, X86_REG_ECX, &r_ecx);
|
||||
uc_reg_read(handle, X86_REG_EDX, &r_edx);
|
||||
printf(">>> ECX = 0x%x\n", r_ecx);
|
||||
printf(">>> EDX = 0x%x\n", r_edx);
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
// emulate code that read invalid memory
|
||||
static void test_i386_invalid_mem_write(void)
|
||||
{
|
||||
uch handle, evh;
|
||||
uc_err err;
|
||||
uch trace1, trace2; //, trace3;
|
||||
uint32_t tmp;
|
||||
|
||||
int r_ecx = 0x1234; // ECX register
|
||||
int r_edx = 0x7890; // EDX register
|
||||
|
||||
printf("===================================\n");
|
||||
printf("Emulate i386 code that write to invalid memory\n");
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
err = uc_open(UC_ARCH_X86, UC_MODE_32, &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_CODE32_MEM_WRITE, sizeof(X86_CODE32_MEM_WRITE) - 1)) {
|
||||
printf("Failed to write emulation code to memory, quit!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, X86_REG_ECX, &r_ecx);
|
||||
uc_reg_write(handle, X86_REG_EDX, &r_edx);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// tracing all instruction by having @begin > @end
|
||||
uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// intercept invalid memory events
|
||||
uc_hook_add(handle, &evh, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL);
|
||||
|
||||
// emulate machine code in infinite time
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(X86_CODE32_MEM_WRITE) - 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, X86_REG_ECX, &r_ecx);
|
||||
uc_reg_read(handle, X86_REG_EDX, &r_edx);
|
||||
printf(">>> ECX = 0x%x\n", r_ecx);
|
||||
printf(">>> EDX = 0x%x\n", r_edx);
|
||||
|
||||
// read from memory
|
||||
if (!uc_mem_read(handle, 0xaaaaaaaa, (uint8_t *)&tmp, 4))
|
||||
printf(">>> Read 4 bytes from [0x%x] = 0x%x\n", 0xaaaaaaaa, tmp);
|
||||
else
|
||||
printf(">>> Failed to read 4 bytes from [0x%x]\n", 0xffffffaa);
|
||||
|
||||
if (!uc_mem_read(handle, 0xffffffaa, (uint8_t *)&tmp, 4))
|
||||
printf(">>> Read 4 bytes from [0x%x] = 0x%x\n", 0xffffffaa, tmp);
|
||||
else
|
||||
printf(">>> Failed to read 4 bytes from [0x%x]\n", 0xffffffaa);
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
// emulate code that jump to invalid memory
|
||||
static void test_i386_jump_invalid(void)
|
||||
{
|
||||
uch handle;
|
||||
uc_err err;
|
||||
uch trace1, trace2;
|
||||
|
||||
int r_ecx = 0x1234; // ECX register
|
||||
int r_edx = 0x7890; // EDX register
|
||||
|
||||
printf("===================================\n");
|
||||
printf("Emulate i386 code that jumps to invalid memory\n");
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
err = uc_open(UC_ARCH_X86, UC_MODE_32, &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_CODE32_JMP_INVALID, sizeof(X86_CODE32_JMP_INVALID) - 1)) {
|
||||
printf("Failed to write emulation code to memory, quit!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, X86_REG_ECX, &r_ecx);
|
||||
uc_reg_write(handle, X86_REG_EDX, &r_edx);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// tracing all instructions by having @begin > @end
|
||||
uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// emulate machine code in infinite time
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(X86_CODE32_JMP_INVALID) - 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, X86_REG_ECX, &r_ecx);
|
||||
uc_reg_read(handle, X86_REG_EDX, &r_edx);
|
||||
printf(">>> ECX = 0x%x\n", r_ecx);
|
||||
printf(">>> EDX = 0x%x\n", r_edx);
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
static void test_i386_inout(void)
|
||||
{
|
||||
uch handle;
|
||||
uc_err err;
|
||||
uch trace1, trace2;
|
||||
uch trace3, trace4;
|
||||
|
||||
int r_eax = 0x1234; // EAX register
|
||||
int r_ecx = 0x6789; // ECX register
|
||||
|
||||
printf("===================================\n");
|
||||
printf("Emulate i386 code with IN/OUT instructions\n");
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
err = uc_open(UC_ARCH_X86, UC_MODE_32, &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_CODE32_INOUT, sizeof(X86_CODE32_INOUT) - 1)) {
|
||||
printf("Failed to write emulation code to memory, quit!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, X86_REG_EAX, &r_eax);
|
||||
uc_reg_write(handle, X86_REG_ECX, &r_ecx);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// tracing all instructions
|
||||
uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// handle IN instruction
|
||||
uc_hook_add(handle, &trace3, UC_HOOK_INSN, hook_in, NULL, X86_INS_IN);
|
||||
// handle OUT instruction
|
||||
uc_hook_add(handle, &trace4, UC_HOOK_INSN, hook_out, NULL, X86_INS_OUT);
|
||||
|
||||
// emulate machine code in infinite time
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(X86_CODE32_INOUT) - 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, X86_REG_EAX, &r_eax);
|
||||
uc_reg_read(handle, X86_REG_ECX, &r_ecx);
|
||||
printf(">>> EAX = 0x%x\n", r_eax);
|
||||
printf(">>> ECX = 0x%x\n", r_ecx);
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
static void test_x86_64(void)
|
||||
{
|
||||
uch handle;
|
||||
uc_err err;
|
||||
uch trace1, trace2, trace3, trace4;
|
||||
|
||||
int64_t rax = 0x71f3029efd49d41d;
|
||||
int64_t rbx = 0xd87b45277f133ddb;
|
||||
int64_t rcx = 0xab40d1ffd8afc461;
|
||||
int64_t rdx = 0x919317b4a733f01;
|
||||
int64_t rsi = 0x4c24e753a17ea358;
|
||||
int64_t rdi = 0xe509a57d2571ce96;
|
||||
int64_t r8 = 0xea5b108cc2b9ab1f;
|
||||
int64_t r9 = 0x19ec097c8eb618c1;
|
||||
int64_t r10 = 0xec45774f00c5f682;
|
||||
int64_t r11 = 0xe17e9dbec8c074aa;
|
||||
int64_t r12 = 0x80f86a8dc0f6d457;
|
||||
int64_t r13 = 0x48288ca5671c5492;
|
||||
int64_t r14 = 0x595f72f6e4017f6e;
|
||||
int64_t r15 = 0x1efd97aea331cccc;
|
||||
|
||||
int64_t rsp = ADDRESS + 0x200000;
|
||||
|
||||
|
||||
printf("Emulate x86_64 code\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, sizeof(X86_CODE64) - 1)) {
|
||||
printf("Failed to write emulation code to memory, quit!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, X86_REG_RSP, &rsp);
|
||||
|
||||
uc_reg_write(handle, X86_REG_RAX, &rax);
|
||||
uc_reg_write(handle, X86_REG_RBX, &rbx);
|
||||
uc_reg_write(handle, X86_REG_RCX, &rcx);
|
||||
uc_reg_write(handle, X86_REG_RDX, &rdx);
|
||||
uc_reg_write(handle, X86_REG_RSI, &rsi);
|
||||
uc_reg_write(handle, X86_REG_RDI, &rdi);
|
||||
uc_reg_write(handle, X86_REG_R8, &r8);
|
||||
uc_reg_write(handle, X86_REG_R9, &r9);
|
||||
uc_reg_write(handle, X86_REG_R10, &r10);
|
||||
uc_reg_write(handle, X86_REG_R11, &r11);
|
||||
uc_reg_write(handle, X86_REG_R12, &r12);
|
||||
uc_reg_write(handle, X86_REG_R13, &r13);
|
||||
uc_reg_write(handle, X86_REG_R14, &r14);
|
||||
uc_reg_write(handle, X86_REG_R15, &r15);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// tracing all instructions in the range [ADDRESS, ADDRESS+20]
|
||||
uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code64, NULL, (uint64_t)ADDRESS, (uint64_t)(ADDRESS+20));
|
||||
|
||||
// tracing all memory WRITE access (with @begin > @end)
|
||||
uc_hook_add(handle, &trace3, UC_HOOK_MEM_WRITE, hook_mem64, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// tracing all memory READ access (with @begin > @end)
|
||||
uc_hook_add(handle, &trace4, UC_HOOK_MEM_READ, hook_mem64, NULL, (uint64_t)1, (uint64_t)0);
|
||||
|
||||
// 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) - 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, X86_REG_RAX, &rax);
|
||||
uc_reg_read(handle, X86_REG_RBX, &rbx);
|
||||
uc_reg_read(handle, X86_REG_RCX, &rcx);
|
||||
uc_reg_read(handle, X86_REG_RDX, &rdx);
|
||||
uc_reg_read(handle, X86_REG_RSI, &rsi);
|
||||
uc_reg_read(handle, X86_REG_RDI, &rdi);
|
||||
uc_reg_read(handle, X86_REG_R8, &r8);
|
||||
uc_reg_read(handle, X86_REG_R9, &r9);
|
||||
uc_reg_read(handle, X86_REG_R10, &r10);
|
||||
uc_reg_read(handle, X86_REG_R11, &r11);
|
||||
uc_reg_read(handle, X86_REG_R12, &r12);
|
||||
uc_reg_read(handle, X86_REG_R13, &r13);
|
||||
uc_reg_read(handle, X86_REG_R14, &r14);
|
||||
uc_reg_read(handle, X86_REG_R15, &r15);
|
||||
|
||||
printf(">>> RAX = 0x%" PRIx64 "\n", rax);
|
||||
printf(">>> RBX = 0x%" PRIx64 "\n", rbx);
|
||||
printf(">>> RCX = 0x%" PRIx64 "\n", rcx);
|
||||
printf(">>> RDX = 0x%" PRIx64 "\n", rdx);
|
||||
printf(">>> RSI = 0x%" PRIx64 "\n", rsi);
|
||||
printf(">>> RDI = 0x%" PRIx64 "\n", rdi);
|
||||
printf(">>> R8 = 0x%" PRIx64 "\n", r8);
|
||||
printf(">>> R9 = 0x%" PRIx64 "\n", r9);
|
||||
printf(">>> R10 = 0x%" PRIx64 "\n", r10);
|
||||
printf(">>> R11 = 0x%" PRIx64 "\n", r11);
|
||||
printf(">>> R12 = 0x%" PRIx64 "\n", r12);
|
||||
printf(">>> R13 = 0x%" PRIx64 "\n", r13);
|
||||
printf(">>> R14 = 0x%" PRIx64 "\n", r14);
|
||||
printf(">>> R15 = 0x%" PRIx64 "\n", r15);
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
if (argc == 2) {
|
||||
if (!strcmp(argv[1], "-32")) {
|
||||
test_i386();
|
||||
test_i386_inout();
|
||||
test_i386_jump();
|
||||
test_i386_loop();
|
||||
test_i386_invalid_mem_read();
|
||||
test_i386_invalid_mem_write();
|
||||
test_i386_jump_invalid();
|
||||
}
|
||||
|
||||
if (!strcmp(argv[1], "-64")) {
|
||||
test_x86_64();
|
||||
}
|
||||
|
||||
// test memleak
|
||||
if (!strcmp(argv[1], "-0")) {
|
||||
while(1) {
|
||||
test_i386();
|
||||
// test_x86_64();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
printf("Syntax: %s <-32|-64>\n", argv[0]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
150
samples/shellcode.c
Normal file
150
samples/shellcode.c
Normal file
@@ -0,0 +1,150 @@
|
||||
/* Unicorn Emulator Engine */
|
||||
/* By Nguyen Anh Quynh & Dang Hoang Vu, 2015 */
|
||||
|
||||
/* Sample code to trace code with Linux code with syscall */
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <unicorn/unicorn.h>
|
||||
|
||||
|
||||
// code to be emulated
|
||||
#define X86_CODE32 "\xeb\x19\x31\xc0\x31\xdb\x31\xd2\x31\xc9\xb0\x04\xb3\x01\x59\xb2\x05\xcd\x80\x31\xc0\xb0\x01\x31\xdb\xcd\x80\xe8\xe2\xff\xff\xff\x68\x65\x6c\x6c\x6f"
|
||||
|
||||
#define X86_CODE32_SELF "\xeb\x1c\x5a\x89\xd6\x8b\x02\x66\x3d\xca\x7d\x75\x06\x66\x05\x03\x03\x89\x02\xfe\xc2\x3d\x41\x41\x41\x41\x75\xe9\xff\xe6\xe8\xdf\xff\xff\xff\x31\xd2\x6a\x0b\x58\x99\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x52\x53\x89\xe1\xca\x7d\x41\x41\x41\x41\x41\x41\x41\x41"
|
||||
|
||||
// memory address where emulation starts
|
||||
#define ADDRESS 0x1000000
|
||||
|
||||
#define MIN(a, b) (a < b? a : b)
|
||||
// callback for tracing instruction
|
||||
static void hook_code(uch handle, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
int r_eip;
|
||||
char tmp[16];
|
||||
|
||||
printf("Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size);
|
||||
|
||||
uc_reg_read(handle, X86_REG_EIP, &r_eip);
|
||||
printf("*** EIP = %x ***: ", r_eip);
|
||||
|
||||
size = MIN(sizeof(tmp), size);
|
||||
if (!uc_mem_read(handle, address, (uint8_t *)tmp, size)) {
|
||||
int i;
|
||||
for (i=0; i<size; i++) {
|
||||
printf("%x ", ((uint8_t*)tmp)[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
#define MIN(a, b) (a < b? a : b)
|
||||
// callback for handling interrupt
|
||||
// ref: http://syscalls.kernelgrok.com/
|
||||
static void hook_intr(uch handle, uint32_t intno, void *user_data)
|
||||
{
|
||||
int32_t r_eax, r_ecx, r_eip;
|
||||
uint32_t r_edx, size;
|
||||
unsigned char buffer[256];
|
||||
|
||||
// only handle Linux syscall
|
||||
if (intno != 0x80)
|
||||
return;
|
||||
|
||||
uc_reg_read(handle, X86_REG_EAX, &r_eax);
|
||||
uc_reg_read(handle, X86_REG_EIP, &r_eip);
|
||||
|
||||
switch(r_eax) {
|
||||
default:
|
||||
printf(">>> 0x%x: interrupt 0x%x, EAX = 0x%x\n", r_eip, intno, r_eax);
|
||||
break;
|
||||
case 1: // sys_exit
|
||||
printf(">>> 0x%x: interrupt 0x%x, SYS_EXIT. quit!\n\n", r_eip, intno);
|
||||
uc_emu_stop(handle);
|
||||
break;
|
||||
case 4: // sys_write
|
||||
// ECX = buffer address
|
||||
uc_reg_read(handle, X86_REG_ECX, &r_ecx);
|
||||
|
||||
// EDX = buffer size
|
||||
uc_reg_read(handle, X86_REG_EDX, &r_edx);
|
||||
|
||||
// read the buffer in
|
||||
size = MIN(sizeof(buffer)-1, r_edx);
|
||||
|
||||
if (!uc_mem_read(handle, r_ecx, buffer, size)) {
|
||||
buffer[size] = '\0';
|
||||
printf(">>> 0x%x: interrupt 0x%x, SYS_WRITE. buffer = 0x%x, size = %u, content = '%s'\n",
|
||||
r_eip, intno, r_ecx, r_edx, buffer);
|
||||
} else {
|
||||
printf(">>> 0x%x: interrupt 0x%x, SYS_WRITE. buffer = 0x%x, size = %u (cannot get content)\n",
|
||||
r_eip, intno, r_ecx, r_edx);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void test_i386(void)
|
||||
{
|
||||
uch handle, evh;
|
||||
uc_err err;
|
||||
uch trace1;
|
||||
|
||||
int r_esp = ADDRESS + 0x200000; // ESP register
|
||||
|
||||
printf("Emulate i386 code\n");
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
err = uc_open(UC_ARCH_X86, UC_MODE_32, &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_CODE32_SELF, sizeof(X86_CODE32_SELF) - 1)) {
|
||||
printf("Failed to write emulation code to memory, quit!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(handle, X86_REG_ESP, &r_esp);
|
||||
|
||||
// tracing all instructions by having @begin > @end
|
||||
uc_hook_add(handle, &trace1, UC_HOOK_CODE, hook_code, NULL, 1, 0);
|
||||
|
||||
// handle interrupt ourself
|
||||
uc_hook_add(handle, &evh, UC_HOOK_INTR, hook_intr, NULL);
|
||||
|
||||
printf("\n>>> Start tracing this Linux code\n");
|
||||
|
||||
// emulate machine code in infinite time
|
||||
// err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(X86_CODE32_SELF), 0, 12); <--- emulate only 12 instructions
|
||||
err = uc_emu_start(handle, ADDRESS, ADDRESS + sizeof(X86_CODE32_SELF) - 1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned %u: %s\n",
|
||||
err, uc_strerror(err));
|
||||
}
|
||||
|
||||
printf("\n>>> Emulation done.\n");
|
||||
|
||||
uc_close(&handle);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
if (argc == 2) {
|
||||
if (!strcmp(argv[1], "-32")) {
|
||||
test_i386();
|
||||
}
|
||||
} else {
|
||||
printf("Syntax: %s <-32|-64>\n", argv[0]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user