static status_t kpcr_symbol_resolve (vmi_instance_t vmi, unsigned long offset, addr_t *address) { uint64_t tmp = 0; addr_t symaddr = vmi->os.windows_instance.kdversion_block + offset; if (VMI_FAILURE == vmi_read_64_va(vmi, symaddr, 0, &tmp)){ return VMI_FAILURE; } *address = tmp; return VMI_SUCCESS; }
status_t vmi_read_addr_va( vmi_instance_t vmi, addr_t vaddr, vmi_pid_t pid, addr_t *value) { if (vmi->page_mode == VMI_PM_IA32E) { return vmi_read_64_va(vmi, vaddr, pid, value); } else { uint32_t tmp = 0; status_t ret = vmi_read_32_va(vmi, vaddr, pid, &tmp); *value = (uint64_t) tmp; return ret; } }
static status_t kdbg_symbol_resolve( vmi_instance_t vmi, unsigned long offset, addr_t *address) { uint64_t tmp = 0; addr_t symaddr = 0; windows_instance_t windows = NULL; if (vmi->os_data == NULL) { return VMI_FAILURE; } windows = vmi->os_data; symaddr = windows->kdbg_va + offset; if (VMI_FAILURE == vmi_read_64_va(vmi, symaddr, 0, &tmp)) { return VMI_FAILURE; } *address = tmp; return VMI_SUCCESS; }
// Helpful resource for describing the Kernel's representation of virtual memory // http://www.seas.ucla.edu/~uentao/spring15/CS33_1A_Week10.1.pdf // More helpful stuff // http://lxr.free-electrons.com/source/include/linux/sched.h#L1378 // http://lxr.free-electrons.com/source/include/linux/mm_types.h#L390 // keep following mm_types -> fs -> path -> dcache addr_t walk_vmmap_for_lib(vmi_instance_t vmi, addr_t proc, char* libname) { // returns base address of first mmap'd occurrence of library // local vars addr_t mm; addr_t mmap; addr_t vm_area_st_itr; addr_t file; addr_t path; addr_t dentry; addr_t iname; addr_t vm_start; char filename[32]; // walk the structs TODO: check each of these dereferences for errors and fail nicely // task_struct->mm->mmap->vm_next->vm_next->vm_next->...->vm_file->path vmi_read_64_va(vmi, proc + mm_offset, 0, &mm); // read address of mm //printf("mm is at 0x%llx\n",mm); vmi_read_64_va(vmi, mm + mmap_offset, 0, &mmap); // read address of first mmap in list //printf("mmap is at 0x%llx\n",mmap); vm_area_st_itr = mmap; // while (vm_area_st_itr != 0) { // iterate over vm_area_structs vmi_read_64_va(vmi, vm_area_st_itr + vm_area_file_offset, 0, &file); // get this struct's file pointer //printf("file is at 0x%llx\n",file); path = file + file_path_offset; // not a pointer, so no need to dereference //printf("path is at 0x%llx\n",path); vmi_read_64_va(vmi, path + dentry_offset, 0, &dentry); // get dentry //printf("dentry is at 0x%llx\n",dentry); iname = dentry + iname_offset; // pointer to filename that was loaded into memory here //printf("iname is at 0x%llx\n",iname); vmi_read_va(vmi, iname, 0, filename, sizeof(filename)); //printf("filename is %s\n",filename); if (strncmp(filename, libname, sizeof(libname)) == 0) { // if we've found the lib vmi_read_64_va(vmi, vm_area_st_itr + vm_area_start_offset, 0, &vm_start); // grab address of start of memory-mapped region //printf("base address of memory is 0x%llx\n", vm_start); return vm_start; } // go to next vmi_read_64_va(vmi, vm_area_st_itr + vm_area_next_offset, 0, &vm_area_st_itr); } return 0; // Failure }
/*根据data,执行vmi函数,并发送返回值*/ int rvmi_handle_data(int fd, char *buf) { int vmiFunNum; int i=0; char delims[] = " "; char *vmiFunArg[5] = {NULL,NULL,NULL,NULL}; char returnBuf[100]; char * returnVal =NULL; vmiFunArg[i] = strtok(buf, delims ); while( vmiFunArg[i] != NULL ) { i++; vmiFunArg[i] = strtok( NULL, delims ); } vmiFunNum = atoi(vmiFunArg[0]); //printf("vmifun %d\n",vmiFunNum); switch(vmiFunNum) { case 1: break; case 2: break; case 3: { int vmiID=atoi(vmiFunArg[1]); sprintf(returnBuf, "%d\0", vmi_destroy(vmiArr[vmiID]) ); vmiFlagArr[vmiID]=0; returnVal = returnBuf; } break; case 13: { sprintf(returnBuf, "%lu\0", vmi_get_offset(vmiArr[atoi(vmiFunArg[1])], vmiFunArg[2]) ); returnVal = returnBuf; } break; case 23: { for(i=0;i<MAX_DOMU_PER_MACHINE;i++) { if(0 == vmiFlagArr[i]) break; } vmi_init(&vmiArr[i], atoi(vmiFunArg[1]), vmiFunArg[2]); vmiFlagArr[i]=1; sprintf(returnBuf,"%d\0",i); returnVal = returnBuf; } break; case 38: { uint16_t tmp; vmi_read_16_va(vmiArr[atoi(vmiFunArg[1])], atol(vmiFunArg[2]), atoi(vmiFunArg[3]), &tmp); sprintf(returnBuf, "%hu\0", tmp); returnVal = returnBuf; } break; case 40: { uint32_t tmp; vmi_read_32_va(vmiArr[atoi(vmiFunArg[1])], atol(vmiFunArg[2]), atoi(vmiFunArg[3]), &tmp); sprintf(returnBuf, "%u\0", tmp); returnVal = returnBuf; } break; case 44: { uint64_t tmp; vmi_read_64_va(vmiArr[atoi(vmiFunArg[1])], atol(vmiFunArg[2]), atoi(vmiFunArg[3]), &tmp); sprintf(returnBuf, "%lu\0", tmp); returnVal = returnBuf; } break; case 50: { addr_t tmp; vmi_read_addr_va(vmiArr[atoi(vmiFunArg[1])], atol(vmiFunArg[2]), atoi(vmiFunArg[3]), &tmp); sprintf(returnBuf, "%lu\0", tmp); returnVal = returnBuf; } break; case 55: { sprintf(returnBuf, "%s\0", vmi_read_str_va(vmiArr[atoi(vmiFunArg[1])], atol(vmiFunArg[2]), atoi(vmiFunArg[3]) )); returnVal = returnBuf; } break; case 67: { sprintf(returnBuf, "%lu\0", vmi_translate_ksym2v(vmiArr[atoi(vmiFunArg[1])], vmiFunArg[2]) ); returnVal = returnBuf; } break; default:break; } write (fd, returnVal, 100); return 1; }
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; }