2015-08-28 13:52:51 +03:00
|
|
|
/*
|
2015-08-28 17:23:44 +03:00
|
|
|
Non-readable memory test case
|
2015-08-28 13:52:51 +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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <unicorn/unicorn.h>
|
|
|
|
|
|
|
|
const uint8_t PROGRAM[] =
|
|
|
|
"\x8b\x1d\x00\x00\x30\x00\xa1\x00\x00\x40\x00";
|
|
|
|
// total size: 11 bytes
|
|
|
|
|
|
|
|
/*
|
|
|
|
bits 32
|
|
|
|
|
|
|
|
mov ebx, [0x300000]
|
|
|
|
mov eax, [0x400000]
|
|
|
|
*/
|
|
|
|
|
|
|
|
// callback for tracing memory access (READ or WRITE)
|
2015-09-03 04:44:43 +03:00
|
|
|
static bool hook_mem_invalid(ucengine *uc, uc_mem_type type,
|
2015-08-28 13:52:51 +03:00
|
|
|
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;
|
2015-09-04 06:04:13 +03:00
|
|
|
case UC_MEM_READ_PROT:
|
2015-08-28 13:52:51 +03:00
|
|
|
printf(">>> non-readable memory is being read at 0x%"PRIx64 ", data size = %u\n",
|
|
|
|
address, size);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv, char **envp)
|
|
|
|
{
|
2015-09-03 04:44:43 +03:00
|
|
|
ucengine *uc;
|
2015-09-04 05:39:23 +03:00
|
|
|
uchook trace1, trace2;
|
2015-08-28 13:52:51 +03:00
|
|
|
uc_err err;
|
2015-08-28 17:56:25 +03:00
|
|
|
uint32_t eax, ebx;
|
2015-08-28 13:52:51 +03:00
|
|
|
|
|
|
|
printf("Memory protections 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-28 13:52:51 +03:00
|
|
|
if (err) {
|
|
|
|
printf("Failed on uc_open() with error returned: %u\n", err);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-09-03 04:25:59 +03:00
|
|
|
uc_mem_map(uc, 0x100000, 0x1000, UC_PROT_READ);
|
|
|
|
uc_mem_map(uc, 0x300000, 0x1000, UC_PROT_READ | UC_PROT_WRITE);
|
|
|
|
uc_mem_map(uc, 0x400000, 0x1000, UC_PROT_WRITE);
|
2015-08-28 13:52:51 +03:00
|
|
|
|
|
|
|
// write machine code to be emulated to memory
|
2015-09-03 04:25:59 +03:00
|
|
|
if (uc_mem_write(uc, 0x100000, PROGRAM, sizeof(PROGRAM))) {
|
2015-08-28 13:52:51 +03:00
|
|
|
printf("Failed to write emulation code to memory, quit!\n");
|
|
|
|
return 2;
|
2015-08-28 17:23:44 +03:00
|
|
|
} else {
|
2015-08-28 13:52:51 +03:00
|
|
|
printf("Allowed to write to read only memory via uc_mem_write\n");
|
|
|
|
}
|
|
|
|
|
2015-09-03 04:25:59 +03:00
|
|
|
uc_mem_write(uc, 0x300000, (const uint8_t*)"\x41\x41\x41\x41", 4);
|
|
|
|
uc_mem_write(uc, 0x400000, (const uint8_t*)"\x42\x42\x42\x42", 4);
|
2015-08-28 13:52:51 +03:00
|
|
|
|
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-28 13:52:51 +03:00
|
|
|
|
|
|
|
// intercept invalid memory events
|
2015-09-03 04:25:59 +03:00
|
|
|
uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL);
|
2015-08-28 13:52:51 +03:00
|
|
|
|
|
|
|
// emulate machine code in infinite time
|
|
|
|
printf("BEGIN execution\n");
|
2015-09-03 04:25:59 +03:00
|
|
|
err = uc_emu_start(uc, 0x100000, 0x100000 + sizeof(PROGRAM), 0, 2);
|
2015-08-28 13:52:51 +03:00
|
|
|
if (err) {
|
|
|
|
printf("Expected failure 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 13:52:51 +03:00
|
|
|
printf("UNEXPECTED uc_emu_start returned UC_ERR_OK\n");
|
|
|
|
}
|
|
|
|
printf("END execution\n");
|
|
|
|
|
2015-09-03 04:25:59 +03:00
|
|
|
uc_reg_read(uc, UC_X86_REG_EAX, &eax);
|
2015-08-28 13:52:51 +03:00
|
|
|
printf("Final eax = 0x%x\n", eax);
|
2015-09-03 04:25:59 +03:00
|
|
|
uc_reg_read(uc, UC_X86_REG_EBX, &ebx);
|
2015-08-28 17:56:25 +03:00
|
|
|
printf("Final ebx = 0x%x\n", ebx);
|
2015-08-28 13:52:51 +03:00
|
|
|
|
2015-09-03 04:25:59 +03:00
|
|
|
uc_close(uc);
|
2015-08-28 13:52:51 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|