static addr_t find_kdversionblock_address( vmi_instance_t vmi) { addr_t kdvb_address = 0; addr_t paddr = 0; unsigned char buf[12]; for (paddr = 0; paddr < vmi_get_memsize(vmi); paddr += 4) { if (12 == vmi_read_pa(vmi, paddr, buf, 12)) { if (VMI_PM_IA32E == vmi->page_mode) { if (memcmp(buf, "\x00\xf8\xff\xffKDBG", 8) == 0) { kdvb_address = paddr - 0xc; break; } } else { if (memcmp (buf, "\x00\x00\x00\x00\x00\x00\x00\x00KDBG", 12) == 0) { kdvb_address = paddr - 0x8; break; } } } } return kdvb_address; }
static int vmifs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { (void) fi; if(strcmp(path, mem_path) != 0) return -ENOENT; unsigned long memsize = vmi_get_memsize(vmi); if(offset < memsize && size) { if(offset + size > memsize) size = memsize-offset; uint8_t *buffer = g_malloc0(sizeof(uint8_t)*size); size_t size_read = vmi_read_pa(vmi, offset, buffer, size); if(size != size_read) { g_free(buffer); } else { memcpy(buf, buffer, size); g_free(buffer); } } else { return 0; } return size; }
static addr_t find_kdversionblock_address_fast( vmi_instance_t vmi) { // Note: this function has several limitations: // -the KD version block signature cannot cross block (frame) boundaries // -reading PA 0 fails; hope the KD version block is not in frame 0 // // Todo: // -support matching across frames (can this happen in windows?) addr_t kdvb_address = 0; addr_t block_pa = 0; addr_t memsize = vmi_get_memsize(vmi); size_t read = 0; void *bm = 0; // boyer-moore internal state int find_ofs = 0; #define BLOCK_SIZE 1024 * 1024 * 1 unsigned char haystack[BLOCK_SIZE]; if (VMI_PM_IA32E == vmi->page_mode) { bm = boyer_moore_init("\x00\xf8\xff\xffKDBG", 8); find_ofs = 0xc; } else { bm = boyer_moore_init("\x00\x00\x00\x00\x00\x00\x00\x00KDBG", 12); find_ofs = 0x8; } // if-else for (block_pa = 4096; block_pa < memsize; block_pa += BLOCK_SIZE) { read = vmi_read_pa(vmi, block_pa, haystack, BLOCK_SIZE); if (BLOCK_SIZE != read) { continue; } int match_offset = boyer_moore2(bm, haystack, BLOCK_SIZE); if (-1 != match_offset) { kdvb_address = block_pa + (unsigned int) match_offset - find_ofs; break; } // if } // outer for if (kdvb_address) dbprint("--Found KD version block at PA %.16"PRIx64"\n", kdvb_address); boyer_moore_fini(bm); return kdvb_address; }
static int vmifs_getattr(const char *path, struct stat *stbuf) { int res = 0; memset(stbuf, 0, sizeof(struct stat)); if(strcmp(path, "/") == 0) { stbuf->st_mode = S_IFDIR | 0755; stbuf->st_nlink = 2; } else if(strcmp(path, mem_path) == 0) { stbuf->st_mode = S_IFREG | 0444; stbuf->st_nlink = 1; stbuf->st_size = vmi_get_memsize(vmi); } else res = -ENOENT; return res; }
END_TEST START_TEST (test_vmi_get_memsize_max_phys_addr) { vmi_instance_t vmi = NULL; uint64_t memsize = 0; addr_t max_physical_addr = 0; vmi_init_complete(&vmi, (void*)get_testvm(), VMI_INIT_DOMAINNAME, NULL, VMI_CONFIG_GLOBAL_FILE_ENTRY, NULL, NULL); memsize = vmi_get_memsize(vmi); max_physical_addr = vmi_get_max_physical_address(vmi); fail_unless(memsize > 0, "guest ram size is 0"); fail_unless(max_physical_addr > 0, "max physical address is 0"); fail_unless(max_physical_addr >= memsize, "max physical address is less than memsize"); vmi_destroy(vmi); }
extern proc_info *process_list (char *name, image_offset *img_offsets, char *config_string) { vmi_instance_t vmi; addr_t list_head = 0, next_list_entry = 0; addr_t current_process_addr = 0; char *procname = NULL; status_t status; int image_offsets_provided = 0; printf("config_string: \n%s\n", config_string); /* initialize the libvmi library */ if (vmi_init(&vmi, VMI_AUTO | VMI_INIT_PARTIAL, name) == VMI_FAILURE) { printf("Failed to init Libvmi Library on the first time."); return NULL; } if(vmi_init_complete_custom(&vmi, VMI_CONFIG_STRING, config_string) == VMI_FAILURE) { printf("Failed to init LibVMI library on the second time.\n"); return NULL; } printf("1\n"); /* initialize the offsets */ if(img_offsets == NULL) { img_offsets = (image_offset*)malloc(sizeof(image_offset)); if (get_vm_offsets(vmi, img_offsets) == VMI_FAILURE) { printf("Failed to load offsets.\n"); vmi_destroy(vmi); free(img_offsets); return NULL; } } else { image_offsets_provided = 1; printf("Using provided image offsets\n"); } printf("2\n"); /* pause the vm for consistent memory access */ if (vmi_pause_vm(vmi) != VMI_SUCCESS) { printf("Failed to pause VM\n"); resume_vm(&vmi); if(!image_offsets_provided) free(img_offsets); return NULL; } /* demonstrate name and id accessors */ char *name2 = vmi_get_name(vmi); if (VMI_FILE != vmi_get_access_mode(vmi)) { uint64_t id = vmi_get_vmid(vmi); printf("Process listing for VM %s (id=%"PRIu64")\n", name2, id); } else { printf("Process listing for file %s\n", name2); } free(name2); /* get the head of the list */ if (VMI_OS_LINUX == vmi_get_ostype(vmi)) { /* Begin at PID 0, the 'swapper' task. It's not typically shown by OS * utilities, but it is indeed part of the task list and useful to * display as such. */ list_head = vmi_translate_ksym2v(vmi, "init_task") + img_offsets->tasks_offset; } else if (VMI_OS_WINDOWS == vmi_get_ostype(vmi)) { /* find PEPROCESS PsInitialSystemProcess */ if(VMI_FAILURE == vmi_read_addr_ksym(vmi, "PsActiveProcessHead", &list_head)) { printf("Failed to find PsActiveProcessHead\n"); resume_vm(&vmi); if(!image_offsets_provided) free(img_offsets); return NULL; } } next_list_entry = list_head; /* * Run process analyse for two rounds. * The first round: * - Collect basic process information: name, pid, etc; * - Collect the initial s_time, u_time, mm for calculating * cpu and memory usage; * - Construct our own process-info struct (process-info linked list). * The second round: * - Read current s_time, u_time, mm, etc. and calculate * cpu and memory usage; * - Finalize process-info struct; */ /* The first round */ /* walk the task list and create */ printf("First round.\n"); proc_info *process_info_list_head = NULL, *previous_process_ptr = NULL, *current_process_ptr = NULL; int is_head = 1; time_t calculation_start_time, calculation_end_time; vmi_read_64_va(vmi, vmi_translate_ksym2v(vmi, "jiffies"), 0, &calculation_start_time); do { current_process_ptr = (proc_info*)malloc(sizeof(proc_info)); current_process_addr = next_list_entry - img_offsets->tasks_offset; /* NOTE: _EPROCESS.UniqueProcessId is a really VOID*, but is never > 32 bits, * so this is safe enough for x64 Windows for example purposes */ vmi_read_32_va(vmi, current_process_addr + img_offsets->pid_offset, 0, (uint32_t*)&(current_process_ptr->pid)); current_process_ptr->name = vmi_read_str_va(vmi, current_process_addr + img_offsets->name_offset, 0); vmi_read_64_va(vmi, current_process_addr + img_offsets->utime_offset, 0, &(current_process_ptr->r_utime)); vmi_read_64_va(vmi, current_process_addr + img_offsets->stime_offset, 0, &(current_process_ptr->r_stime)); /* current_process_ptr->name == NULL implies reading process info is not successful */ if (!current_process_ptr->name) { printf("Failed to find process name\n"); resume_vm(&vmi); if(!image_offsets_provided) free(img_offsets); return NULL; } /* follow the next pointer and load the entry of next process*/ status = vmi_read_addr_va(vmi, next_list_entry, 0, &next_list_entry); if (status == VMI_FAILURE) { printf("Failed to read next pointer in loop at %"PRIx64"\n", next_list_entry); resume_vm(&vmi); if(!image_offsets_provided) free(img_offsets); return NULL; } if(is_head){ is_head = 0; process_info_list_head = current_process_ptr; previous_process_ptr = current_process_ptr; }else{ previous_process_ptr->next = current_process_ptr; previous_process_ptr = current_process_ptr; } } while(next_list_entry != list_head); current_process_ptr->next = NULL; vmi_resume_vm(vmi); sleep(3); /* The second round */ printf("Second round.\n"); uint64_t total_memory_size = vmi_get_memsize(vmi) / 1024; // total_memory_size unit: KB int dont_read_next_process = 0; vmi_pause_vm(vmi); is_head = 1; int old_list_has_ended = 0; proc_info* old_list_ptr = process_info_list_head; current_process_ptr = process_info_list_head; previous_process_ptr = process_info_list_head; next_list_entry = list_head; vmi_pid_t proc_pid; uint64_t new_utime, new_stime, sum_process_time_before, sum_process_time_after; /* walk the task list */ vmi_read_64_va(vmi, vmi_translate_ksym2v(vmi, "jiffies"), 0, &calculation_end_time); do { if (old_list_ptr == NULL) { old_list_has_ended = 1; } current_process_addr = next_list_entry - img_offsets->tasks_offset; /* NOTE: _EPROCESS.UniqueProcessId is a really VOID*, but is never > 32 bits, * so this is safe enough for x64 Windows for example purposes */ vmi_read_32_va(vmi, current_process_addr + img_offsets->pid_offset, 0, (uint32_t*)&proc_pid); if(old_list_has_ended || proc_pid < old_list_ptr->pid) { /* new process was created between sleep time * and pid is smaller than current process pid */ proc_info *new_process_ptr = (proc_info*)malloc(sizeof(proc_info)); new_process_ptr->name = vmi_read_str_va(vmi, current_process_addr + img_offsets->name_offset, 0); new_process_ptr->type = P_NEW_PROCESS; vmi_read_32_va(vmi, current_process_addr + img_offsets->pid_offset, 0, (uint32_t*)&(new_process_ptr->pid)); /* pointer related operations */ if(is_head) { process_info_list_head = new_process_ptr; current_process_ptr = new_process_ptr; previous_process_ptr = new_process_ptr; is_head = 0; } else { current_process_ptr = new_process_ptr; previous_process_ptr->next = current_process_ptr; previous_process_ptr = current_process_ptr; } } else if (proc_pid > old_list_ptr->pid) { /* previous process has ended between sleep time */ old_list_ptr->type = P_ENDED_PROCESS; dont_read_next_process = 1; if(is_head) { is_head = 0; old_list_ptr = old_list_ptr->next; continue; } current_process_ptr = old_list_ptr; previous_process_ptr->next = current_process_ptr; previous_process_ptr = current_process_ptr; old_list_ptr = old_list_ptr->next; } else { /* the process still exists. cpu% and mem% can be calculated*/ current_process_ptr = old_list_ptr; current_process_ptr->type = P_EXIST_PROCESS; /* get priority and state */ vmi_read_64_va(vmi, current_process_addr + img_offsets->state_offset, 0, (uint64_t*)&(current_process_ptr->state)); vmi_read_32_va(vmi, current_process_addr + img_offsets->rt_priority_offset, 0, (uint32_t*)&(current_process_ptr->priority)); /* read process cpu time */ vmi_read_64_va(vmi, current_process_addr + img_offsets->utime_offset, 0, &new_utime); vmi_read_64_va(vmi, current_process_addr + img_offsets->stime_offset, 0, &new_stime); /* calculate cpu usage */ sum_process_time_after = new_utime + new_stime; sum_process_time_before = current_process_ptr->r_stime + current_process_ptr->r_utime; current_process_ptr->cpu_percent = (double)(sum_process_time_after - sum_process_time_before) / (calculation_end_time - calculation_start_time) * 100.0; /* get total vm pages for calculating virtual memory usage */ addr_t mm_addr = NULL; uint32_t total_vm_pages = 0; vmi_read_addr_va(vmi, current_process_addr + img_offsets->mm_offset, 0, &mm_addr); vmi_read_32_va(vmi, mm_addr + img_offsets->total_vm_offset,0, &total_vm_pages); current_process_ptr->virtual_memory_usage = total_vm_pages * PAGE_SIZE_KB; /* get rss count for calculating physical memory usage*/ int i = 0; uint64_t rss_count = 0, temp = 0; for(i = 0; i < NR_MM_COUNTERS; i++) { vmi_read_64_va(vmi, mm_addr + img_offsets->rss_stat_offset + i * img_offsets->element_offset, 0, (uint64_t*)&temp); rss_count += temp; } current_process_ptr->physical_memory_usage = rss_count * PAGE_SIZE_KB; /* calculate memory usage (percent) */ current_process_ptr->memory_percent = (double)rss_count * PAGE_SIZE_KB / total_memory_size * 100.0; /* deal with pointer */ if(is_head) is_head = 0; else { previous_process_ptr->next = current_process_ptr; previous_process_ptr = current_process_ptr; } old_list_ptr = old_list_ptr->next; } if(!dont_read_next_process) { /* follow the next pointer and load the entry of next process*/ status = vmi_read_addr_va(vmi, next_list_entry, 0, &next_list_entry); if (status == VMI_FAILURE) { printf("Failed to read next pointer in loop at %"PRIx64"\n", next_list_entry); resume_vm(&vmi); free(img_offsets); return NULL; } } else dont_read_next_process = 0; } while(next_list_entry != list_head); if(!old_list_has_ended) current_process_ptr->next = old_list_ptr; else if(current_process_ptr != NULL) current_process_ptr->next = NULL; if(!image_offsets_provided) free(img_offsets); vmi_resume_vm(vmi); vmi_destroy(vmi); //current_process_ptr = process_info_list_head; //while(current_process_ptr != NULL) //{ //current_process_ptr = process_info_list_head; //while(current_process_ptr != NULL) //{ // printf("%d | %s | %04.2lf \n", current_process_ptr->pid, current_process_ptr->name, current_process_ptr->cpu_percent); // current_process_ptr = current_process_ptr->next; //} return process_info_list_head; }
int main( int argc, char **argv) { vmi_instance_t vmi; char *filename = NULL; FILE *f = NULL; unsigned char memory[PAGE_SIZE]; unsigned char zeros[PAGE_SIZE]; memset(zeros, 0, PAGE_SIZE); uint32_t offset = 0; addr_t address = 0; /* this is the VM or file that we are looking at */ char *name = argv[1]; /* this is the file name to write the memory image to */ filename = strndup(argv[2], 50); /* initialize the libvmi library */ if (vmi_init(&vmi, VMI_AUTO | VMI_INIT_PARTIAL, name) == VMI_FAILURE) { printf("Failed to init LibVMI library.\n"); goto error_exit; } /* open the file for writing */ if ((f = fopen(filename, "w+")) == NULL) { printf("failed to open file for writing.\n"); goto error_exit; } while (address < vmi_get_memsize(vmi)) { /* write memory to file */ if (PAGE_SIZE == vmi_read_pa(vmi, address, memory, PAGE_SIZE)) { /* memory mapped, just write to file */ size_t written = fwrite(memory, 1, PAGE_SIZE, f); if (written != PAGE_SIZE) { printf("failed to write memory to file.\n"); goto error_exit; } } else { /* memory not mapped, write zeros to maintain offset */ size_t written = fwrite(zeros, 1, PAGE_SIZE, f); if (written != PAGE_SIZE) { printf("failed to write zeros to file.\n"); goto error_exit; } } /* move on to the next page */ address += PAGE_SIZE; } error_exit: if (f) fclose(f); /* cleanup any memory associated with the libvmi instance */ vmi_destroy(vmi); return 0; }