2015-08-28 13:42:25 +03:00
|
|
|
/*
|
2015-08-28 17:23:44 +03:00
|
|
|
Non-writable memory test case
|
2015-08-28 13:42:25 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2015-08-26 23:29:54 +03:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <unicorn/unicorn.h>
|
|
|
|
|
2015-08-28 04:03:17 +03:00
|
|
|
const uint8_t PROGRAM[] =
|
2015-08-28 17:20:14 +03:00
|
|
|
"\xeb\x1a\x58\x83\xc0\x04\x83\xe0\xfc\x83\xc0\x01\xc7\x00\x78\x56"
|
|
|
|
"\x34\x12\x83\xc0\x07\xc7\x00\x21\x43\x65\x87\x90\xe8\xe1\xff\xff"
|
|
|
|
"\xff" "xxxxAAAAxxxBBBB";
|
2015-08-28 04:03:17 +03:00
|
|
|
// total size: 33 bytes
|
2015-08-26 23:29:54 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
jmp short bottom
|
|
|
|
top:
|
2015-08-28 17:20:14 +03:00
|
|
|
pop eax
|
|
|
|
add eax, 4
|
|
|
|
and eax, 0xfffffffc
|
|
|
|
add eax, 1 ; unaligned
|
|
|
|
mov dword [eax], 0x12345678 ; try to write into code section
|
|
|
|
add eax, 7 ; aligned
|
|
|
|
mov dword [eax], 0x87654321 ; try to write into code section
|
|
|
|
nop
|
2015-08-26 23:29:54 +03:00
|
|
|
bottom:
|
2015-08-28 17:20:14 +03:00
|
|
|
call top
|
|
|
|
*/
|
2015-08-26 23:29:54 +03:00
|
|
|
|
2015-08-27 07:28:36 +03:00
|
|
|
// callback for tracing instruction
|
2015-09-05 06:20:32 +03:00
|
|
|
static void hook_code(uc_engine *uc, uint64_t address, uint32_t size, void *user_data)
|
2015-08-27 07:28:36 +03:00
|
|
|
{
|
|
|
|
uint32_t esp;
|
|
|
|
printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size);
|
|
|
|
|
2015-09-03 04:25:59 +03:00
|
|
|
uc_reg_read(uc, UC_X86_REG_ESP, &esp);
|
2015-08-27 07:28:36 +03:00
|
|
|
printf(">>> --- ESP is 0x%x\n", esp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-27 09:21:41 +03:00
|
|
|
// callback for tracing memory access (READ or WRITE)
|
2015-09-05 06:20:32 +03:00
|
|
|
static bool hook_mem_invalid(uc_engine *uc, uc_mem_type type,
|
2015-08-27 09:21:41 +03:00
|
|
|
uint64_t address, int size, int64_t value, void *user_data)
|
|
|
|
{
|
|
|
|
uint32_t esp;
|
2015-09-03 04:25:59 +03:00
|
|
|
uc_reg_read(uc, UC_X86_REG_ESP, &esp);
|
2015-08-27 09:21:41 +03:00
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
default:
|
|
|
|
// return false to indicate we want to stop emulation
|
|
|
|
return false;
|
|
|
|
case UC_MEM_WRITE:
|
|
|
|
//if this is a push, esp has not been adjusted yet
|
|
|
|
if (esp == (address + size)) {
|
2015-08-28 13:21:36 +03:00
|
|
|
uint32_t upper;
|
|
|
|
upper = (esp + 0xfff) & ~0xfff;
|
|
|
|
printf(">>> Stack appears to be missing at 0x%"PRIx64 ", allocating now\n", address);
|
|
|
|
// map this memory in with 2MB in size
|
2015-09-03 04:25:59 +03:00
|
|
|
uc_mem_map(uc, upper - 0x8000, 0x8000, UC_PROT_READ | UC_PROT_WRITE);
|
2015-08-28 13:21:36 +03:00
|
|
|
// return true to indicate we want to continue
|
|
|
|
return true;
|
2015-08-27 09:21:41 +03:00
|
|
|
}
|
2015-08-28 04:03:17 +03:00
|
|
|
printf(">>> Missing memory is being WRITTEN at 0x%"PRIx64 ", data size = %u, data value = 0x%"PRIx64 "\n",
|
2015-08-28 13:21:36 +03:00
|
|
|
address, size, value);
|
2015-08-28 04:03:17 +03:00
|
|
|
return false;
|
2015-09-04 06:04:13 +03:00
|
|
|
case UC_MEM_WRITE_PROT:
|
2015-08-28 04:03:17 +03:00
|
|
|
printf(">>> RO memory is being WRITTEN at 0x%"PRIx64 ", data size = %u, data value = 0x%"PRIx64 "\n",
|
2015-08-28 13:21:36 +03:00
|
|
|
address, size, value);
|
2015-08-27 09:21:41 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-27 07:28:36 +03:00
|
|
|
#define STACK 0x500000
|
|
|
|
#define STACK_SIZE 0x5000
|
|
|
|
|
2015-08-28 13:21:36 +03:00
|
|
|
int main(int argc, char **argv, char **envp)
|
|
|
|
{
|
2015-09-05 06:20:32 +03:00
|
|
|
uc_engine *uc;
|
|
|
|
uc_hook trace1, trace2;
|
2015-08-26 23:29:54 +03:00
|
|
|
uc_err err;
|
|
|
|
uint8_t bytes[8];
|
2015-08-27 07:28:36 +03:00
|
|
|
uint32_t esp;
|
2015-08-27 07:47:15 +03:00
|
|
|
int result;
|
2015-08-27 09:21:41 +03:00
|
|
|
int map_stack = 0;
|
2015-08-28 13:21:36 +03:00
|
|
|
|
2015-08-27 09:21:41 +03:00
|
|
|
if (argc == 2 && strcmp(argv[1], "--map-stack") == 0) {
|
|
|
|
map_stack = 1;
|
|
|
|
}
|
2015-08-26 23:29:54 +03:00
|
|
|
|
|
|
|
printf("Memory mapping test\n");
|
|
|
|
|
|
|
|
// Initialize emulator in X86-32bit mode
|
2015-09-03 04:25:59 +03:00
|
|
|
err = uc_open(UC_ARCH_X86, UC_MODE_32, &uc);
|
2015-08-26 23:29:54 +03:00
|
|
|
if (err) {
|
|
|
|
printf("Failed on uc_open() with error returned: %u\n", err);
|
2015-08-27 07:47:15 +03:00
|
|
|
return 1;
|
2015-08-26 23:29:54 +03:00
|
|
|
}
|
|
|
|
|
2015-09-03 04:25:59 +03:00
|
|
|
uc_mem_map(uc, 0x100000, 0x1000, UC_PROT_ALL);
|
|
|
|
uc_mem_map(uc, 0x200000, 0x2000, UC_PROT_ALL);
|
|
|
|
uc_mem_map(uc, 0x300000, 0x3000, UC_PROT_ALL);
|
|
|
|
uc_mem_map(uc, 0x400000, 0x4000, UC_PROT_READ);
|
2015-08-28 13:21:36 +03:00
|
|
|
|
2015-08-27 09:21:41 +03:00
|
|
|
if (map_stack) {
|
2015-08-28 13:21:36 +03:00
|
|
|
printf("Pre-mapping stack\n");
|
2015-09-03 04:25:59 +03:00
|
|
|
uc_mem_map(uc, STACK, STACK_SIZE, UC_PROT_READ | UC_PROT_WRITE);
|
2015-08-28 17:23:44 +03:00
|
|
|
} else {
|
2015-08-28 04:03:17 +03:00
|
|
|
printf("Mapping stack on first invalid memory access\n");
|
|
|
|
}
|
2015-08-27 07:28:36 +03:00
|
|
|
|
|
|
|
esp = STACK + STACK_SIZE;
|
|
|
|
|
2015-09-03 04:25:59 +03:00
|
|
|
uc_reg_write(uc, UC_X86_REG_ESP, &esp);
|
2015-08-26 23:29:54 +03:00
|
|
|
|
|
|
|
// write machine code to be emulated to memory
|
2015-09-03 04:25:59 +03:00
|
|
|
if (uc_mem_write(uc, 0x400000, PROGRAM, sizeof(PROGRAM))) {
|
2015-08-26 23:29:54 +03:00
|
|
|
printf("Failed to write emulation code to memory, quit!\n");
|
2015-08-27 07:47:15 +03:00
|
|
|
return 2;
|
2015-08-28 17:23:44 +03:00
|
|
|
} else {
|
2015-08-26 23:29:54 +03:00
|
|
|
printf("Allowed to write to read only memory via uc_mem_write\n");
|
|
|
|
}
|
|
|
|
|
2015-09-03 04:25:59 +03:00
|
|
|
//uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)0x400000, (uint64_t)0x400fff);
|
2015-08-27 07:28:36 +03:00
|
|
|
|
2015-08-27 09:21:41 +03:00
|
|
|
// intercept invalid memory events
|
2015-09-30 09:46:55 +03:00
|
|
|
uc_hook_add(uc, &trace1, UC_HOOK_MEM_WRITE_UNMAPPED | UC_HOOK_MEM_WRITE_PROT, hook_mem_invalid, NULL);
|
2015-08-27 09:21:41 +03:00
|
|
|
|
2015-08-26 23:29:54 +03:00
|
|
|
// emulate machine code in infinite time
|
2015-08-28 04:03:17 +03:00
|
|
|
printf("BEGIN execution - 1\n");
|
2015-09-03 04:25:59 +03:00
|
|
|
err = uc_emu_start(uc, 0x400000, 0x400000 + sizeof(PROGRAM), 0, 10);
|
2015-08-28 04:03:17 +03:00
|
|
|
if (err) {
|
|
|
|
printf("Expected failue on uc_emu_start() with error returned %u: %s\n",
|
|
|
|
err, uc_strerror(err));
|
2015-08-28 17:23:44 +03:00
|
|
|
} else {
|
2015-08-28 04:03:17 +03:00
|
|
|
printf("UNEXPECTED uc_emu_start returned UC_ERR_OK\n");
|
|
|
|
}
|
|
|
|
printf("END execution - 1\n");
|
|
|
|
|
|
|
|
// emulate machine code in infinite time
|
|
|
|
printf("BEGIN execution - 2\n");
|
2015-08-28 22:58:53 +03:00
|
|
|
//update eax to point to aligned memory (same as add eax,7 above)
|
2015-08-28 04:03:17 +03:00
|
|
|
uint32_t eax = 0x40002C;
|
2015-09-03 04:25:59 +03:00
|
|
|
uc_reg_write(uc, UC_X86_REG_EAX, &eax);
|
2015-08-28 22:58:53 +03:00
|
|
|
//resume execution at the mov dword [eax], 0x87654321
|
2015-09-03 13:16:49 +03:00
|
|
|
//to test an aligned write as well
|
2015-09-03 04:25:59 +03:00
|
|
|
err = uc_emu_start(uc, 0x400015, 0x400000 + sizeof(PROGRAM), 0, 2);
|
2015-08-26 23:29:54 +03:00
|
|
|
if (err) {
|
2015-08-28 04:03:17 +03:00
|
|
|
printf("Expected failure on uc_emu_start() with error returned %u: %s\n",
|
2015-08-26 23:29:54 +03:00
|
|
|
err, uc_strerror(err));
|
2015-08-28 17:23:44 +03:00
|
|
|
} else {
|
2015-08-28 04:03:17 +03:00
|
|
|
printf("UNEXPECTED uc_emu_start returned UC_ERR_OK\n");
|
2015-08-27 09:35:23 +03:00
|
|
|
}
|
2015-08-28 04:03:17 +03:00
|
|
|
printf("END execution - 2\n");
|
2015-08-26 23:29:54 +03:00
|
|
|
|
2015-08-28 04:03:17 +03:00
|
|
|
printf("Verifying content at 0x400025 is unchanged\n");
|
2015-09-03 04:25:59 +03:00
|
|
|
if (!uc_mem_read(uc, 0x400025, bytes, 4)) {
|
2015-08-28 04:03:17 +03:00
|
|
|
printf(">>> Read 4 bytes from [0x%x] = 0x%x\n", (uint32_t)0x400025, *(uint32_t*) bytes);
|
2015-08-27 09:35:23 +03:00
|
|
|
if (0x41414141 != *(uint32_t*) bytes) {
|
|
|
|
printf("ERROR content in read only memory changed\n");
|
2015-08-28 17:23:44 +03:00
|
|
|
} else {
|
2015-08-27 09:35:23 +03:00
|
|
|
printf("SUCCESS content in read only memory unchanged\n");
|
|
|
|
}
|
2015-08-28 17:23:44 +03:00
|
|
|
} else {
|
2015-08-27 09:35:23 +03:00
|
|
|
printf(">>> Failed to read 4 bytes from [0x%x]\n", (uint32_t)(esp - 4));
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2015-08-28 04:03:17 +03:00
|
|
|
printf("Verifying content at 0x40002C is unchanged\n");
|
2015-09-03 04:25:59 +03:00
|
|
|
if (!uc_mem_read(uc, 0x40002C, bytes, 4)) {
|
2015-08-28 04:03:17 +03:00
|
|
|
printf(">>> Read 4 bytes from [0x%x] = 0x%x\n", (uint32_t)0x40002C, *(uint32_t*) bytes);
|
|
|
|
if (0x42424242 != *(uint32_t*) bytes) {
|
|
|
|
printf("ERROR content in read only memory changed\n");
|
2015-08-28 17:23:44 +03:00
|
|
|
} else {
|
2015-08-28 04:03:17 +03:00
|
|
|
printf("SUCCESS content in read only memory unchanged\n");
|
|
|
|
}
|
2015-08-28 17:23:44 +03:00
|
|
|
} else {
|
2015-08-28 04:03:17 +03:00
|
|
|
printf(">>> Failed to read 4 bytes from [0x%x]\n", (uint32_t)(esp - 4));
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2015-08-27 09:21:41 +03:00
|
|
|
printf("Verifying content at bottom of stack is readable and correct\n");
|
2015-09-03 04:25:59 +03:00
|
|
|
if (!uc_mem_read(uc, esp - 4, bytes, 4)) {
|
2015-08-27 09:21:41 +03:00
|
|
|
printf(">>> Read 4 bytes from [0x%x] = 0x%x\n", (uint32_t)(esp - 4), *(uint32_t*) bytes);
|
2015-08-28 17:23:44 +03:00
|
|
|
} else {
|
2015-08-27 09:21:41 +03:00
|
|
|
printf(">>> Failed to read 4 bytes from [0x%x]\n", (uint32_t)(esp - 4));
|
2015-08-27 07:47:15 +03:00
|
|
|
return 4;
|
|
|
|
}
|
2015-08-26 23:29:54 +03:00
|
|
|
|
2015-09-03 04:25:59 +03:00
|
|
|
uc_close(uc);
|
2015-08-28 13:21:36 +03:00
|
|
|
|
2015-08-27 07:47:15 +03:00
|
|
|
return 0;
|
2015-08-26 23:29:54 +03:00
|
|
|
}
|