2015-09-04 20:27:03 +03:00
|
|
|
/*
|
|
|
|
Sample use of uc_mem_unmap, uc_mem_protect, and memory permissions
|
|
|
|
|
|
|
|
Copyright(c) 2015 Chris Eagle
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
version 2 as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2021-10-29 13:44:49 +03:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
USA.
|
2015-09-04 20:27:03 +03:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define __STDC_FORMAT_MACROS
|
2015-12-08 10:21:32 +03:00
|
|
|
|
|
|
|
#include <unicorn/unicorn.h>
|
2015-09-04 20:27:03 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
static int insts_executed;
|
|
|
|
|
|
|
|
// callback for tracing instructions, detect HLT and terminate emulation
|
2021-10-29 13:44:49 +03:00
|
|
|
static void hook_code(uc_engine *uc, uint64_t addr, uint32_t size,
|
|
|
|
void *user_data)
|
2015-09-04 20:27:03 +03:00
|
|
|
{
|
|
|
|
uint8_t opcode;
|
|
|
|
unsigned char buf[256];
|
2015-09-05 04:42:47 +03:00
|
|
|
|
2015-09-04 20:27:03 +03:00
|
|
|
insts_executed++;
|
2015-09-05 04:42:47 +03:00
|
|
|
|
2015-09-04 20:27:03 +03:00
|
|
|
if (uc_mem_read(uc, addr, buf, size) != UC_ERR_OK) {
|
2021-10-29 13:44:49 +03:00
|
|
|
printf("not ok - uc_mem_read fail during hook_code callback, addr: "
|
|
|
|
"0x%" PRIx64 "\n",
|
|
|
|
addr);
|
2015-09-04 20:27:03 +03:00
|
|
|
if (uc_emu_stop(uc) != UC_ERR_OK) {
|
2021-10-29 13:44:49 +03:00
|
|
|
printf("not ok - uc_emu_stop fail during hook_code callback, addr: "
|
|
|
|
"0x%" PRIx64 "\n",
|
|
|
|
addr);
|
2015-09-04 20:27:03 +03:00
|
|
|
_exit(-1);
|
2015-10-31 00:34:35 +03:00
|
|
|
}
|
2015-09-04 20:27:03 +03:00
|
|
|
}
|
2015-09-05 04:42:47 +03:00
|
|
|
|
2015-09-04 20:27:03 +03:00
|
|
|
opcode = buf[0];
|
|
|
|
switch (opcode) {
|
2021-10-29 13:44:49 +03:00
|
|
|
case 0x41: // inc ecx
|
|
|
|
if (uc_mem_protect(uc, 0x101000, 0x1000, UC_PROT_READ) != UC_ERR_OK) {
|
|
|
|
printf("not ok - uc_mem_protect fail during hook_code callback, "
|
|
|
|
"addr: 0x%" PRIx64 "\n",
|
|
|
|
addr);
|
|
|
|
_exit(-1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x42: // inc edx
|
|
|
|
if (uc_mem_unmap(uc, 0x101000, 0x1000) != UC_ERR_OK) {
|
|
|
|
printf("not ok - uc_mem_unmap fail during hook_code callback, "
|
|
|
|
"addr: 0x%" PRIx64 "\n",
|
|
|
|
addr);
|
|
|
|
_exit(-1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xf4: // hlt
|
|
|
|
if (uc_emu_stop(uc) != UC_ERR_OK) {
|
|
|
|
printf("not ok - uc_emu_stop fail during hook_code callback, addr: "
|
|
|
|
"0x%" PRIx64 "\n",
|
|
|
|
addr);
|
|
|
|
_exit(-1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: // all others
|
|
|
|
break;
|
2015-09-04 20:27:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// callback for tracing invalid memory access (READ/WRITE/EXEC)
|
2021-10-29 13:44:49 +03:00
|
|
|
static bool hook_mem_invalid(uc_engine *uc, uc_mem_type type, uint64_t addr,
|
|
|
|
int size, int64_t value, void *user_data)
|
2015-09-04 20:27:03 +03:00
|
|
|
{
|
2021-10-29 13:44:49 +03:00
|
|
|
switch (type) {
|
|
|
|
default:
|
|
|
|
printf("not ok - UC_HOOK_MEM_INVALID type: %d at 0x%" PRIx64 "\n", type,
|
|
|
|
addr);
|
|
|
|
return false;
|
|
|
|
case UC_MEM_READ_UNMAPPED:
|
|
|
|
printf("not ok - Read from invalid memory at 0x%" PRIx64
|
|
|
|
", data size = %u\n",
|
|
|
|
addr, size);
|
|
|
|
return false;
|
|
|
|
case UC_MEM_WRITE_UNMAPPED:
|
|
|
|
printf("not ok - Write to invalid memory at 0x%" PRIx64
|
|
|
|
", data size = %u, data value = 0x%" PRIx64 "\n",
|
|
|
|
addr, size, value);
|
|
|
|
return false;
|
|
|
|
case UC_MEM_FETCH_PROT:
|
|
|
|
printf("not ok - Fetch from non-executable memory at 0x%" PRIx64 "\n",
|
|
|
|
addr);
|
|
|
|
return false;
|
|
|
|
case UC_MEM_WRITE_PROT:
|
|
|
|
printf("not ok - Write to non-writeable memory at 0x%" PRIx64
|
|
|
|
", data size = %u, data value = 0x%" PRIx64 "\n",
|
|
|
|
addr, size, value);
|
|
|
|
return false;
|
|
|
|
case UC_MEM_READ_PROT:
|
|
|
|
printf("not ok - Read from non-readable memory at 0x%" PRIx64
|
|
|
|
", data size = %u\n",
|
|
|
|
addr, size);
|
|
|
|
return false;
|
2015-09-04 20:27:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_nx_demo(bool cause_fault)
|
|
|
|
{
|
2015-09-05 06:20:32 +03:00
|
|
|
uc_engine *uc;
|
|
|
|
uc_hook trace1, trace2;
|
2015-09-04 20:27:03 +03:00
|
|
|
uc_err err;
|
|
|
|
uint8_t code_buf[0x3000];
|
|
|
|
|
|
|
|
insts_executed = 0;
|
|
|
|
|
|
|
|
printf("===================================\n");
|
2021-10-29 13:44:49 +03:00
|
|
|
printf("# Example of marking memory NX (%s)\n",
|
|
|
|
cause_fault ? "faulting" : "non-faulting");
|
2015-09-04 20:27:03 +03:00
|
|
|
|
|
|
|
// Initialize emulator in X86-32bit mode
|
|
|
|
err = uc_open(UC_ARCH_X86, UC_MODE_32, &uc);
|
|
|
|
if (err) {
|
|
|
|
printf("not ok - Failed on uc_open() with error returned: %u\n", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uc_mem_map(uc, 0x100000, 0x3000, UC_PROT_READ | UC_PROT_EXEC);
|
|
|
|
|
2015-09-05 04:42:47 +03:00
|
|
|
/*
|
|
|
|
bits 32
|
2016-01-26 17:50:35 +03:00
|
|
|
page0: @0
|
2015-09-05 04:42:47 +03:00
|
|
|
times 4091 inc eax
|
|
|
|
jmp page2
|
2016-01-26 17:50:35 +03:00
|
|
|
page1: @1000
|
|
|
|
times 4095 inc eax (or INC ECX)
|
2015-09-05 04:42:47 +03:00
|
|
|
hlt
|
2016-01-26 17:50:35 +03:00
|
|
|
page2: @2000
|
2015-09-05 04:42:47 +03:00
|
|
|
jmp page1
|
|
|
|
*/
|
2021-10-29 13:44:49 +03:00
|
|
|
memset(code_buf, 0x40, sizeof(code_buf)); // fill with inc eax
|
|
|
|
memcpy(code_buf + 0x1000 - 5, "\xe9\x00\x10\x00\x00",
|
|
|
|
5); // jump to 0x102000
|
2015-09-05 04:42:47 +03:00
|
|
|
memcpy(code_buf + 0x2000, "\xe9\xfb\xef\xff\xff", 5); // jump to 0x101000
|
2021-10-29 13:44:49 +03:00
|
|
|
code_buf[0x1fff] = 0xf4; // hlt
|
2015-09-05 04:42:47 +03:00
|
|
|
|
2015-09-04 20:27:03 +03:00
|
|
|
if (cause_fault) {
|
2021-10-29 13:44:49 +03:00
|
|
|
// insert instruction to trigger U_PROT_EXEC change (see hook_code
|
|
|
|
// function)
|
|
|
|
code_buf[0x1000] = 0x41; // inc ecx at page1
|
2015-10-31 00:34:35 +03:00
|
|
|
}
|
2015-09-04 20:27:03 +03:00
|
|
|
|
|
|
|
// write machine code to be emulated to memory
|
|
|
|
if (uc_mem_write(uc, 0x100000, code_buf, sizeof(code_buf))) {
|
|
|
|
printf("not ok - Failed to write emulation code to memory, quit!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// intercept code and invalid memory events
|
2021-10-29 13:44:49 +03:00
|
|
|
if (uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0) !=
|
|
|
|
UC_ERR_OK ||
|
|
|
|
uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL, 1,
|
|
|
|
0) != UC_ERR_OK) {
|
2015-09-04 20:27:03 +03:00
|
|
|
printf("not ok - Failed to install hooks\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// emulate machine code until told to stop by hook_code
|
|
|
|
printf("BEGINNING EXECUTION\n");
|
|
|
|
err = uc_emu_start(uc, 0x100000, 0x103000, 0, 0);
|
|
|
|
if (err != UC_ERR_OK) {
|
2021-10-29 13:44:49 +03:00
|
|
|
printf("not ok - Failure on uc_emu_start() with error %u: %s\n", err,
|
|
|
|
uc_strerror(err));
|
2015-09-04 20:27:03 +03:00
|
|
|
printf("FAILED EXECUTION\n");
|
|
|
|
} else {
|
|
|
|
printf("SUCCESSFUL EXECUTION\n");
|
|
|
|
}
|
2015-09-05 04:42:47 +03:00
|
|
|
|
2015-09-04 20:27:03 +03:00
|
|
|
printf("Executed %d instructions\n\n", insts_executed);
|
2015-09-05 04:42:47 +03:00
|
|
|
|
2015-09-04 20:27:03 +03:00
|
|
|
uc_close(uc);
|
|
|
|
}
|
|
|
|
|
2015-09-05 04:42:47 +03:00
|
|
|
static void nx_test()
|
2015-09-04 20:27:03 +03:00
|
|
|
{
|
|
|
|
printf("NX demo - step 1: show that code runs to completion\n");
|
|
|
|
do_nx_demo(false);
|
|
|
|
printf("NX demo - step 2: show that code fails without UC_PROT_EXEC\n");
|
|
|
|
do_nx_demo(true);
|
|
|
|
}
|
|
|
|
|
2015-09-05 04:42:47 +03:00
|
|
|
static const uint8_t WRITE_DEMO[] =
|
|
|
|
"\x90\xc7\x05\x00\x20\x10\x00\x78\x56\x34\x12\xc7\x05\xfc\x0f\x10"
|
|
|
|
"\x00\x78\x56\x34\x12\xc7\x05\x00\x10\x10\x00\x21\x43\x65\x87";
|
2015-09-04 20:27:03 +03:00
|
|
|
|
|
|
|
static void do_perms_demo(bool change_perms)
|
|
|
|
{
|
2015-09-05 06:20:32 +03:00
|
|
|
uc_engine *uc;
|
|
|
|
uc_hook trace1, trace2;
|
2015-09-04 20:27:03 +03:00
|
|
|
uc_err err;
|
|
|
|
uint8_t code_buf[0x3000];
|
|
|
|
|
|
|
|
insts_executed = 0;
|
|
|
|
|
|
|
|
printf("===================================\n");
|
|
|
|
printf("# Example of manipulating memory permissions\n");
|
|
|
|
|
|
|
|
// Initialize emulator in X86-32bit mode
|
|
|
|
err = uc_open(UC_ARCH_X86, UC_MODE_32, &uc);
|
|
|
|
if (err) {
|
|
|
|
printf("not ok - Failed on uc_open() with error returned: %u\n", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uc_mem_map(uc, 0x100000, 0x3000, UC_PROT_ALL);
|
|
|
|
|
2015-09-05 04:42:47 +03:00
|
|
|
/*
|
|
|
|
bits 32
|
|
|
|
nop
|
|
|
|
mov dword [0x102000], 0x12345678
|
|
|
|
mov dword [0x100ffc], 0x12345678
|
|
|
|
mov dword [0x101000], 0x87654321 ; crashing case crashes here
|
|
|
|
times 1000 nop
|
|
|
|
hlt
|
|
|
|
*/
|
2015-09-04 20:27:03 +03:00
|
|
|
memcpy(code_buf, WRITE_DEMO, sizeof(WRITE_DEMO) - 1);
|
|
|
|
memset(code_buf + sizeof(WRITE_DEMO) - 1, 0x90, 1000);
|
2021-10-29 13:44:49 +03:00
|
|
|
code_buf[sizeof(WRITE_DEMO) - 1 + 1000] = 0xf4; // hlt
|
2015-09-05 04:42:47 +03:00
|
|
|
|
2015-09-04 20:27:03 +03:00
|
|
|
if (change_perms) {
|
2021-10-29 13:44:49 +03:00
|
|
|
// write protect memory area [0x101000, 0x101fff]. see hook_code
|
|
|
|
// function
|
|
|
|
code_buf[0] = 0x41; // inc ecx
|
2015-09-04 20:27:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// write machine code to be emulated to memory
|
|
|
|
if (uc_mem_write(uc, 0x100000, code_buf, sizeof(code_buf))) {
|
|
|
|
printf("not ok - Failed to write emulation code to memory, quit!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// intercept code and invalid memory events
|
2021-10-29 13:44:49 +03:00
|
|
|
if (uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0) !=
|
|
|
|
UC_ERR_OK ||
|
|
|
|
uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL, 1,
|
|
|
|
0) != UC_ERR_OK) {
|
2015-09-04 20:27:03 +03:00
|
|
|
printf("not ok - Failed to install hooks\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// emulate machine code until told to stop by hook_code
|
|
|
|
printf("BEGINNING EXECUTION\n");
|
|
|
|
err = uc_emu_start(uc, 0x100000, 0x103000, 0, 0);
|
|
|
|
if (err != UC_ERR_OK) {
|
|
|
|
printf("FAILED EXECUTION\n");
|
2021-10-29 13:44:49 +03:00
|
|
|
printf("not ok - Failure on uc_emu_start() with error %u: %s\n", err,
|
|
|
|
uc_strerror(err));
|
2015-09-04 20:27:03 +03:00
|
|
|
} else {
|
|
|
|
printf("SUCCESSFUL EXECUTION\n");
|
|
|
|
}
|
2015-09-05 04:42:47 +03:00
|
|
|
|
2015-09-04 20:27:03 +03:00
|
|
|
printf("Executed %d instructions\n\n", insts_executed);
|
2015-09-05 04:42:47 +03:00
|
|
|
|
2015-09-04 20:27:03 +03:00
|
|
|
uc_close(uc);
|
|
|
|
}
|
|
|
|
|
2015-09-05 04:42:47 +03:00
|
|
|
static void perms_test()
|
2015-09-04 20:27:03 +03:00
|
|
|
{
|
|
|
|
printf("Permissions demo - step 1: show that area is writeable\n");
|
|
|
|
do_perms_demo(false);
|
2021-10-29 13:44:49 +03:00
|
|
|
printf("Permissions demo - step 2: show that code fails when memory marked "
|
|
|
|
"unwriteable\n");
|
2015-09-04 20:27:03 +03:00
|
|
|
do_perms_demo(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_unmap_demo(bool do_unmap)
|
|
|
|
{
|
2015-09-05 06:20:32 +03:00
|
|
|
uc_engine *uc;
|
|
|
|
uc_hook trace1, trace2;
|
2015-09-04 20:27:03 +03:00
|
|
|
uc_err err;
|
|
|
|
uint8_t code_buf[0x3000];
|
|
|
|
|
|
|
|
insts_executed = 0;
|
|
|
|
|
|
|
|
printf("===================================\n");
|
|
|
|
printf("# Example of unmapping memory\n");
|
|
|
|
|
|
|
|
// Initialize emulator in X86-32bit mode
|
|
|
|
err = uc_open(UC_ARCH_X86, UC_MODE_32, &uc);
|
|
|
|
if (err) {
|
|
|
|
printf("not ok - Failed on uc_open() with error returned: %u\n", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uc_mem_map(uc, 0x100000, 0x3000, UC_PROT_ALL);
|
|
|
|
|
2015-09-05 04:42:47 +03:00
|
|
|
/*
|
|
|
|
bits 32
|
|
|
|
nop
|
|
|
|
mov dword [0x102000], 0x12345678
|
|
|
|
mov dword [0x100ffc], 0x12345678
|
|
|
|
mov dword [0x101000], 0x87654321 ; crashing case crashes here
|
|
|
|
times 1000 nop
|
|
|
|
hlt
|
|
|
|
*/
|
2015-09-04 20:27:03 +03:00
|
|
|
memcpy(code_buf, WRITE_DEMO, sizeof(WRITE_DEMO) - 1);
|
|
|
|
memset(code_buf + sizeof(WRITE_DEMO) - 1, 0x90, 1000);
|
2021-10-29 13:44:49 +03:00
|
|
|
code_buf[sizeof(WRITE_DEMO) - 1 + 1000] = 0xf4; // hlt
|
2015-09-05 04:42:47 +03:00
|
|
|
|
2015-09-04 20:27:03 +03:00
|
|
|
if (do_unmap) {
|
2015-09-05 04:42:47 +03:00
|
|
|
// unmap memory area [0x101000, 0x101fff]. see hook_code function
|
2021-10-29 13:44:49 +03:00
|
|
|
code_buf[0] = 0x42; // inc edx (see hook_code function)
|
2015-09-04 20:27:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// write machine code to be emulated to memory
|
|
|
|
if (uc_mem_write(uc, 0x100000, code_buf, 0x1000)) {
|
|
|
|
printf("not ok - Failed to write emulation code to memory, quit!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// intercept code and invalid memory events
|
2021-10-29 13:44:49 +03:00
|
|
|
if (uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0) !=
|
|
|
|
UC_ERR_OK ||
|
|
|
|
uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL, 1,
|
|
|
|
0) != UC_ERR_OK) {
|
2015-09-04 20:27:03 +03:00
|
|
|
printf("not ok - Failed to install hooks\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// emulate machine code until told to stop by hook_code
|
|
|
|
printf("BEGINNING EXECUTION\n");
|
|
|
|
err = uc_emu_start(uc, 0x100000, 0x103000, 0, 0);
|
|
|
|
if (err != UC_ERR_OK) {
|
|
|
|
printf("FAILED EXECUTION\n");
|
2021-10-29 13:44:49 +03:00
|
|
|
printf("not ok - Failure on uc_emu_start() with error %u: %s\n", err,
|
|
|
|
uc_strerror(err));
|
2015-09-04 20:27:03 +03:00
|
|
|
} else {
|
|
|
|
printf("SUCCESSFUL EXECUTION\n");
|
|
|
|
}
|
2015-09-05 04:42:47 +03:00
|
|
|
|
2015-09-04 20:27:03 +03:00
|
|
|
printf("Executed %d instructions\n\n", insts_executed);
|
2015-09-05 04:42:47 +03:00
|
|
|
|
2015-09-04 20:27:03 +03:00
|
|
|
uc_close(uc);
|
|
|
|
}
|
|
|
|
|
2015-09-05 04:42:47 +03:00
|
|
|
static void unmap_test()
|
2015-09-04 20:27:03 +03:00
|
|
|
{
|
|
|
|
printf("Unmap demo - step 1: show that area is writeable\n");
|
|
|
|
do_unmap_demo(false);
|
2021-10-29 13:44:49 +03:00
|
|
|
printf(
|
|
|
|
"Unmap demo - step 2: show that code fails when memory is unmapped\n");
|
2015-09-04 20:27:03 +03:00
|
|
|
do_unmap_demo(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv, char **envp)
|
|
|
|
{
|
2017-01-21 04:28:22 +03:00
|
|
|
nx_test();
|
2015-09-04 20:27:03 +03:00
|
|
|
perms_test();
|
|
|
|
unmap_test();
|
2015-09-05 04:42:47 +03:00
|
|
|
|
2017-01-21 04:28:22 +03:00
|
|
|
return 0;
|
2015-09-04 20:27:03 +03:00
|
|
|
}
|