samples: cleanup & put mem_apis into sample_all.sh

This commit is contained in:
Nguyen Anh Quynh 2015-09-05 09:42:47 +08:00
parent 7b5bb95147
commit 74c069dc22
2 changed files with 68 additions and 54 deletions

View file

@ -35,7 +35,9 @@ static void hook_code(ucengine *uc, uint64_t addr, uint32_t size, void *user_dat
{
uint8_t opcode;
unsigned char buf[256];
insts_executed++;
if (uc_mem_read(uc, addr, buf, size) != UC_ERR_OK) {
printf("not ok - uc_mem_read fail during hook_code callback, addr: 0x%" PRIx64 "\n", addr);
if (uc_emu_stop(uc) != UC_ERR_OK) {
@ -43,6 +45,7 @@ static void hook_code(ucengine *uc, uint64_t addr, uint32_t size, void *user_dat
_exit(-1);
}
}
opcode = buf[0];
switch (opcode) {
case 0x41: // inc ecx
@ -130,6 +133,7 @@ page2:
memcpy(code_buf + 0x1000 - 5, "\xe9\x00\x10\x00\x00", 5); // jump to 0x102000
memcpy(code_buf + 0x2000, "\xe9\xfb\xef\xff\xff", 5); // jump to 0x101000
code_buf[0x1fff] = 0xf4; //hlt
if (cause_fault) {
// insert instruction to trigger U_PROT_EXEC change (see hook_code function)
code_buf[0x1000] = 0x41; // inc ecx at page1
@ -144,7 +148,6 @@ page2:
// intercept code and invalid memory events
if (uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0) != UC_ERR_OK ||
uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL) != UC_ERR_OK) {
printf("not ok - Failed to install hooks\n");
return;
}
@ -158,11 +161,13 @@ page2:
} else {
printf("SUCCESSFUL EXECUTION\n");
}
printf("Executed %d instructions\n\n", insts_executed);
uc_close(uc);
}
void nx_test()
static void nx_test()
{
printf("NX demo - step 1: show that code runs to completion\n");
do_nx_demo(false);
@ -170,7 +175,7 @@ void nx_test()
do_nx_demo(true);
}
const uint8_t WRITE_DEMO[] =
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";
@ -207,8 +212,10 @@ bits 32
memcpy(code_buf, WRITE_DEMO, sizeof(WRITE_DEMO) - 1);
memset(code_buf + sizeof(WRITE_DEMO) - 1, 0x90, 1000);
code_buf[sizeof(WRITE_DEMO) - 1 + 1000] = 0xf4; // hlt
if (change_perms) {
code_buf[0] = 0x41; //inc ecx (see hook_code function)
// write protect memory area [0x101000, 0x101fff]. see hook_code function
code_buf[0] = 0x41; // inc ecx
}
// write machine code to be emulated to memory
@ -220,7 +227,6 @@ bits 32
// intercept code and invalid memory events
if (uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0) != UC_ERR_OK ||
uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL) != UC_ERR_OK) {
printf("not ok - Failed to install hooks\n");
return;
}
@ -234,11 +240,13 @@ bits 32
} else {
printf("SUCCESSFUL EXECUTION\n");
}
printf("Executed %d instructions\n\n", insts_executed);
uc_close(uc);
}
void perms_test()
static void perms_test()
{
printf("Permissions demo - step 1: show that area is writeable\n");
do_perms_demo(false);
@ -280,7 +288,9 @@ bits 32
memcpy(code_buf, WRITE_DEMO, sizeof(WRITE_DEMO) - 1);
memset(code_buf + sizeof(WRITE_DEMO) - 1, 0x90, 1000);
code_buf[sizeof(WRITE_DEMO) - 1 + 1000] = 0xf4; // hlt
if (do_unmap) {
// unmap memory area [0x101000, 0x101fff]. see hook_code function
code_buf[0] = 0x42; // inc edx (see hook_code function)
}
@ -293,7 +303,6 @@ bits 32
// intercept code and invalid memory events
if (uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0) != UC_ERR_OK ||
uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL) != UC_ERR_OK) {
printf("not ok - Failed to install hooks\n");
return;
}
@ -307,11 +316,13 @@ bits 32
} else {
printf("SUCCESSFUL EXECUTION\n");
}
printf("Executed %d instructions\n\n", insts_executed);
uc_close(uc);
}
void unmap_test()
static void unmap_test()
{
printf("Unmap demo - step 1: show that area is writeable\n");
do_unmap_demo(false);
@ -324,5 +335,6 @@ int main(int argc, char **argv, char **envp)
nx_test();
perms_test();
unmap_test();
return 0;
}

View file

@ -19,6 +19,8 @@ if test -e $DIR/sample_x86; then
$DIR/sample_x86 -16
echo "=========================="
$DIR/shellcode -32
echo "=========================="
$DIR/mem_apis
fi
if test -e $DIR/sample_arm; then
echo "=========================="