diff options
| author | serpilliere <devnull@localhost> | 2011-08-05 11:34:00 +0200 |
|---|---|---|
| committer | serpilliere <devnull@localhost> | 2011-08-05 11:34:00 +0200 |
| commit | ea68b1ff9bda805ed6191928ade152271efda213 (patch) | |
| tree | ff74a1a837be8a99c9885f2b6fe81ad885416880 | |
| parent | 3150b9452d9038742d9527a149c15eebd6562b28 (diff) | |
| download | miasm-ea68b1ff9bda805ed6191928ade152271efda213.tar.gz miasm-ea68b1ff9bda805ed6191928ade152271efda213.zip | |
clean libcodenat code
| -rw-r--r-- | miasm/tools/emul_lib/libcodenat.c | 305 | ||||
| -rw-r--r-- | miasm/tools/emul_lib/libcodenat_interface.c | 328 |
2 files changed, 229 insertions, 404 deletions
diff --git a/miasm/tools/emul_lib/libcodenat.c b/miasm/tools/emul_lib/libcodenat.c index c670224d..67929e88 100644 --- a/miasm/tools/emul_lib/libcodenat.c +++ b/miasm/tools/emul_lib/libcodenat.c @@ -34,37 +34,6 @@ struct memory_page_list_head memory_page_pool; struct code_bloc_list_head code_bloc_pool; -//#define RAISE(errtype,msg) { PyErr_SetString(errtype,msg); RE_RAISE; } -//#define RE_RAISE { Py_XDECREF(string); return NULL; } -//#define RAISE(errtype, msg) {PyObject* p; p = PyErr_Format( errtype, msg ); return p;} - -/* -unsigned int eax, ebx, ecx, edx, esi, edi, esp, ebp, eip; -unsigned int zf, nf, pf, of, cf, af, df; - -unsigned int eax_new, ebx_new, ecx_new, edx_new, esi_new, edi_new, esp_new, ebp_new, eip_new; -unsigned int zf_new, nf_new, pf_new, of_new, cf_new, af_new, df_new; -unsigned int tf, i_f, iopl_f, nt, rf, vm, ac, vif, vip, i_d; -unsigned int tf_new, i_f_new, iopl_f_new, nt_new, rf_new, vm_new, ac_new, vif_new, vip_new, i_d_new; - -unsigned int my_tick = 0; - -unsigned int reg_float_control; - -unsigned int cond; - - -unsigned int ds; - - -unsigned int vm_exception_flags = 0; -unsigned int vm_exception_flags_new = 0; - - -unsigned int vm_last_write_ad = 0; -unsigned int vm_last_write_size = 0; -*/ - vm_cpu_t vmcpu; /****************memory manager**************/ @@ -73,9 +42,6 @@ unsigned int min_page_ad = 0x22000000; extern unsigned int *code_addr_tab; -//LIST_HEAD(memory_page_list_head, memory_page_node) memory_page_pool; -//LIST_HEAD(code_bloc_list_head, code_bloc_node) code_bloc_pool; - unsigned int code_bloc_pool_ad_min; unsigned int code_bloc_pool_ad_max; @@ -116,7 +82,7 @@ struct memory_page_node * get_memory_page_from_address(unsigned int ad) return mpn; printf("cannot find address!! %X\n", ad); - dump_memory_page_pool(); + dump_memory_page_pool(); dump_gpregs(); //exit(-1); vmcpu.vm_exception_flags |= EXCEPT_ACCESS_VIOL; @@ -130,7 +96,7 @@ struct memory_page_node * get_memory_page_from_address(unsigned int ad) return mpn; } printf("cannot find address!! %X\n", ad); - dump_memory_page_pool(); + dump_memory_page_pool(); dump_gpregs(); //exit(-1); vmcpu.vm_exception_flags |= EXCEPT_ACCESS_VIOL; @@ -180,14 +146,13 @@ static inline unsigned long long memory_page_read(unsigned int my_size, unsigned default: exit(0); break; - } } /* read is multiple page wide */ else{ unsigned int new_size = my_size; printf("read multiple page! %X %X\n", ad, new_size); - dump_memory_page_pool(); + dump_memory_page_pool(); while (new_size){ ret <<=8; mpn = get_memory_page_from_address(ad); @@ -231,7 +196,6 @@ static inline void memory_page_write(unsigned int my_size, unsigned int ad, unsi if ((mpn->access & PAGE_WRITE) == 0){ printf("access to non writable page!! %X\n", ad); vmcpu.vm_exception_flags |= EXCEPT_ACCESS_VIOL; - return ; } @@ -263,7 +227,7 @@ static inline void memory_page_write(unsigned int my_size, unsigned int ad, unsi /* write is multiple page wide */ else{ printf("write multiple page! %X %X\n", ad, my_size); - dump_memory_page_pool(); + dump_memory_page_pool(); switch(my_size){ case 8: @@ -282,7 +246,6 @@ static inline void memory_page_write(unsigned int my_size, unsigned int ad, unsi exit(0); break; } - while (my_size){ mpn = get_memory_page_from_address(ad); if (!mpn) @@ -293,7 +256,6 @@ static inline void memory_page_write(unsigned int my_size, unsigned int ad, unsi my_size -= 8; ad ++; } - } } @@ -302,7 +264,6 @@ static inline void memory_page_write(unsigned int my_size, unsigned int ad, unsi inline void check_write_code_bloc(unsigned int my_size, unsigned int addr) { struct code_bloc_node * cbp; - vmcpu.vm_last_write_ad = addr; vmcpu.vm_last_write_size = my_size; @@ -344,25 +305,25 @@ void MEM_WRITE(unsigned int my_size, unsigned int addr , unsigned int src) void MEM_WRITE_08(unsigned int addr , unsigned char src) { - check_write_code_bloc(8, addr); + check_write_code_bloc(8, addr); memory_page_write(8, addr, src); } void MEM_WRITE_16(unsigned int addr , unsigned short src) { - check_write_code_bloc(16, addr); + check_write_code_bloc(16, addr); memory_page_write(16, addr, src); } void MEM_WRITE_32(unsigned int addr , unsigned int src) { - check_write_code_bloc(32, addr); + check_write_code_bloc(32, addr); memory_page_write(32, addr, src); } void MEM_WRITE_64(unsigned int addr , unsigned long long src) { - check_write_code_bloc(64, addr); + check_write_code_bloc(64, addr); memory_page_write(64, addr, src); } @@ -371,8 +332,6 @@ unsigned int MEM_LOOKUP(unsigned int my_size, unsigned int addr) { unsigned int ret; ret = memory_page_read(my_size, addr); - //if(vmcpu.my_tick> my_tick) - //printf("M_READ %2d %.8X %.8X\n", my_size, addr, ret); return ret; } @@ -470,9 +429,9 @@ inline unsigned int parity(unsigned int a) tmp = a&0xFF; cpt = 1; while (tmp!=0){ - cpt^=tmp&1; - tmp>>=1; - } + cpt^=tmp&1; + tmp>>=1; + } return cpt; } @@ -483,18 +442,18 @@ int shift_right_arith(unsigned int size, int a, unsigned int b) short i16_a; char i8_a; switch(size){ - case 8: - i8_a = a; - return (i8_a >> b)&0xff; - case 16: - i16_a = a; - return (i16_a >> b)&0xffff; - case 32: - i32_a = a; - return (i32_a >> b)&0xffffffff; - default: - printf("inv size in shift %d\n", size); - exit(0); + case 8: + i8_a = a; + return (i8_a >> b)&0xff; + case 16: + i16_a = a; + return (i16_a >> b)&0xffff; + case 32: + i32_a = a; + return (i32_a >> b)&0xffffffff; + default: + printf("inv size in shift %d\n", size); + exit(0); } } /* @@ -525,18 +484,18 @@ unsigned int shift_right_logic(unsigned int size, unsigned int a, unsigned int b unsigned short u16_a; unsigned char u8_a; switch(size){ - case 8: - u8_a = a; - return (u8_a >> b)&0xff; - case 16: - u16_a = a; - return (u16_a >> b)&0xffff; - case 32: - u32_a = a; - return (u32_a >> b)&0xffffffff; - default: - printf("inv size in shift %d\n", size); - exit(0); + case 8: + u8_a = a; + return (u8_a >> b)&0xff; + case 16: + u16_a = a; + return (u16_a >> b)&0xffff; + case 32: + u32_a = a; + return (u32_a >> b)&0xffffffff; + default: + printf("inv size in shift %d\n", size); + exit(0); } } /* @@ -564,15 +523,15 @@ int shift_right_logic_32(unsigned int a, unsigned int b) int shift_left_logic(unsigned int size, unsigned int a, unsigned int b) { switch(size){ - case 8: - return (a<<b)&0xff; - case 16: - return (a<<b)&0xffff; - case 32: - return (a<<b)&0xffffffff; - default: - printf("inv size in shift %d\n", size); - exit(0); + case 8: + return (a<<b)&0xff; + case 16: + return (a<<b)&0xffff; + case 32: + return (a<<b)&0xffffffff; + default: + printf("inv size in shift %d\n", size); + exit(0); } } /* @@ -701,18 +660,18 @@ int rot_left(unsigned int size, unsigned int a, unsigned int b) b = b&0x1F; b %= size; switch(size){ - case 8: - tmp = (a << b) | ((a&0xFF) >> (size-b)); - return tmp&0xff; - case 16: - tmp = (a << b) | ((a&0xFFFF) >> (size-b)); - return tmp&0xffff; - case 32: - tmp = (a << b) | ((a&0xFFFFFFFF) >> (size-b)); - return tmp&0xffffffff; - default: - printf("inv size in rotleft %d\n", size); - exit(0); + case 8: + tmp = (a << b) | ((a&0xFF) >> (size-b)); + return tmp&0xff; + case 16: + tmp = (a << b) | ((a&0xFFFF) >> (size-b)); + return tmp&0xffff; + case 32: + tmp = (a << b) | ((a&0xFFFFFFFF) >> (size-b)); + return tmp&0xffffffff; + default: + printf("inv size in rotleft %d\n", size); + exit(0); } } @@ -723,18 +682,18 @@ int rot_right(unsigned int size, unsigned int a, unsigned int b) b = b&0x1F; b %= size; switch(size){ - case 8: - tmp = ((a&0xFF) >> b) | (a << (size-b)); - return tmp&0xff; - case 16: - tmp = ((a&0xFFFF) >> b) | (a << (size-b)); - return tmp&0xffff; - case 32: - tmp = ((a&0xFFFFFFFF) >> b) | (a << (size-b)); - return tmp&0xffffffff; - default: - printf("inv size in rotleft %d\n", size); - exit(0); + case 8: + tmp = ((a&0xFF) >> b) | (a << (size-b)); + return tmp&0xff; + case 16: + tmp = ((a&0xFFFF) >> b) | (a << (size-b)); + return tmp&0xffff; + case 32: + tmp = ((a&0xFFFFFFFF) >> b) | (a << (size-b)); + return tmp&0xffffffff; + default: + printf("inv size in rotleft %d\n", size); + exit(0); } } @@ -750,18 +709,18 @@ int rcl_rez_op(unsigned int size, unsigned int a, unsigned int b, unsigned int c b %= size; switch(size){ - case 8+1: - tmp = (tmp << b) | ((tmp&0x1FF) >> (size-b)); - return tmp&0xff; - case 16+1: - tmp = (tmp << b) | ((tmp&0x1FFFF) >> (size-b)); - return tmp&0xffff; - case 32+1: - tmp = (tmp << b) | ((tmp&0x1FFFFFFFFULL) >> (size-b)); - return tmp&0xffffffff; - default: - printf("inv size in rclleft %d\n", size); - exit(0); + case 8+1: + tmp = (tmp << b) | ((tmp&0x1FF) >> (size-b)); + return tmp&0xff; + case 16+1: + tmp = (tmp << b) | ((tmp&0x1FFFF) >> (size-b)); + return tmp&0xffff; + case 32+1: + tmp = (tmp << b) | ((tmp&0x1FFFFFFFFULL) >> (size-b)); + return tmp&0xffffffff; + default: + printf("inv size in rclleft %d\n", size); + exit(0); } } @@ -783,18 +742,18 @@ int rcl_cf_op(unsigned int size, unsigned int a, unsigned int b, unsigned int cf b %= size; switch(size){ - case 8+1: - tmp = (tmp << b) | ((tmp&0x1FF) >> (size-b)); - return (tmp>>8)&1; - case 16+1: - tmp = (tmp << b) | ((tmp&0x1FFFF) >> (size-b)); - return (tmp>>16)&1; - case 32+1: - tmp = (tmp << b) | ((tmp&0x1FFFFFFFFULL) >> (size-b)); - return (tmp>>32)&1; - default: - printf("inv size in rclleft %d\n", size); - exit(0); + case 8+1: + tmp = (tmp << b) | ((tmp&0x1FF) >> (size-b)); + return (tmp>>8)&1; + case 16+1: + tmp = (tmp << b) | ((tmp&0x1FFFF) >> (size-b)); + return (tmp>>16)&1; + case 32+1: + tmp = (tmp << b) | ((tmp&0x1FFFFFFFFULL) >> (size-b)); + return (tmp>>32)&1; + default: + printf("inv size in rclleft %d\n", size); + exit(0); } } @@ -806,7 +765,7 @@ unsigned int my_bsr(unsigned int a, unsigned int b) { int i; - for (i=31; i>=0; i--){ + for (i=31; i>=0; i--){ if (b & (1<<i)) return i; } @@ -817,7 +776,7 @@ unsigned int my_bsf(unsigned int a, unsigned int b) { int i; - for (i=0; i<32; i++){ + for (i=0; i<32; i++){ if (b & (1<<i)) return i; } @@ -874,7 +833,6 @@ unsigned int cpuid(unsigned int a, unsigned int reg_num) printf("WARNING zarb cpuid index %X!\n", a); //exit(-1); } - return 0; } @@ -979,13 +937,11 @@ struct memory_page_node * create_memory_page_node(unsigned int ad, unsigned int printf("cannot alloc mpn\n"); exit(-1); } - p = malloc(size); if (!p){ printf("cannot alloc %d\n", size); exit(-1); } - mpn->ad = ad; mpn->size = size; mpn->access = access; @@ -1026,10 +982,9 @@ void dump_code_bloc_pool(void) struct code_bloc_node * cbp; LIST_FOREACH(cbp, &code_bloc_pool, next){ - printf("ad start %.8X ad_stop %.8X\n", + printf("ad start %.8X ad_stop %.8X\n", cbp->ad_start, cbp->ad_stop); - } } @@ -1040,7 +995,6 @@ void init_memory_page_pool(void) LIST_INIT(&memory_page_pool); for (i=0;i<MAX_MEMORY_PAGE_POOL_TAB; i++) memory_page_pool_tab[i] = NULL; - } void init_code_bloc_pool(void) @@ -1113,7 +1067,6 @@ void add_memory_page(struct memory_page_node* mpn_a) LIST_INSERT_BEFORE(mpn, mpn_a, next); insert_mpn_in_tab(mpn_a); return; - } LIST_INSERT_AFTER(lmpn, mpn_a, next); insert_mpn_in_tab(mpn_a); @@ -1125,7 +1078,7 @@ void dump_memory_page_pool() struct memory_page_node * mpn; LIST_FOREACH(mpn, &memory_page_pool, next){ - printf("ad %.8X size %.8X %c%c%c hpad %p\n", + printf("ad %.8X size %.8X %c%c%c hpad %p\n", mpn->ad, mpn->size, mpn->access & PAGE_READ? 'R':'_', @@ -1134,8 +1087,6 @@ void dump_memory_page_pool() mpn->ad_hp ); } - - } @@ -1172,7 +1123,7 @@ unsigned int get_memory_page_next(unsigned int n_ad) { struct memory_page_node * mpn; unsigned int ad = 0; - + LIST_FOREACH(mpn, &memory_page_pool, next){ if (mpn->ad < n_ad) continue; @@ -1180,9 +1131,7 @@ unsigned int get_memory_page_next(unsigned int n_ad) if (ad == 0 || mpn->ad <ad) ad = mpn->ad; } - return ad; - } unsigned int get_memory_page_from_min_ad(unsigned int size) @@ -1192,24 +1141,23 @@ unsigned int get_memory_page_from_min_ad(unsigned int size) unsigned int min_ad = min_page_ad; int end = 0; /* first, find free min ad */ - while (!end){ + while (!end){ end = 1; - LIST_FOREACH(mpn, &memory_page_pool, next){ - c_ad = (mpn->ad + mpn->size+0x1000)&0xfffff000; - if (c_ad <= min_ad) - continue; - if (mpn->ad <= min_ad){ - min_ad = c_ad; + LIST_FOREACH(mpn, &memory_page_pool, next){ + c_ad = (mpn->ad + mpn->size+0x1000)&0xfffff000; + if (c_ad <= min_ad) + continue; + if (mpn->ad <= min_ad){ + min_ad = c_ad; end = 0; - break; - } - - if (mpn->ad - min_ad < size){ - min_ad = c_ad; + break; + } + if (mpn->ad - min_ad < size){ + min_ad = c_ad; end = 0; - break; - } - } + break; + } + } } return min_ad; } @@ -1226,38 +1174,34 @@ void hexdump(char* m, unsigned int l) for (i=0;i<l;i++){ if (!(i%0x10) && i){ last = i; - printf(" "); - - for (j=-0x10;j<0;j++){ - if (isprint(m[i+j])){ - printf("%c", m[i+j]); - } - else{ - printf("."); - } - } - printf("\n"); + printf(" "); + for (j=-0x10;j<0;j++){ + if (isprint(m[i+j])){ + printf("%c", m[i+j]); + } + else{ + printf("."); + } + } + printf("\n"); } printf("%.2X ", m[i]&0xFF); } - l-=last; if (l){ - for (j=i;j<last+0x10;j++) printf(" "); printf(" "); - for (j = 0;l;j++){ if (isprint(m[last+j])){ - printf("%c", m[last+j]); + printf("%c", m[last+j]); } else{ - printf("."); + printf("."); } l--; } - } + } printf("\n"); } @@ -1270,7 +1214,6 @@ void _vm_init_regs() { vmcpu.eax = vmcpu.ebx = vmcpu.ecx = vmcpu.edx = vmcpu.esi = vmcpu.edi = vmcpu.esp = vmcpu.ebp = 0; vmcpu.zf = vmcpu.nf = vmcpu.pf = vmcpu.of = vmcpu.cf = vmcpu.af = vmcpu.df = 0; - vmcpu.eax_new = vmcpu.ebx_new = vmcpu.ecx_new = vmcpu.edx_new = vmcpu.esi_new = vmcpu.edi_new = vmcpu.esp_new = vmcpu.ebp_new = 0; vmcpu.zf_new = vmcpu.nf_new = vmcpu.pf_new = vmcpu.of_new = vmcpu.cf_new = vmcpu.af_new = vmcpu.df_new = 0; vmcpu.esp = 0; diff --git a/miasm/tools/emul_lib/libcodenat_interface.c b/miasm/tools/emul_lib/libcodenat_interface.c index c94295b2..cee5e4a6 100644 --- a/miasm/tools/emul_lib/libcodenat_interface.c +++ b/miasm/tools/emul_lib/libcodenat_interface.c @@ -41,7 +41,6 @@ PyObject* _vm_get_exception(unsigned int xcpt) p = PyErr_Format( PyExc_RuntimeError, "EXCEPT_UNK_MEM_AD" ); else p = PyErr_Format( PyExc_RuntimeError, "EXCEPT_UNKNOWN" ); - return p; } @@ -56,19 +55,9 @@ PyObject* _vm_get_all_memory(void) dict = PyDict_New(); LIST_FOREACH(mpn, &memory_page_pool, next){ - /* - printf("ad %.8X size %.8X %c%c%c hpad %p\n", - mpn->ad, - mpn->size, - mpn->access & PAGE_READ? 'R':'_', - mpn->access & PAGE_WRITE? 'W':'_', - mpn->access & PAGE_EXEC? 'X':'_', - mpn->ad_hp - ); - */ dict2 = PyDict_New(); - + o = PyString_FromStringAndSize(mpn->ad_hp, mpn->size); PyDict_SetItemString(dict2, "data", o); Py_DECREF(o); @@ -80,7 +69,7 @@ PyObject* _vm_get_all_memory(void) o = PyInt_FromLong((long)mpn->access); PyDict_SetItemString(dict2, "access", o); Py_DECREF(o); - + o = PyInt_FromLong((long)mpn->ad); PyDict_SetItem(dict, o, dict2); Py_DECREF(o); @@ -94,7 +83,7 @@ PyObject* _vm_get_gpreg(void) { PyObject *dict = PyDict_New(); PyObject *o; - + o = PyInt_FromLong((long)vmcpu.eax); PyDict_SetItemString(dict, "eax", o); Py_DECREF(o); @@ -122,22 +111,19 @@ PyObject* _vm_get_gpreg(void) o = PyInt_FromLong((long)vmcpu.eip); PyDict_SetItemString(dict, "eip", o); Py_DECREF(o); - return dict; - - } reg_dict gpreg_dict[] = { {.name = "eax", .ptr = &(vmcpu.eax)}, - {.name = "ebx", .ptr = &(vmcpu.ebx)}, - {.name = "ecx", .ptr = &(vmcpu.ecx)}, - {.name = "edx", .ptr = &(vmcpu.edx)}, - {.name = "esi", .ptr = &(vmcpu.esi)}, - {.name = "edi", .ptr = &(vmcpu.edi)}, - {.name = "esp", .ptr = &(vmcpu.esp)}, - {.name = "ebp", .ptr = &(vmcpu.ebp)}, - {.name = "eip", .ptr = &(vmcpu.eip)}, + {.name = "ebx", .ptr = &(vmcpu.ebx)}, + {.name = "ecx", .ptr = &(vmcpu.ecx)}, + {.name = "edx", .ptr = &(vmcpu.edx)}, + {.name = "esi", .ptr = &(vmcpu.esi)}, + {.name = "edi", .ptr = &(vmcpu.edi)}, + {.name = "esp", .ptr = &(vmcpu.esp)}, + {.name = "ebp", .ptr = &(vmcpu.ebp)}, + {.name = "eip", .ptr = &(vmcpu.eip)}, }; PyObject* _vm_set_gpreg(PyObject *dict) @@ -146,42 +132,36 @@ PyObject* _vm_set_gpreg(PyObject *dict) int pos = 0; unsigned int val; unsigned int i, found; - if(!PyDict_Check(dict)) - RAISE(PyExc_TypeError, "arg must be dict"); - + RAISE(PyExc_TypeError, "arg must be dict"); while(PyDict_Next(dict, &pos, &d_key, &d_value)){ - if(!PyString_Check(d_key)) - RAISE(PyExc_TypeError, "key must be str"); - - - if (PyInt_Check(d_value)){ - val = (unsigned int)PyInt_AsLong(d_value); - } - else if (PyLong_Check(d_value)){ - val = (unsigned int)PyInt_AsUnsignedLongLongMask(d_value); - } - else{ - RAISE(PyExc_TypeError,"value must be int"); - } + if(!PyString_Check(d_key)) + RAISE(PyExc_TypeError, "key must be str"); + if (PyInt_Check(d_value)){ + val = (unsigned int)PyInt_AsLong(d_value); + } + else if (PyLong_Check(d_value)){ + val = (unsigned int)PyInt_AsUnsignedLongLongMask(d_value); + } + else{ + RAISE(PyExc_TypeError,"value must be int"); + } - found = 0; - for (i=0; i < sizeof(gpreg_dict)/sizeof(reg_dict); i++){ - if (strcmp(PyString_AsString(d_key), gpreg_dict[i].name)) - continue; - *(gpreg_dict[i].ptr) = val; - found = 1; - break; - } - - if (found) - continue; - - printf("unkown key: %s\n", PyString_AsString(d_key)); + found = 0; + for (i=0; i < sizeof(gpreg_dict)/sizeof(reg_dict); i++){ + if (strcmp(PyString_AsString(d_key), gpreg_dict[i].name)) + continue; + *(gpreg_dict[i].ptr) = val; + found = 1; + break; + } - RAISE(PyExc_ValueError, "unkown reg"); + if (found) + continue; + printf("unkown key: %s\n", PyString_AsString(d_key)); + RAISE(PyExc_ValueError, "unkown reg"); } return NULL; } @@ -191,7 +171,6 @@ PyObject* _vm_add_memory_page(PyObject *item, PyObject *access, PyObject *item_s { unsigned int buf_size; char* buf_data; - //char* buf_data_aligned; Py_ssize_t length; int ret = 0x1337beef; unsigned int page_addr; @@ -200,37 +179,33 @@ PyObject* _vm_add_memory_page(PyObject *item, PyObject *access, PyObject *item_s struct memory_page_node * mpn; if (PyInt_Check(item)){ - page_addr = (unsigned int)PyInt_AsLong(item); + page_addr = (unsigned int)PyInt_AsLong(item); } else if (PyLong_Check(item)){ - page_addr = (unsigned int)PyInt_AsUnsignedLongLongMask(item); + page_addr = (unsigned int)PyInt_AsUnsignedLongLongMask(item); } else{ - RAISE(PyExc_TypeError,"arg1 must be int"); + RAISE(PyExc_TypeError,"arg1 must be int"); } if (PyInt_Check(access)){ - page_access = (unsigned int)PyInt_AsLong(access); + page_access = (unsigned int)PyInt_AsLong(access); } else if (PyLong_Check(item)){ - page_access = (unsigned int)PyInt_AsUnsignedLongLongMask(access); + page_access = (unsigned int)PyInt_AsUnsignedLongLongMask(access); } else{ - RAISE(PyExc_TypeError,"arg2 must be int"); + RAISE(PyExc_TypeError,"arg2 must be int"); } - //printf("add page: %X\n", page_addr); - if(!PyString_Check(item_str)) RAISE(PyExc_TypeError,"arg must be str"); buf_size = PyString_Size(item_str); - //printf("add page %X size: %X\n", page_addr, buf_size); - PyString_AsStringAndSize(item_str, &buf_data, &length); @@ -244,51 +219,44 @@ PyObject* _vm_add_memory_page(PyObject *item, PyObject *access, PyObject *item_s PyObject* _call_pyfunc_from_globals(char* funcname) { PyObject *mod, *func, *rslt, *globals, *func_globals; - + printf("getting pyfunc %s\n", funcname); - mod = PyEval_GetBuiltins(); if (!mod) { - printf("cannot find module\n"); - exit(0); + printf("cannot find module\n"); + exit(0); } - + func_globals = PyDict_GetItemString(mod, "globals"); if (!func_globals) { - printf("cannot find function globals\n"); - exit(0); + printf("cannot find function globals\n"); + exit(0); } - + if (!PyCallable_Check (func_globals)) { - printf("function not callable\n"); - exit(0); + printf("function not callable\n"); + exit(0); } globals = PyObject_CallObject (func_globals, NULL); if (!globals) { - printf("cannot get globals\n"); - exit(0); + printf("cannot get globals\n"); + exit(0); } - //Py_XDECREF(func_globals); - //Py_XDECREF(mod); - - func = PyDict_GetItemString (globals, funcname); if (!func) { - printf("cannot find function %s\n", funcname); - exit(0); + printf("cannot find function %s\n", funcname); + exit(0); } - + if (!PyCallable_Check (func)) { - printf("function not callable\n"); - exit(0); + printf("function not callable\n"); + exit(0); } - + rslt = PyObject_CallObject (func, NULL); - - return rslt; } @@ -302,78 +270,62 @@ PyObject* _call_pyfunc_from_eip(void) printf("getting pybloc %X\n", vmcpu.eip); sprintf(funcname, "bloc_%.8X", vmcpu.eip); printf("bloc name %s\n", funcname); - + mod = PyEval_GetBuiltins(); if (!mod) { - printf("cannot find module\n"); - exit(0); + printf("cannot find module\n"); + exit(0); } - func_globals = PyDict_GetItemString(mod, "globals"); if (!func_globals) { - printf("cannot find function globals\n"); - exit(0); + printf("cannot find function globals\n"); + exit(0); } - if (!PyCallable_Check (func_globals)) { - printf("function not callable\n"); - exit(0); + printf("function not callable\n"); + exit(0); } - globals = PyObject_CallObject (func_globals, NULL); if (!globals) { - printf("cannot get globals\n"); - exit(0); + printf("cannot get globals\n"); + exit(0); } - //Py_XDECREF(func_globals); - //Py_XDECREF(mod); - func = PyDict_GetItemString (globals, funcname); if (!func) { - printf("cannot find function %s\n", funcname); - exit(0); + printf("cannot find function %s\n", funcname); + exit(0); } - if (!PyCallable_Check (func)) { - printf("function not callable\n"); - exit(0); + printf("function not callable\n"); + exit(0); } - rslt = PyObject_CallObject (func, NULL); - - return rslt; } - - - PyObject* _vm_get_cpu_state(void) { PyObject * o; o = PyString_FromStringAndSize((char*)&vmcpu, sizeof(vmcpu)); return o; - } - PyObject* _vm_set_cpu_state(PyObject * s_cpustate) { unsigned int buf_size; Py_ssize_t length; char* buf; - + if(!PyString_Check(s_cpustate)) RAISE(PyExc_TypeError,"arg must be str"); - + buf_size = PyString_Size(s_cpustate); if (buf_size != sizeof(vmcpu)) RAISE(PyExc_TypeError,"bad str len"); - - + PyString_AsStringAndSize(s_cpustate, (char**)&buf, &length); memcpy(&vmcpu, buf, length); return PyInt_FromLong((long)0); @@ -393,7 +345,7 @@ PyObject* _vm_push_uint32_t(int val) PyObject* _vm_pop_uint32_t(void) { unsigned int val; - + val = MEM_LOOKUP(32, vmcpu.esp); vmcpu.esp+=4; @@ -404,7 +356,6 @@ PyObject* _vm_set_mem(PyObject *addr, PyObject *item_str) { unsigned int buf_size; char* buf_data; - //char* buf_data_aligned; Py_ssize_t length; int ret = 0x1337; unsigned int val; @@ -412,13 +363,13 @@ PyObject* _vm_set_mem(PyObject *addr, PyObject *item_str) struct memory_page_node * mpn; if (PyInt_Check(addr)){ - val = (unsigned int)PyInt_AsLong(addr); + val = (unsigned int)PyInt_AsLong(addr); } else if (PyLong_Check(addr)){ - val = (unsigned int)PyInt_AsUnsignedLongLongMask(addr); + val = (unsigned int)PyInt_AsUnsignedLongLongMask(addr); } else{ - RAISE(PyExc_TypeError,"arg1 must be int"); + RAISE(PyExc_TypeError,"arg1 must be int"); } printf("set addr: %X\n", val); @@ -427,12 +378,8 @@ PyObject* _vm_set_mem(PyObject *addr, PyObject *item_str) RAISE(PyExc_TypeError,"arg must be str"); buf_size = PyString_Size(item_str); - //printf("buf size: %X\n", buf_size); - PyString_AsStringAndSize(item_str, &buf_data, &length); - mpn = get_memory_page_from_address(val); - //memcpy((void*)val, buf_data, buf_size); memcpy(mpn->ad_hp + (val-mpn->ad), buf_data, buf_size); return PyInt_FromLong((long)ret); @@ -448,23 +395,23 @@ PyObject* _vm_set_mem_access(PyObject *addr, PyObject *access) struct memory_page_node * mpn; if (PyInt_Check(addr)){ - page_addr = (unsigned int)PyInt_AsLong(addr); + page_addr = (unsigned int)PyInt_AsLong(addr); } else if (PyLong_Check(addr)){ - page_addr = (unsigned int)PyInt_AsUnsignedLongLongMask(addr); + page_addr = (unsigned int)PyInt_AsUnsignedLongLongMask(addr); } else{ - RAISE(PyExc_TypeError,"arg1 must be int"); + RAISE(PyExc_TypeError,"arg1 must be int"); } if (PyInt_Check(access)){ - page_access = (unsigned int)PyInt_AsLong(access); + page_access = (unsigned int)PyInt_AsLong(access); } else if (PyLong_Check(access)){ - page_access = (unsigned int)PyInt_AsUnsignedLongLongMask(access); + page_access = (unsigned int)PyInt_AsUnsignedLongLongMask(access); } else{ - RAISE(PyExc_TypeError,"arg2 must be int"); + RAISE(PyExc_TypeError,"arg2 must be int"); } mpn = get_memory_page_from_address(page_addr); @@ -475,11 +422,6 @@ PyObject* _vm_set_mem_access(PyObject *addr, PyObject *access) PyObject* _vm_get_str(PyObject *addr, PyObject *item_len) { - //unsigned int buf_size; - //char* buf_data; - //char* buf_data_aligned; - //Py_ssize_t length; - //int ret; unsigned int buf_addr; unsigned int buf_len; PyObject *obj_out; @@ -492,27 +434,25 @@ PyObject* _vm_get_str(PyObject *addr, PyObject *item_len) unsigned int my_size; if (PyInt_Check(addr)){ - buf_addr = (unsigned int)PyInt_AsLong(addr); + buf_addr = (unsigned int)PyInt_AsLong(addr); } else if (PyLong_Check(addr)){ - buf_addr = (unsigned int)PyInt_AsUnsignedLongLongMask(addr); + buf_addr = (unsigned int)PyInt_AsUnsignedLongLongMask(addr); } else{ - RAISE(PyExc_TypeError,"arg1 must be int"); + RAISE(PyExc_TypeError,"arg1 must be int"); } - if (PyInt_Check(item_len)){ - buf_len = (unsigned int)PyInt_AsLong(item_len); + buf_len = (unsigned int)PyInt_AsLong(item_len); } else if (PyLong_Check(item_len)){ - buf_len = (unsigned int)PyInt_AsUnsignedLongLongMask(item_len); + buf_len = (unsigned int)PyInt_AsUnsignedLongLongMask(item_len); } else{ - RAISE(PyExc_TypeError,"arg must be int"); + RAISE(PyExc_TypeError,"arg must be int"); } my_size = buf_len; - buf_out = malloc(buf_len); if (!buf_out){ printf("cannot alloc read\n"); @@ -520,9 +460,6 @@ PyObject* _vm_get_str(PyObject *addr, PyObject *item_len) } addr_out = buf_out; - - - /* read is multiple page wide */ while (my_size){ @@ -538,13 +475,13 @@ PyObject* _vm_get_str(PyObject *addr, PyObject *item_len) l = MIN(my_size, mpn->size - off); memcpy(addr_out, addr_tmp, l); - my_size -= l; + my_size -= l; addr_out +=l; buf_addr +=l; } obj_out = PyString_FromStringAndSize(buf_out, buf_len); - free(buf_out); + free(buf_out); return obj_out; } @@ -568,7 +505,7 @@ PyObject* vm_get_last_write_size(PyObject* self, PyObject* args) PyObject* vm_reset_exception(PyObject* self, PyObject* args) { - vmcpu.vm_exception_flags = 0; + vmcpu.vm_exception_flags = 0; Py_INCREF(Py_None); return Py_None; } @@ -603,15 +540,6 @@ PyObject* vm_pop_uint32_t(PyObject* self, PyObject* args) return p; } -/* -PyObject* vm_put_str(PyObject *item) -{ - PyObject* p; - p = _vm_put_str(item); - return p; -} -*/ - PyObject* vm_set_mem(PyObject* self, PyObject* args) { PyObject* p; @@ -755,57 +683,38 @@ PyObject* _vm_add_code_bloc(PyObject* self, PyObject* args) if (PyInt_Check(item1)){ - ad_start = (unsigned int)PyInt_AsLong(item1); + ad_start = (unsigned int)PyInt_AsLong(item1); } else if (PyLong_Check(item1)){ - ad_start = (unsigned int)PyInt_AsUnsignedLongLongMask(item1); + ad_start = (unsigned int)PyInt_AsUnsignedLongLongMask(item1); } else{ - RAISE(PyExc_TypeError,"arg1 must be int"); + RAISE(PyExc_TypeError,"arg1 must be int"); } - - if (PyInt_Check(item2)){ - ad_stop = (unsigned int)PyInt_AsLong(item2); + ad_stop = (unsigned int)PyInt_AsLong(item2); } else if (PyLong_Check(item2)){ - ad_stop = (unsigned int)PyInt_AsUnsignedLongLongMask(item2); + ad_stop = (unsigned int)PyInt_AsUnsignedLongLongMask(item2); } else{ - RAISE(PyExc_TypeError,"arg2 must be int"); - } - /* - if (PyInt_Check(item3)){ - ad_code = (unsigned int)PyInt_AsLong(item3); - } - else if (PyLong_Check(item3)){ - ad_code = (unsigned int)PyInt_AsUnsignedLongLongMask(item3); + RAISE(PyExc_TypeError,"arg2 must be int"); } - else{ - RAISE(PyExc_TypeError,"arg3 must be int"); - } - */ - - //printf("add code bloc %X %X\n", ad_start, ad_stop); - cbp = create_code_bloc_node(ad_start, ad_stop); cbp->ad_start = ad_start; cbp->ad_stop = ad_stop; cbp->ad_code = ad_code; - - add_code_bloc(cbp); - return PyInt_FromLong((long)ret); } -PyObject* vm_add_code_bloc(PyObject *item1, PyObject *item2)//, PyObject *item3) +PyObject* vm_add_code_bloc(PyObject *item1, PyObject *item2) { PyObject* p; - p = _vm_add_code_bloc(item1, item2);//, item3); + p = _vm_add_code_bloc(item1, item2); return p; } @@ -831,7 +740,6 @@ PyObject* vm_set_cpu_state(PyObject * s_cpustate) PyObject *o; o = _vm_set_cpu_state(s_cpustate); return o; - } @@ -897,7 +805,6 @@ PyObject* _vm_exec_blocs(PyObject* self, PyObject* args) else{ RAISE(PyExc_TypeError,"arg1 must be int"); } - meip = PyInt_FromLong((long)tmp); while (1){ b = PyDict_GetItem(known_blocs, meip); @@ -906,18 +813,14 @@ PyObject* _vm_exec_blocs(PyObject* self, PyObject* args) module = PyObject_GetAttrString(b, "module_c"); if (module == NULL) return meip; - //Py_DECREF(b); - func = PyObject_GetAttrString(module, "func"); if (func == NULL) return meip; Py_DECREF(module); - - if (!PyCallable_Check (func)) { printf("function not callable\n"); - exit(0); + exit(0); } Py_DECREF(meip); meip = PyObject_CallObject (func, NULL); @@ -933,7 +836,6 @@ PyObject* _vm_exec_blocs(PyObject* self, PyObject* args) return meip; } - } @@ -972,34 +874,24 @@ PyObject* vm_exec_bloc(PyObject* self, PyObject* args) RAISE(PyExc_TypeError,"arg1 must be int"); } - - - //printf("eip val: %x\n", tmp); meip = PyInt_FromLong((long)tmp); - //printf("x %p\n", meip); b = PyDict_GetItem(known_blocs, my_eip); if (b == NULL) return meip; - module = PyObject_GetAttrString(b, "module_c"); if (module == NULL) return meip; - //Py_DECREF(b); - func = PyObject_GetAttrString(module, "func"); if (func == NULL) return meip; - Py_DECREF(module); - - if (!PyCallable_Check (func)) { printf("function not callable\n"); - exit(0); + exit(0); } Py_DECREF(meip); meip = PyObject_CallObject (func, NULL); - + Py_DECREF(func); e = PyErr_Occurred (); if (e){ @@ -1008,17 +900,7 @@ PyObject* vm_exec_bloc(PyObject* self, PyObject* args) } return meip; - -} - -/* -PyObject* vm_exec_bloc(PyObject* my_eip, PyObject* known_blocs) -{ - my_eip = _vm_exec_bloc(my_eip, known_blocs); - return my_eip; } -*/ - static PyObject *CodenatError; @@ -1099,7 +981,7 @@ initlibcodenat_interface(void) m = Py_InitModule("libcodenat_interface", CodenatMethods); if (m == NULL) - return; + return; CodenatError = PyErr_NewException("codenat.error", NULL, NULL); Py_INCREF(CodenatError); |